Skip to content

Commit

Permalink
Add step to rotate screen, fix issue with UIAlertViews in landscape.
Browse files Browse the repository at this point in the history
The following method should provide hours of fun:

    [system
simulateDeviceRotationToOrientation:UIDeviceOrientationLandscapeLeft];

I used this to validate and fix issues #60 using the approach outlined
in that issue.
  • Loading branch information
bnickel committed Sep 12, 2013
1 parent fe22434 commit ba31296
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Additions/UIView-KIFAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ typedef CGPoint KIFDisplacement;
*/
- (BOOL)isUserInteractionActuallyEnabled;

/*!
@abstract Returns either the current window or another window if a transform is applied. Returns `nil` if all windows in the application have transforms.
*/
@property (nonatomic, readonly) UIWindow *windowOrIdentityWindow;

@end
15 changes: 15 additions & 0 deletions Additions/UIView-KIFAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,19 @@ - (BOOL)isNavigationItemView;
return [self isKindOfClass:NSClassFromString(@"UINavigationItemView")] || [self isKindOfClass:NSClassFromString(@"_UINavigationBarBackIndicatorView")];
}

- (UIWindow *)windowOrIdentityWindow
{
if (CGAffineTransformIsIdentity(self.window.transform)) {
return self.window;
}

for (UIWindow *window in [[UIApplication sharedApplication] windowsWithKeyWindow]) {
if (CGAffineTransformIsIdentity(window.transform)) {
return window;
}
}

return nil;
}

@end
8 changes: 8 additions & 0 deletions Classes/KIFSystemTestActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// which Square, Inc. licenses this file to you.

#import "KIFTestActor.h"
#import <UIKit/UIKit.h>

#define system KIFActorWithClass(KIFSystemTestActor)

Expand Down Expand Up @@ -38,6 +39,13 @@
*/
- (void)simulateMemoryWarning;

/*!
@abstract Simulates a device rotation to a specific orentation from its last set orientation.
@discussion The first time this method is called, it will be from the device's natural orientation to the orientation described.
@param orientation The desired orientation.
*/
- (void)simulateDeviceRotationToOrientation:(UIDeviceOrientation)orientation;

