Skip to content

Commit

Permalink
[java] add pointer event properties test
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed May 27, 2022
1 parent d0a0df9 commit 2259f63
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
70 changes: 70 additions & 0 deletions java/test/org/openqa/selenium/interactions/PenPointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.openqa.selenium.interactions;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -36,7 +38,11 @@
import org.openqa.selenium.testing.SwitchToTopAfterTest;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -371,6 +377,70 @@ public void testCanMoveOverAndOutOfAnElement() {
wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));
}

@Test
@Ignore(value = FIREFOX, issue = "https://github.com/mozilla/geckodriver/issues/789")
public void setPointerEventProperties() {
driver.get(pages.pointerActionsPage);
long start = System.currentTimeMillis();

WebElement pointerArea = driver.findElement(By.id("pointerArea"));
PointerInput pen = new PointerInput(PointerInput.Kind.PEN, "default pen");
PointerInput.PointerEventProperties eventProperties = PointerInput.eventProperties()
.setTiltX(-72)
.setTiltY(9)
.setTwist(86);
PointerInput.Origin origin = PointerInput.Origin.fromElement(pointerArea);

Sequence actionListPen = new Sequence(pen, 0)
.addAction(pen.createPointerMove(Duration.ZERO, origin, 0, 0))
.addAction(pen.createPointerDown(0))
.addAction(pen.createPointerMove(Duration.ofMillis(800), origin, 2, 2, eventProperties))
.addAction(pen.createPointerUp(0));

((RemoteWebDriver) driver).perform(List.of(actionListPen));

long duration = System.currentTimeMillis() - start;
Assertions.assertThat(duration).isGreaterThan(2);

List<WebElement> moves = driver.findElements(By.className("pointermove"));
Map<String, String> moveTo = properties(moves.get(0));
Map<String, String> down = properties(driver.findElement(By.className("pointerdown")));
Map<String, String> moveBy = properties(moves.get(1));
Map<String, String> up = properties(driver.findElement(By.className("pointerup")));

Rectangle rect = pointerArea.getRect();

int centerX = (int) Math.floor(rect.width / 2 + rect.getX());
int centerY = (int) Math.floor(rect.height / 2 + rect.getY());
Assertions.assertThat(moveTo.get("button")).isEqualTo("-1");
Assertions.assertThat(moveTo.get("pageX")).isEqualTo("" + centerX);
Assertions.assertThat(moveTo.get("pageY")).isEqualTo("" + centerY);
Assertions.assertThat(down.get("button")).isEqualTo("0");
Assertions.assertThat(down.get("pageX")).isEqualTo("" + centerX);
Assertions.assertThat(down.get("pageY")).isEqualTo("" + centerY);
Assertions.assertThat(moveBy.get("button")).isEqualTo("-1");
Assertions.assertThat(moveBy.get("pageX")).isEqualTo("" + (centerX + 2));
Assertions.assertThat(moveBy.get("pageY")).isEqualTo("" + (centerY + 2));
Assertions.assertThat(moveBy.get("tiltX")).isEqualTo("-72");
Assertions.assertThat(moveBy.get("tiltY")).isEqualTo("9");
Assertions.assertThat(moveBy.get("twist")).isEqualTo("86");
Assertions.assertThat(up.get("button")).isEqualTo("0");
Assertions.assertThat(up.get("pageX")).isEqualTo("" + (centerX + 2));
Assertions.assertThat(up.get("pageY")).isEqualTo("" + (centerY + 2));
}

private Map<String, String> properties(WebElement element) {
String text = element.getText();
text = text.substring(text.indexOf(' ') + 1);

return Arrays.stream(text.split(", "))
.map(s -> s.split(": "))
.collect(Collectors.toMap(
a -> a[0], //key
a -> a[1] //value
));
}

private boolean fuzzyPositionMatching(int expectedX, int expectedY, String locationTuple) {
String[] splitString = locationTuple.split(",");
int gotX = Integer.parseInt(splitString[0].trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void encodesWrappedElementInMoveOrigin() {
@Test
public void acceptsPointerEventProperties() {
PointerInput pen = new PointerInput(PointerInput.Kind.PEN, "my pen");
Interaction pointerDown = pen.createPointerDown(PointerInput.eventProperties().setHeight(12).setTiltX(30));
Interaction pointerDown = pen.createPointerDown(0, PointerInput.eventProperties().setHeight(12).setTiltX(30));

Map<String, Object> encode = ((Encodable) pointerDown).encode();

Expand Down
4 changes: 4 additions & 0 deletions java/test/org/openqa/selenium/testing/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public class Pages {
public String mapVisibilityPage;
public String metaRedirectPage;
public String missedJsReferencePage;
public String mouseInteractionPage;
public String mouseOverPage;
public String mouseTrackerPage;
public String nestedPage;
public String pointerActionsPage;
public String printPage;
public String readOnlyPage;
public String rectanglesPage;
Expand Down Expand Up @@ -116,9 +118,11 @@ public Pages(AppServer appServer) {
mapVisibilityPage = appServer.whereIs("map_visibility.html");
metaRedirectPage = appServer.whereIs("meta-redirect.html");
missedJsReferencePage = appServer.whereIs("missedJsReference.html");
mouseInteractionPage = appServer.whereIs("mouse_interaction.html");
mouseOverPage = appServer.whereIs("mouseOver.html");
mouseTrackerPage = appServer.whereIs("mousePositionTracker.html");
nestedPage = appServer.whereIs("nestedElements.html");
pointerActionsPage = appServer.whereIs("pointerActionsPage.html");
printPage = appServer.whereIs("printPage.html");
readOnlyPage = appServer.whereIs("readOnlyPage.html");
rectanglesPage = appServer.whereIs("rectangles.html");
Expand Down

0 comments on commit 2259f63

Please sign in to comment.