@@ -11,24 +11,30 @@ @interface ImagePickerFromGalleryUITests : XCTestCase
11
11
12
12
@property (nonatomic , strong ) XCUIApplication *app;
13
13
14
+ @property (nonatomic , assign ) BOOL interceptedPermissionInterruption;
15
+
14
16
@end
15
17
16
18
@implementation ImagePickerFromGalleryUITests
17
19
18
20
- (void )setUp {
19
21
[super setUp ];
20
- // Delete the app if already exists, to test permission popups
21
22
22
23
self.continueAfterFailure = NO ;
23
24
self.app = [[XCUIApplication alloc ] init ];
25
+ if (@available (iOS 13.4 , *)) {
26
+ // Reset the authorization status for Photos to test permission popups
27
+ [self .app resetAuthorizationStatusForResource: XCUIProtectedResourcePhotos];
28
+ }
24
29
[self .app launch ];
30
+ self.interceptedPermissionInterruption = NO ;
25
31
__weak typeof (self) weakSelf = self;
26
32
[self addUIInterruptionMonitorWithDescription: @" Permission popups"
27
33
handler: ^BOOL (XCUIElement *_Nonnull interruptingElement) {
28
34
if (@available (iOS 14 , *)) {
29
35
XCUIElement *allPhotoPermission =
30
36
interruptingElement
31
- .buttons [@" Allow Access to All Photos " ];
37
+ .buttons [weakSelf.allowAccessPermissionText ];
32
38
if (![allPhotoPermission waitForExistenceWithTimeout:
33
39
kElementWaitingTime ]) {
34
40
os_log_error (OS_LOG_DEFAULT, " %@" ,
@@ -50,6 +56,7 @@ - (void)setUp {
50
56
}
51
57
[ok tap ];
52
58
}
59
+ weakSelf.interceptedPermissionInterruption = YES ;
53
60
return YES ;
54
61
}];
55
62
}
@@ -59,6 +66,46 @@ - (void)tearDown {
59
66
[self .app terminate ];
60
67
}
61
68
69
+ - (NSString *)allowAccessPermissionText {
70
+ NSString *fullAccessButtonText = @" Allow Access to All Photos" ;
71
+ if (@available (iOS 17 , *)) {
72
+ fullAccessButtonText = @" Allow Full Access" ;
73
+ }
74
+ return fullAccessButtonText;
75
+ }
76
+
77
+ - (void )handlePermissionInterruption {
78
+ // addUIInterruptionMonitorWithDescription is only invoked when trying to interact with an element
79
+ // (the app in this case) the alert is blocking. We expect a permission popup here so do a swipe
80
+ // up action (which should be harmless).
81
+ [self .app swipeUp ];
82
+
83
+ if (@available (iOS 17 , *)) {
84
+ // addUIInterruptionMonitorWithDescription does not work consistently on Xcode 15 simulators, so
85
+ // use a backup method of accepting permissions popup.
86
+
87
+ if (self.interceptedPermissionInterruption == YES ) {
88
+ return ;
89
+ }
90
+
91
+ // If cancel button exists, permission has already been given.
92
+ XCUIElement *cancelButton = self.app .buttons [@" Cancel" ].firstMatch ;
93
+ if ([cancelButton waitForExistenceWithTimeout: kElementWaitingTime ]) {
94
+ return ;
95
+ }
96
+
97
+ XCUIApplication *springboardApp =
98
+ [[XCUIApplication alloc ] initWithBundleIdentifier: @" com.apple.springboard" ];
99
+ XCUIElement *allowButton = springboardApp.buttons [self .allowAccessPermissionText];
100
+ if (![allowButton waitForExistenceWithTimeout: kElementWaitingTime ]) {
101
+ os_log_error (OS_LOG_DEFAULT, " %@" , self.app .debugDescription );
102
+ XCTFail (@" Failed due to not able to find Allow Access button with %@ seconds" ,
103
+ @(kElementWaitingTime ));
104
+ }
105
+ [allowButton tap ];
106
+ }
107
+ }
108
+
62
109
- (void )testCancel {
63
110
// Find and tap on the pick from gallery button.
64
111
XCUIElement *imageFromGalleryButton =
@@ -80,9 +127,7 @@ - (void)testCancel {
80
127
81
128
[pickButton tap ];
82
129
83
- // There is a known bug where the permission popups interruption won't get fired until a tap
84
- // happened in the app. We expect a permission popup so we do a tap here.
85
- [self .app tap ];
130
+ [self handlePermissionInterruption ];
86
131
87
132
// Find and tap on the `Cancel` button.
88
133
XCUIElement *cancelButton = self.app .buttons [@" Cancel" ].firstMatch ;
@@ -151,9 +196,7 @@ - (void)launchPickerAndPickWithMaxWidth:(NSNumber *)maxWidth
151
196
}
152
197
[pickButton tap ];
153
198
154
- // There is a known bug where the permission popups interruption won't get fired until a tap
155
- // happened in the app. We expect a permission popup so we do a tap here.
156
- [self .app tap ];
199
+ [self handlePermissionInterruption ];
157
200
158
201
// Find an image and tap on it. (IOS 14 UI, images are showing directly)
159
202
XCUIElement *aImage;
0 commit comments