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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With