/*!
@abstract Waits for the application to request a specific URL while executing a block.
@param URLString The absolute string representation of the URL to detect.
Expand Down
9 changes: 9 additions & 0 deletions Classes/KIFSystemTestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#import "UIApplication-KIFAdditions.h"
#import "NSError-KIFAdditions.h"

@interface UIApplication (Private)
- (BOOL)rotateIfNeeded:(UIDeviceOrientation)orientation;
@end

@implementation KIFSystemTestActor

- (NSNotification *)waitForNotificationName:(NSString*)name object:(id)object
Expand Down Expand Up @@ -55,6 +59,11 @@ - (void)simulateMemoryWarning
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:[UIApplication sharedApplication]];
}

- (void)simulateDeviceRotationToOrientation:(UIDeviceOrientation)orientation
{
[[UIApplication sharedApplication] rotateIfNeeded:orientation];
}

- (void)waitForApplicationToOpenAnyURLWhileExecutingBlock:(void (^)())block returning:(BOOL)returnValue
{
[self waitForApplicationToOpenURL:nil whileExecutingBlock:block returning:returnValue];
Expand Down
10 changes: 5 additions & 5 deletions Classes/KIFUITestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ - (void)tapAccessibilityElement:(UIAccessibilityElement *)element inView:(UIView
elementFrame.origin = CGPointZero;
elementFrame.size = view.frame.size;
} else {
elementFrame = [view.window convertRect:element.accessibilityFrame toView:view];
elementFrame = [view.windowOrIdentityWindow convertRect:element.accessibilityFrame toView:view];
}
CGPoint tappablePointInElement = [view tappablePointInRect:elementFrame];

Expand Down Expand Up @@ -196,7 +196,7 @@ - (void)longPressAccessibilityElement:(UIAccessibilityElement *)element inView:(

KIFTestWaitCondition(view.isUserInteractionActuallyEnabled, error, @"View is not enabled for interaction");

CGRect elementFrame = [view.window convertRect:element.accessibilityFrame toView:view];
CGRect elementFrame = [view.windowOrIdentityWindow convertRect:element.accessibilityFrame toView:view];
CGPoint tappablePointInElement = [view tappablePointInRect:elementFrame];

// This is mostly redundant of the test in _accessibilityElementWithLabel:
Expand Down Expand Up @@ -456,7 +456,7 @@ - (void)choosePhotoInAlbum:(NSString *)albumName atRow:(NSInteger)row column:(NS
return KIFTestStepResultWait;
}

CGRect elementFrame = [view.window convertRect:element.accessibilityFrame toView:view];
CGRect elementFrame = [view.windowOrIdentityWindow convertRect:element.accessibilityFrame toView:view];
CGPoint tappablePointInElement = [view tappablePointInRect:elementFrame];

[view tapAtPoint:tappablePointInElement];
Expand Down Expand Up @@ -537,7 +537,7 @@ - (void)swipeViewWithAccessibilityLabel:(NSString *)label inDirection:(KIFSwipeD

// Within this method, all geometry is done in the coordinate system of the view to swipe.

CGRect elementFrame = [viewToSwipe.window convertRect:element.accessibilityFrame toView:viewToSwipe];
CGRect elementFrame = [viewToSwipe.windowOrIdentityWindow convertRect:element.accessibilityFrame toView:viewToSwipe];
CGPoint swipeStart = CGPointCenteredInRect(elementFrame);
KIFDisplacement swipeDisplacement = KIFDisplacementForSwipingInDirection(direction);

Expand All @@ -555,7 +555,7 @@ - (void)scrollViewWithAccessibilityLabel:(NSString *)label byFractionOfSizeHoriz

// Within this method, all geometry is done in the coordinate system of the view to scroll.

CGRect elementFrame = [viewToScroll.window convertRect:element.accessibilityFrame toView:viewToScroll];
CGRect elementFrame = [viewToScroll.windowOrIdentityWindow convertRect:element.accessibilityFrame toView:viewToScroll];

KIFDisplacement scrollDisplacement = CGPointMake(elementFrame.size.width * horizontalFraction, elementFrame.size.height * verticalFraction);

Expand Down
39 changes: 39 additions & 0 deletions KIF Tests/LandscapeTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// LandscapeTests.m
// KIF
//
// Created by Brian Nickel on 9/11/13.
//
//

#import <KIF/KIF.h>

@interface LandscapeTests : KIFTestCase
@end

@implementation LandscapeTests

- (void)beforeAll
{
[system simulateDeviceRotationToOrientation:UIDeviceOrientationLandscapeLeft];
[tester scrollViewWithAccessibilityLabel:@"Test Suite TableView" byFractionOfSizeHorizontal:0 vertical:-0.2];
}

- (void)afterAll
{
[system simulateDeviceRotationToOrientation:UIDeviceOrientationPortrait];
}


- (void)beforeEach
{
[tester waitForTimeInterval:0.25];
}

- (void)testThatAlertViewsCanBeTappedInLandscape
{
[tester tapViewWithAccessibilityLabel:@"UIAlertView"];
[tester tapViewWithAccessibilityLabel:@"Continue"];
}

@end
4 changes: 4 additions & 0 deletions KIF.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
EB7204651680DDAD00278DA2 /* KIFSystemTestActor.h in Headers */ = {isa = PBXBuildFile; fileRef = EB4C3130167BA3AC00E31109 /* KIFSystemTestActor.h */; settings = {ATTRIBUTES = (Public, ); }; };
EB7204661680DDAD00278DA2 /* KIFUITestActor.h in Headers */ = {isa = PBXBuildFile; fileRef = EB4C3132167BA3AC00E31109 /* KIFUITestActor.h */; settings = {ATTRIBUTES = (Public, ); }; };
EB9FB42717A5BACB00DDF160 /* GestureViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = EB9FB42617A5BACB00DDF160 /* GestureViewController.m */; };
EB9FC00517E144B700138266 /* LandscapeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EB9FC00417E144B700138266 /* LandscapeTests.m */; };
EBAE487C17A45A8E0005EE19 /* NSBundle-KIFAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = EBAE487A17A45A8E0005EE19 /* NSBundle-KIFAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
EBAE487D17A45A8E0005EE19 /* NSBundle-KIFAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = EBAE487B17A45A8E0005EE19 /* NSBundle-KIFAdditions.m */; };
EBAE488117A460E50005EE19 /* NSError-KIFAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = EBAE487F17A460E50005EE19 /* NSError-KIFAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -159,6 +160,7 @@
EB60ED19177F90C2005A041A /* GestureTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GestureTests.m; sourceTree = "<group>"; };
EB72047C1680DDAD00278DA2 /* libKIF.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libKIF.a; sourceTree = BUILT_PRODUCTS_DIR; };
EB9FB42617A5BACB00DDF160 /* GestureViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GestureViewController.m; sourceTree = "<group>"; };
EB9FC00417E144B700138266 /* LandscapeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LandscapeTests.m; sourceTree = "<group>"; };
EBAE487A17A45A8E0005EE19 /* NSBundle-KIFAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSBundle-KIFAdditions.h"; sourceTree = "<group>"; };
EBAE487B17A45A8E0005EE19 /* NSBundle-KIFAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSBundle-KIFAdditions.m"; sourceTree = "<group>"; };
EBAE487F17A460E50005EE19 /* NSError-KIFAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError-KIFAdditions.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -359,6 +361,7 @@
EB60ED0E177F90BA005A041A /* WaitForTappableViewTests.m */,
EB60ED0F177F90BA005A041A /* WaitForViewTests.m */,
EB22B5AF17AF52640090B848 /* CascadingFailureTests.m */,
EB9FC00417E144B700138266 /* LandscapeTests.m */,
EB60ECF0177F8DB3005A041A /* Supporting Files */,
);
path = "KIF Tests";
Expand Down Expand Up @@ -573,6 +576,7 @@
EB60ED16177F90BA005A041A /* WaitForAbscenceTests.m in Sources */,
EB60ED17177F90BA005A041A /* WaitForTappableViewTests.m in Sources */,
EB60ED18177F90BA005A041A /* WaitForViewTests.m in Sources */,
EB9FC00517E144B700138266 /* LandscapeTests.m in Sources */,
EB3F654517AA0B8400469D18 /* TableViewTests.m in Sources */,
EB60ED1A177F90C2005A041A /* GestureTests.m in Sources */,
);
Expand Down

0 comments on commit ba31296

Please sign in to comment.