From 277dbb3496dbb083e6f09ebe74b152a6e1147010 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 26 Apr 2018 13:16:44 +0200 Subject: [PATCH] Add a possibility to perform press with pressure --- .../Utilities/FBAppiumActionsSynthesizer.m | 22 +++++++++++++-- .../Utilities/FBW3CActionsSynthesizer.m | 18 ++++++++++-- .../Classes/FBAlertViewController.m | 28 +++++++++++++++---- .../Resources/Base.lproj/Main.storyboard | 9 ++++++ .../FBAppiumTouchActionsIntegrationTests.m | 25 +++++++++++++++++ .../IntegrationTests/FBIntegrationTestCase.h | 1 + .../IntegrationTests/FBIntegrationTestCase.m | 1 + 7 files changed, 94 insertions(+), 10 deletions(-) diff --git a/WebDriverAgentLib/Utilities/FBAppiumActionsSynthesizer.m b/WebDriverAgentLib/Utilities/FBAppiumActionsSynthesizer.m index 46a7d32e9..c439bf1fd 100644 --- a/WebDriverAgentLib/Utilities/FBAppiumActionsSynthesizer.m +++ b/WebDriverAgentLib/Utilities/FBAppiumActionsSynthesizer.m @@ -32,6 +32,7 @@ static NSString *const FB_OPTION_DURATION = @"duration"; static NSString *const FB_OPTION_COUNT = @"count"; static NSString *const FB_OPTION_MS = @"ms"; +static NSString *const FB_OPTION_PRESSURE = @"pressure"; // Some useful constants might be found at // https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/ViewConfiguration.java @@ -50,7 +51,7 @@ @interface FBTapItem : FBAppiumGestureItem @end @interface FBPressItem : FBAppiumGestureItem - +@property (nonatomic, nullable, readonly) NSNumber *pressure; @end @interface FBLongPressItem : FBAppiumGestureItem @@ -189,6 +190,19 @@ - (double)durationWithOptions:(nullable NSDictionary *)options @implementation FBPressItem +- (nullable instancetype)initWithActionItem:(NSDictionary *)item application:(XCUIApplication *)application atPosition:(nullable NSValue *)atPosition offset:(double)offset error:(NSError **)error +{ + self = [super initWithActionItem:item application:application atPosition:atPosition offset:offset error:error]; + if (self) { + _pressure = nil; + id options = [item objectForKey:FB_OPTIONS_KEY]; + if ([options isKindOfClass:NSDictionary.class]) { + _pressure = [options objectForKey:FB_OPTION_PRESSURE]; + } + } + return self; +} + + (NSString *)actionName { return FB_ACTION_PRESS; @@ -201,7 +215,11 @@ + (BOOL)hasAbsolutePositioning - (NSArray *)addToEventPath:(XCPointerEventPath *)eventPath allItems:(NSArray *)allItems currentItemIndex:(NSUInteger)currentItemIndex error:(NSError **)error { - return @[[[XCPointerEventPath alloc] initForTouchAtPoint:self.atPosition offset:FBMillisToSeconds(self.offset)]]; + XCPointerEventPath *result = [[XCPointerEventPath alloc] initForTouchAtPoint:self.atPosition offset:FBMillisToSeconds(self.offset)]; + if (nil != self.pressure) { + [result pressDownWithPressure:self.pressure.doubleValue atOffset:self.offset]; + } + return @[result]; } - (double)durationWithOptions:(nullable NSDictionary *)options diff --git a/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m b/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m index 3d777d71c..ad7a2fc28 100644 --- a/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m +++ b/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m @@ -49,6 +49,7 @@ static NSString *const FB_ACTION_ITEM_KEY_X = @"x"; static NSString *const FB_ACTION_ITEM_KEY_Y = @"y"; static NSString *const FB_ACTION_ITEM_KEY_BUTTON = @"button"; +static NSString *const FB_ACTION_ITEM_KEY_PRESSURE = @"pressure"; static NSString *const FB_KEY_ID = @"id"; static NSString *const FB_KEY_PARAMETERS = @"parameters"; @@ -62,7 +63,7 @@ @interface FBW3CGestureItem : FBBaseGestureItem @end @interface FBPointerDownItem : FBW3CGestureItem - +@property (nullable, readonly, nonatomic) NSNumber *pressure; @end @interface FBPauseItem : FBW3CGestureItem @@ -153,6 +154,15 @@ - (nullable NSValue *)hitpointWithElement:(nullable XCUIElement *)element positi @implementation FBPointerDownItem +- (nullable instancetype)initWithActionItem:(NSDictionary *)actionItem application:(XCUIApplication *)application previousItem:(nullable FBBaseGestureItem *)previousItem offset:(double)offset error:(NSError **)error +{ + self = [super initWithActionItem:actionItem application:application previousItem:previousItem offset:offset error:error]; + if (self) { + _pressure = [actionItem objectForKey:FB_ACTION_ITEM_KEY_PRESSURE];; + } + return self; +} + + (NSString *)actionName { return FB_ACTION_ITEM_TYPE_POINTER_DOWN; @@ -166,7 +176,11 @@ + (NSString *)actionName return @[eventPath]; } } - return @[[[XCPointerEventPath alloc] initForTouchAtPoint:self.atPosition offset:FBMillisToSeconds(self.offset)]]; + XCPointerEventPath *result = [[XCPointerEventPath alloc] initForTouchAtPoint:self.atPosition offset:FBMillisToSeconds(self.offset)]; + if (nil != self.pressure) { + [result pressDownWithPressure:self.pressure.doubleValue atOffset:FBMillisToSeconds(self.offset)]; + } + return @[result]; } @end diff --git a/WebDriverAgentTests/IntegrationApp/Classes/FBAlertViewController.m b/WebDriverAgentTests/IntegrationApp/Classes/FBAlertViewController.m index 0ca495889..c90b58ab4 100644 --- a/WebDriverAgentTests/IntegrationApp/Classes/FBAlertViewController.m +++ b/WebDriverAgentTests/IntegrationApp/Classes/FBAlertViewController.m @@ -20,12 +20,7 @@ @implementation FBAlertViewController - (IBAction)createAppAlert:(UIButton *)sender { - UIAlertController *alerController = - [UIAlertController alertControllerWithTitle:@"Magic" - message:@"Should read" - preferredStyle:UIAlertControllerStyleAlert]; - [alerController addAction:[UIAlertAction actionWithTitle:@"Will do" style:UIAlertActionStyleDefault handler:nil]]; - [self presentViewController:alerController animated:YES completion:nil]; + [self presentAlertController]; } - (IBAction)createAppSheet:(UIButton *)sender @@ -57,4 +52,25 @@ - (IBAction)createGPSAccessAlert:(UIButton *)sender [self.locationManager requestAlwaysAuthorization]; } +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event +{ + [super touchesMoved:touches withEvent:event]; + for (UITouch *touch in touches) { + if (fabs(touch.maximumPossibleForce - touch.force) < 0.0001) { + [self presentAlertController]; + return; + } + } +} + +- (void)presentAlertController +{ + UIAlertController *alerController = + [UIAlertController alertControllerWithTitle:@"Magic" + message:@"Should read" + preferredStyle:UIAlertControllerStyleAlert]; + [alerController addAction:[UIAlertAction actionWithTitle:@"Will do" style:UIAlertActionStyleDefault handler:nil]]; + [self presentViewController:alerController animated:YES completion:nil]; +} + @end diff --git a/WebDriverAgentTests/IntegrationApp/Resources/Base.lproj/Main.storyboard b/WebDriverAgentTests/IntegrationApp/Resources/Base.lproj/Main.storyboard index dc82b12b0..fd9a72814 100644 --- a/WebDriverAgentTests/IntegrationApp/Resources/Base.lproj/Main.storyboard +++ b/WebDriverAgentTests/IntegrationApp/Resources/Base.lproj/Main.storyboard @@ -380,10 +380,18 @@ + + @@ -392,6 +400,7 @@ + diff --git a/WebDriverAgentTests/IntegrationTests/FBAppiumTouchActionsIntegrationTests.m b/WebDriverAgentTests/IntegrationTests/FBAppiumTouchActionsIntegrationTests.m index 8191bc1aa..7e5dedd95 100644 --- a/WebDriverAgentTests/IntegrationTests/FBAppiumTouchActionsIntegrationTests.m +++ b/WebDriverAgentTests/IntegrationTests/FBAppiumTouchActionsIntegrationTests.m @@ -269,6 +269,31 @@ - (void)testLongPress [self verifyGesture:gesture orientation:orientation]; } +- (void)testForcePress +{ + NSArray *> *gesture = + @[@{ + @"action": @"press", + @"options": @{ + @"element": self.testedApplication.buttons[FBShowAlertForceTouchButtonName], + @"x": @1, + @"y": @1, + @"pressure": @1 + } + }, + @{ + @"action": @"wait", + @"options": @{ + @"ms": @300 + } + }, + @{ + @"action": @"release" + } + ]; + [self verifyGesture:gesture orientation:UIDeviceOrientationPortrait]; +} + @end diff --git a/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.h b/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.h index 9b3265439..4fc2239e0 100644 --- a/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.h +++ b/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.h @@ -13,6 +13,7 @@ extern NSString *const FBShowAlertButtonName; extern NSString *const FBShowSheetAlertButtonName; +extern NSString *const FBShowAlertForceTouchButtonName; /** XCTestCase helper class used for integration tests diff --git a/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.m b/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.m index e1f3e0f7e..00020518d 100644 --- a/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.m +++ b/WebDriverAgentTests/IntegrationTests/FBIntegrationTestCase.m @@ -21,6 +21,7 @@ NSString *const FBShowAlertButtonName = @"Create App Alert"; NSString *const FBShowSheetAlertButtonName = @"Create Sheet Alert"; +NSString *const FBShowAlertForceTouchButtonName = @"Create Alert (Force Touch)"; @interface FBIntegrationTestCase () @property (nonatomic, strong) XCUIApplication *testedApplication;