From: Denis Anisimov Date: Mon, 19 Sep 2016 12:25:33 +0000 (+0300) Subject: Tests that selection works when enabled and doesn't when disabled (#291) X-Git-Tag: 8.0.0.alpha3~21 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c6e195aa63d1dfd4568453bcb33b40c332f1b953;p=vaadin-framework.git Tests that selection works when enabled and doesn't when disabled (#291) Change-Id: Ic0e198645ea039f9bd8a87c8242358e47530c856 --- diff --git a/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java index e8af72bfa6..6553c0ae6e 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java @@ -15,8 +15,11 @@ */ package com.vaadin.tests.components.checkbox; +import java.util.function.Function; import java.util.stream.IntStream; +import com.vaadin.server.FontAwesome; +import com.vaadin.server.Resource; import com.vaadin.shared.data.selection.SelectionModel.Multi; import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI; import com.vaadin.ui.CheckBoxGroup; @@ -31,19 +34,19 @@ public class CheckBoxGroupTestUI private final String selectionCategory = "Selection"; + private static final Function DEFAULT_ICON_PROVIDER = item -> "Item 2" + .equals(item) ? ICON_16_HELP_PNG_CACHEABLE : null; + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected Class> getTestClass() { return (Class) CheckBoxGroup.class; } - @Override protected CheckBoxGroup constructComponent() { CheckBoxGroup checkBoxGroup = super.constructComponent(); - checkBoxGroup.setItemIconProvider( - item -> "Item 2".equals(item) ? ICON_16_HELP_PNG_CACHEABLE : - null); + checkBoxGroup.setItemIconProvider(DEFAULT_ICON_PROVIDER); checkBoxGroup.setItemEnabledProvider(item -> !"Item 10".equals(item)); return checkBoxGroup; } @@ -53,6 +56,7 @@ public class CheckBoxGroupTestUI super.createActions(); createListenerMenu(); createSelectionMenu(); + createItemProviderMenu(); } protected void createSelectionMenu() { @@ -80,9 +84,54 @@ public class CheckBoxGroupTestUI } } + private void createItemProviderMenu() { + createBooleanAction("Use Item Caption Provider", "Item Provider", false, + this::useItemCaptionProvider); + createBooleanAction("Use Item Icon Provider", "Item Provider", false, + this::useItemIconProvider); + } + + private void useItemCaptionProvider(CheckBoxGroup group, + boolean activate, Object data) { + if (activate) { + group.setItemCaptionProvider(item -> item.toString() + " Caption"); + } else { + group.setItemCaptionProvider(item -> item.toString()); + } + group.getDataSource().refreshAll(); + } + + private void useItemIconProvider(CheckBoxGroup group, + boolean activate, Object data) { + if (activate) { + group.setItemIconProvider( + item -> FontAwesome.values()[getIndex(item) + 1]); + } else { + group.setItemIconProvider(DEFAULT_ICON_PROVIDER); + } + group.getDataSource().refreshAll(); + } + protected void createListenerMenu() { createListenerAction("Selection listener", "Listeners", c -> c.addSelectionListener( e -> log("Selected: " + e.getNewSelection()))); } + + private int getIndex(Object item) { + int index = item.toString().indexOf(' '); + if (index < 0) { + return 0; + } + String postfix = item.toString().substring(index + 1); + index = postfix.indexOf(' '); + if (index >= 0) { + postfix = postfix.substring(0, index); + } + try { + return Integer.parseInt(postfix); + } catch (NumberFormatException e) { + return 0; + } + } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java index 1b4e9ac0df..19d9ffd3f3 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java @@ -15,12 +15,12 @@ */ package com.vaadin.tests.components.radiobutton; +import java.util.stream.IntStream; + import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI; import com.vaadin.ui.RadioButtonGroup; -import java.util.stream.IntStream; - /** * Test UI for RadioButtonGroup component * @@ -42,6 +42,7 @@ public class RadioButtonGroupTestUI super.createActions(); createListenerMenu(); createSelectionMenu(); + createItemProviderMenu(); } protected void createSelectionMenu() { @@ -54,12 +55,28 @@ public class RadioButtonGroupTestUI item, data) -> toggleSelection(item); IntStream.of(0, 1, 5, 10, 25).mapToObj(i -> "Item " + i) - .forEach(item -> createClickAction("Toggle " + item, selectionCategory, - toggleSelection, item)); + .forEach(item -> createClickAction("Toggle " + item, + selectionCategory, toggleSelection, item)); + } + + private void createItemProviderMenu() { + createBooleanAction("Use Item Caption Provider", "Item Provider", false, + this::useItemCaptionProvider); + } + + private void useItemCaptionProvider(RadioButtonGroup group, + boolean activate, Object data) { + if (activate) { + group.setItemCaptionProvider(item -> item.toString() + " Caption"); + } else { + group.setItemCaptionProvider(item -> item.toString()); + } + group.getDataSource().refreshAll(); } private void toggleSelection(String item) { - SelectionModel.Single selectionModel = getComponent().getSelectionModel(); + SelectionModel.Single selectionModel = getComponent() + .getSelectionModel(); if (selectionModel.isSelected(item)) { selectionModel.deselect(item); } else { @@ -72,4 +89,5 @@ public class RadioButtonGroupTestUI c -> c.addSelectionListener( e -> log("Selected: " + e.getSelectedItem()))); } + } diff --git a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java index 087ab55b6c..a30f5d71e2 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java @@ -16,26 +16,24 @@ package com.vaadin.tests.components.checkboxgroup; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import com.vaadin.server.FontAwesome; import com.vaadin.testbench.customelements.CheckBoxGroupElement; import com.vaadin.tests.components.checkbox.CheckBoxGroupTestUI; import com.vaadin.tests.tb3.MultiBrowserTest; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; /** * Test for CheckBoxGroup @@ -82,6 +80,66 @@ public class CheckBoxGroupTest extends MultiBrowserTest { Assert.assertEquals("3. Selected: [Item 2]", getLogRow(0)); } + @Test + public void disabled_clickToSelect() { + selectMenuPath("Component", "State", "Enabled"); + + Assert.assertTrue(getSelect().findElements(By.tagName("input")).stream() + .allMatch(element -> element.getAttribute("disabled") != null)); + + selectMenuPath("Component", "Listeners", "Selection listener"); + + String lastLogRow = getLogRow(0); + + getSelect().selectByText("Item 4"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + + getSelect().selectByText("Item 2"); + // Selection order (most recently selected is last) + Assert.assertEquals(lastLogRow, getLogRow(0)); + + getSelect().selectByText("Item 4"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + } + + @Test + public void clickToSelect_reenable() { + selectMenuPath("Component", "State", "Enabled"); + selectMenuPath("Component", "Listeners", "Selection listener"); + + getSelect().selectByText("Item 4"); + + selectMenuPath("Component", "State", "Enabled"); + + getSelect().selectByText("Item 5"); + Assert.assertEquals("3. Selected: [Item 5]", getLogRow(0)); + + getSelect().selectByText("Item 2"); + Assert.assertEquals("4. Selected: [Item 5, Item 2]", getLogRow(0)); + + getSelect().selectByText("Item 5"); + Assert.assertEquals("5. Selected: [Item 2]", getLogRow(0)); + } + + @Test + public void itemCaptionProvider() { + selectMenuPath("Component", "Item Provider", + "Use Item Caption Provider"); + assertItems(20, " Caption"); + } + + @Test + public void itemIconProvider() { + selectMenuPath("Component", "Item Provider", "Use Item Icon Provider"); + assertItemSuffices(20); + List icons = getSelect() + .findElements(By.cssSelector(".v-icon.FontAwesome")); + for (int i = 0; i < icons.size(); i++) { + Assert.assertEquals(FontAwesome.values()[i + 1].getCodepoint(), + icons.get(i).getText().charAt(0)); + } + } + @Test public void selectProgramatically() { selectMenuPath("Component", "Listeners", "Selection listener"); @@ -116,9 +174,22 @@ public class CheckBoxGroupTest extends MultiBrowserTest { } protected void assertItems(int count) { + assertItems(count, ""); + } + + protected void assertItems(int count, String suffix) { + int i = 0; + for (String text : getSelect().getOptions()) { + assertEquals("Item " + i + suffix, text); + i++; + } + assertEquals("Number of items", count, i); + } + + protected void assertItemSuffices(int count) { int i = 0; for (String text : getSelect().getOptions()) { - assertEquals("Item " + i, text); + assertTrue(text.endsWith("Item " + i)); i++; } assertEquals("Number of items", count, i); @@ -131,10 +202,10 @@ public class CheckBoxGroupTest extends MultiBrowserTest { String cssClassList = optionsCssClasses.get(i); if (i == 10) { assertTrue("10th item should be disabled", - cssClassList.toLowerCase().contains("disabled")); + cssClassList.toLowerCase().contains("disabled")); } else { assertFalse("Only 10th item should be disabled", - cssClassList.toLowerCase().contains("disabled")); + cssClassList.toLowerCase().contains("disabled")); } } } @@ -151,4 +222,5 @@ public class CheckBoxGroupTest extends MultiBrowserTest { } } } + } diff --git a/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java b/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java index a591924641..c909333290 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java @@ -15,15 +15,17 @@ */ package com.vaadin.tests.components.radiobutton; -import com.vaadin.testbench.customelements.RadioButtonGroupElement; -import com.vaadin.tests.tb3.MultiBrowserTest; +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.openqa.selenium.By; -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import com.vaadin.testbench.customelements.RadioButtonGroupElement; +import com.vaadin.tests.tb3.MultiBrowserTest; /** * Test for RadioButtonGroup @@ -69,6 +71,53 @@ public class RadioButtonGroupTest extends MultiBrowserTest { Assert.assertEquals("3. Selected: Optional[Item 4]", getLogRow(0)); } + @Test + public void disabled_clickToSelect() { + selectMenuPath("Component", "State", "Enabled"); + + Assert.assertTrue(getSelect().findElements(By.tagName("input")).stream() + .allMatch(element -> element.getAttribute("disabled") != null)); + + selectMenuPath("Component", "Listeners", "Selection listener"); + + String lastLogRow = getLogRow(0); + + getSelect().selectByText("Item 4"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + + getSelect().selectByText("Item 2"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + + getSelect().selectByText("Item 4"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + } + + @Test + public void clickToSelect_reenable() { + selectMenuPath("Component", "State", "Enabled"); + selectMenuPath("Component", "Listeners", "Selection listener"); + + getSelect().selectByText("Item 4"); + + selectMenuPath("Component", "State", "Enabled"); + + getSelect().selectByText("Item 5"); + Assert.assertEquals("3. Selected: Optional[Item 5]", getLogRow(0)); + + getSelect().selectByText("Item 2"); + Assert.assertEquals("4. Selected: Optional[Item 2]", getLogRow(0)); + + getSelect().selectByText("Item 4"); + Assert.assertEquals("5. Selected: Optional[Item 4]", getLogRow(0)); + } + + @Test + public void itemCaptionProvider() { + selectMenuPath("Component", "Item Provider", + "Use Item Caption Provider"); + assertItems(20, " Caption"); + } + @Test public void selectProgramatically() { selectMenuPath("Component", "Listeners", "Selection listener"); @@ -102,11 +151,16 @@ public class RadioButtonGroupTest extends MultiBrowserTest { } protected void assertItems(int count) { + assertItems(count, ""); + } + + protected void assertItems(int count, String suffix) { int i = 0; for (String text : getSelect().getOptions()) { - assertEquals("Item " + i, text); + assertEquals("Item " + i + suffix, text); i++; } assertEquals("Number of items", count, i); } + }