Skip to content

Commit

Permalink
Modified swipe to take options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
navaneeth committed Oct 30, 2013
1 parent f3b3a9c commit a7dcef4
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/calabash/java/CalabashWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/calabash/java/Force.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package calabash.java;

29 changes: 27 additions & 2 deletions src/calabash/java/IAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
*
Expand Down
93 changes: 93 additions & 0 deletions src/calabash/java/SwipeOptions.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
14 changes: 13 additions & 1 deletion src/calabash/java/UIElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.jruby.RubyArray;
import org.jruby.RubyHash;

import calabash.java.SwipeOptions.Force;

/**
* Represents an UI element.
*
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 17 additions & 2 deletions src/calabash/java/UIElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.jruby.RubyArray;
import org.jruby.RubyHash;

import calabash.java.SwipeOptions.Force;

/**
*
*
Expand Down Expand Up @@ -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();
Expand All @@ -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");
}
}

Expand Down
54 changes: 54 additions & 0 deletions tests/calabash/java/SwipeOptionsTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit a7dcef4

Please sign in to comment.