diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-01-13 17:03:01 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-01-16 11:14:29 +0000 |
commit | 168788959373f561b8862128fa6aedc20a5591c2 (patch) | |
tree | 58029d9784218cd3c0dcf30a72366b4fe45d5cdd /uitest/src | |
parent | b9a6a48ab6ce9e3c7d8d025520e866643d19c004 (diff) | |
download | vaadin-framework-168788959373f561b8862128fa6aedc20a5591c2.tar.gz vaadin-framework-168788959373f561b8862128fa6aedc20a5591c2.zip |
Retain focus while changing DOM in OrderedLayout (#12967)
Change-Id: Id25177a2dfecc2d0d3b8bb5803656a39bec9c5d6
Diffstat (limited to 'uitest/src')
3 files changed, 179 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/components/orderedlayout/VerticalLayoutFocusWithDOMChanges.java b/uitest/src/com/vaadin/tests/components/orderedlayout/VerticalLayoutFocusWithDOMChanges.java new file mode 100644 index 0000000000..1e7d817094 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/orderedlayout/VerticalLayoutFocusWithDOMChanges.java @@ -0,0 +1,63 @@ +/* + * Copyright 2000-2013 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.orderedlayout; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; + +public class VerticalLayoutFocusWithDOMChanges extends AbstractTestUI implements + ValueChangeListener { + + Button dummyButton = new Button("Just a button"); + TextField listenedTextField = new TextField(); + TextField changingTextField = new TextField(); + + @Override + protected void setup(VaadinRequest request) { + VerticalLayout content = new VerticalLayout(); + setSizeFull(); + listenedTextField.addValueChangeListener(this); + listenedTextField.setImmediate(true); + changingTextField.setImmediate(true); + content.addComponent(dummyButton); + content.addComponent(listenedTextField); + content.addComponent(changingTextField); + content.setMargin(true); + content.setSpacing(true); + setContent(content); + } + + @Override + protected String getTestDescription() { + return "Check that creating or removing caption wrap doesn't lose focus"; + } + + @Override + protected Integer getTicketNumber() { + return 12967; + } + + @Override + public void valueChange(ValueChangeEvent event) { + changingTextField.setRequired(!changingTextField.isRequired()); + } + +} diff --git a/uitest/src/com/vaadin/tests/components/orderedlayout/VerticalLayoutFocusWithDOMChangesTest.java b/uitest/src/com/vaadin/tests/components/orderedlayout/VerticalLayoutFocusWithDOMChangesTest.java new file mode 100644 index 0000000000..14c26a0e17 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/orderedlayout/VerticalLayoutFocusWithDOMChangesTest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2000-2013 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.orderedlayout; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.remote.DesiredCapabilities; + +import com.vaadin.testbench.By; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class VerticalLayoutFocusWithDOMChangesTest extends MultiBrowserTest { + + private String initialText = "Some"; + private String incrementalText = " text"; + + @Test + public void inputTextAndChangeFocus() throws InterruptedException { + openTestURL(); + List<WebElement> textFields = getDriver().findElements( + By.tagName("input")); + WebElement tf1 = textFields.get(0); + WebElement tf2 = textFields.get(1); + tf1.sendKeys(initialText); + new Actions(getDriver()).moveToElement(tf2).click().build().perform(); + + WebElement activeElement = getFocusedElement(); + Assert.assertEquals("input", activeElement.getTagName()); + Assert.assertEquals("", activeElement.getAttribute("value")); + + tf1.sendKeys(incrementalText); + new Actions(getDriver()) + .moveToElement( + getDriver().findElement(By.className("v-button"))) + .click().build().perform(); + activeElement = getFocusedElement(); + Assert.assertEquals("Just a button", activeElement.getText()); + + DesiredCapabilities capabilities = getDesiredCapabilities(); + if (capabilities.equals(BrowserUtil.ie(8)) + || capabilities.equals(BrowserUtil.ie(9))) { + // IE8 and IE9 insert cursor in the start of input instead of end. + Assert.assertEquals(incrementalText + initialText, + tf1.getAttribute("value")); + } else { + Assert.assertEquals(initialText + incrementalText, + tf1.getAttribute("value")); + } + } + + @Test + public void moveFocusAndChangeFieldWithValue() { + openTestURL(); + List<WebElement> textFields = getDriver().findElements( + By.tagName("input")); + WebElement tf1 = textFields.get(0); + WebElement tf2 = textFields.get(1); + + String firstText = "This is"; + String secondText = " default value"; + + tf2.sendKeys(firstText); + tf1.sendKeys(initialText); + new Actions(getDriver()).moveToElement(tf2).click().build().perform(); + + WebElement activeElement = getFocusedElement(); + Assert.assertEquals("input", activeElement.getTagName()); + Assert.assertEquals(firstText, activeElement.getAttribute("value")); + + new Actions(getDriver()).sendKeys(secondText).build().perform(); + DesiredCapabilities capabilities = getDesiredCapabilities(); + if (capabilities.equals(BrowserUtil.ie(8)) + || capabilities.equals(BrowserUtil.ie(9))) { + // IE8 and IE9 insert cursor in the start of input instead of end. + Assert.assertEquals(secondText + firstText, + tf2.getAttribute("value")); + } else { + Assert.assertEquals(firstText + secondText, + tf2.getAttribute("value")); + } + } + +} diff --git a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java index 7a214bd60c..55a2b80918 100644 --- a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java +++ b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java @@ -28,6 +28,7 @@ import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; @@ -278,6 +279,21 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { } /** + * Uses JavaScript to determine the currently focused element. + * + * @return Focused element or null + */ + protected WebElement getFocusedElement() { + Object focusedElement = ((JavascriptExecutor) getDriver()) + .executeScript("return document.activeElement"); + if (null != focusedElement) { + return (WebElement) focusedElement; + } else { + return null; + } + } + + /** * Find a Vaadin element based on its id given using Component.setId * * @param id |