appium-webdriveragent 17.0.6 → 17.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,20 +128,3 @@ BOOL FBScrollGestureOffsets(CGRect scrollingFrame,
128
128
  (endPoint.y - anchorFrame.origin.y) / anchorFrame.size.height);
129
129
  return YES;
130
130
  }
131
-
132
- #if !TARGET_OS_TV && !TARGET_OS_WATCH
133
- CGVector FBDisplayCoordinateOffset(CGSize mainSize, CGSize displaySize,
134
- UIInterfaceOrientation orientation)
135
- {
136
- CGVector offset = CGVectorMake(0, 0);
137
- if (orientation == UIInterfaceOrientationLandscapeRight
138
- || orientation == UIInterfaceOrientationPortraitUpsideDown) {
139
- offset.dx = displaySize.width - mainSize.width;
140
- }
141
- if (orientation == UIInterfaceOrientationLandscapeLeft
142
- || orientation == UIInterfaceOrientationPortraitUpsideDown) {
143
- offset.dy = displaySize.height - mainSize.height;
144
- }
145
- return offset;
146
- }
147
- #endif
@@ -17,6 +17,9 @@ NS_ASSUME_NONNULL_BEGIN
17
17
  */
18
18
  + (nullable NSArray<NSDictionary<NSString *, id> *> *)screensWithError:(NSError **)error;
19
19
 
