Skip to content

Commit

Permalink
UIAutomationHelper.m: fix acknowledgeSystemAlert to hang infinitely i…
Browse files Browse the repository at this point in the history
…n case of multiple alerts
  • Loading branch information
bagage committed Jul 18, 2015
1 parent 829ae5f commit 7cf0630
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Classes/KIFUITestActor.h
Expand Up @@ -435,10 +435,10 @@ static inline KIFDisplacement KIFDisplacementForSwipingInDirection(KIFSwipeDirec

#if TARGET_IPHONE_SIMULATOR
/*!
@abstract If present, dismisses a system alert with the last button, usually 'Allow'.
@abstract If present, dismisses a system alert with the last button, usually 'Allow'. Returns YES if a dialog was dismissed, NO otherwise.
@discussion Use this to dissmiss a location services authorization dialog or a photos access dialog by tapping the 'Allow' button. No action is taken if no alert is present.
*/
- (void)acknowledgeSystemAlert;
- (BOOL)acknowledgeSystemAlert;
#endif

/*!
Expand Down
4 changes: 2 additions & 2 deletions Classes/KIFUITestActor.m
Expand Up @@ -774,8 +774,8 @@ - (void)tapItemAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessib
[self tapItemAtIndexPath:indexPath inCollectionView:collectionView];
}

- (void)acknowledgeSystemAlert {
[UIAutomationHelper acknowledgeSystemAlert];
- (BOOL)acknowledgeSystemAlert {
return [UIAutomationHelper acknowledgeSystemAlert];
}

- (void)tapItemAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
Expand Down
2 changes: 1 addition & 1 deletion Classes/UIAutomationHelper.h
Expand Up @@ -12,7 +12,7 @@

@interface UIAutomationHelper : NSObject

+ (void)acknowledgeSystemAlert;
+ (BOOL)acknowledgeSystemAlert;

+ (void)deactivateAppForDuration:(NSNumber *)duration;

Expand Down
24 changes: 14 additions & 10 deletions Classes/UIAutomationHelper.m
Expand Up @@ -8,13 +8,16 @@

#import "UIAutomationHelper.h"
#include <dlfcn.h>
#import <UIView-KIFAdditions.h>

@interface UIAElement : NSObject <NSCopying>
- (void)tap;
@end

@interface UIAAlert : UIAElement
- (NSArray *)buttons;
- (BOOL)isValid;
- (BOOL)isVisible;
@end

@interface UIAApplication : UIAElement
Expand Down Expand Up @@ -44,22 +47,23 @@ + (UIAutomationHelper *)sharedHelper
return sharedHelper;
}

+ (void)acknowledgeSystemAlert {
[[self sharedHelper] acknowledgeSystemAlert];
+ (BOOL)acknowledgeSystemAlert {
return [[self sharedHelper] acknowledgeSystemAlert];
}

+ (void)deactivateAppForDuration:(NSNumber *)duration {
[[self sharedHelper] deactivateAppForDuration:duration];
}

- (void)acknowledgeSystemAlert {
- (BOOL)acknowledgeSystemAlert {
UIAApplication *application = [[self target] frontMostApp];
UIAAlert *alert = application.alert;

if (![alert isKindOfClass:[self nilElementClass]]) {
[[alert.buttons lastObject] tap];
while (![application.alert isKindOfClass:[self nilElementClass]]) { }
}
UIAAlert* alert = application.alert;
if (![alert isKindOfClass:[self nilElementClass]]) {
[[alert.buttons lastObject] tap];
while ([alert isValid] && [alert isVisible]) {
}
return YES;
}
return NO;
}

- (void)deactivateAppForDuration:(NSNumber *)duration {
Expand Down
13 changes: 5 additions & 8 deletions KIF Tests/SystemAlertTests.m
Expand Up @@ -24,9 +24,11 @@ - (void)afterEach
[tester tapViewWithAccessibilityLabel:@"Test Suite" traits:UIAccessibilityTraitButton];
}

- (void)testAuthorizingLocationServices {
[tester tapViewWithAccessibilityLabel:@"Location Services"];
[tester acknowledgeSystemAlert];
- (void)testAuthorizingLocationServicesAndNotificationsScheduling {
[tester tapViewWithAccessibilityLabel:@"Location Services and Notifications"];
XCTAssertTrue([tester acknowledgeSystemAlert]);
XCTAssertTrue([tester acknowledgeSystemAlert]);
XCTAssertFalse([tester acknowledgeSystemAlert]);
}

- (void)testAuthorizingPhotosAccess {
Expand All @@ -35,9 +37,4 @@ - (void)testAuthorizingPhotosAccess {
[tester tapViewWithAccessibilityLabel:@"Cancel"];
}

- (void)testNotificationScheduling {
[tester tapViewWithAccessibilityLabel:@"Notifications"];
[tester acknowledgeSystemAlert];
}

@end
5 changes: 5 additions & 0 deletions Test Host/SystemAlertViewController.m
Expand Up @@ -39,4 +39,9 @@ - (IBAction)requestNotificationScheduling {
}
}

- (IBAction)requestLocationServicesAndNotificicationsSchedulingAccesses {
[self requestLocationServicesAccess];
[self requestNotificationScheduling];
}

@end
19 changes: 17 additions & 2 deletions Test Host/en.lproj/MainStoryboard.storyboard
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" initialViewController="3">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="7706" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" initialViewController="3">
<dependencies>
<deployment identifier="iOS"/>
<development version="4600" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
Expand Down Expand Up @@ -1855,6 +1855,21 @@ Line Break
<action selector="requestNotificationScheduling" destination="k4d-l4-aKf" eventType="touchUpInside" id="iWn-UO-XJV"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="NYu-v0-8ac">
<rect key="frame" x="24" y="182" width="273" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" title="Location Services and Notifications">
<color key="titleColor" red="0.19607843137254902" green="0.30980392156862746" blue="0.52156862745098043" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="requestLocationServicesAndNotificicationsSchedulingAccesses" destination="k4d-l4-aKf" eventType="touchUpInside" id="up3-fx-DyS"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
Expand Down

0 comments on commit 7cf0630

Please sign in to comment.