diff options
author | Anna Koskinen <anna@vaadin.com> | 2015-05-28 16:41:07 +0300 |
---|---|---|
committer | Anna Koskinen <anna@vaadin.com> | 2015-05-28 18:46:41 +0300 |
commit | c61187a34cf7fe6a38030231f3529400a34e9087 (patch) | |
tree | 95071edec4908dda86f69564ae5ce759627d3adb | |
parent | 96c08cd52e13e6d2ba5fee0580e9c2c7a5091a0f (diff) | |
download | vaadin-framework-c61187a34cf7fe6a38030231f3529400a34e9087.tar.gz vaadin-framework-c61187a34cf7fe6a38030231f3529400a34e9087.zip |
Input prompt shouldn't get set when input has focus (#18027)
Change-Id: Ide792fec9bf9050cea0b7616536965e42d74b16a
3 files changed, 164 insertions, 1 deletions
diff --git a/client/src/com/vaadin/client/ui/VTextualDate.java b/client/src/com/vaadin/client/ui/VTextualDate.java index 9055609e69..4d80b30f4f 100644 --- a/client/src/com/vaadin/client/ui/VTextualDate.java +++ b/client/src/com/vaadin/client/ui/VTextualDate.java @@ -363,7 +363,12 @@ public class VTextualDate extends VDateField implements Field, ChangeHandler, } protected void setText(String text) { - if (inputPrompt != null && (text == null || "".equals(text))) { + if (inputPrompt != null + && (text == null || "".equals(text)) + && !this.text.getStyleName() + .contains( + VTextField.CLASSNAME + "-" + + VTextField.CLASSNAME_FOCUS)) { text = readonly ? "" : inputPrompt; setPrompting(true); } else { diff --git a/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldInputPrompt.java b/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldInputPrompt.java new file mode 100644 index 0000000000..a267d72b7f --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldInputPrompt.java @@ -0,0 +1,68 @@ +/* + * 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.datefield; + +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.PopupDateField; +import com.vaadin.ui.TextField; + +/** + * Tests that state change doesn't set input prompt back to PopupDateField if + * focus is still in the input field. + * + * @author Vaadin Ltd + */ +public class PopupDateFieldInputPrompt extends AbstractTestUI { + + private TextField text = new TextField("TextField"); + private PopupDateField dateField = new PopupDateField(); + + @Override + protected void setup(VaadinRequest request) { + + text.addValueChangeListener(new ValueChangeListener() { + private static final long serialVersionUID = 1L; + + @Override + public void valueChange(ValueChangeEvent event) { + // update PopupDateField's state + dateField.setRequired(!dateField.isRequired()); + } + }); + + dateField.setInputPrompt("prompt"); + dateField.setCaption("PopupDateField"); + + addComponent(text); + addComponent(dateField); + } + + @Override + protected String getTestDescription() { + return "Write something to the TextField and use tabulator to move to PopupDateField." + + "<br>PopupDateField shouldn't get input prompt back before focus leaves the input field," + + "<br>even if TextField's value change updates PopupDateField's state."; + } + + @Override + protected Integer getTicketNumber() { + return 18027; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldInputPromptTest.java b/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldInputPromptTest.java new file mode 100644 index 0000000000..4e5fa744c7 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldInputPromptTest.java @@ -0,0 +1,90 @@ +/* + * 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.datefield; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedCondition; + +import com.vaadin.testbench.elements.PopupDateFieldElement; +import com.vaadin.testbench.elements.TextFieldElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Tests that state change doesn't set input prompt back to PopupDateField if + * focus is still in the input field. + * + * @author Vaadin Ltd + */ +public class PopupDateFieldInputPromptTest extends MultiBrowserTest { + + @Test + public void testInputPrompt() { + openTestURL(); + TextFieldElement textField = $(TextFieldElement.class).first(); + final PopupDateFieldElement dateField = $(PopupDateFieldElement.class) + .first(); + + // ensure initial state + Assert.assertFalse("DateField required when it shouldn't be.", + isRequired(dateField)); + WebElement input = dateField.findElement(By.className("v-textfield")); + Assert.assertEquals("prompt", input.getAttribute("value")); + + // trigger ValueChange and move focus + textField.sendKeys("foo", Keys.TAB); + + // wait for ValueChange to update DateField's state and the DateField to + // gain focus. + waitForElementRequiredAndFocused(dateField, + By.className("v-textfield-focus")); + + // ensure prompt hasn't come back when field was set required + Assert.assertNotEquals("prompt", input.getAttribute("value")); + } + + private void waitForElementRequiredAndFocused( + final PopupDateFieldElement dateField, final By locator) { + waitUntil(new ExpectedCondition<Boolean>() { + + @Override + public Boolean apply(WebDriver arg0) { + if (isRequired(dateField)) { + List<WebElement> elements = dateField.findElements(locator); + return !elements.isEmpty(); + } + + return false; + } + + @Override + public String toString() { + return "dateField to become required and presence of element located by: " + + locator; + } + }); + } + + private boolean isRequired(PopupDateFieldElement dateField) { + return dateField.getAttribute("class").contains("v-required"); + } +} |