|
| 1 | +#import "RunnerSynthesizedGesture.h" |
| 2 | + |
| 3 | +#import <CoreGraphics/CoreGraphics.h> |
| 4 | +#import <math.h> |
| 5 | +#import <objc/message.h> |
| 6 | + |
| 7 | +static const double RunnerTransformGestureRadius = 80.0; |
| 8 | + |
| 9 | +typedef long long (*RunnerMsgSendLongLong)(id, SEL); |
| 10 | +typedef id (*RunnerMsgSendInitRecord)(id, SEL, NSString *, long long); |
| 11 | +typedef id (*RunnerMsgSendInitPath)(id, SEL, CGPoint, NSTimeInterval); |
| 12 | +typedef void (*RunnerMsgSendPathMove)(id, SEL, CGPoint, NSTimeInterval); |
| 13 | +typedef void (*RunnerMsgSendPathOffset)(id, SEL, NSTimeInterval); |
| 14 | +typedef void (*RunnerMsgSendAddPath)(id, SEL, id); |
| 15 | +typedef void (*RunnerMsgSendSetLongLong)(id, SEL, long long); |
| 16 | +typedef BOOL (*RunnerMsgSendSynthesize)(id, SEL, NSError **); |
| 17 | + |
| 18 | +typedef struct RunnerPointerPair { |
| 19 | + CGPoint a; |
| 20 | + CGPoint b; |
| 21 | +} RunnerPointerPair; |
| 22 | + |
| 23 | +static long long RunnerInterfaceOrientationForApplication(id application); |
| 24 | +static long long RunnerProcessIDForApplication(id application); |
| 25 | +static RunnerPointerPair RunnerPointerPairAt( |
| 26 | + double x, |
| 27 | + double y, |
| 28 | + double dx, |
| 29 | + double dy, |
| 30 | + double scale, |
| 31 | + double baseRadius, |
| 32 | + double t |
| 33 | +); |
| 34 | + |
| 35 | +@implementation RunnerSynthesizedGesture |
| 36 | + |
| 37 | ++ (NSString * _Nullable)synthesizeTransformWithApplication:(id)application |
| 38 | + x:(double)x |
| 39 | + y:(double)y |
| 40 | + dx:(double)dx |
| 41 | + dy:(double)dy |
| 42 | + scale:(double)scale |
| 43 | + durationMs:(double)durationMs { |
| 44 | + @try { |
| 45 | + return [self trySynthesizeTransformWithApplication:application |
| 46 | + x:x |
| 47 | + y:y |
| 48 | + dx:dx |
| 49 | + dy:dy |
| 50 | + scale:scale |
| 51 | + durationMs:durationMs]; |
| 52 | + } @catch (NSException *exception) { |
| 53 | + NSString *name = exception.name ?: @"NSException"; |
| 54 | + NSString *reason = exception.reason ?: @"private XCTest event synthesis failed"; |
| 55 | + return [NSString stringWithFormat:@"%@: %@", name, reason]; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | ++ (NSString * _Nullable)trySynthesizeTransformWithApplication:(id)application |
| 60 | + x:(double)x |
| 61 | + y:(double)y |
| 62 | + dx:(double)dx |
| 63 | + dy:(double)dy |
| 64 | + scale:(double)scale |
| 65 | + durationMs:(double)durationMs { |
| 66 | + Class recordClass = NSClassFromString(@"XCSynthesizedEventRecord"); |
| 67 | + Class pathClass = NSClassFromString(@"XCPointerEventPath"); |
| 68 | + if (recordClass == Nil || pathClass == Nil) { |
| 69 | + return @"private XCTest event synthesis unavailable: missing XCUIAutomation classes"; |
| 70 | + } |
| 71 | + |
| 72 | + SEL initRecordSelector = NSSelectorFromString(@"initWithName:interfaceOrientation:"); |
| 73 | + SEL addPathSelector = NSSelectorFromString(@"addPointerEventPath:"); |
| 74 | + SEL setTargetProcessIDSelector = NSSelectorFromString(@"setTargetProcessID:"); |
| 75 | + SEL synthesizeSelector = NSSelectorFromString(@"synthesizeWithError:"); |
| 76 | + SEL initPathSelector = NSSelectorFromString(@"initForTouchAtPoint:offset:"); |
| 77 | + SEL moveSelector = NSSelectorFromString(@"moveToPoint:atOffset:"); |
| 78 | + SEL liftSelector = NSSelectorFromString(@"liftUpAtOffset:"); |
| 79 | + if (![recordClass instancesRespondToSelector:initRecordSelector] || |
| 80 | + ![recordClass instancesRespondToSelector:addPathSelector] || |
| 81 | + ![recordClass instancesRespondToSelector:setTargetProcessIDSelector] || |
| 82 | + ![recordClass instancesRespondToSelector:synthesizeSelector] || |
| 83 | + ![pathClass instancesRespondToSelector:initPathSelector] || |
| 84 | + ![pathClass instancesRespondToSelector:moveSelector] || |
| 85 | + ![pathClass instancesRespondToSelector:liftSelector]) { |
| 86 | + return @"private XCTest event synthesis unavailable: required selectors are missing"; |
| 87 | + } |
| 88 | + |
| 89 | + long long interfaceOrientation = RunnerInterfaceOrientationForApplication(application); |
| 90 | + long long targetProcessID = RunnerProcessIDForApplication(application); |
| 91 | + if (targetProcessID <= 0) { |
| 92 | + return @"private XCTest event synthesis unavailable: could not resolve target process ID"; |
| 93 | + } |
| 94 | + double radius = RunnerTransformGestureRadius; |
| 95 | + RunnerPointerPair start = RunnerPointerPairAt(x, y, dx, dy, scale, radius, 0.0); |
| 96 | + |
| 97 | + id record = ((RunnerMsgSendInitRecord)objc_msgSend)( |
| 98 | + [recordClass alloc], |
| 99 | + initRecordSelector, |
| 100 | + @"agent-device-transform", |
| 101 | + interfaceOrientation |
| 102 | + ); |
| 103 | + if (record == nil) { |
| 104 | + return @"private XCTest event synthesis failed: could not create event record"; |
| 105 | + } |
| 106 | + ((RunnerMsgSendSetLongLong)objc_msgSend)(record, setTargetProcessIDSelector, targetProcessID); |
| 107 | + |
| 108 | + id pathA = [self pathWithClass:pathClass |
| 109 | + start:start.a |
| 110 | + x:x |
| 111 | + y:y |
| 112 | + dx:dx |
| 113 | + dy:dy |
| 114 | + scale:scale |
| 115 | + radius:radius |
| 116 | + durationMs:durationMs |
| 117 | + finger:0]; |
| 118 | + id pathB = [self pathWithClass:pathClass |
| 119 | + start:start.b |
| 120 | + x:x |
| 121 | + y:y |
| 122 | + dx:dx |
| 123 | + dy:dy |
| 124 | + scale:scale |
| 125 | + radius:radius |
| 126 | + durationMs:durationMs |
| 127 | + finger:1]; |
| 128 | + if (pathA == nil || pathB == nil) { |
| 129 | + return @"private XCTest event synthesis failed: could not create pointer paths"; |
| 130 | + } |
| 131 | + |
| 132 | + ((RunnerMsgSendAddPath)objc_msgSend)(record, addPathSelector, pathA); |
| 133 | + ((RunnerMsgSendAddPath)objc_msgSend)(record, addPathSelector, pathB); |
| 134 | + |
| 135 | + NSError *error = nil; |
| 136 | + BOOL ok = ((RunnerMsgSendSynthesize)objc_msgSend)(record, synthesizeSelector, &error); |
| 137 | + if (!ok) { |
| 138 | + NSString *detail = error.localizedDescription ?: @"synthesizeWithError returned false"; |
| 139 | + return [NSString stringWithFormat:@"private XCTest event synthesis failed: %@", detail]; |
| 140 | + } |
| 141 | + return nil; |
| 142 | +} |
| 143 | + |
| 144 | +static long long RunnerInterfaceOrientationForApplication(id application) { |
| 145 | + SEL interfaceOrientationSelector = NSSelectorFromString(@"interfaceOrientation"); |
| 146 | + if (![application respondsToSelector:interfaceOrientationSelector]) { |
| 147 | + return 1; |
| 148 | + } |
| 149 | + long long interfaceOrientation = |
| 150 | + ((RunnerMsgSendLongLong)objc_msgSend)(application, interfaceOrientationSelector); |
| 151 | + return interfaceOrientation > 0 ? interfaceOrientation : 1; |
| 152 | +} |
| 153 | + |
| 154 | +static long long RunnerProcessIDForApplication(id application) { |
| 155 | + SEL processIDSelector = NSSelectorFromString(@"processID"); |
| 156 | + if (![application respondsToSelector:processIDSelector]) { |
| 157 | + return 0; |
| 158 | + } |
| 159 | + return ((RunnerMsgSendLongLong)objc_msgSend)(application, processIDSelector); |
| 160 | +} |
| 161 | + |
| 162 | ++ (id)pathWithClass:(Class)pathClass |
| 163 | + start:(CGPoint)start |
| 164 | + x:(double)x |
| 165 | + y:(double)y |
| 166 | + dx:(double)dx |
| 167 | + dy:(double)dy |
| 168 | + scale:(double)scale |
| 169 | + radius:(double)radius |
| 170 | + durationMs:(double)durationMs |
| 171 | + finger:(int)finger { |
| 172 | + SEL initPathSelector = NSSelectorFromString(@"initForTouchAtPoint:offset:"); |
| 173 | + SEL moveSelector = NSSelectorFromString(@"moveToPoint:atOffset:"); |
| 174 | + SEL liftSelector = NSSelectorFromString(@"liftUpAtOffset:"); |
| 175 | + |
| 176 | + id path = ((RunnerMsgSendInitPath)objc_msgSend)([pathClass alloc], initPathSelector, start, 0.0); |
| 177 | + if (path == nil) { |
| 178 | + return nil; |
| 179 | + } |
| 180 | + |
| 181 | + int frameCount = MAX(3, (int)lround(durationMs / 16.0)); |
| 182 | + NSTimeInterval durationSeconds = durationMs / 1000.0; |
| 183 | + for (int index = 1; index < frameCount; index += 1) { |
| 184 | + double t = (double)index / (double)frameCount; |
| 185 | + RunnerPointerPair pair = RunnerPointerPairAt(x, y, dx, dy, scale, radius, t); |
| 186 | + CGPoint point = finger == 0 ? pair.a : pair.b; |
| 187 | + NSTimeInterval offset = durationSeconds * t; |
| 188 | + ((RunnerMsgSendPathMove)objc_msgSend)(path, moveSelector, point, offset); |
| 189 | + } |
| 190 | + |
| 191 | + ((RunnerMsgSendPathOffset)objc_msgSend)(path, liftSelector, durationSeconds); |
| 192 | + return path; |
| 193 | +} |
| 194 | + |
| 195 | +static RunnerPointerPair RunnerPointerPairAt( |
| 196 | + double x, |
| 197 | + double y, |
| 198 | + double dx, |
| 199 | + double dy, |
| 200 | + double scale, |
| 201 | + double baseRadius, |
| 202 | + double t |
| 203 | +) { |
| 204 | + double centerX = x + dx * t; |
| 205 | + double centerY = y + dy * t; |
| 206 | + double startRadius = baseRadius / MAX(scale, 1.0); |
| 207 | + double endRadius = baseRadius; |
| 208 | + if (scale < 1.0) { |
| 209 | + startRadius = baseRadius; |
| 210 | + endRadius = baseRadius * scale; |
| 211 | + } |
| 212 | + double radius = startRadius + (endRadius - startRadius) * t; |
| 213 | + RunnerPointerPair pair = { |
| 214 | + .a = CGPointMake(centerX, centerY - radius), |
| 215 | + .b = CGPointMake(centerX, centerY + radius), |
| 216 | + }; |
| 217 | + return pair; |
| 218 | +} |
| 219 | + |
| 220 | +@end |
0 commit comments