diff --git a/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/ChosenSampleIntegrationTests.java b/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/ChosenSampleIntegrationTests.java index 85c5f8d..7e8ef55 100644 --- a/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/ChosenSampleIntegrationTests.java +++ b/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/ChosenSampleIntegrationTests.java @@ -21,6 +21,7 @@ import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect; import com.arcbees.chosen.integrationtest.client.testcases.ChooseOption; +import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems; import com.arcbees.chosen.integrationtest.client.testcases.DisableSearchThreshold; import com.arcbees.chosen.integrationtest.client.testcases.EnabledDisabled; import com.arcbees.chosen.integrationtest.client.testcases.HideEmptyValues; @@ -70,6 +71,7 @@ public ChosenSampleIntegrationTests() { registerTestCase(new SingleBackstrokeDelete()); registerTestCase(new SimpleValueListBoxOnChange()); registerTestCase(new SimpleMultiValueListBoxOnChange()); + registerTestCase(new ChosenListBoxMultipleSelectAddItems()); } @Override diff --git a/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/testcases/ChosenListBoxMultipleSelectAddItems.java b/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/testcases/ChosenListBoxMultipleSelectAddItems.java new file mode 100644 index 0000000..1f1ebdd --- /dev/null +++ b/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/testcases/ChosenListBoxMultipleSelectAddItems.java @@ -0,0 +1,55 @@ +/* + * Copyright 2015 ArcBees Inc. + * + * 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.arcbees.chosen.integrationtest.client.testcases; + +import com.arcbees.chosen.client.gwt.ChosenListBox; +import com.arcbees.chosen.integrationtest.client.TestCase; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.RootPanel; + +public class ChosenListBoxMultipleSelectAddItems extends TestCase { + public static final String BUTTON_ID = "ChosenListBoxMultipleSelectAddItems-button"; + public static final String SELECTED_VALUE = "Six"; + + @Override + public void run() { + final ChosenListBox listBox = new ChosenListBox(true); + listBox.setWidth("200px"); + listBox.addItem("One"); + listBox.addItem("Two"); + listBox.addItem("Three"); + + Button button = new Button("Add items and select Six"); + button.getElement().setId(BUTTON_ID); + button.addClickHandler(new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + listBox.clear(); + listBox.addItem("Four"); + listBox.addItem("Five"); + listBox.addItem("Six"); + listBox.update(); + listBox.setSelectedValue(SELECTED_VALUE); + } + }); + + RootPanel.get().add(listBox); + RootPanel.get().add(button); + } +} diff --git a/integration-test/src/test/java/DesktopChosenIT.java b/integration-test/src/test/java/DesktopChosenIT.java index 6e190d2..75a2a6a 100644 --- a/integration-test/src/test/java/DesktopChosenIT.java +++ b/integration-test/src/test/java/DesktopChosenIT.java @@ -24,6 +24,7 @@ import com.arcbees.chosen.integrationtest.client.domain.CarBrand; import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect; +import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems; import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions; import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox; import com.arcbees.chosen.integrationtest.client.testcases.SimpleValueListBox; @@ -269,6 +270,22 @@ public void singleMobile_mobileLayoutUsed() throws InterruptedException { assertThat(getOptions()).isEqualTo(CarBrand.getAllNames(RENDERER)); } + /** + * Tests that the values are added/selected when using update/setItemSelected. + * See https://github.com/ArcBees/gwtchosen/issues/271 + */ + @Test + public void chosenListBox_updateAndSelect_addsAndSelectItem() { + // Given + loadTestCase(new ChosenListBoxMultipleSelectAddItems()); + + // When + getElementById(ChosenListBoxMultipleSelectAddItems.BUTTON_ID).click(); + + // Then + assertThat(getSelectedOptionText()).isEqualTo(ChosenListBoxMultipleSelectAddItems.SELECTED_VALUE); + } + /** * Goal: verify that tab navigation is possible when Chosen is within a form. */ diff --git a/integration-test/src/test/java/MobileChosenIT.java b/integration-test/src/test/java/MobileChosenIT.java index a78be4a..ed9d77d 100644 --- a/integration-test/src/test/java/MobileChosenIT.java +++ b/integration-test/src/test/java/MobileChosenIT.java @@ -23,6 +23,7 @@ import org.openqa.selenium.WebElement; import com.arcbees.chosen.integrationtest.client.domain.CarBrand; +import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems; import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions; import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox; import com.arcbees.chosen.integrationtest.client.testcases.SimpleValueListBox; @@ -133,6 +134,22 @@ public void singleMobile_closeDropdown() throws InterruptedException { assertDropdownIsClosed(); } + /** + * Tests that the values are added/selected when using update/setItemSelected. + * See https://github.com/ArcBees/gwtchosen/issues/271 + */ + @Test + public void chosenListBox_updateAndSelect_addsAndSelectItem() { + // Given + loadTestCase(new ChosenListBoxMultipleSelectAddItems()); + + // When + getElementById(ChosenListBoxMultipleSelectAddItems.BUTTON_ID).click(); + + // Then + assertThat(getSelectedOptionText()).isEqualTo("1 item selected"); + } + @Override protected void assertDropdownIsClosed() { webDriverWait().until(new Predicate() { diff --git a/plugin/src/main/java/com/arcbees/chosen/client/AbstractMobileChosenImpl.java b/plugin/src/main/java/com/arcbees/chosen/client/AbstractMobileChosenImpl.java index 57641c0..62af640 100644 --- a/plugin/src/main/java/com/arcbees/chosen/client/AbstractMobileChosenImpl.java +++ b/plugin/src/main/java/com/arcbees/chosen/client/AbstractMobileChosenImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2014 ArcBees Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not diff --git a/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java b/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java index cca8edb..270a5e4 100644 --- a/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java +++ b/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 ArcBees Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not @@ -630,7 +630,9 @@ protected void resultsHide() { } protected void resultsResetCleanup() { - selectedItem.find("abbr").remove(); + if (selectedItem != null) { + selectedItem.find("abbr").remove(); + } } protected void resultsSearch() { @@ -837,9 +839,6 @@ protected void closeField() { container.removeClass(css.chznContainerActive()); winnowResultsClear(); - // TODO check if it's needed - // clearBackstroke(); - showSearchFieldDefault(defaultText); searchFieldScale(fWidth); } diff --git a/plugin/src/main/java/com/arcbees/chosen/client/MobileMultipleChosenImpl.java b/plugin/src/main/java/com/arcbees/chosen/client/MobileMultipleChosenImpl.java index 74ec1b4..c9a0aa6 100644 --- a/plugin/src/main/java/com/arcbees/chosen/client/MobileMultipleChosenImpl.java +++ b/plugin/src/main/java/com/arcbees/chosen/client/MobileMultipleChosenImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2014 ArcBees Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not @@ -80,6 +80,13 @@ protected void onResultSelected(OptionItem item, String newValue, String oldValu fireChosenChangeEventIfNotEqual(item, newValue, oldValue); } + @Override + protected void update() { + super.update(); + + closeField(); + } + private void resultDeselect(OptionItem item, GQuery element) { choices--; @@ -103,9 +110,9 @@ protected void resultsHide() { private void updateSelectedText() { String selectedText; if (choices > 1) { - selectedText = getOptions().getManySelectedTextMultipleMobile(); + selectedText = getOptions().getManySelectedTextMultipleMobile(); } else { - selectedText = getOptions().getOneSelectedTextMultipleMobile(); + selectedText = getOptions().getOneSelectedTextMultipleMobile(); } selectedText = selectedText.replace("{}", "" + choices);