From a7dcef4ff906159093368336ddd20d745828cbec Mon Sep 17 00:00:00 2001 From: navaneethkn Date: Wed, 30 Oct 2013 11:25:18 +0530 Subject: [PATCH] Modified swipe to take options With calabash 162, swipe can be done in many ways. It can take a optional "force" argument, "swipe-delta" etc. This commit adds support for these options --- src/calabash/java/CalabashWrapper.java | 19 +++-- src/calabash/java/Force.java | 2 + src/calabash/java/IAction.java | 29 ++++++- src/calabash/java/SwipeOptions.java | 93 +++++++++++++++++++++++ src/calabash/java/UIElement.java | 14 +++- src/calabash/java/UIElements.java | 19 ++++- tests/calabash/java/SwipeOptionsTest.java | 54 +++++++++++++ 7 files changed, 220 insertions(+), 10 deletions(-) create mode 100644 src/calabash/java/Force.java create mode 100644 src/calabash/java/SwipeOptions.java create mode 100644 tests/calabash/java/SwipeOptionsTest.java diff --git a/src/calabash/java/CalabashWrapper.java b/src/calabash/java/CalabashWrapper.java index 107a06b..e1a7238 100644 --- a/src/calabash/java/CalabashWrapper.java +++ b/src/calabash/java/CalabashWrapper.java @@ -15,6 +15,7 @@ import org.jruby.RubyArray; import org.jruby.embed.LocalContextScope; +import org.jruby.embed.LocalVariableBehavior; import org.jruby.embed.PathType; import org.jruby.embed.ScriptingContainer; @@ -25,7 +26,7 @@ public final class CalabashWrapper { private final ScriptingContainer container = new ScriptingContainer( - LocalContextScope.SINGLETHREAD); + LocalContextScope.SINGLETHREAD, LocalVariableBehavior.PERSISTENT); private final File rbScriptsDir; private final File projectDir; private final File gemsDir; @@ -212,17 +213,25 @@ public void scroll(String query, Direction direction) } } - public void swipe(String query, Direction direction) + public void swipe(String query, Direction direction, SwipeOptions options) throws CalabashException { try { - info("Swiping: %s", query); + info("Swiping: %s, with options: %s", query, + options == null ? "null" : options.toString()); container.clear(); addRequiresAndIncludes("Calabash::Cucumber::Core", "Calabash::Cucumber::Operations"); container.put("cjQueryString", query); container.put("cjDirection", direction.getDirection()); - container - .runScriptlet("swipe(cjDirection, {:query => cjQueryString})"); + if (options != null) { + container.put("cjSwipeOptsString", options.toString()); + container.runScriptlet("cjSwipeOpts = eval(cjSwipeOptsString)"); + container.runScriptlet("cjSwipeOpts[:query] = cjQueryString"); + container.runScriptlet("swipe(cjDirection, cjSwipeOpts)"); + } else { + container + .runScriptlet("swipe(cjDirection, {:query => cjQueryString})"); + } pause(); } catch (Exception e) { error("Failed to swipe: %s", e, query); diff --git a/src/calabash/java/Force.java b/src/calabash/java/Force.java new file mode 100644 index 0000000..1690862 --- /dev/null +++ b/src/calabash/java/Force.java @@ -0,0 +1,2 @@ +package calabash.java; + diff --git a/src/calabash/java/IAction.java b/src/calabash/java/IAction.java index 77c78db..eae1c0c 100644 --- a/src/calabash/java/IAction.java +++ b/src/calabash/java/IAction.java @@ -3,9 +3,11 @@ */ package calabash.java; +import calabash.java.SwipeOptions.Force; + /** * Interface to be implemented by all elements which supports actions - * + * */ public interface IAction { @@ -36,11 +38,34 @@ public interface IAction { * Swipe the element to the specified direction * * @param direction - * Direction to swipe to + * Direction to swipe * @throws CalabashException */ void swipe(Direction direction) throws CalabashException; + /** + * Swipe the element to the specified direction with the specified force + * + * @param direction + * Direction to swipe + * @param force + * Swipe force + * @throws CalabashException + */ + void swipe(Direction direction, Force force) throws CalabashException; + + /** + * Swipe the element to the specified direction with the specified options + * + * @param direction + * Direction to swipe + * @param options + * Options to use + * @throws CalabashException + */ + void swipe(Direction direction, SwipeOptions options) + throws CalabashException; + /** * Pinch the element in * diff --git a/src/calabash/java/SwipeOptions.java b/src/calabash/java/SwipeOptions.java new file mode 100644 index 0000000..bea5dc6 --- /dev/null +++ b/src/calabash/java/SwipeOptions.java @@ -0,0 +1,93 @@ +package calabash.java; + +/** + * Options for Swipe action + * + */ +public class SwipeOptions { + + private Force force; + private SwipeDelta swipeDelta; + + public SwipeOptions(Force force, SwipeDelta swipeDelta) { + this.force = force; + this.swipeDelta = swipeDelta; + } + + public SwipeOptions(Force force) { + this.force = force; + } + + public SwipeOptions(SwipeDelta swipeDelta) { + this.swipeDelta = swipeDelta; + } + + @Override + public String toString() { + StringBuffer result = new StringBuffer("{"); + if (force != null) + result.append(force.toString()); + + if (swipeDelta != null) { + if (!result.toString().endsWith("{")) + result.append(", "); + result.append(swipeDelta.toString()); + } + + result.append("}"); + + return result.toString(); + } + + public static class SwipeDelta { + private Offset horizontal; + private Offset vertical; + + public SwipeDelta(Offset horizontal, Offset vertical) { + this.horizontal = horizontal; + this.vertical = vertical; + } + + @Override + public String toString() { + StringBuffer result = new StringBuffer(); + result.append("'swipe-delta' => {"); + if (horizontal != null) { + result.append(String.format( + ":horizontal => {:dx => %d, :dy => %d}", + horizontal.getX(), horizontal.getY())); + } + + if (vertical != null) { + if (horizontal != null) + result.append(", "); + result.append(String.format( + ":vertical => {:dx => %d, :dy => %d}", vertical.getX(), + vertical.getY())); + } + result.append("}"); + return result.toString(); + } + } + + public static enum Force { + + Strong("strong"), Normal("normal"), Light("light"); + + private String name; + + Force(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return String.format(":force => :%s", name); + } + } + +} diff --git a/src/calabash/java/UIElement.java b/src/calabash/java/UIElement.java index 74d70b7..4e44440 100644 --- a/src/calabash/java/UIElement.java +++ b/src/calabash/java/UIElement.java @@ -9,6 +9,8 @@ import org.jruby.RubyArray; import org.jruby.RubyHash; +import calabash.java.SwipeOptions.Force; + /** * Represents an UI element. * @@ -170,7 +172,17 @@ public void scroll(Direction direction) throws CalabashException { } public void swipe(Direction direction) throws CalabashException { - calabashWrapper.swipe(query, direction); + calabashWrapper.swipe(query, direction, null); + } + + public void swipe(Direction direction, Force force) + throws CalabashException { + calabashWrapper.swipe(query, direction, new SwipeOptions(force, null)); + } + + public void swipe(Direction direction, SwipeOptions options) + throws CalabashException { + calabashWrapper.swipe(query, direction, options); } public void pinchIn() throws CalabashException { diff --git a/src/calabash/java/UIElements.java b/src/calabash/java/UIElements.java index b33bae1..41a6f50 100644 --- a/src/calabash/java/UIElements.java +++ b/src/calabash/java/UIElements.java @@ -10,6 +10,8 @@ import org.jruby.RubyArray; import org.jruby.RubyHash; +import calabash.java.SwipeOptions.Force; + /** * * @@ -77,6 +79,18 @@ public void swipe(Direction direction) throws CalabashException { this.first().swipe(direction); } + public void swipe(Direction direction, Force force) + throws CalabashException { + ensureCollectionIsNotEmpty(); + this.first().swipe(direction, force); + } + + public void swipe(Direction direction, SwipeOptions options) + throws CalabashException { + ensureCollectionIsNotEmpty(); + this.first().swipe(direction, options); + } + public void pinchIn() throws CalabashException { ensureCollectionIsNotEmpty(); this.first().pinchIn(); @@ -86,10 +100,11 @@ public void pinchOut() throws CalabashException { ensureCollectionIsNotEmpty(); this.first().pinchOut(); } - + private void ensureCollectionIsNotEmpty() throws CalabashException { if (this.size() == 0) { - throw new CalabashException("Cannot perform action on an empty list"); + throw new CalabashException( + "Cannot perform action on an empty list"); } } diff --git a/tests/calabash/java/SwipeOptionsTest.java b/tests/calabash/java/SwipeOptionsTest.java new file mode 100644 index 0000000..5e202f9 --- /dev/null +++ b/tests/calabash/java/SwipeOptionsTest.java @@ -0,0 +1,54 @@ +package calabash.java; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import calabash.java.SwipeOptions.Force; +import calabash.java.SwipeOptions.SwipeDelta; + +public class SwipeOptionsTest { + + @Test + public void swipeOptionsWithForce() { + SwipeOptions swipeOptions = new SwipeOptions(Force.Strong, null); + assertEquals("{:force => :strong}", swipeOptions.toString()); + } + + @Test + public void swipeOptionsWithSwipeDeltaHorizontal() { + SwipeOptions swipeOptions = new SwipeOptions(null, new SwipeDelta( + new Offset(10, 20), null)); + assertEquals( + "{'swipe-delta' => {:horizontal => {:dx => 10, :dy => 20}}}", + swipeOptions.toString()); + } + + @Test + public void swipeOptionsWithSwipeDeltaVertical() { + SwipeOptions swipeOptions = new SwipeOptions(null, new SwipeDelta(null, + new Offset(10, 20))); + assertEquals( + "{'swipe-delta' => {:vertical => {:dx => 10, :dy => 20}}}", + swipeOptions.toString()); + } + + @Test + public void swipeOptionsWithSwipeDeltaHorizontalAndVertical() { + SwipeOptions swipeOptions = new SwipeOptions(null, new SwipeDelta( + new Offset(10, 20), new Offset(10, 20))); + assertEquals( + "{'swipe-delta' => {:horizontal => {:dx => 10, :dy => 20}, :vertical => {:dx => 10, :dy => 20}}}", + swipeOptions.toString()); + } + + @Test + public void swipeOptionsWithForceAndSwipeDeltaHorizontalAndVertical() { + SwipeOptions swipeOptions = new SwipeOptions(Force.Normal, + new SwipeDelta(new Offset(10, 20), new Offset(10, 20))); + assertEquals( + "{:force => :normal, 'swipe-delta' => {:horizontal => {:dx => 10, :dy => 20}, :vertical => {:dx => 10, :dy => 20}}}", + swipeOptions.toString()); + } + +}