From 84cfcf3be4e3f819b128bb3aed2704a2462ccc13 Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Mon, 13 Aug 2018 18:50:51 +0300 Subject: [PATCH] Expand touch options API to accept coordinates as Point --- .../touch/offset/ElementOption.java | 30 +++++++++++++++++-- .../java_client/touch/offset/PointOption.java | 22 ++++++++++++++ .../java_client/touch/TouchOptionsTests.java | 5 ++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/appium/java_client/touch/offset/ElementOption.java b/src/main/java/io/appium/java_client/touch/offset/ElementOption.java index 3dc54823d..8cf1c8c6a 100644 --- a/src/main/java/io/appium/java_client/touch/offset/ElementOption.java +++ b/src/main/java/io/appium/java_client/touch/offset/ElementOption.java @@ -15,6 +15,18 @@ public class ElementOption extends PointOption { private String elementId; + /** + * This method creates a build instance of the {@link ElementOption}. + * + * @param element is the element to calculate offset from. + * @param offset is the offset from the upper left corner of the given element. + * @return the built option + */ + public static ElementOption element(WebElement element, Point offset) { + return new ElementOption().withElement(element).withCoordinates(offset); + } + + /** * This method creates a build instance of the {@link ElementOption}. * @@ -40,13 +52,25 @@ public static ElementOption element(WebElement element) { /** * It defines x and y offset from the upper left corner of an element. * - * @param xOffset is x value. - * @param yOffset is y value. + * @param offset is the offset from the upper left corner of the given element. + * @return self-reference + */ + @Override + public ElementOption withCoordinates(Point offset) { + super.withCoordinates(offset); + return this; + } + + /** + * It defines x and y offset from the upper left corner of an element. + * + * @param xOffset is the x-offset from the upper left corner of the given element. + * @param yOffset is the y-offset from the upper left corner of the given element. * @return self-reference */ @Override public ElementOption withCoordinates(int xOffset, int yOffset) { - coordinates = new Point(xOffset, yOffset); + super.withCoordinates(xOffset, yOffset); return this; } diff --git a/src/main/java/io/appium/java_client/touch/offset/PointOption.java b/src/main/java/io/appium/java_client/touch/offset/PointOption.java index 24c2dceb2..1cccd2486 100644 --- a/src/main/java/io/appium/java_client/touch/offset/PointOption.java +++ b/src/main/java/io/appium/java_client/touch/offset/PointOption.java @@ -11,6 +11,17 @@ public class PointOption> extends ActionOptions { protected Point coordinates; + /** + * It creates a built instance of {@link PointOption} which takes x and y coordinates. + * This is offset from the upper left corner of the screen. + * + * @param offset is an offset value. + * @return a built option + */ + public static PointOption point(Point offset) { + return new PointOption().withCoordinates(offset); + } + /** * It creates a built instance of {@link PointOption} which takes x and y coordinates. * This is offset from the upper left corner of the screen. @@ -23,6 +34,17 @@ public static PointOption point(int xOffset, int yOffset) { return new PointOption().withCoordinates(xOffset, yOffset); } + /** + * It defines x and y coordinates. + * This is offset from the upper left corner of the screen. + * + * @param offset is an offset value. + * @return self-reference + */ + public T withCoordinates(Point offset) { + return withCoordinates(offset.x, offset.y); + } + /** * It defines x and y coordinates. * This is offset from the upper left corner of the screen. diff --git a/src/test/java/io/appium/java_client/touch/TouchOptionsTests.java b/src/test/java/io/appium/java_client/touch/TouchOptionsTests.java index debdd95fc..db4c7236c 100644 --- a/src/test/java/io/appium/java_client/touch/TouchOptionsTests.java +++ b/src/test/java/io/appium/java_client/touch/TouchOptionsTests.java @@ -16,6 +16,7 @@ import io.appium.java_client.touch.offset.ElementOption; import io.appium.java_client.touch.offset.PointOption; import org.junit.Test; +import org.openqa.selenium.Point; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.RemoteWebElement; @@ -43,7 +44,7 @@ public void invalidEmptyElementOptionsShouldFailOnBuild() { public void invalidOptionsArgumentsShouldFailOnAltering() { final List invalidOptions = new ArrayList<>(); invalidOptions.add(() -> waitOptions(ofMillis(-1))); - invalidOptions.add(() -> new ElementOption().withCoordinates(0, 0).withElement(null)); + invalidOptions.add(() -> new ElementOption().withCoordinates(new Point(0, 0)).withElement(null)); invalidOptions.add(() -> new WaitOptions().withDuration(null)); invalidOptions.add(() -> tapOptions().withTapsCount(-1)); invalidOptions.add(() -> longPressOptions().withDuration(null)); @@ -71,7 +72,7 @@ public void longPressOptionsShouldBuildProperly() { @Test public void tapOptionsShouldBuildProperly() { final Map actualOpts = tapOptions() - .withPosition(point(0, 0)) + .withPosition(point(new Point(0, 0))) .withTapsCount(2) .build(); final Map expectedOpts = new HashMap<>();