-
Notifications
You must be signed in to change notification settings - Fork 917
/
Copy pathUIAutomationHelper.m
235 lines (195 loc) · 8.48 KB
/
UIAutomationHelper.m
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
//
// UIAutomationHelper.m
// KIF
//
// Created by Joe Masilotti on 12/1/14.
//
//
#import "UIAutomationHelper.h"
#import <dlfcn.h>
#import <objc/runtime.h>
#import "UIApplication-KIFAdditions.h"
@interface UIAXElement : NSObject
- (BOOL)isValid;
@end
@interface UIAElement : NSObject <NSCopying>
- (void)tap;
- (void)tapWithOptions:(NSDictionary *)options;
- (NSNumber *)pid;
- (UIAXElement *)uiaxElement;
- (NSArray<UIAElement *> *)elements;
@end
@interface UIAElementArray : NSArray
- (id)firstWithPredicate:(id)predicate;
@end
@interface UIAAlert : UIAElement
- (NSArray *)buttons;
- (BOOL)isValid;
- (BOOL)isVisible;
@end
@interface UIAApplication : UIAElement
- (UIAAlert *)alert;
- (NSArray<UIAElement *> *)windows;
- (NSString *)name;
- (id)appItemScrollView;
@end
@interface UIATarget : UIAElement
+ (UIATarget *)localTarget;
- (UIAApplication *)frontMostApp;
- (void)deactivateAppForDuration:(NSNumber *)duration;
@end
@interface UIAElementNil : UIAElement
@end
@implementation UIAutomationHelper
static UIAApplication * (*frontMostAppIMP)(id, SEL);
static id (*firstWithPredicateIMP)(id, SEL, id);
static UIAApplication * KIF_frontMostApp(id self, SEL _cmd)
{
UIAApplication *frontMostApp = frontMostAppIMP(self, _cmd);
if (![frontMostApp name] && [@(getpid()) isEqual:[frontMostApp pid]]) {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *appName = [mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"] ?: [mainBundle objectForInfoDictionaryKey:@"CFBundleName"];
[frontMostApp setValue:appName forKey:@"name"];
}
return frontMostApp;
}
static id KIF_firstWithPredicate(id self, SEL _cmd, id predicate)
{
NSArray *callStackSymbols = [NSThread callStackSymbols];
if (callStackSymbols.count > 1 && [callStackSymbols[1] containsString:@"-[UIATarget reactivateApp]"]) {
id firstWithPredicate = firstWithPredicateIMP(self, _cmd, predicate);
// -[UIATarget reactivateApp] was not rewritten for the new iOS 9 app switcher
return [firstWithPredicate isValid] ? firstWithPredicate : [[[[UIAutomationHelper sharedHelper] target] frontMostApp] appItemScrollView];
} else {
return firstWithPredicateIMP(self, _cmd, predicate);
}
}
static void FixReactivateApp(void)
{
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] && [processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9, 0, 0}]) {
// Workaround bug in iOS 9: https://github.com/kif-framework/KIF/issues/703
Method frontMostApp = class_getInstanceMethod(objc_lookUpClass("UIATarget"), @selector(frontMostApp));
frontMostAppIMP = (__typeof__(frontMostAppIMP))method_getImplementation(frontMostApp);
method_setImplementation(frontMostApp, (IMP)KIF_frontMostApp);
Method firstWithPredicate = class_getInstanceMethod(objc_lookUpClass("UIAElementArray"), @selector(firstWithPredicate:));
firstWithPredicateIMP = (__typeof__(firstWithPredicateIMP))method_getImplementation(firstWithPredicate);
method_setImplementation(firstWithPredicate, (IMP)KIF_firstWithPredicate);
}
}
+ (UIAutomationHelper *)sharedHelper
{
static dispatch_once_t once;
static UIAutomationHelper *sharedHelper = nil;
dispatch_once(&once, ^{
sharedHelper = [[self alloc] init];
[sharedHelper linkAutomationFramework];
});
return sharedHelper;
}
+ (BOOL)acknowledgeSystemAlert {
return [[self sharedHelper] acknowledgeSystemAlert];
}
+ (BOOL)acknowledgeSystemAlertWithIndex:(NSUInteger)index {
return [[self sharedHelper] acknowledgeSystemAlertWithIndex:index];
}
+ (void)deactivateAppForDuration:(NSNumber *)duration {
[[self sharedHelper] deactivateAppForDuration:duration];
}
- (UIAAlert *)currentSystemAlert
{
UIAApplication *application = [[self target] frontMostApp];
UIAAlert *alert;
if (@available(iOS 13.1, *)) {
// application.alert returns UIAElementNil on iOS 13.1
// Instead find the alert by looking for the alert's window and getting the UIAAlert off of it
alert = (UIAAlert *)[[[[[application windows] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIAElement *_Nullable evaluatedObject, NSDictionary<NSString *, id> *_Nullable bindings) {
return [[evaluatedObject valueForKey:@"type"] isEqualToString:@"SBAlertItemWindow"];
}]] firstObject] elements] firstObject];
} else {
alert = application.alert;
}
return alert;
}
- (BOOL)acknowledgeSystemAlert {
UIAAlert *alert = [self currentSystemAlert];
// Even though `acknowledgeSystemAlertWithIndex:` checks the index, we have to have
// an additional check here to ensure that when `alert.buttons.count` is 0, subtracting one doesn't cause a wrap-around (2^63 - 1).
if (alert.buttons.count > 0) {
return [self acknowledgeSystemAlertWithIndex:alert.buttons.count - 1];
}
return NO;
}
// Inspired by: https://github.com/jamesjn/KIF/tree/acknowledge-location-alert
- (BOOL)acknowledgeSystemAlertWithIndex:(NSUInteger)index {
UIAAlert *alert = [self currentSystemAlert];
BOOL isIndexInRange = index < alert.buttons.count;
if (![alert isKindOfClass:[self nilElementClass]] && [self _alertIsValidAndVisible:alert] && isIndexInRange) {
[alert.buttons[index] tap];
while ([self _alertIsValidAndVisible:alert]) {
// Wait for button press to complete.
KIFRunLoopRunInModeRelativeToAnimationSpeed(UIApplicationCurrentRunMode, 0.1, false);
}
// Wait for alert dismissial animation.
KIFRunLoopRunInModeRelativeToAnimationSpeed(UIApplicationCurrentRunMode, 0.4, false);
return YES;
}
return NO;
}
- (void)deactivateAppForDuration:(NSNumber *)duration {
@try {
[[self target] deactivateAppForDuration:duration];
}
@catch(NSException *e) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
NSOperatingSystemVersion iOS11 = {11, 0, 0};
NSAssert([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)] && [[NSProcessInfo new] isOperatingSystemAtLeastVersion:iOS11], @"The issue of resuming from SpringBoard is only known to occur on iOS 11+.");
NSAssert([[[[self target] frontMostApp] name] isEqual:@"SpringBoard"], @"If reactivation is failing, the app is likely still open to SpringBoard.");
// Tap slightly above the middle of the screen, otherwise it doesn't resume on an iPad Pro
[[[self target] frontMostApp] tapWithOptions:@{@"tapOffset": @{@"x": @(.5), @"y": @(.36)}}];
// Wait for app to foreground
CFRunLoopRunInMode(UIApplicationCurrentRunMode, 0.1, false);
// Ensure our test app has returned to being the front most app
NSString *testAppName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSAssert([[[[self target] frontMostApp] name] isEqual:testAppName], @"After tapping, the main app should be relaunched.");
#pragma clang diagnostic pop
}
}
#pragma mark - Private
- (BOOL)_alertIsValidAndVisible:(UIAAlert *)alert;
{
// Ignore alert if in process; calling isVisible on alert in process causes a signal such as EXC_BAD_INSTRUCTION (not a catchable exception)
UIAApplication *application = [[self target] frontMostApp];
if ([@(getpid()) isEqual:[application pid]])
return false;
// [alert isValid] is returning an __NSCFBoolean which is really hard to compare against.
// Translate the __NSCFBoolean into a vanilla BOOL.
// See https://www.bignerdranch.com/blog/bools-sharp-corners/ for more details.
BOOL visible = NO;
@try {
visible = [[alert valueForKeyPath:@"isVisible"] boolValue];
}
@catch (NSException *exception) { }
return ([alert isValid] && visible);
}
- (void)linkAutomationFramework {
dlopen([@"/Developer/Library/PrivateFrameworks/UIAutomation.framework/UIAutomation" fileSystemRepresentation], RTLD_LOCAL);
FixReactivateApp();
// Keep trying until the accessibility server starts up (it takes a little while on iOS 7)
UIATarget *target = nil;
while (!target) {
@try {
target = [self target];
}
@catch (NSException *exception) { }
@finally { }
}
}
- (UIATarget *)target {
return [NSClassFromString(@"UIATarget") localTarget];
}
- (Class)nilElementClass {
return NSClassFromString(@"UIAElementNil");
}
@end