diff options
author | Anna Koskinen <Ansku@users.noreply.github.com> | 2018-02-27 17:05:22 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-02-27 17:05:22 +0200 |
commit | a0a9eee14fed0d5c6b7a8ae5b5f9185d7cef1279 (patch) | |
tree | b33c2e9a0847c32d54db5a30b02f1186a8eb154c | |
parent | 6d84351fee4bdb6e877d9100c8645e6c6044864e (diff) | |
download | vaadin-framework-a0a9eee14fed0d5c6b7a8ae5b5f9185d7cef1279.tar.gz vaadin-framework-a0a9eee14fed0d5c6b7a8ae5b5f9185d7cef1279.zip |
Fix client and server filter state mismatch in Combobox (#10630)
Fixes #10624
5 files changed, 98 insertions, 9 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java index 334b630724..544f041770 100644 --- a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java @@ -196,7 +196,7 @@ public class ComboBoxConnector extends AbstractListingConnector * the current filter string */ protected void setFilter(String filter) { - if (!Objects.equals(filter, getWidget().lastFilter)) { + if (!Objects.equals(filter, getState().currentFilterText)) { getDataReceivedHandler().clearPendingNavigation(); rpc.setFilter(filter); @@ -243,10 +243,9 @@ public class ComboBoxConnector extends AbstractListingConnector page = 0; } VComboBox widget = getWidget(); - int adjustment = widget.nullSelectionAllowed && filter.isEmpty() - ? 1 : 0; - int startIndex = Math.max(0, - page * widget.pageLength - adjustment); + int adjustment = widget.nullSelectionAllowed && filter.isEmpty() ? 1 + : 0; + int startIndex = Math.max(0, page * widget.pageLength - adjustment); int pageLength = widget.pageLength > 0 ? widget.pageLength : getDataSource().size(); getDataSource().ensureAvailability(startIndex, pageLength); diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 9500d08c96..50665fd877 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -206,7 +206,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> @Override public void setFilter(String filterText) { - currentFilterText = filterText; + getState().currentFilterText = filterText; filterSlot.accept(filterText); } }; @@ -224,8 +224,6 @@ public class ComboBox<T> extends AbstractSingleSelect<T> private StyleGenerator<T> itemStyleGenerator = item -> null; - private String currentFilterText; - private SerializableConsumer<String> filterSlot = filter -> { // Just ignore when neither setDataProvider nor setItems has been called }; @@ -890,7 +888,8 @@ public class ComboBox<T> extends AbstractSingleSelect<T> }; SerializableConsumer<C> providerFilterSlot = internalSetDataProvider( - dataProvider, convertOrNull.apply(currentFilterText)); + dataProvider, + convertOrNull.apply(getState(false).currentFilterText)); filterSlot = filter -> providerFilterSlot .accept(convertOrNull.apply(filter)); diff --git a/shared/src/main/java/com/vaadin/shared/ui/combobox/ComboBoxState.java b/shared/src/main/java/com/vaadin/shared/ui/combobox/ComboBoxState.java index 666fff1e58..95500af2fd 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/combobox/ComboBoxState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/combobox/ComboBoxState.java @@ -100,4 +100,11 @@ public class ComboBoxState extends AbstractSingleSelectState { */ public String selectedItemIcon; + /** + * Filter string that is currently in use in the suggestion listing. + * + * @since + */ + public String currentFilterText; + } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxFilterClear.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxFilterClear.java new file mode 100644 index 0000000000..62239612f7 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxFilterClear.java @@ -0,0 +1,32 @@ +package com.vaadin.tests.components.combobox; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Button; + +public class ComboBoxFilterClear extends ComboBoxSelecting { + + @Override + protected void setup(VaadinRequest request) { + super.setup(request); + + Button toggleVisibility = new Button("Toggle visibility", + e -> comboBox.setVisible(!comboBox.isVisible())); + toggleVisibility.setId("toggleVisibility"); + + Button setNull = new Button("Set null", e -> comboBox.setValue(null)); + setNull.setId("setNull"); + + addComponents(toggleVisibility, setNull); + } + + @Override + protected String getTestDescription() { + return "Clearing selection while ComboBox is not visible should not " + + "leave the suggestion items stuck on the previous filter"; + } + + @Override + protected Integer getTicketNumber() { + return 10624; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxFilterClearTest.java b/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxFilterClearTest.java new file mode 100644 index 0000000000..29beb29b3a --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxFilterClearTest.java @@ -0,0 +1,52 @@ +package com.vaadin.tests.components.combobox; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.ComboBoxElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class ComboBoxFilterClearTest extends MultiBrowserTest { + ComboBoxElement comboBox; + + @Test + public void testFilterCleared() { + openTestURL(); + comboBox = $(ComboBoxElement.class).first(); + ButtonElement toggleVisibility = $(ButtonElement.class) + .id("toggleVisibility"); + ButtonElement setNull = $(ButtonElement.class).id("setNull"); + + sendKeysToInput("b0", Keys.TAB); + assertEquals("b0", comboBox.getText()); + + toggleVisibility.click(); + waitForElementNotPresent(By.className("v-filterselect")); + + setNull.click(); + + toggleVisibility.click(); + waitForElementPresent(By.className("v-filterselect")); + comboBox = $(ComboBoxElement.class).first(); + + WebElement suggestionPopup = comboBox.getSuggestionPopup(); + + List<WebElement> menuItems = suggestionPopup + .findElements(By.className("gwt-MenuItem")); + assertEquals("a0", menuItems.get(1).getText()); + } + + private void sendKeysToInput(CharSequence... keys) { + // ensure mouse is located over the ComboBox to avoid hover issues + new Actions(getDriver()).moveToElement(comboBox).perform(); + comboBox.sendKeys(keys); + } +} |