20
+ /** Whether multiple displays are available, or nil if enumeration fails. */
21
+ + (nullable NSNumber *)hasMultipleScreensWithError:(NSError **)error;
22
+
20
23
  /**
21
24
  The display with the given identifier
22
25
 
@@ -56,6 +56,25 @@ static NSArray<XCUIScreen *> *FBScreensForDevice(id device, NSError **error)
56
56
  return result.copy;
57
57
  }
58
58
 
59
+ // Kept separate from sharedDevice so enumeration failures can be tested.
60
+ + (nullable NSNumber *)hasMultipleScreensForDevice:(id)device error:(NSError **)error
61
+ {
62
+ NSError *enumerationError = nil;
63
+ NSArray<XCUIScreen *> *screens = FBScreensForDevice(device, &enumerationError);
64
+ if (nil == screens) {
65
+ if (NULL != error) {
66
+ *error = enumerationError ?: [[FBErrorBuilder.builder withDescription:@"Could not enumerate device displays"] build];
67
+ }
68
+ return nil;
69
+ }
70
+ return @(screens.count > 1);
71
+ }
72
+
73
+ + (nullable NSNumber *)hasMultipleScreensWithError:(NSError **)error
74
+ {
75
+ return [self hasMultipleScreensForDevice:XCUIDevice.sharedDevice error:error];
76
+ }
77
+
59
78
  // Kept separate from sharedDevice so legacy lookup behavior can be tested.
60
79
  + (nullable XCUIScreen *)screenWithDisplayID:(long long)displayID
61
80
  device:(XCUIDevice *)device
@@ -15,7 +15,7 @@
15
15
  #import "FBMacros.h"
16
16
  #import "FBMathUtils.h"
17
17
  #import "FBProtocolHelpers.h"
18
- #import "FBScreen.h"
18
+ #import "FBGestureCoordinateContext.h"
19
19
  #import "FBW3CActionsHelpers.h"
20
20
  #import "FBXCodeCompatibility.h"
21
21
  #import "FBXCTestDaemonsProxy.h"
@@ -69,9 +69,8 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
69
69
  @interface FBW3CGestureItem : FBBaseGestureItem
70
70
 
71
71
  @property (nullable, readonly, nonatomic) FBW3CGestureItem *previousItem;
72
- @property (nonatomic) CGVector displayCorrection;
73
- @property (nonatomic) BOOL usesDisplayCorrection;
74
72
  @property (readonly, nonatomic) CGPoint screenPoint;
73
+ @property (nonatomic) FBGestureCoordinateContext *coordinateContext;
75
74
 
76
75
  @end
77
76
 
@@ -123,6 +122,7 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
123
122
  - (nullable instancetype)initWithActionItem:(NSDictionary<NSString *, id> *)actionItem
124
123
  application:(XCUIApplication *)application
125
124
  previousItem:(nullable FBW3CGestureItem *)previousItem
125
+ coordinateContext:(FBGestureCoordinateContext *)coordinateContext
126
126
  offset:(double)offset
127
127
  error:(NSError **)error
128
128
  {
@@ -132,7 +132,7 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
132
132
  self.application = application;
133
133
  self.offset = offset;
134
134
  _previousItem = previousItem;
135
- _usesDisplayCorrection = previousItem.usesDisplayCorrection;
135
+ _coordinateContext = coordinateContext;
136
136
  NSNumber *durationObj = FBOptDuration(actionItem, @0, error);
137
137
  if (nil == durationObj) {
138
138
  return nil;
@@ -150,10 +150,7 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
150
150
 
151
151
  - (CGPoint)screenPoint
152
152
  {
153
- CGPoint point = self.atPosition.screenPoint;
154
- return self.usesDisplayCorrection
155
- ? CGPointMake(point.x + self.displayCorrection.dx, point.y + self.displayCorrection.dy)
156
- : point;
153
+ return self.atPosition.screenPoint;
157
154
  }
158
155
 
159
156
  - (nullable XCUICoordinate *)positionWithError:(NSError **)error
@@ -172,27 +169,25 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
172
169
  positionOffset:(nullable NSValue *)positionOffset
173
170
  error:(NSError **)error
174
171
  {
175
- XCUIScreen *screen = nil;
176
- if (element.elementType == XCUIElementTypeApplication) {
177
- screen = [FBScreen currentScreenWithError:error];
178
- if (nil == screen) {
172
+ if (self.coordinateContext.usesWindowCoordinates) {
173
+ CGPoint point = nil == positionOffset ? CGPointZero : positionOffset.CGPointValue;
174
+ CGVector offset = CGVectorMake(point.x, point.y);
175
+ if (nil == element) {
176
+ return [self.coordinateContext coordinateWithApplication:self.application offset:offset error:error];
177
+ }
178
+ if (element.elementType == XCUIElementTypeApplication) {
179
+ XCUIElement *application = element;
180
+ return [self.coordinateContext coordinateWithApplicationOrigin:application offset:offset error:error];
181
+ }
182
+ // Resolve display affinity before combining element and viewport origins.
183
+ (void)element.frame;
184
+ if (element.screen.displayID != self.coordinateContext.screen.displayID) {
185
+ [[[FBErrorBuilder builder] withDescriptionFormat:
186
+ @"The element origin is not on the selected display %lld", self.coordinateContext.screen.displayID] buildError:error];
179
187
  return nil;
180
188
  }
181
189
  }
182
- if (nil != screen && screen.displayID != XCUIScreen.mainScreen.displayID) {
183
- // Unlike ordinary elements, application-root coordinates rotate using the
184
- // main display. Resolve the client-visible rect's center into viewport
185
- // coordinates, then let screenPoint apply the selected-display correction.
186
- // Do not use a normalized app center: XCTest may report a portrait app rect
187
- // for a landscape window, so its normalized center uses a different space.
188
- CGRect frame = element.wdFrame;
189
- if (CGRectIsEmpty(frame)) {
190
- return [super hitpointWithElement:element positionOffset:positionOffset error:error];
191
- }
192
- CGPoint offset = nil == positionOffset ? CGPointZero : positionOffset.CGPointValue;
193
- CGPoint point = CGPointMake(CGRectGetMidX(frame) + offset.x, CGRectGetMidY(frame) + offset.y);
194
- return [super hitpointWithElement:nil positionOffset:[NSValue valueWithCGPoint:point] error:error];
195
- }
190
+ // Single-display viewport and application origins retain the legacy path.
196
191
  if (nil == element || nil == positionOffset) {
197
192
  return [super hitpointWithElement:element positionOffset:positionOffset error:error];
198
193
  }
@@ -221,10 +216,11 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
221
216
  - (nullable instancetype)initWithActionItem:(NSDictionary<NSString *, id> *)actionItem
222
217
  application:(XCUIApplication *)application
223
218
  previousItem:(nullable FBW3CGestureItem *)previousItem
219
+ coordinateContext:(FBGestureCoordinateContext *)coordinateContext
224
220
  offset:(double)offset
225
221
  error:(NSError **)error
226
222
  {
227
- self = [super initWithActionItem:actionItem application:application previousItem:previousItem offset:offset error:error];
223
+ self = [super initWithActionItem:actionItem application:application previousItem:previousItem coordinateContext:coordinateContext offset:offset error:error];
228
224
  if (self) {
229
225
  _pressure = [actionItem objectForKey:FB_ACTION_ITEM_KEY_PRESSURE];
230
226
  }
@@ -315,10 +311,6 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
315
311
  }
316
312
 
317
313
  if (nil != element) {
318
- // Application origins need viewport display correction; other elements use their own display.
319
- // This correction does not fix XCTest's legacy-SDK compatibility issue:
320
- // see docs/multiple-displays.md#legacy-sdk-compatibility-windows.
321
- self.usesDisplayCorrection = element.elementType == XCUIElementTypeApplication;
322
314
  if (nil == x && nil == y) {
323
315
  return [self hitpointWithElement:element positionOffset:nil error:error];
324
316
  }
@@ -326,7 +318,6 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
326
318
  }
327
319
 
328
320
  if ([origin isKindOfClass:NSString.class] && [origin isEqualToString:FB_ORIGIN_TYPE_VIEWPORT]) {
329
- self.usesDisplayCorrection = YES;
330
321
  return [self hitpointWithElement:nil positionOffset:[NSValue valueWithCGPoint:CGPointMake(x.floatValue, y.floatValue)] error:error];
331
322
  }
332
323
 
@@ -678,10 +669,6 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
678
669
  @end
679
670
 
680
671
 
681
- @interface FBW3CActionsSynthesizer ()
682
- @property (nonatomic) CGVector displayCorrection;
683
- @end
684
-
685
672
  @implementation FBW3CActionsSynthesizer
686
673
 
687
674
  - (NSArray<NSDictionary<NSString *, id> *> *)preprocessedActionItemsWith:(NSArray<NSDictionary<NSString *, id> *> *)actionItems
@@ -798,7 +785,10 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
798
785
  return [chain asEventPathsWithError:error];
799
786
  }
800
787
 
801
- - (nullable NSArray<XCPointerEventPath *> *)eventPathsWithGestureAction:(NSDictionary<NSString *, id> *)actionDescription forActionId:(NSString *)actionId error:(NSError **)error
788
+ - (nullable NSArray<XCPointerEventPath *> *)eventPathsWithGestureAction:(NSDictionary<NSString *, id> *)actionDescription
789
+ forActionId:(NSString *)actionId
790
+ coordinateContext:(FBGestureCoordinateContext *)coordinateContext
791
+ error:(NSError **)error
802
792
  {
803
793
  static NSDictionary<NSString *, Class> *gestureItemsMapping;
804
794
  static NSArray<NSString *> *supportedActionItemTypes;
@@ -861,19 +851,21 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
861
851
  return nil;
862
852
  }
863
853
 
864
- FBW3CGestureItem *gestureItem = [[gestureItemClass alloc] initWithActionItem:actionItem application:self.application previousItem:[chain.items lastObject] offset:chain.durationOffset error:error];
854
+ FBW3CGestureItem *gestureItem = [[gestureItemClass alloc] initWithActionItem:actionItem application:self.application previousItem:[chain.items lastObject] coordinateContext:coordinateContext offset:chain.durationOffset error:error];
865
855
  if (nil == gestureItem) {
866
856
  return nil;
867
857
  }
868
858
 
869
- gestureItem.displayCorrection = self.displayCorrection;
870
859
  [chain addItem:gestureItem];
871
860
  }
872
861
 
873
862
  return [chain asEventPathsWithError:error];
874
863
  }
875
864
 
876
- - (nullable NSArray<XCPointerEventPath *> *)eventPathsWithActionDescription:(NSDictionary<NSString *, id> *)actionDescription forActionId:(NSString *)actionId error:(NSError **)error
865
+ - (nullable NSArray<XCPointerEventPath *> *)eventPathsWithActionDescription:(NSDictionary<NSString *, id> *)actionDescription
866
+ forActionId:(NSString *)actionId
867
+ coordinateContext:(FBGestureCoordinateContext *)coordinateContext
868
+ error:(NSError **)error
877
869
  {
878
870
  id actionType = [actionDescription objectForKey:FB_KEY_TYPE];
879
871
  if (![actionType isKindOfClass:NSString.class] ||
@@ -887,7 +879,7 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
887
879
  }
888
880
 
889
881
  if ([actionType isEqualToString:FB_ACTION_TYPE_POINTER]) {
890
- return [self eventPathsWithGestureAction:actionDescription forActionId:actionId error:error];
882
+ return [self eventPathsWithGestureAction:actionDescription forActionId:actionId coordinateContext:coordinateContext error:error];
891
883
  }
892
884
 
893
885
  return [self eventPathsWithKeyAction:actionDescription forActionId:actionId error:error];
@@ -947,41 +939,25 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
947
939
  return NO;
948
940
  }
949
941
 
950
- - (nullable XCSynthesizedEventRecord *)eventRecordForActions:(NSArray<NSDictionary<NSString *, id> *> *)actions
951
- error:(NSError **)error
942
+ - (nullable XCSynthesizedEventRecord *)eventRecordForContext:(FBGestureCoordinateContext *)coordinateContext
943
+ error:(NSError **)error
952
944
  {
953
- // Keyboard and pause-only sequences have no display coordinates. A display
954
- // disappearing must not prevent these input sources from being synthesized.
955
- XCUIScreen *screen = [self actionsRequireDisplay:actions] ? [FBScreen currentScreenWithError:error] : XCUIScreen.mainScreen;
956
- if (nil == screen) {
957
- return nil;
945
+ XCUIScreen *screen = coordinateContext.screen;
946
+ if (coordinateContext.usesWindowCoordinates
947
+ && [XCSynthesizedEventRecord instancesRespondToSelector:@selector(initWithName:displayID:interfaceOrientation:)]) {
948
+ return [[XCSynthesizedEventRecord alloc] initWithName:@"W3C Touch Action"
949
+ displayID:(unsigned long long)screen.displayID
950
+ interfaceOrientation:self.application.interfaceOrientation];
958
951
  }
959
- XCSynthesizedEventRecord *eventRecord;
960
- self.displayCorrection = CGVectorMake(0, 0);
961
- if (screen.displayID == XCUIScreen.mainScreen.displayID) {
962
- eventRecord = [[XCSynthesizedEventRecord alloc] initWithName:@"W3C Touch Action"
963
- interfaceOrientation:self.application.interfaceOrientation];
964
- } else {
965
- if (![XCSynthesizedEventRecord instancesRespondToSelector:@selector(initWithName:displayID:interfaceOrientation:)]) {
966
- if (error) {
967
- *error = [[FBErrorBuilder.builder
968
- withDescription:@"Actions on a display other than the main one are not supported by this XCTest version"] build];
969
- }
970
- return nil;
952
+ if (screen.displayID != XCUIScreen.mainScreen.displayID) {
953
+ if (error) {
954
+ *error = [[FBErrorBuilder.builder
955
+ withDescription:@"Actions on a display other than the main one are not supported by this XCTest version"] build];
971
956
  }
972
- eventRecord = [[XCSynthesizedEventRecord alloc] initWithName:@"W3C Touch Action"
973
- displayID:(unsigned long long)screen.displayID
974
- interfaceOrientation:self.application.interfaceOrientation];
975
- // Viewport coordinates use the main display's size when rotating screenPoint.
976
- // Element coordinates already use their own display. Apply this correction
977
- // per gesture item so mixed origins and pointer-relative moves stay aligned.
978
- XCUIScreen *mainScreen = XCUIScreen.mainScreen;
979
- self.displayCorrection = FBDisplayCoordinateOffset(
980
- CGSizeMake(mainScreen.bounds.size.width / mainScreen.scale, mainScreen.bounds.size.height / mainScreen.scale),
981
- CGSizeMake(screen.bounds.size.width / screen.scale, screen.bounds.size.height / screen.scale),
982
- eventRecord.interfaceOrientation);
957
+ return nil;
983
958
  }
984
- return eventRecord;
959
+ return [[XCSynthesizedEventRecord alloc] initWithName:@"W3C Touch Action"
960
+ interfaceOrientation:self.application.interfaceOrientation];
985
961
  }
986
962
 
987
963
  - (nullable XCSynthesizedEventRecord *)synthesizeWithError:(NSError **)error
@@ -990,13 +966,19 @@ static NSString *const FB_KEY_ACTIONS = @"actions";
990
966
  if (nil == actions) {
991
967
  return nil;
992
968
  }
993
- XCSynthesizedEventRecord *eventRecord = [self eventRecordForActions:actions error:error];
969
+ // A fresh context owns routing and anchors for every pointer source in this command.
970
+ FBGestureCoordinateContext *coordinateContext = [FBGestureCoordinateContext
971
+ contextWithDisplayRequired:[self actionsRequireDisplay:actions] error:error];
972
+ if (nil == coordinateContext) {
973
+ return nil;
974
+ }
975
+ XCSynthesizedEventRecord *eventRecord = [self eventRecordForContext:coordinateContext error:error];
994
976
  if (nil == eventRecord) {
995
977
  return nil;
996
978
  }
997
979
  for (NSDictionary<NSString *, id> *actionDescription in actions) {
998
980
  NSString *actionId = actionDescription[FB_KEY_ID];
999
- NSArray<XCPointerEventPath *> *eventPaths = [self eventPathsWithActionDescription:actionDescription forActionId:actionId error:error];
981
+ NSArray<XCPointerEventPath *> *eventPaths = [self eventPathsWithActionDescription:actionDescription forActionId:actionId coordinateContext:coordinateContext error:error];
1000
982
  if (nil == eventPaths) {
1001
983
  return nil;
1002
984
  }
@@ -7,18 +7,28 @@
7
7
  */
