Simulate orientation changes in iOS for testing purposes
Asked Answered
M

4

8

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.

Moynahan answered 18/6, 2012 at 9:25 Comment(0)
P
9

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.

Perverse answered 14/8, 2012 at 8:25 Comment(2)
This doesn't seem to work in simulator. I don't have device so I haven't tested it on it but KIF tests will run on simulators through VaxSim so it has to run on simulator. Can you please confirm that this does/doesn't run on simulator? I am using iOS 6.1 simulator.Nace
My bad! It works only if App supports the orientation in question!Nace
T
4

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.

Truce answered 23/8, 2012 at 11:47 Comment(1)
What you suggest is something that has to be run from the UI Automation tool. That's not going to work for me. I need Objective C code that can be called from within the app. That's how the KIF testing framework works. Sorry for not making that clear enough.Moynahan
H
0

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.

Havildar answered 25/6, 2012 at 22:51 Comment(1)
True, I have found this as well. I'm still looking for a way to do it from within the app itself, in Objective-C. That's how the KIF testing framework operates.Moynahan
K
-3

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.

Krutz answered 25/6, 2012 at 23:33 Comment(1)
I want to do it programmatically because I want to have automated UI tests that run without my intervention on the build server.Moynahan

© 2022 - 2024 — McMap. All rights reserved.