Tomas Andrle
Tomas Andrle

Reputation: 13354

Simulate orientation changes in iOS for testing purposes

I would like to test my app's ability to handle orientation changes (portrait/landscape). I'm currently using KIF and as far as I know, it cannot do this. Is there a way to simulate the rotation events programmatically for the iOS simulator?

I don't care if it is some undocumented private API or hack because this will only run during testing and will not be part of production builds.

Upvotes: 8

Views: 3966

Answers (4)

Sh_Andrew
Sh_Andrew

Reputation: 352

To simulate orientation change in UI Automation you can use the setDeviceOrientation method for UIATarget. Example:

UIATarget.localTarget().setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);

Method needs one parameter 'deviceOrientation' constant. More info here

This 100% works on the real iOS device. I'm not sure about simulator.

Upvotes: 4

Paul de Lange
Paul de Lange

Reputation: 10633

Here is a step to achieve this:

+ (KIFTestStep*) stepToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation {

    NSString* orientation = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? @"Landscape" : @"Portrait";
        return [KIFTestStep stepWithDescription: [NSString stringWithFormat: @"Rotate to orientation %@", orientation]
                             executionBlock: ^KIFTestStepResult(KIFTestStep *step, NSError *__autoreleasing *error) {
                                 if( [UIApplication sharedApplication].statusBarOrientation != toInterfaceOrientation ) {
                                     UIDevice* device = [UIDevice currentDevice];
                                     SEL message = NSSelectorFromString(@"setOrientation:");

                                     if( [device respondsToSelector: message] ) {
                                         NSMethodSignature* signature = [UIDevice instanceMethodSignatureForSelector: message];
                                         NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
                                         [invocation setTarget: device];
                                         [invocation setSelector: message];
                                         [invocation setArgument: &toInterfaceOrientation atIndex: 2];
                                         [invocation invoke];
                                     }
                                 }

                                 return KIFTestStepResultSuccess;
                             }];
}

Note: Keep your device flat on a table or the accelerometer updates will rotate the view back.

Upvotes: 9

daihovey
daihovey

Reputation: 3575

Why do it programatically? The Simulator does exactly what you want, it test the apps ability to handle orientation changes.

In the Simulator either use the top menu Hardware > Rotate Left / Right or hold down Command and use the left and right arrows.

Upvotes: -3

Farhan Ahmed Wasim
Farhan Ahmed Wasim

Reputation: 949

I don't know what you mean by 'programmatically' but if you use the UIAutomation library provided by apple along with Automation template of the Instruments application you can simulate different orientations supported by the iPhone.

Upvotes: 0

Related Questions