|
| 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