]> source.dussan.org Git - vaadin-framework.git/commitdiff
Tests that selection works when enabled and doesn't when disabled (#291)
authorDenis Anisimov <denis@vaadin.com>
Mon, 19 Sep 2016 12:25:33 +0000 (15:25 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 20 Sep 2016 07:16:13 +0000 (07:16 +0000)
Change-Id: Ic0e198645ea039f9bd8a87c8242358e47530c856

uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java
uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java
uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java
uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java

index e8af72bfa6954d89db3ccb2077d50e9711030caa..6553c0ae6e653382ebb3ec860578ac74404555e8 100644 (file)
  */
 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<Object, Resource> DEFAULT_ICON_PROVIDER = item -> "Item 2"
+            .equals(item) ? ICON_16_HELP_PNG_CACHEABLE : null;
+
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Override
     protected Class<CheckBoxGroup<Object>> getTestClass() {
         return (Class) CheckBoxGroup.class;
     }
 
-
     @Override
     protected CheckBoxGroup<Object> constructComponent() {
         CheckBoxGroup<Object> 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<Object> 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<Object> 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;
+        }
+    }
 }
index 1b4e9ac0df1450ae71e2cd978c3f7a9f0c06386e..19d9ffd3f3791e73b9b5033b48fe6a3fe4b3725a 100644 (file)
  */
 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<Object> 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<Object> selectionModel = getComponent().getSelectionModel();
+        SelectionModel.Single<Object> 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())));
     }
+
 }
index 087ab55b6c432f0de73defdb21c55896836daa76..a30f5d71e267c2b56cd570acaf300eeaa72dcdb2 100644 (file)
 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<WebElement> 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 {
             }
         }
     }
+
 }
index a5919246412b2bc70a114887e16a28578cd601c6..c90933329018f6497e6eb101011c0c9848c3b31d 100644 (file)
  */
 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);
     }
+
 }