From e04931b248aa4d0070c71d7bc5ede88ebb4f35e3 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Sun, 7 Sep 2014 21:06:59 +0300 Subject: [PATCH] Pass (x,y) coordinates for button click triggered via keyboard (#12650). Change-Id: I24363d34f7007650fc66f9b85d7e22a4ab10bb6b --- client/src/com/vaadin/client/ui/VButton.java | 11 +- .../button/ButtonKeyboardClick.java | 77 +++++++++++++ .../button/ButtonKeyboardClickTest.java | 108 ++++++++++++++++++ 3 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClick.java create mode 100644 uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClickTest.java diff --git a/client/src/com/vaadin/client/ui/VButton.java b/client/src/com/vaadin/client/ui/VButton.java index 7dfb5c7c86..2e5494ec18 100644 --- a/client/src/com/vaadin/client/ui/VButton.java +++ b/client/src/com/vaadin/client/ui/VButton.java @@ -351,9 +351,16 @@ public class VButton extends FocusWidget implements ClickHandler { disallowNextClick = false; - // Mouse coordinates are not always available (e.g., when the click is + // Screen coordinates are not always available (e.g., when the click is // caused by a keyboard event). - NativeEvent evt = Document.get().createClickEvent(1, 0, 0, 0, 0, false, + // Set (x,y) client coordinates to the middle of the button + int x = getElement().getAbsoluteLeft() - getElement().getScrollLeft() + - getElement().getOwnerDocument().getScrollLeft() + + Util.getRequiredWidth(getElement()) / 2; + int y = getElement().getAbsoluteTop() - getElement().getScrollTop() + - getElement().getOwnerDocument().getScrollTop() + + Util.getRequiredHeight(getElement()) / 2; + NativeEvent evt = Document.get().createClickEvent(1, 0, 0, x, y, false, false, false, false); getElement().dispatchEvent(evt); } diff --git a/uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClick.java b/uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClick.java new file mode 100644 index 0000000000..35e9f737c6 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClick.java @@ -0,0 +1,77 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.button; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.Label; + +/** + * Test UI for availability (x,y) coordinates for button activated via keyboard. + * + * @author Vaadin Ltd + */ +public class ButtonKeyboardClick extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + final Label[] labels = new Label[4]; + for (int i = 0; i < labels.length; i++) { + labels[i] = new Label(); + } + + Button button = new Button("button", new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + Label label = new Label(String.valueOf(event.getClientX())); + label.addStyleName("x"); + getLayout().replaceComponent(labels[0], label); + labels[0] = label; + + label = new Label(String.valueOf(event.getClientY())); + label.addStyleName("y"); + getLayout().replaceComponent(labels[1], label); + labels[1] = label; + + label = new Label(String.valueOf(event.getRelativeX())); + label.addStyleName("xRelative"); + getLayout().replaceComponent(labels[2], label); + labels[2] = label; + + label = new Label(String.valueOf(event.getRelativeY())); + label.addStyleName("yRelative"); + getLayout().replaceComponent(labels[3], label); + labels[3] = label; + } + }); + addComponent(button); + } + + @Override + protected String getTestDescription() { + return "Set client coordinates to the middle of the button when click is triggered from keyboard"; + } + + @Override + protected Integer getTicketNumber() { + return 12650; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClickTest.java b/uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClickTest.java new file mode 100644 index 0000000000..110d729a36 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/button/ButtonKeyboardClickTest.java @@ -0,0 +1,108 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.button; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebElement; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Test for availability (x,y) coordinates for button activated via keyboard. + * + * @author Vaadin Ltd + */ +public class ButtonKeyboardClickTest extends MultiBrowserTest { + + @Test + public void testCoordinatesForClickedButtonViaSpace() { + openTestURL(); + + WebElement button = getDriver().findElement(By.className("v-button")); + button.sendKeys(Keys.SPACE); + + checkCoordinates(button); + } + + @Test + public void testCoordinatesForClickedButtonViaEnter() { + openTestURL(); + + WebElement button = getDriver().findElement(By.className("v-button")); + button.sendKeys(Keys.ENTER); + + checkCoordinates(button); + } + + private void checkCoordinates(WebElement button) { + int xRelative = getValue("xRelative"); + Assert.assertTrue( + "X relative click coordinate is greater than middle of the button", + button.getSize().getWidth() / 2 >= xRelative - 1); + Assert.assertTrue( + "X relative click coordinate is lower than middle of the button", + button.getSize().getWidth() / 2 <= xRelative + 1); + + int yRelative = getValue("yRelative"); + Assert.assertTrue( + "Y relative click coordinate is greater than middle of the button", + button.getSize().getHeight() / 2 >= yRelative - 1); + Assert.assertTrue( + "Y relative click coordinate is lower than middle of the button", + button.getSize().getHeight() / 2 <= yRelative + 1); + + Assert.assertTrue( + "Client X click cooridnate is lower than X button coordinate", + getValue("x") > button.getLocation().getX()); + Assert.assertTrue( + "Client X click cooridnate is greater than right button " + + "border coordinate", getValue("x") < button + .getLocation().getX() + button.getSize().getWidth()); + + Assert.assertTrue( + "Client Y click cooridnate is lower than Y button coordinate", + getValue("y") > button.getLocation().getY()); + Assert.assertTrue( + "Client Y click cooridnate is greater than bottom button " + + "border coordinate", getValue("y") < button + .getLocation().getY() + button.getSize().getHeight()); + + Assert.assertTrue( + "Client X click cooridnate is greater than X middle button " + + "coordinate", button.getLocation().getX() + + button.getSize().getWidth() / 2 >= getValue("x") - 1); + Assert.assertTrue( + "Client Y click cooridnate is greater than Y middle button coordinate", + button.getLocation().getY() + button.getSize().getHeight() / 2 >= getValue("y") - 1); + + Assert.assertTrue( + "Client X click cooridnate is lower than X middle button " + + "coordinate", button.getLocation().getX() + + button.getSize().getWidth() / 2 <= getValue("x") + 1); + Assert.assertTrue( + "Client Y click cooridnate is lower than Y middle button coordinate", + button.getLocation().getY() + button.getSize().getHeight() / 2 <= getValue("y") + 1); + } + + private int getValue(String style) { + return Integer.parseInt(getDriver().findElement( + By.className("v-label-" + style)).getText()); + } + +} -- 2.39.5