From 9ef479303bb514622ef90d95b94d912780c1812d Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 27 Jan 2017 15:44:03 +0200 Subject: Introduce empty selection functionality for NativeSelect. (#8336) * Introduce empty selection functionality for NativeSelect. Fixes vaadin/framework8-issues#545 --- .../nativeselect/NativeSelectEmptySelection.java | 50 +++++++++++++++ .../components/nativeselect/NativeSelects.java | 7 +++ .../components/nativeselect/TestComponent.html | 2 +- .../NativeSelectEmptySelectionTest.java | 71 ++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelection.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelectionTest.java (limited to 'uitest') diff --git a/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelection.java b/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelection.java new file mode 100644 index 0000000000..2d222490f6 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelection.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2016 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.nativeselect; + +import java.util.stream.IntStream; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.NativeSelect; + +/** + * @author Vaadin Ltd + * + */ +public class NativeSelectEmptySelection extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + NativeSelect select = new NativeSelect<>(); + select.setItems(IntStream.range(1, 50) + .mapToObj(index -> String.valueOf(index))); + select.setEmptySelectionCaption("empty"); + addComponent(select); + + Button update = new Button("Update Empty Caption to 'updated'", + event -> select.setEmptySelectionCaption("updated")); + + Button disallow = new Button("Disallow empty selection item", + event -> select.setEmptySelectionAllowed(false)); + + Button enable = new Button("Allow empty selection item", + event -> select.setEmptySelectionAllowed(true)); + addComponents(update, disallow, enable); + } + +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelects.java b/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelects.java index 1a28fd41fb..34b78089bb 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelects.java +++ b/uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelects.java @@ -11,4 +11,11 @@ public class NativeSelects protected Class> getTestClass() { return (Class) NativeSelect.class; } + + @Override + protected NativeSelect constructComponent() { + NativeSelect component = super.constructComponent(); + component.setEmptySelectionAllowed(false); + return component; + } } diff --git a/uitest/src/main/resources/com/vaadin/tests/components/nativeselect/TestComponent.html b/uitest/src/main/resources/com/vaadin/tests/components/nativeselect/TestComponent.html index f4228bf60f..17adb460e4 100644 --- a/uitest/src/main/resources/com/vaadin/tests/components/nativeselect/TestComponent.html +++ b/uitest/src/main/resources/com/vaadin/tests/components/nativeselect/TestComponent.html @@ -7,7 +7,7 @@ - + diff --git a/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelectionTest.java b/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelectionTest.java new file mode 100644 index 0000000000..102b504605 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectEmptySelectionTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2000-2016 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.nativeselect; + +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.NativeSelectElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * @author Vaadin Ltd + * + */ +public class NativeSelectEmptySelectionTest extends MultiBrowserTest { + + @Test + public void checkEmptySelection() { + openTestURL(); + + checkOptions("empty"); + + // change the caption + $(ButtonElement.class).first().click(); + checkOptions("updated"); + + // disable empty caption + $(ButtonElement.class).get(1).click(); + checkOptions(null); + + // enable back + $(ButtonElement.class).get(2).click(); + checkOptions("updated"); + } + + private void checkOptions(String emptyCaption) { + NativeSelectElement select = $(NativeSelectElement.class).first(); + Set originalOptions = IntStream.range(1, 50) + .mapToObj(index -> String.valueOf(index)) + .collect(Collectors.toSet()); + Set options = select.getOptions().stream() + .map(TestBenchElement::getText).collect(Collectors.toSet()); + if (emptyCaption == null) { + Assert.assertEquals(49, options.size()); + Assert.assertTrue(options.containsAll(originalOptions)); + } else { + options.contains(emptyCaption); + Assert.assertEquals(50, options.size()); + Assert.assertTrue(options.containsAll(originalOptions)); + } + } +} -- cgit v1.2.3