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);
}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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());
+ }
+
+}