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.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92

4 Answers4

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.

Paul de Lange
  • 10,613
  • 10
  • 41
  • 56
  • 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. – Paresh Masani May 14 '13 at 14:29
  • My bad! It works only if App supports the orientation in question! – Paresh Masani May 14 '13 at 15:22
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.

Sh_Andrew
  • 352
  • 2
  • 6
  • 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. – Tomas Andrle Aug 23 '12 at 20:00
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.

  • 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. – Tomas Andrle Jun 26 '12 at 09:05
-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.

daihovey
  • 3,485
  • 13
  • 66
  • 110
  • 4
    I want to do it programmatically because I want to have automated UI tests that run without my intervention on the build server. – Tomas Andrle Jun 26 '12 at 09:05