Skip to content

Commit 5fdf0e1

Browse files
author
Mykola Mokhnach
committed
Add unit tests
1 parent 55039ab commit 5fdf0e1

5 files changed

Lines changed: 280 additions & 9 deletions

File tree

src/main/java/io/appium/java_client/touch/OptionsWithAbsolutePositioning.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,15 @@ public T withRelativeOffset(int xOffset, int yOffset) {
7272
@Override
7373
protected void verify() {
7474
if (elementId == null) {
75-
if (absoluteOffset != null) {
76-
throw new IllegalArgumentException("Absolute offset must not be defined if 'element' option is set");
77-
}
78-
if (relativeOffset == null) {
79-
throw new IllegalArgumentException("Relative offset must be defined if 'element' option is set");
80-
}
81-
} else {
8275
if (absoluteOffset == null) {
83-
throw new IllegalArgumentException("Absolute offset must be defined if 'element' option not set");
76+
throw new IllegalArgumentException("Absolute offset must be defined if 'element' option is not set");
8477
}
8578
if (relativeOffset != null) {
86-
throw new IllegalArgumentException("Relative offset must not be defined if 'element' option not set");
79+
throw new IllegalArgumentException("Relative offset must not be defined if 'element' option is not set");
80+
}
81+
} else {
82+
if (absoluteOffset != null) {
83+
throw new IllegalArgumentException("Absolute offset must not be defined if 'element' option set");
8784
}
8885
}
8986
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package io.appium.java_client.touch;
2+
3+
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.Dimension;
6+
import org.openqa.selenium.Point;
7+
import org.openqa.selenium.Rectangle;
8+
import org.openqa.selenium.OutputType;
9+
import org.openqa.selenium.WebElement;
10+
import org.openqa.selenium.internal.HasIdentity;
11+
12+
import java.util.List;
13+
14+
public class DummyElement implements WebElement, HasIdentity {
15+
@Override
16+
public void click() {
17+
// dummy
18+
}
19+
20+
@Override
21+
public void submit() {
22+
// dummy
23+
}
24+
25+
@Override
26+
public void sendKeys(CharSequence... charSequences) {
27+
// dummy
28+
}
29+
30+
@Override
31+
public void clear() {
32+
// dummy
33+
}
34+
35+
@Override
36+
public String getTagName() {
37+
return "";
38+
}
39+
40+
@Override
41+
public String getAttribute(String s) {
42+
return "";
43+
}
44+
45+
@Override
46+
public boolean isSelected() {
47+
return false;
48+
}
49+
50+
@Override
51+
public boolean isEnabled() {
52+
return false;
53+
}
54+
55+
@Override
56+
public String getText() {
57+
return "";
58+
}
59+
60+
@Override
61+
public List<WebElement> findElements(By by) {
62+
return null;
63+
}
64+
65+
@Override
66+
public WebElement findElement(By by) {
67+
return null;
68+
}
69+
70+
@Override
71+
public boolean isDisplayed() {
72+
return false;
73+
}
74+
75+
@Override
76+
public Point getLocation() {
77+
return null;
78+
}
79+
80+
@Override
81+
public Dimension getSize() {
82+
return null;
83+
}
84+
85+
@Override
86+
public Rectangle getRect() {
87+
return null;
88+
}
89+
90+
@Override
91+
public String getCssValue(String s) {
92+
return "";
93+
}
94+
95+
@Override
96+
public <X> X getScreenshotAs(OutputType<X> outputType) {
97+
return null;
98+
}
99+
100+
@Override
101+
public String getId() {
102+
return "123";
103+
}
104+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.appium.java_client.touch;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeMatcher;
6+
7+
import static org.hamcrest.core.AllOf.allOf;
8+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
9+
10+
public final class FailsWithMatcher<Ex extends Throwable>
11+
extends TypeSafeMatcher<IThrowingRunnable<Ex>> {
12+
13+
private final Matcher<? super Ex> matcher;
14+
15+
private FailsWithMatcher(final Matcher<? super Ex> matcher) {
16+
this.matcher = matcher;
17+
}
18+
19+
public static <Ex extends Throwable> Matcher<IThrowingRunnable<Ex>> failsWith(
20+
final Class<Ex> throwableType) {
21+
return new FailsWithMatcher<>(instanceOf(throwableType));
22+
}
23+
24+
public static <Ex extends Throwable> Matcher<IThrowingRunnable<Ex>> failsWith(
25+
final Class<Ex> throwableType, final Matcher<? super Ex> throwableMatcher) {
26+
return new FailsWithMatcher<>(allOf(instanceOf(throwableType), throwableMatcher));
27+
}
28+
29+
@Override
30+
protected boolean matchesSafely(final IThrowingRunnable<Ex> runnable) {
31+
try {
32+
runnable.run();
33+
return false;
34+
} catch (final Throwable ex) {
35+
return matcher.matches(ex);
36+
}
37+
}
38+
39+
@Override
40+
public void describeTo(final Description description) {
41+
description.appendText("fails with ").appendDescriptionOf(matcher);
42+
}
43+
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.appium.java_client.touch;
2+
3+
@FunctionalInterface
4+
public interface IThrowingRunnable<E extends Throwable> {
5+
void run() throws E;
6+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package io.appium.java_client.touch;
2+
3+
import org.junit.Test;
4+
import org.openqa.selenium.WebElement;
5+
import org.openqa.selenium.internal.HasIdentity;
6+
7+
import java.time.Duration;
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import static io.appium.java_client.touch.FailsWithMatcher.failsWith;
14+
import static org.hamcrest.CoreMatchers.everyItem;
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.Matchers.isIn;
17+
18+
public class TouchOptionsTests {
19+
private static final WebElement dummyElement = new DummyElement();
20+
21+
@Test
22+
public void invalidAbsolutePositionOptionsShouldFailOnBuild() throws Exception {
23+
final List<ActionOptions> invalidOptions = new ArrayList<>();
24+
invalidOptions.add(new PressOptions()
25+
.withElement(dummyElement)
26+
.withAbsoluteOffset(0, 0));
27+
invalidOptions.add(new LongPressOptions()
28+
.withRelativeOffset(0, 0));
29+
invalidOptions.add(new TapOptions());
30+
invalidOptions.add(new TapOptions()
31+
.withAbsoluteOffset(0, 0)
32+
.withRelativeOffset(0, 0));
33+
invalidOptions.forEach(opts -> assertThat(opts::build, failsWith(IllegalArgumentException.class)));
34+
}
35+
36+
@Test
37+
public void invalidRelativePositionOptionsShouldFailOnBuild() throws Exception {
38+
final List<ActionOptions> invalidOptions = new ArrayList<>();
39+
invalidOptions.add(new MoveToOptions());
40+
invalidOptions.forEach(opts -> assertThat(opts::build, failsWith(IllegalArgumentException.class)));
41+
}
42+
43+
@Test
44+
public void invalidOptionsArgumentsShouldFailOnAltering() throws Exception {
45+
final List<IThrowingRunnable<RuntimeException>> invalidOptions = new ArrayList<>();
46+
invalidOptions.add(() -> new WaitOptions().withDuration(Duration.ofMillis(-1)));
47+
invalidOptions.add(() -> new PressOptions().withElement(null));
48+
invalidOptions.add(() -> new MoveToOptions().withElement(null));
49+
invalidOptions.add(() -> new WaitOptions().withDuration(null));
50+
for (IThrowingRunnable<RuntimeException> item : invalidOptions) {
51+
assertThat(item, failsWith(RuntimeException.class));
52+
}
53+
}
54+
55+
@Test
56+
public void longPressOptionsShouldBuildProperly() throws Exception {
57+
final Map<String, Object> actualOpts = new LongPressOptions()
58+
.withElement(dummyElement)
59+
.withRelativeOffset(0, 0)
60+
.withDuration(Duration.ofMillis(1))
61+
.build();
62+
final Map<String, Object> expectedOpts = new HashMap<>();
63+
expectedOpts.put("element", ((HasIdentity) dummyElement).getId());
64+
expectedOpts.put("x", 0);
65+
expectedOpts.put("y", 0);
66+
expectedOpts.put("duration", 1L);
67+
assertThat(actualOpts.entrySet(), everyItem(isIn(expectedOpts.entrySet())));
68+
assertThat(expectedOpts.entrySet(), everyItem(isIn(actualOpts.entrySet())));
69+
}
70+
71+
@Test
72+
public void tapOptionsShouldBuildProperly() throws Exception {
73+
final Map<String, Object> actualOpts = new TapOptions()
74+
.withAbsoluteOffset(0, 0)
75+
.withTapsCount(2)
76+
.build();
77+
final Map<String, Object> expectedOpts = new HashMap<>();
78+
expectedOpts.put("x", 0);
79+
expectedOpts.put("y", 0);
80+
expectedOpts.put("count", 2);
81+
assertThat(actualOpts.entrySet(), everyItem(isIn(expectedOpts.entrySet())));
82+
assertThat(expectedOpts.entrySet(), everyItem(isIn(actualOpts.entrySet())));
83+
}
84+
85+
@Test
86+
public void pressOptionsShouldBuildProperly() throws Exception {
87+
final Map<String, Object> actualOpts = new PressOptions()
88+
.withElement(dummyElement)
89+
.build();
90+
final Map<String, Object> expectedOpts = new HashMap<>();
91+
expectedOpts.put("element", ((HasIdentity) dummyElement).getId());
92+
assertThat(actualOpts.entrySet(), everyItem(isIn(expectedOpts.entrySet())));
93+
assertThat(expectedOpts.entrySet(), everyItem(isIn(actualOpts.entrySet())));
94+
}
95+
96+
@Test
97+
public void moveToOptionsShouldBuildProperly() throws Exception {
98+
final Map<String, Object> actualOpts = new MoveToOptions()
99+
.withElement(dummyElement)
100+
.withRelativeOffset(-1,-1)
101+
.build();
102+
final Map<String, Object> expectedOpts = new HashMap<>();
103+
expectedOpts.put("element", ((HasIdentity) dummyElement).getId());
104+
expectedOpts.put("x", -1);
105+
expectedOpts.put("y", -1);
106+
assertThat(actualOpts.entrySet(), everyItem(isIn(expectedOpts.entrySet())));
107+
assertThat(expectedOpts.entrySet(), everyItem(isIn(actualOpts.entrySet())));
108+
}
109+
110+
@Test
111+
public void waitOptionsShouldBuildProperly() throws Exception {
112+
final Map<String, Object> actualOpts = new WaitOptions()
113+
.withDuration(Duration.ofSeconds(1))
114+
.build();
115+
final Map<String, Object> expectedOpts = new HashMap<>();
116+
expectedOpts.put("ms", 1000L);
117+
assertThat(actualOpts.entrySet(), everyItem(isIn(expectedOpts.entrySet())));
118+
assertThat(expectedOpts.entrySet(), everyItem(isIn(actualOpts.entrySet())));
119+
}
120+
}

0 commit comments

Comments
 (0)