8
8
 
9
9
  #import <XCTest/XCTest.h>
10
+ #import <objc/runtime.h>
10
11
 
11
12
  #import "FBIntegrationTestCase.h"
12
13
  #import "FBConfiguration.h"
13
14
  #import "FBScreen.h"
15
+ #import "FBGestureCoordinateContext.h"
14
16
  #import "FBRunLoopSpinner.h"
15
17
  #import "FBExceptions.h"
16
18
  #import "FBElementCommands.h"
17
19
  #import "FBElementCache.h"
20
+ #import "FBBaseActionsSynthesizer.h"
21
+ #import "FBW3CActionsSynthesizer.h"
22
+ #import "FBMathUtils.h"
18
23
  #import "XCUIElement+FBWebDriverAttributes.h"
19
24
 
20
25
  @interface FBElementCommands (CoordinateTests)
21
26
  + (nullable XCUICoordinate *)gestureCoordinateWithOffset:(CGVector)offset element:(XCUIElement *)element error:(NSError **)error;
27
+ + (nullable NSArray<XCUICoordinate *> *)gestureCoordinatesWithOffsets:(NSArray<NSValue *> *)offsets element:(XCUIElement *)element error:(NSError **)error;
28
+ @end
29
+
30
+ @interface FBGestureCoordinateContext (CoordinateTests)
31
+ + (nullable id)windowAnchorWithApplication:(XCUIElement *)application screen:(XCUIScreen *)screen error:(NSError **)error;
22
32
  @end
