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