-
Notifications
You must be signed in to change notification settings - Fork 125
feat: support iOS simulator transform gesture #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...ner/AgentDeviceRunner/AgentDeviceRunnerUITests/AgentDeviceRunnerUITests-Bridging-Header.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #import "RunnerObjCExceptionCatcher.h" | ||
| #import "RunnerSynthesizedGesture.h" |
19 changes: 19 additions & 0 deletions
19
ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedGesture.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #import <Foundation/Foundation.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @interface RunnerSynthesizedGesture : NSObject | ||
|
|
||
| + (NSString * _Nullable)synthesizeTransformWithApplication:(id)application | ||
| x:(double)x | ||
| y:(double)y | ||
| dx:(double)dx | ||
| dy:(double)dy | ||
| scale:(double)scale | ||
| degrees:(double)degrees | ||
| radius:(double)radius | ||
| durationMs:(double)durationMs; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
297 changes: 297 additions & 0 deletions
297
ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedGesture.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| #import "RunnerSynthesizedGesture.h" | ||
|
|
||
| #import <CoreGraphics/CoreGraphics.h> | ||
| #import <math.h> | ||
| #import <objc/message.h> | ||
|
|
||
| typedef NSInteger (*RunnerMsgSendInteger)(id, SEL); | ||
| typedef id (*RunnerMsgSendInitRecord)(id, SEL, NSString *, NSInteger); | ||
| typedef id (*RunnerMsgSendInitPath)(id, SEL, CGPoint, NSTimeInterval); | ||
| typedef void (*RunnerMsgSendPathMove)(id, SEL, CGPoint, NSTimeInterval); | ||
| typedef void (*RunnerMsgSendPathOffset)(id, SEL, NSTimeInterval); | ||
| typedef void (*RunnerMsgSendAddPath)(id, SEL, id); | ||
| typedef void (*RunnerMsgSendSetInteger)(id, SEL, NSInteger); | ||
| typedef BOOL (*RunnerMsgSendSynthesize)(id, SEL, NSError **); | ||
|
|
||
| typedef struct { | ||
| Class recordClass; | ||
| Class pathClass; | ||
| SEL initRecordSelector; | ||
| SEL addPathSelector; | ||
| SEL setTargetProcessIDSelector; | ||
| SEL synthesizeSelector; | ||
| SEL interfaceOrientationSelector; | ||
| SEL processIDSelector; | ||
| SEL initPathSelector; | ||
| SEL moveSelector; | ||
| SEL liftSelector; | ||
| } RunnerXCTestEventBridge; | ||
|
|
||
| static NSString * _Nullable RunnerResolveXCTestEventBridge( | ||
| id application, | ||
| RunnerXCTestEventBridge *bridge | ||
| ); | ||
| static NSString * _Nullable RunnerRequireClass(Class cls, NSString *className); | ||
| static NSString * _Nullable RunnerRequireSelector(Class cls, SEL selector, NSString *selectorName); | ||
| static NSString * _Nullable RunnerRequireApplicationSelector(id application, SEL selector, NSString *selectorName); | ||
| static id RunnerPointerPath( | ||
| const RunnerXCTestEventBridge *bridge, | ||
| CGPoint start, | ||
| double x, | ||
| double y, | ||
| double dx, | ||
| double dy, | ||
| double scale, | ||
| double degrees, | ||
| double radius, | ||
| double durationMs, | ||
| double side | ||
| ); | ||
| static CGPoint RunnerPointerPointAt( | ||
| double x, | ||
| double y, | ||
| double dx, | ||
| double dy, | ||
| double scale, | ||
| double degrees, | ||
| double baseRadius, | ||
| double t, | ||
| double side | ||
| ); | ||
|
|
||
| @implementation RunnerSynthesizedGesture | ||
|
|
||
| + (NSString * _Nullable)synthesizeTransformWithApplication:(id)application | ||
| x:(double)x | ||
| y:(double)y | ||
| dx:(double)dx | ||
| dy:(double)dy | ||
| scale:(double)scale | ||
| degrees:(double)degrees | ||
| radius:(double)radius | ||
| durationMs:(double)durationMs { | ||
| @try { | ||
| return [self trySynthesizeTransformWithApplication:application | ||
| x:x | ||
| y:y | ||
| dx:dx | ||
| dy:dy | ||
| scale:scale | ||
| degrees:degrees | ||
| radius:radius | ||
| durationMs:durationMs]; | ||
| } @catch (NSException *exception) { | ||
| NSString *name = exception.name ?: @"NSException"; | ||
| NSString *reason = exception.reason ?: @"private XCTest event synthesis failed"; | ||
| return [NSString stringWithFormat:@"%@: %@", name, reason]; | ||
| } | ||
| } | ||
|
|
||
| + (NSString * _Nullable)trySynthesizeTransformWithApplication:(id)application | ||
| x:(double)x | ||
| y:(double)y | ||
| dx:(double)dx | ||
| dy:(double)dy | ||
| scale:(double)scale | ||
| degrees:(double)degrees | ||
| radius:(double)radius | ||
| durationMs:(double)durationMs { | ||
| RunnerXCTestEventBridge bridge; | ||
| NSString *missing = RunnerResolveXCTestEventBridge(application, &bridge); | ||
| if (missing != nil) { | ||
| return missing; | ||
| } | ||
|
|
||
| NSInteger interfaceOrientation = | ||
| ((RunnerMsgSendInteger)objc_msgSend)(application, bridge.interfaceOrientationSelector); | ||
| NSInteger targetProcessID = ((RunnerMsgSendInteger)objc_msgSend)(application, bridge.processIDSelector); | ||
| if (targetProcessID <= 0) { | ||
| return @"private XCTest event synthesis unavailable: could not resolve target process ID"; | ||
| } | ||
|
|
||
| id record = ((RunnerMsgSendInitRecord)objc_msgSend)( | ||
| [bridge.recordClass alloc], | ||
| bridge.initRecordSelector, | ||
| @"agent-device-transform", | ||
| interfaceOrientation | ||
| ); | ||
| if (record == nil) { | ||
| return @"private XCTest event synthesis failed: could not create event record"; | ||
| } | ||
| ((RunnerMsgSendSetInteger)objc_msgSend)(record, bridge.setTargetProcessIDSelector, targetProcessID); | ||
|
|
||
| double sides[] = {1.0, -1.0}; | ||
| for (int index = 0; index < 2; index += 1) { | ||
| double side = sides[index]; | ||
| id path = RunnerPointerPath( | ||
| &bridge, | ||
| RunnerPointerPointAt(x, y, dx, dy, scale, degrees, radius, 0.0, side), | ||
| x, | ||
| y, | ||
| dx, | ||
| dy, | ||
| scale, | ||
| degrees, | ||
| radius, | ||
| durationMs, | ||
| side | ||
| ); | ||
| if (path == nil) { | ||
| return @"private XCTest event synthesis failed: could not create pointer path"; | ||
| } | ||
| ((RunnerMsgSendAddPath)objc_msgSend)(record, bridge.addPathSelector, path); | ||
| } | ||
|
|
||
| NSError *error = nil; | ||
| BOOL ok = ((RunnerMsgSendSynthesize)objc_msgSend)(record, bridge.synthesizeSelector, &error); | ||
| if (!ok) { | ||
| NSString *detail = error.localizedDescription ?: @"synthesizeWithError returned false"; | ||
| return [NSString stringWithFormat:@"private XCTest event synthesis failed: %@", detail]; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| static NSString * _Nullable RunnerResolveXCTestEventBridge( | ||
| id application, | ||
| RunnerXCTestEventBridge *bridge | ||
| ) { | ||
| Class recordClass = NSClassFromString(@"XCSynthesizedEventRecord"); | ||
| Class pathClass = NSClassFromString(@"XCPointerEventPath"); | ||
| SEL initRecordSelector = NSSelectorFromString(@"initWithName:interfaceOrientation:"); | ||
| SEL addPathSelector = NSSelectorFromString(@"addPointerEventPath:"); | ||
| SEL setTargetProcessIDSelector = NSSelectorFromString(@"setTargetProcessID:"); | ||
| SEL synthesizeSelector = NSSelectorFromString(@"synthesizeWithError:"); | ||
| SEL interfaceOrientationSelector = NSSelectorFromString(@"interfaceOrientation"); | ||
| SEL processIDSelector = NSSelectorFromString(@"processID"); | ||
| SEL initPathSelector = NSSelectorFromString(@"initForTouchAtPoint:offset:"); | ||
| SEL moveSelector = NSSelectorFromString(@"moveToPoint:atOffset:"); | ||
| SEL liftSelector = NSSelectorFromString(@"liftUpAtOffset:"); | ||
|
|
||
| NSString *missing = RunnerRequireClass(recordClass, @"XCSynthesizedEventRecord"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireClass(pathClass, @"XCPointerEventPath"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(recordClass, initRecordSelector, @"initWithName:interfaceOrientation:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(recordClass, addPathSelector, @"addPointerEventPath:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(recordClass, setTargetProcessIDSelector, @"setTargetProcessID:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(recordClass, synthesizeSelector, @"synthesizeWithError:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(pathClass, initPathSelector, @"initForTouchAtPoint:offset:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(pathClass, moveSelector, @"moveToPoint:atOffset:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireSelector(pathClass, liftSelector, @"liftUpAtOffset:"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireApplicationSelector(application, interfaceOrientationSelector, @"interfaceOrientation"); | ||
| if (missing != nil) return missing; | ||
| missing = RunnerRequireApplicationSelector(application, processIDSelector, @"processID"); | ||
| if (missing != nil) return missing; | ||
|
|
||
| *bridge = (RunnerXCTestEventBridge){ | ||
| .recordClass = recordClass, | ||
| .pathClass = pathClass, | ||
| .initRecordSelector = initRecordSelector, | ||
| .addPathSelector = addPathSelector, | ||
| .setTargetProcessIDSelector = setTargetProcessIDSelector, | ||
| .synthesizeSelector = synthesizeSelector, | ||
| .interfaceOrientationSelector = interfaceOrientationSelector, | ||
| .processIDSelector = processIDSelector, | ||
| .initPathSelector = initPathSelector, | ||
| .moveSelector = moveSelector, | ||
| .liftSelector = liftSelector, | ||
| }; | ||
| return nil; | ||
| } | ||
|
|
||
| static NSString * _Nullable RunnerRequireClass(Class cls, NSString *className) { | ||
| if (cls == Nil) { | ||
| return [NSString stringWithFormat:@"private XCTest event synthesis unavailable: missing %@", className]; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| static NSString * _Nullable RunnerRequireSelector(Class cls, SEL selector, NSString *selectorName) { | ||
| if (![cls instancesRespondToSelector:selector]) { | ||
| return [NSString stringWithFormat: | ||
| @"private XCTest event synthesis unavailable: %@ missing %@", | ||
| NSStringFromClass(cls), | ||
| selectorName | ||
| ]; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| static NSString * _Nullable RunnerRequireApplicationSelector( | ||
| id application, | ||
| SEL selector, | ||
| NSString *selectorName | ||
| ) { | ||
| if (![application respondsToSelector:selector]) { | ||
| return [NSString stringWithFormat: | ||
| @"private XCTest event synthesis unavailable: XCUIApplication missing %@", | ||
| selectorName | ||
| ]; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| static id RunnerPointerPath( | ||
| const RunnerXCTestEventBridge *bridge, | ||
| CGPoint start, | ||
| double x, | ||
| double y, | ||
| double dx, | ||
| double dy, | ||
| double scale, | ||
| double degrees, | ||
| double radius, | ||
| double durationMs, | ||
| double side | ||
| ) { | ||
| id path = | ||
| ((RunnerMsgSendInitPath)objc_msgSend)([bridge->pathClass alloc], bridge->initPathSelector, start, 0.0); | ||
| if (path == nil) { | ||
| return nil; | ||
| } | ||
|
|
||
| int frameCount = MAX(3, (int)(durationMs / 16.0)); | ||
| NSTimeInterval durationSeconds = durationMs / 1000.0; | ||
| for (int index = 1; index <= frameCount; index += 1) { | ||
| double t = (double)index / (double)frameCount; | ||
| CGPoint point = RunnerPointerPointAt(x, y, dx, dy, scale, degrees, radius, t, side); | ||
| NSTimeInterval offset = durationSeconds * t; | ||
| ((RunnerMsgSendPathMove)objc_msgSend)(path, bridge->moveSelector, point, offset); | ||
| } | ||
|
|
||
| ((RunnerMsgSendPathOffset)objc_msgSend)(path, bridge->liftSelector, durationSeconds); | ||
| return path; | ||
| } | ||
|
|
||
| static CGPoint RunnerPointerPointAt( | ||
| double x, | ||
| double y, | ||
| double dx, | ||
| double dy, | ||
| double scale, | ||
| double degrees, | ||
| double baseRadius, | ||
| double t, | ||
| double side | ||
| ) { | ||
| double centerX = x + dx * t; | ||
| double centerY = y + dy * t; | ||
| double startRadius = baseRadius / MAX(scale, 1.0); | ||
| double endRadius = baseRadius; | ||
| if (scale < 1.0) { | ||
| startRadius = baseRadius; | ||
| endRadius = baseRadius * scale; | ||
| } | ||
| double radius = startRadius + (endRadius - startRadius) * t; | ||
| double angle = (-M_PI_2) + (degrees * M_PI / 180.0) * t; | ||
| return CGPointMake(centerX + cos(angle) * radius * side, centerY + sin(angle) * radius * side); | ||
| } | ||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.