Simulate orientation changes in iOS for testing purposes

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

I don’t care if this is some kind of undocumented private API or hack, because it will only work during testing and will not be part of the production builds.

+8
ios testing ui-automation ios-ui-automation kif-framework
source share
4 answers

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 on the table or accelerometer updates will rotate the view back.

+9
source share

To model orientation changes in UI Automation, you can use the setDeviceOrientation method for UIATarget. Example:

 UIATarget.localTarget().setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT); 

The method needs a constant parameter "deviceOrientation". More here

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

+4
source share

I don’t know what you mean by software, but if you use the UIAutomation library provided by the apple along with the automation template of the Instruments application, you can simulate various orientations supported by the iPhone.

0
source share

Why is this software? The simulator does exactly what you want; it tests the ability of applications to handle orientation changes.

In the simulator, either use the top menu "Hardware"> "Rotate Left / Right" or "Hold Down" and use the left and right arrows.

-2
source share

All Articles