This repository was archived by the owner on Jul 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSpringBoard.m
More file actions
278 lines (220 loc) · 8.31 KB
/
Copy pathSpringBoard.m
File metadata and controls
278 lines (220 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "SpringBoard.h"
#import "CBX-XCTest-Umbrella.h"
#import "XCTest+CBXAdditions.h"
#import "Application.h"
#import "SpringBoardAlert.h"
#import "SpringBoardAlerts.h"
#import "GestureFactory.h"
#import "CBXException.h"
#import <UIKit/UIKit.h>
#import "CBXConstants.h"
#import "XCTest+CBXAdditions.h"
#import "CBXMachClock.h"
typedef enum : NSUInteger {
SpringBoardAlertHandlerIgnoringAlerts = 0,
SpringBoardAlertHandlerNoAlert,
SpringBoardAlertHandlerDismissedAlert,
SpringBoardAlertHandlerUnrecognizedAlert,
SpringBoardAlertHandlerFailed
} SpringBoardAlertHandlerResult;
@interface SpringBoard ()
- (BOOL)shouldDismissAlertsAutomatically;
- (SpringBoardAlertHandlerResult)handleAlert;
@end
@implementation SpringBoard
- (instancetype)initWithBundleIdentifier:(NSString *)bundleIdentifier {
self = [super initWithBundleIdentifier:bundleIdentifier];
if (self) {
_shouldDismissAlertsAutomatically = YES;
}
return self;
}
+ (instancetype)application {
static SpringBoard *_springBoard;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_springBoard = [[SpringBoard alloc] initWithBundleIdentifier:@"com.apple.springboard"];
[XCUIApplication cbxResolveApplication:_springBoard];
});
return _springBoard;
}
- (XCUIElement *)queryForAlert {
@synchronized (self) {
XCUIElement *alert = nil;
// Collect timing info
NSTimeInterval startTime = [[CBXMachClock sharedClock] absoluteTime];
XCUIElementQuery *query = [self descendantsMatchingType:XCUIElementTypeAlert];
NSArray <XCUIElement *> *elements = [query allElementsBoundByIndex];
if ([elements count] != 0) {
alert = elements[0];
}
NSTimeInterval elapsedSeconds = [[CBXMachClock sharedClock] absoluteTime] - startTime;
DDLogDebug(@"SpringBoard.queryForAlert took %@ seconds", @(elapsedSeconds));
return alert;
}
}
- (void)handleAlertsOrThrow {
@synchronized (self) {
if (![self shouldDismissAlertsAutomatically]) { return; }
SpringBoardAlertHandlerResult current = SpringBoardAlertHandlerNoAlert;
// There are fewer than 20 kinds of SpringBoard alerts.
NSUInteger maxTries = 20;
NSUInteger try = 0;
current = [self handleAlert];
while(current != SpringBoardAlertHandlerNoAlert && try < maxTries) {
current = [self handleAlert];
if (current == SpringBoardAlertHandlerUnrecognizedAlert) {
break;
}
try = try + 1;
}
if (try == maxTries || current == SpringBoardAlertHandlerUnrecognizedAlert) {
XCUIElement *alert = nil;
NSString *alertTitle = nil;
NSArray *alertButtonTitles = @[];
alert = [self queryForAlert];
if (alert && alert.exists) {
alertTitle = alert.label;
XCUIElementQuery *query = [alert descendantsMatchingType:XCUIElementTypeButton];
NSArray<XCUIElement *> *buttons = [query allElementsBoundByIndex];
if (buttons.count > 0) {
NSMutableArray *mutable = [NSMutableArray arrayWithCapacity:buttons.count];
for(XCUIElement *button in buttons) {
if (button.exists) {
NSString *name = button.label;
if (name) {
[mutable addObject:name];
}
}
}
alertButtonTitles = [NSArray arrayWithArray:mutable];
}
}
NSString *message;
message = @"A SpringBoard alert is blocking test execution and it cannot be dismissed.";
@throw [CBXException withMessage:message
userInfo:@{
@"title" : alertTitle ?: [NSNull null],
@"buttons" : alertButtonTitles,
@"tries" : @(maxTries)
}];
}
}
}
- (XCUIElement *)findDismissButtonOnAlert: (XCUIElement *) alert marks: (NSArray *) marks {
XCUIElement *button = nil;
for (NSString *mark in marks) {
button = alert.buttons[mark];
// Resolve before asking if the button exists.
[button cbx_resolve];
if (button && button.exists) {
return button;
}
}
return button;
}
// If something goes wrong, SpringBoardAlertHandlerNoAlert is returned.
// This method is not protected by a lock! It should only be called by
// handleAlertsOrThrow
- (SpringBoardAlertHandlerResult)handleAlert {
XCUIElement *alert = [self queryForAlert];
// There is not alert.
if (!alert || !alert.exists) {
return SpringBoardAlertHandlerNoAlert;
}
// .label is the title for English and German. Hopefully for others too.
NSString *title = alert.label;
SpringBoardAlert *springBoardAlert;
springBoardAlert = [[SpringBoardAlerts shared] alertMatchingTitle:title];
// We don't know about this alert.
if (!springBoardAlert) {
return SpringBoardAlertHandlerUnrecognizedAlert;
}
XCUIElement *button = nil;
NSArray *marks = springBoardAlert.defaultDismissButtonMarks;
// Alert is now gone? It can happen...
if (!alert.exists) {
return SpringBoardAlertHandlerNoAlert;
}
button = [self findDismissButtonOnAlert: alert marks: marks];
// A button with the expected title does not exist.
// It probably changed after an iOS update.
if (!button || !button.exists) {
button = nil;
}
// Use the default accept/deny button, but only if we recognize this alert.
if (!button) {
if (!alert.exists) {
return SpringBoardAlertHandlerNoAlert;
}
XCUIElementQuery *query = [alert descendantsMatchingType:XCUIElementTypeButton];
NSArray<XCUIElement *> *buttons = [query allElementsBoundByIndex];
if ([buttons count] == 0) {
return SpringBoardAlertHandlerNoAlert;
}
if (springBoardAlert.shouldAccept) {
switch ([buttons count]) {
case 1: {
// Single button alert
button = buttons[0];
break;
}
case 2: {
// Two button alert
button = buttons[1];
break;
}
case 3: {
// Three button alert
// Allow Location Always notification started popping this
// alert in iOS 11.
button = buttons[1];
break;
}
default: {
button = buttons.lastObject;
break;
}
}
} else {
button = buttons.firstObject;
}
}
// Resolve before asking if the button exists.
[button cbx_resolve];
if (!button || !button.exists) {
return SpringBoardAlertHandlerNoAlert;
}
@try {
[button tap];
} @catch (NSException *e) {
DDLogError(@"Caught an exception '%@': '%@'.", [e name], [e reason]);
return SpringBoardAlertHandlerFailed;
}
return SpringBoardAlertHandlerDismissedAlert;
}
- (SpringBoardDismissAlertResult)dismissAlertByTappingButtonWithTitle:(NSString *)title {
@synchronized (self) {
XCUIElement *alert = [self queryForAlert];
if (!alert) {
return SpringBoardDismissAlertNoAlert;
} else {
XCUIElement *button = alert.buttons[title];
[button cbx_resolve];
if (!button || !button.exists) {
return SpringBoardDismissAlertNoMatchingButton;
}
[button tap];
return SpringBoardDismissAlertDismissedAlert;
}
}
}
@end