From 4f99b7f57f3c2d2596629f4c5aa75c27b2fdae50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20M=C3=A9riouma-Caron?= Date: Thu, 15 Oct 2015 19:03:59 -0400 Subject: [PATCH 1/4] Fix for #271 : "this.selectedItem is undefined" when adding new items to ChosenListBox and calling update() --- .../client/ChosenSampleIntegrationTests.java | 2 + .../ChosenListBoxMultipleSelectAddItems.java | 38 +++++++++++++++++++ integration-test/src/test/java/ChosenIT.java | 1 + .../src/test/java/DesktopChosenIT.java | 17 +++++++++ .../src/test/java/MobileChosenIT.java | 17 +++++++++ .../client/AbstractMobileChosenImpl.java | 2 +- .../com/arcbees/chosen/client/ChosenImpl.java | 14 +++---- .../client/MobileMultipleChosenImpl.java | 13 +++++-- 8 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/testcases/ChosenListBoxMultipleSelectAddItems.java 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..0cc97e1 --- /dev/null +++ b/integration-test/src/main/java/com/arcbees/chosen/integrationtest/client/testcases/ChosenListBoxMultipleSelectAddItems.java @@ -0,0 +1,38 @@ +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.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/ChosenIT.java b/integration-test/src/test/java/ChosenIT.java index c788edd..0850ffe 100644 --- a/integration-test/src/test/java/ChosenIT.java +++ b/integration-test/src/test/java/ChosenIT.java @@ -31,6 +31,7 @@ import com.arcbees.chosen.integrationtest.client.TestCase; import com.arcbees.chosen.integrationtest.client.domain.CarBrand; 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; diff --git a/integration-test/src/test/java/DesktopChosenIT.java b/integration-test/src/test/java/DesktopChosenIT.java index 6e190d2..d0633f2 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..844df8a 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..85b9ea3 100644 --- a/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java +++ b/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java @@ -1,12 +1,12 @@ /** * 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 @@ -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); } @@ -1065,6 +1064,7 @@ private int positionRelativeToWindow() { } private void rebuildResultItems(boolean init) { + GQuery.console.log("rebuild"); SafeHtmlBuilder content = new SafeHtmlBuilder(); SafeHtmlBuilder optionsHtml = new SafeHtmlBuilder(); 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); From e8229a489a482263f4f2404282a57afb1901d0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20M=C3=A9riouma-Caron?= Date: Thu, 15 Oct 2015 19:13:15 -0400 Subject: [PATCH 2/4] Fixed checkstyle --- .../ChosenListBoxMultipleSelectAddItems.java | 16 ++++++++++++++++ integration-test/src/test/java/ChosenIT.java | 1 - .../src/test/java/DesktopChosenIT.java | 2 +- .../src/test/java/MobileChosenIT.java | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) 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 index 0cc97e1..bad6c2f 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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; diff --git a/integration-test/src/test/java/ChosenIT.java b/integration-test/src/test/java/ChosenIT.java index 0850ffe..c788edd 100644 --- a/integration-test/src/test/java/ChosenIT.java +++ b/integration-test/src/test/java/ChosenIT.java @@ -31,7 +31,6 @@ import com.arcbees.chosen.integrationtest.client.TestCase; import com.arcbees.chosen.integrationtest.client.domain.CarBrand; 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; diff --git a/integration-test/src/test/java/DesktopChosenIT.java b/integration-test/src/test/java/DesktopChosenIT.java index d0633f2..75a2a6a 100644 --- a/integration-test/src/test/java/DesktopChosenIT.java +++ b/integration-test/src/test/java/DesktopChosenIT.java @@ -271,7 +271,7 @@ public void singleMobile_mobileLayoutUsed() throws InterruptedException { } /** - * Tests that the values are added/selected when using update/setItemSelected + * Tests that the values are added/selected when using update/setItemSelected. * See https://github.com/ArcBees/gwtchosen/issues/271 */ @Test diff --git a/integration-test/src/test/java/MobileChosenIT.java b/integration-test/src/test/java/MobileChosenIT.java index 844df8a..ed9d77d 100644 --- a/integration-test/src/test/java/MobileChosenIT.java +++ b/integration-test/src/test/java/MobileChosenIT.java @@ -135,7 +135,7 @@ public void singleMobile_closeDropdown() throws InterruptedException { } /** - * Tests that the values are added/selected when using update/setItemSelected + * Tests that the values are added/selected when using update/setItemSelected. * See https://github.com/ArcBees/gwtchosen/issues/271 */ @Test From 349814ab5cc94fcf186873c99723d680875c844b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20M=C3=A9riouma-Caron?= Date: Thu, 15 Oct 2015 20:02:31 -0400 Subject: [PATCH 3/4] Removed debug --- .../client/testcases/ChosenListBoxMultipleSelectAddItems.java | 1 + plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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 index bad6c2f..1f1ebdd 100644 --- 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 @@ -40,6 +40,7 @@ public void run() { button.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { + listBox.clear(); listBox.addItem("Four"); listBox.addItem("Five"); listBox.addItem("Six"); 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 85b9ea3..ce78c16 100644 --- a/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java +++ b/plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java @@ -1064,7 +1064,6 @@ private int positionRelativeToWindow() { } private void rebuildResultItems(boolean init) { - GQuery.console.log("rebuild"); SafeHtmlBuilder content = new SafeHtmlBuilder(); SafeHtmlBuilder optionsHtml = new SafeHtmlBuilder(); From 13098e8591fe01344db60d2e26c4cef51ec6604d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20M=C3=A9riouma-Caron?= Date: Thu, 15 Oct 2015 22:50:24 -0400 Subject: [PATCH 4/4] Revert copyright --- .../main/java/com/arcbees/chosen/client/ChosenImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 ce78c16..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,12 +1,12 @@ -/** +/* * 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