23
33
 
24
34
  #import "XCUIApplication+FBTouchAction.h"
@@ -26,6 +36,11 @@
26
36
  #import "XCUIDevice+FBRotation.h"
27
37
  #import "XCUIDevice+FBHinge.h"
28
38
 
39
+ // The private gesture item is exercised directly to isolate origin resolution.
40
+ @interface FBW3CGestureItem : FBBaseGestureItem
41
+ @property (nonatomic) FBGestureCoordinateContext *coordinateContext;
42
+ @end
43
+
29
44
  @interface FBDisplayGestureTests : FBIntegrationTestCase
30
45
  @property (nonatomic) NSNumber *previousDisplayId;
31
46
  @property (nonatomic) XCUIElement *canvas;
@@ -234,7 +249,7 @@
234
249
  NSArray<NSNumber *> *frame = [self measurement][@"canvasWindowRect"];
235
250
  BOOL checkedSecondaryDisplay = NO;
236
251
  for (NSDictionary *screen in [FBScreen screensWithError:nil]) {
237
- if ([screen[@"isMain"] boolValue] || [screen[@"displayId"] isEqual:fixtureDisplay]) {
252
+ if ([screen[@"displayId"] isEqual:fixtureDisplay]) {
238
253
  continue;
239
254
  }
240
255
  checkedSecondaryDisplay = YES;
@@ -251,6 +266,261 @@
251
266
  XCTSkipIf(!checkedSecondaryDisplay, @"Requires an available secondary display without the fixture");
252
267
  }
253
268
 
269
+ - (void)testApplicationOriginUsesItsOwnAppInsteadOfTheSessionApp
270
+ {
271
+ XCTSkipIf(![FBScreen hasMultipleScreensWithError:nil].boolValue, @"Requires multiple displays");
272
+ FBW3CGestureItem *item = [NSClassFromString(@"FBW3CGestureItem") new];
273
+ // Accessing this app's windows would fail. Only the supplied origin should be used.
274
+ item.application = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.appium.nonexistent-session-app"];
275
+ NSError *error = nil;
276
+ item.coordinateContext = [FBGestureCoordinateContext contextWithDisplayRequired:YES error:&error];
277
+ XCTAssertNotNil(item.coordinateContext);
278
+ XCUICoordinate *coordinate = [item hitpointWithElement:self.testedApplication
279
+ positionOffset:[NSValue valueWithCGPoint:CGPointZero] error:&error];
280
+ XCTAssertNotNil(coordinate);
281
+ XCTAssertNil(error);
282
+ CGRect frame = self.testedApplication.wdFrame;
283
+ XCUICoordinate *expected = [item.coordinateContext coordinateWithApplication:self.testedApplication
284
+ offset:CGVectorMake(CGRectGetMidX(frame), CGRectGetMidY(frame)) error:&error];
285
+ XCTAssertNotNil(expected);
286
+ XCTAssertEqualWithAccuracy(coordinate.screenPoint.x, expected.screenPoint.x, 1);
287
+ XCTAssertEqualWithAccuracy(coordinate.screenPoint.y, expected.screenPoint.y, 1);
288
+ }
289
+
290
+ - (void)testSingleDisplayW3COriginsRetainLegacyCoordinates
291
+ {
292
+ XCTSkipIf([FBScreen hasMultipleScreensWithError:nil].boolValue, @"Requires a single display");
293
+ FBW3CGestureItem *item = [NSClassFromString(@"FBW3CGestureItem") new];
294
+ item.application = self.testedApplication;
295
+ item.coordinateContext = [FBGestureCoordinateContext contextWithDisplayRequired:YES error:nil];
296
+ XCTAssertNotNil(item.coordinateContext);
297
+ FBBaseGestureItem *legacy = [FBBaseGestureItem new];
298
+ legacy.application = self.testedApplication;
299
+ for (NSNumber *orientation in @[@(UIDeviceOrientationPortrait), @(UIDeviceOrientationLandscapeLeft), @(UIDeviceOrientationLandscapeRight)]) {
300
+ [XCUIDevice.sharedDevice fb_setDeviceInterfaceOrientation:orientation.integerValue];
301
+ NSError *error = nil;
302
+ NSValue *offset = [NSValue valueWithCGPoint:CGPointMake(30, 40)];
303
+ XCUICoordinate *viewport = [item hitpointWithElement:nil positionOffset:offset error:&error];
304
+ XCUICoordinate *expectedViewport = [legacy hitpointWithElement:nil positionOffset:offset error:&error];
305
+ XCTAssertNotNil(viewport);
306
+ XCTAssertTrue(CGPointEqualToPoint(viewport.screenPoint, expectedViewport.screenPoint));
307
+ XCUICoordinate *app = [item hitpointWithElement:self.testedApplication positionOffset:nil error:&error];
308
+ XCUICoordinate *expectedApp = [legacy hitpointWithElement:self.testedApplication positionOffset:nil error:&error];
309
+ XCTAssertNotNil(app);
310
+ XCTAssertTrue(CGPointEqualToPoint(app.screenPoint, expectedApp.screenPoint));
311
+ XCUICoordinate *appOffset = [item hitpointWithElement:self.testedApplication positionOffset:offset error:&error];
312
+ XCUICoordinate *expectedAppOffset = FBCoordinateWithAnchorOffset(self.testedApplication,
313
+ CGVectorMake(0.5, 0.5), CGVectorMake(30, 40), &error);
314
+ XCTAssertNotNil(appOffset);
315
+ XCTAssertTrue(CGPointEqualToPoint(appOffset.screenPoint, expectedAppOffset.screenPoint));
316
+ XCTAssertNil(error);
317
+ }
318
+ }
319
+
320
+ - (void)testDisplayAndWindowResolutionRunsOncePerCommand
321
+ {
322
+ BOOL hasMultipleScreens = [FBScreen hasMultipleScreensWithError:nil].boolValue;
323
+ SEL selector = @selector(hasMultipleScreensWithError:);
324
+ Method method = class_getClassMethod(FBScreen.class, selector);
325
+ IMP original = method_getImplementation(method);
326
+ __block NSUInteger calls = 0;
327
+ IMP counting = imp_implementationWithBlock(^NSNumber *(id receiver, NSError **error) {
328
+ calls++;
329
+ return ((NSNumber *(*)(id, SEL, NSError **))original)(receiver, selector, error);
330
+ });
331
+ SEL anchorSelector = @selector(windowAnchorWithApplication:screen:error:);
332
+ Method anchorMethod = class_getClassMethod(FBGestureCoordinateContext.class, anchorSelector);
333
+ IMP originalAnchor = method_getImplementation(anchorMethod);
334
+ __block NSUInteger anchorCalls = 0;
335
+ IMP countingAnchors = imp_implementationWithBlock(^id(id receiver, XCUIElement *application, XCUIScreen *screen, NSError **error) {
336
+ anchorCalls++;
337
+ return ((id (*)(id, SEL, XCUIElement *, XCUIScreen *, NSError **))originalAnchor)(receiver, anchorSelector, application, screen, error);
338
+ });
339
+ method_setImplementation(method, counting);
340
+ method_setImplementation(anchorMethod, countingAnchors);
341
+ @try {
342
+ // Exercise serialized origins and sharing between independent pointer sources.
343
+ self.actionElementCache = [FBElementCache new];
344
+ NSDictionary *finger = @{
345
+ @"type": @"pointer", @"id": @"finger", @"parameters": @{@"pointerType": @"touch"},
346
+ @"actions": @[
347
+ [self moveFrom:@"viewport" x:100 y:100 duration:0],
348
+ [self moveFrom:self.testedApplication x:0 y:0 duration:100],
349
+ [self moveFrom:self.canvas x:0 y:0 duration:100],
350
+ [self moveFrom:@"pointer" x:10 y:10 duration:100],
351
+ [self moveFrom:@"viewport" x:120 y:120 duration:100],
352
+ ],
353
+ };
354
+ NSMutableDictionary *secondFinger = finger.mutableCopy;
355
+ secondFinger[@"id"] = @"secondFinger";
356
+ NSError *error = nil;
357
+ FBW3CActionsSynthesizer *synthesizer = [[FBW3CActionsSynthesizer alloc]
358
+ initWithActions:@[finger, secondFinger] forApplication:self.testedApplication elementCache:self.actionElementCache error:&error];
359
+ // Even the same synthesizer must resolve a fresh anchor for each command.
360
+ for (NSUInteger invocation = 1; invocation <= 2; invocation++) {
361
+ XCTAssertNotNil([synthesizer synthesizeWithError:&error]);
362
+ XCTAssertNil(error);
363
+ XCTAssertEqual(calls, invocation);
364
+ XCTAssertEqual(anchorCalls, hasMultipleScreens ? invocation : 0);
365
+ }
366
+ NSArray<NSValue *> *offsets = @[
367
+ [NSValue valueWithCGVector:CGVectorMake(100, 100)],
368
+ [NSValue valueWithCGVector:CGVectorMake(120, 120)],
369
+ ];
370
+ // A native drag shares one resolution between its endpoints, on iPhone too.
371
+ for (NSUInteger invocation = 3; invocation <= 4; invocation++) {
372
+ NSArray<XCUICoordinate *> *coordinates = [FBElementCommands gestureCoordinatesWithOffsets:offsets
373
+ element:self.testedApplication error:&error];
374
+ XCTAssertEqual(coordinates.count, 2);
375
+ XCTAssertNil(error);
376
+ XCTAssertEqual(calls, invocation);
377
+ XCTAssertEqual(anchorCalls, hasMultipleScreens ? invocation : 0);
378
+ }
379
+ XCTAssertEqual([FBElementCommands gestureCoordinatesWithOffsets:offsets element:self.canvas error:&error].count, 2);
380
+ XCTAssertNil(error);
381
+ XCTAssertEqual(calls, 4); // Ordinary element gestures do not enumerate displays.
382
+ XCTAssertEqual(anchorCalls, hasMultipleScreens ? 4 : 0);
383
+ } @finally {
384
+ method_setImplementation(method, original);
385
+ method_setImplementation(anchorMethod, originalAnchor);
386
+ imp_removeBlock(counting);
387
+ imp_removeBlock(countingAnchors);
388
+ }
389
+ }
390
+
391
+ - (void)testWindowAnchorCacheDoesNotHideAnotherApplicationsFailure
392
+ {
393
+ XCTSkipIf(![FBScreen hasMultipleScreensWithError:nil].boolValue, @"Requires multiple displays");
394
+ XCUIApplication *otherApplication = self.springboard;
395
+ NSError *expectedError = [NSError errorWithDomain:@"WindowAnchorLookup" code:1 userInfo:nil];
396
+ SEL selector = @selector(windowAnchorWithApplication:screen:error:);
397
+ Method method = class_getClassMethod(FBGestureCoordinateContext.class, selector);
398
+ IMP original = method_getImplementation(method);
399
+ __block NSUInteger sessionAppCalls = 0;
400
+ __block NSUInteger otherAppCalls = 0;
401
+ IMP replacement = imp_implementationWithBlock(^id(id receiver, XCUIElement *application, XCUIScreen *screen, NSError **error) {
402
+ if (application == otherApplication) {
403
+ otherAppCalls++;
404
+ if (NULL != error) {
405
+ *error = expectedError;
406
+ }
407
+ return nil;
408
+ }
409
+ sessionAppCalls++;
410
+ return ((id (*)(id, SEL, XCUIElement *, XCUIScreen *, NSError **))original)(receiver, selector, application, screen, error);
411
+ });
412
+ method_setImplementation(method, replacement);
413
+ @try {
414
+ NSArray *actions = @[@{
415
+ @"type": @"pointer", @"id": @"finger", @"parameters": @{@"pointerType": @"touch"},
416
+ @"actions": @[
417
+ [self moveFrom:@"viewport" x:100 y:100 duration:0],
418
+ [self moveFrom:self.testedApplication x:0 y:0 duration:100],
419
+ [self moveFrom:otherApplication x:0 y:0 duration:100],
420
+ ],
421
+ }];
422
+ NSError *error = nil;
423
+ FBW3CActionsSynthesizer *synthesizer = [[FBW3CActionsSynthesizer alloc]
424
+ initWithActions:actions forApplication:self.testedApplication elementCache:nil error:&error];
425
+ XCTAssertNil([synthesizer synthesizeWithError:&error]);
426
+ XCTAssertEqualObjects(error, expectedError);
427
+ XCTAssertEqual(sessionAppCalls, 1);
428
+ XCTAssertEqual(otherAppCalls, 1);
429
+ } @finally {
430
+ method_setImplementation(method, original);
431
+ imp_removeBlock(replacement);
432
+ }
433
+ }
434
+
435
+ - (void)testMixedOriginsRejectAnElementOnAnotherDisplay
436
+ {
437
+ NSNumber *fixtureDisplay = FBConfiguration.sharedInstance.currentDisplayId;
438
+ BOOL checkedOtherDisplay = NO;
439
+ for (NSDictionary *screen in [FBScreen screensWithError:nil]) {
440
+ if ([screen[@"displayId"] isEqual:fixtureDisplay]) {
441
+ continue;
442
+ }
443
+ checkedOtherDisplay = YES;
444
+ FBConfiguration.sharedInstance.currentDisplayId = screen[@"displayId"];
445
+ NSArray *actions = @[@{
446
+ @"type": @"pointer", @"id": @"finger", @"parameters": @{@"pointerType": @"touch"},
447
+ @"actions": @[
448
+ [self moveFrom:self.canvas x:0 y:0 duration:0],
449
+ [self moveFrom:@"viewport" x:100 y:100 duration:0],
450
+ [self moveFrom:@"pointer" x:10 y:10 duration:0],
451
+ ],
452
+ }];
453
+ NSError *error = nil;
454
+ FBW3CActionsSynthesizer *synthesizer = [[FBW3CActionsSynthesizer alloc]
455
+ initWithActions:actions forApplication:self.testedApplication elementCache:nil error:&error];
456
+ XCTAssertNil([synthesizer synthesizeWithError:&error]);
457
+ XCTAssertNotNil(error);
458
+ XCTAssertTrue([error.localizedDescription containsString:@"element origin is not on the selected display"]);
459
+ }
460
+ XCTSkipIf(!checkedOtherDisplay, @"Requires another display without the fixture");
461
+ }
462
+
463
+ - (void)testDisplayEnumerationFailureStopsGestureResolution
464
+ {
465
+ NSError *expectedError = [NSError errorWithDomain:@"DisplayEnumeration" code:1 userInfo:nil];
466
+ SEL selector = @selector(hasMultipleScreensWithError:);
467
+ Method method = class_getClassMethod(FBScreen.class, selector);
468
+ IMP original = method_getImplementation(method);
469
+ IMP replacement = imp_implementationWithBlock(^NSNumber *(id receiver, NSError **error) {
470
+ if (NULL != error) {
471
+ *error = expectedError;
472
+ }
473
+ return nil;
474
+ });
475
+ method_setImplementation(method, replacement);
476
+ @try {
477
+ NSError *error = nil;
478
+ NSArray *offsets = @[[NSValue valueWithCGVector:CGVectorMake(100, 100)],
479
+ [NSValue valueWithCGVector:CGVectorMake(120, 120)]];
480
+ XCTAssertNil([FBElementCommands gestureCoordinatesWithOffsets:offsets element:self.testedApplication error:&error]);
481
+ XCTAssertEqualObjects(error, expectedError);
482
+ error = nil;
483
+ NSArray *actions = @[@{
484
+ @"type": @"pointer", @"id": @"finger", @"parameters": @{@"pointerType": @"touch"},
485
+ @"actions": @[[self moveFrom:@"viewport" x:100 y:100 duration:0]],
486
+ }];
487
+ FBW3CActionsSynthesizer *synthesizer = [[FBW3CActionsSynthesizer alloc]
488
+ initWithActions:actions forApplication:self.testedApplication elementCache:nil error:&error];
489
+ XCTAssertNil([synthesizer synthesizeWithError:&error]);
490
+ XCTAssertEqualObjects(error, expectedError);
491
+ } @finally {
492
+ method_setImplementation(method, original);
493
+ imp_removeBlock(replacement);
494
+ }
495
+ }
496
+
497
+ - (void)verifyNativeDragCoordinates
498
+ {
499
+ NSDictionary *geometry = [self measurement];
500
+ NSArray<NSNumber *> *bounds = geometry[@"canvasBounds"];
501
+ NSArray<NSNumber *> *frame = geometry[@"canvasWindowRect"];
502
+ CGPoint end = CGPointMake(bounds[0].doubleValue / 2, bounds[1].doubleValue / 2 - 40);
503
+ CGVector endOffset = CGVectorMake(frame[0].doubleValue + end.x, frame[1].doubleValue + end.y);
504
+ CGVector startOffset = CGVectorMake(endOffset.dx, endOffset.dy + 40);
505
+ NSError *error = nil;
506
+ NSArray<XCUICoordinate *> *coordinates = [FBElementCommands gestureCoordinatesWithOffsets:
507
+ @[[NSValue valueWithCGVector:startOffset], [NSValue valueWithCGVector:endOffset]]
508
+ element:self.testedApplication error:&error];
509
+ XCTAssertEqual(coordinates.count, 2);
510
+ XCTAssertNil(error);
511
+ [coordinates[0] pressForDuration:0.1 thenDragToCoordinate:coordinates[1]];
512
+ NSDictionary *result = [self measurement];
513
+ XCTAssertEqual([result[@"count"] unsignedIntegerValue], [geometry[@"count"] unsignedIntegerValue] + 1);
514
+ XCTAssertEqualObjects(result[@"phase"], @"ended");
515
+ XCTAssertEqualWithAccuracy([result[@"last"][0] doubleValue], end.x, 1);
516
+ XCTAssertEqualWithAccuracy([result[@"last"][1] doubleValue], end.y, 1);
517
+ }
518
+
519
+ - (void)testNativeDragCoordinates
520
+ {
521
+ [self verifyNativeDragCoordinates];
522
+ }
523
+
254
524
  - (void)testNativeCoordinatesAcrossFoldStates
255
525
  {
256
526
  XCUIDevice *device = XCUIDevice.sharedDevice;
@@ -271,6 +541,7 @@
271
541
  [self selectFixtureDisplay];
272
542
  [self verifyNativeApplicationCoordinates];
273
543
  [self verifyNativeEdgeCoordinates];
544
+ [self verifyNativeDragCoordinates];
274
545
  }
275
546
  }
276
547