diff options
author | Anastasia Smirnova <anasmi@utu.fi> | 2018-10-18 15:28:23 +0300 |
---|---|---|
committer | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2018-10-18 15:28:23 +0300 |
commit | 5e33b383fdf31f2ea15603f3a30bcb1e3c22d081 (patch) | |
tree | 41fb8f69c83178728bb5822e636e2da36042e3a8 /uitest | |
parent | a52993ba9ca67c15e6ca311ca308528752b589c9 (diff) | |
download | vaadin-framework-5e33b383fdf31f2ea15603f3a30bcb1e3c22d081.tar.gz vaadin-framework-5e33b383fdf31f2ea15603f3a30bcb1e3c22d081.zip |
Display the caption of the Empty selection in NativeSelect (#11191)
* Fixes #10937
- Previously if selected value is null, then index is set to -1; in current implementation if value is null and emptySelection is allowed then set the index to 0. (The position for the empty selection)
- Also, if changing the allowEmptySelection on the fly, ensure, that either index is to-reset to -1 by setting the selected value to null on the client-side (the value before was null) or preserve the value(value was different than empty).
* Change the test case
Since in this pr the behaviour of the NS is changed, therefore old test need to be adjusted.
Change: setting null as value will select empty selection. Before that nothing would be selected and value will be cleared.
Behaviour change in PR: Allow selecting null as value
Diffstat (limited to 'uitest')
3 files changed, 98 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/abstractsingleselect/AbstractSingleSelection.java b/uitest/src/main/java/com/vaadin/tests/components/abstractsingleselect/AbstractSingleSelection.java index fc10d13b53..44ea73dc88 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/abstractsingleselect/AbstractSingleSelection.java +++ b/uitest/src/main/java/com/vaadin/tests/components/abstractsingleselect/AbstractSingleSelection.java @@ -50,6 +50,9 @@ public class AbstractSingleSelection extends AbstractTestUI { .newInstance(); select.setItems("Foo", "Bar", "Baz", "Reset"); select.setSelectedItem("Bar"); + if (select instanceof NativeSelect) { + ((NativeSelect) select).setEmptySelectionAllowed(false); + } select.addValueChangeListener(event -> { if ("Reset".equals(event.getValue())) { diff --git a/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectSetNull.java b/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectSetNull.java new file mode 100644 index 0000000000..fc6c0f2f6c --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectSetNull.java @@ -0,0 +1,36 @@ +package com.vaadin.tests.components.nativeselect; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.NativeSelect; +import com.vaadin.ui.Button; + +public class NativeSelectSetNull extends AbstractTestUI { + public static String EMPTY_SELECTION_TEXT = "Empty Selection"; + + @Override + protected void setup(VaadinRequest request) { + NativeSelect<Integer> select = new NativeSelect<>("Native Selection"); + + // Add some items + select.setItems(1, 2, 3, 45, 6); + select.setEmptySelectionAllowed(true); + select.setEmptySelectionCaption(EMPTY_SELECTION_TEXT); + + Button changeSelect = new Button("Set value to 3", + e -> select.setValue(3)); + changeSelect.setId("changeSelect"); + Button setNull = new Button("Set value to null", + e -> select.setValue(null)); + setNull.setId("setNull"); + Button clear = new Button("Clear", e -> select.clear()); + clear.setId("clear"); + + Button disable = new Button("Disable", e -> select + .setEmptySelectionAllowed(!select.isEmptySelectionAllowed())); + disable.setId("disable"); + + addComponent(select); + addComponents(changeSelect, setNull, clear, disable); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectSetNullTest.java b/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectSetNullTest.java new file mode 100644 index 0000000000..90afe9f463 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectSetNullTest.java @@ -0,0 +1,59 @@ +package com.vaadin.tests.components.nativeselect; + +import com.vaadin.testbench.elements.NativeSelectElement; +import com.vaadin.tests.tb3.MultiBrowserTest; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static junit.framework.TestCase.assertEquals; + +public class NativeSelectSetNullTest extends MultiBrowserTest { + + @Before + public void setUp() { + openTestURL(); + } + + @Test + public void testCaptionSelected() { + getButtonOnId("setNull"); + assertEquals(NativeSelectSetNull.EMPTY_SELECTION_TEXT, + getSelect().getValue()); + } + + @Test + public void changeSelectedValue() { + getButtonOnId("changeSelect").click(); + assertEquals(3, Integer.valueOf(getSelect().getValue()).intValue()); + } + + @Test + public void clearSelection() { + getButtonOnId("clear").click(); + assertEquals(NativeSelectSetNull.EMPTY_SELECTION_TEXT, + getSelect().getValue()); + } + + @Test + public void valuePreservedAfterAllowEmptySelectionChanged() { + getSelect().setValue("2"); + getButtonOnId("disable").click(); + assertEquals(2, Integer.valueOf(getSelect().getValue()).intValue()); + + getButtonOnId("disable").click(); + getButtonOnId("setNull").click(); + assertEquals(NativeSelectSetNull.EMPTY_SELECTION_TEXT, + getSelect().getValue()); + + } + + protected NativeSelectElement getSelect() { + return $(NativeSelectElement.class).first(); + } + + protected WebElement getButtonOnId(String id) { + return findElement(By.id(id)); + } +} |