diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-09-22 14:19:34 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-27 07:23:36 +0000 |
commit | 7e78d52dfe72678cf275585bc552b1612844da44 (patch) | |
tree | a6d3454fdb59f6a2e69c0bab125c228dca93a169 /uitest | |
parent | 0052d59a318075a3ce8202b2eab84e9d643fc544 (diff) | |
download | vaadin-framework-7e78d52dfe72678cf275585bc552b1612844da44.tar.gz vaadin-framework-7e78d52dfe72678cf275585bc552b1612844da44.zip |
TwinColSelect with new databinding API
Removes feature for adding new items.
Introduces a AbstractMultiSelect-abstraction layer,
which is used in server side by TwinColSelect & CheckBoxGroup and
on client side only TwinColSelect for now. Plan is to use it for
ListSelect too.
Further improvement would be to make AbstractMultiSelect use
SelectionModel that extends AbstractSelectionModel and is thus used
as an extension both as client & server side.
Updates to JUnit 4.12 for easier use of @Parameterized test..
Change-Id: I64258c2229b9514d382693748e2ca562a1e448d4
Diffstat (limited to 'uitest')
7 files changed, 407 insertions, 77 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java new file mode 100644 index 0000000000..fadd94a4be --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java @@ -0,0 +1,83 @@ +package com.vaadin.tests.components.abstractlisting; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import com.vaadin.shared.data.selection.SelectionModel.Multi; +import com.vaadin.ui.AbstractMultiSelect; + +public abstract class AbstractMultiSelectTestUI<MULTISELECT extends AbstractMultiSelect<Object>> + extends AbstractListingTestUI<MULTISELECT> { + + protected final String selectionCategory = "Selection"; + + @Override + protected void createActions() { + super.createActions(); + createItemCaptionGeneratorMenu(); + createSelectionMenu(); + createListenerMenu(); + } + + protected void createItemCaptionGeneratorMenu() { + createBooleanAction("Use Item Caption Generator", "Item Generator", + false, this::useItemCaptionProvider); + } + + private void useItemCaptionProvider(MULTISELECT select, boolean activate, + Object data) { + if (activate) { + select.setItemCaptionGenerator( + item -> item.toString() + " Caption"); + } else { + select.setItemCaptionGenerator(item -> item.toString()); + } + select.getDataSource().refreshAll(); + } + + protected void createSelectionMenu() { + createClickAction( + "Clear selection", selectionCategory, (component, item, + data) -> component.getSelectionModel().deselectAll(), + ""); + + Command<MULTISELECT, String> toggleSelection = (component, item, + data) -> toggleSelection(item); + + List<String> items = IntStream.of(0, 1, 5, 10, 25) + .mapToObj(i -> "Item " + i).collect(Collectors.toList()); + items.forEach(item -> createClickAction("Toggle " + item, + selectionCategory, toggleSelection, item)); + + Command<MULTISELECT, Boolean> toggleMultiSelection = (component, i, + data) -> toggleMultiSelection(i, items); + + createBooleanAction("Toggle items 0, 1, 5, 10, 25", selectionCategory, + false, toggleMultiSelection, items); + } + + private void toggleSelection(String item) { + Multi<Object> selectionModel = getComponent().getSelectionModel(); + if (selectionModel.isSelected(item)) { + selectionModel.deselect(item); + } else { + selectionModel.select(item); + } + } + + private void toggleMultiSelection(boolean add, List<String> items) { + Multi<Object> selectionModel = getComponent().getSelectionModel(); + if (add) { + selectionModel.selectItems(items.toArray()); + } else { + selectionModel.deselectItems(items.toArray()); + } + } + + protected void createListenerMenu() { + createListenerAction("Selection listener", "Listeners", + c -> c.addSelectionListener( + e -> log("Selected: " + e.getNewSelection()))); + } +} 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 6553c0ae6e..95d2c016b1 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,14 +15,10 @@ */ 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.tests.components.abstractlisting.AbstractMultiSelectTestUI; import com.vaadin.ui.CheckBoxGroup; +import com.vaadin.ui.IconGenerator; /** * Test UI for CheckBoxGroup component @@ -30,11 +26,9 @@ import com.vaadin.ui.CheckBoxGroup; * @author Vaadin Ltd */ public class CheckBoxGroupTestUI - extends AbstractListingTestUI<CheckBoxGroup<Object>> { - - private final String selectionCategory = "Selection"; + extends AbstractMultiSelectTestUI<CheckBoxGroup<Object>> { - private static final Function<Object, Resource> DEFAULT_ICON_PROVIDER = item -> "Item 2" + private static final IconGenerator<Object> DEFAULT_ICON_GENERATOR = item -> "Item 2" .equals(item) ? ICON_16_HELP_PNG_CACHEABLE : null; @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -46,7 +40,7 @@ public class CheckBoxGroupTestUI @Override protected CheckBoxGroup<Object> constructComponent() { CheckBoxGroup<Object> checkBoxGroup = super.constructComponent(); - checkBoxGroup.setItemIconProvider(DEFAULT_ICON_PROVIDER); + checkBoxGroup.setItemIconGenerator(DEFAULT_ICON_GENERATOR); checkBoxGroup.setItemEnabledProvider(item -> !"Item 10".equals(item)); return checkBoxGroup; } @@ -54,70 +48,25 @@ public class CheckBoxGroupTestUI @Override protected void createActions() { super.createActions(); - createListenerMenu(); - createSelectionMenu(); - createItemProviderMenu(); - } - - protected void createSelectionMenu() { - createClickAction( - "Clear selection", selectionCategory, (component, item, - data) -> component.getSelectionModel().deselectAll(), - ""); - - Command<CheckBoxGroup<Object>, String> toggleSelection = (component, - item, data) -> toggleSelection(item); - - IntStream.of(0, 1, 5, 10, 25).mapToObj(i -> "Item " + i) - .forEach(item -> { - createClickAction("Toggle " + item, selectionCategory, - toggleSelection, item); - }); - } - - private void toggleSelection(String item) { - Multi<Object> selectionModel = getComponent().getSelectionModel(); - if (selectionModel.isSelected(item)) { - selectionModel.deselect(item); - } else { - selectionModel.select(item); - } + createItemIconGenerator(); } - private void createItemProviderMenu() { - createBooleanAction("Use Item Caption Provider", "Item Provider", false, - this::useItemCaptionProvider); - createBooleanAction("Use Item Icon Provider", "Item Provider", false, + private void createItemIconGenerator() { + createBooleanAction("Use Item Icon Generator", "Item Generator", 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( + group.setItemIconGenerator( item -> FontAwesome.values()[getIndex(item) + 1]); } else { - group.setItemIconProvider(DEFAULT_ICON_PROVIDER); + group.setItemIconGenerator(DEFAULT_ICON_GENERATOR); } 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) { diff --git a/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelectCaptionStyles.java b/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelectCaptionStyles.java index 563e730681..6491651c4b 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelectCaptionStyles.java +++ b/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelectCaptionStyles.java @@ -3,14 +3,15 @@ package com.vaadin.tests.components.select; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; -import com.vaadin.v7.ui.TwinColSelect; +import com.vaadin.ui.TwinColSelect; public class TwinColSelectCaptionStyles extends TestBase { @Override protected void setup() { setTheme("tests-tickets"); - final TwinColSelect sel = new TwinColSelect("Component caption"); + final TwinColSelect<String> sel = new TwinColSelect<>( + "Component caption"); sel.setLeftColumnCaption("Left caption"); sel.setRightColumnCaption("Right caption"); sel.setStyleName("styled-twincol-captions"); diff --git a/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelects.java b/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelects.java index 7a769de843..fbc2567096 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelects.java +++ b/uitest/src/main/java/com/vaadin/tests/components/select/TwinColSelects.java @@ -1,7 +1,10 @@ package com.vaadin.tests.components.select; +import java.util.ArrayList; +import java.util.List; + import com.vaadin.tests.components.ComponentTestCase; -import com.vaadin.v7.ui.TwinColSelect; +import com.vaadin.ui.TwinColSelect; public class TwinColSelects extends ComponentTestCase<TwinColSelect> { @@ -13,7 +16,7 @@ public class TwinColSelects extends ComponentTestCase<TwinColSelect> { @Override protected void initializeComponents() { - TwinColSelect tws = createTwinColSelect("400x<auto>"); + TwinColSelect<String> tws = createTwinColSelect("400x<auto>"); tws.setWidth("400px"); tws.setHeight("-1px"); addTestComponent(tws); @@ -34,14 +37,13 @@ public class TwinColSelects extends ComponentTestCase<TwinColSelect> { } - private TwinColSelect createTwinColSelect(String caption) { - TwinColSelect select = new TwinColSelect(caption); - select.addContainerProperty(CAPTION, String.class, null); + private TwinColSelect<String> createTwinColSelect(String caption) { + TwinColSelect<String> select = new TwinColSelect<>(caption); + List<String> items = new ArrayList<>(); for (int i = 0; i < 20; i++) { - select.addItem("" + i).getItemProperty(CAPTION) - .setValue("Item " + i); + items.add("Item " + i); } - select.setImmediate(true); + select.setItems(items); return select; } diff --git a/uitest/src/main/java/com/vaadin/tests/components/twincolselect/TwinColSelectTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/twincolselect/TwinColSelectTestUI.java new file mode 100644 index 0000000000..761c412bd6 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/twincolselect/TwinColSelectTestUI.java @@ -0,0 +1,35 @@ +package com.vaadin.tests.components.twincolselect; + +import java.util.LinkedHashMap; + +import com.vaadin.tests.components.abstractlisting.AbstractMultiSelectTestUI; +import com.vaadin.ui.TwinColSelect; + +public class TwinColSelectTestUI + extends AbstractMultiSelectTestUI<TwinColSelect<Object>> { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Override + protected Class<TwinColSelect<Object>> getTestClass() { + return (Class) TwinColSelect.class; + } + + @Override + protected void createActions() { + super.createActions(); + createRows(); + } + + private void createRows() { + LinkedHashMap<String, Integer> options = new LinkedHashMap<>(); + options.put("0", 0); + options.put("1", 1); + options.put("2", 2); + options.put("5", 5); + options.put("10 (default)", 10); + options.put("50", 50); + + createSelectAction("Rows", CATEGORY_STATE, options, "10 (default)", + (c, value, data) -> c.setRows(value), null); + } +} 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 a30f5d71e2..4bb1defbf6 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 @@ -1,6 +1,6 @@ /* * Copyright 2000-2014 Vaadin Ltd. - * + * * 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 @@ -122,15 +122,16 @@ public class CheckBoxGroupTest extends MultiBrowserTest { } @Test - public void itemCaptionProvider() { - selectMenuPath("Component", "Item Provider", - "Use Item Caption Provider"); + public void itemCaptionGenerator() { + selectMenuPath("Component", "Item Generator", + "Use Item Caption Generator"); assertItems(20, " Caption"); } @Test - public void itemIconProvider() { - selectMenuPath("Component", "Item Provider", "Use Item Icon Provider"); + public void itemIconGenerator() { + selectMenuPath("Component", "Item Generator", + "Use Item Icon Generator"); assertItemSuffices(20); List<WebElement> icons = getSelect() .findElements(By.cssSelector(".v-icon.FontAwesome")); diff --git a/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java b/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java new file mode 100644 index 0000000000..42c8128d10 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java @@ -0,0 +1,259 @@ +package com.vaadin.tests.components.twincolselect; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.Select; + +import com.vaadin.testbench.elements.TwinColSelectElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class TwinColSelectTest extends MultiBrowserTest { + + @Before + public void setUp() throws Exception { + openTestURL(); + } + + @Test + public void initialLoad_containsCorrectItems() { + assertItems(20); + } + + @Test + public void initialItems_reduceItemCount_containsCorrectItems() { + selectMenuPath("Component", "Data source", "Items", "5"); + assertItems(5); + } + + @Test + public void initialItems_increaseItemCount_containsCorrectItems() { + selectMenuPath("Component", "Data source", "Items", "100"); + assertItems(100); + } + + @Test + public void itemsMovedFromLeftToRight() { + selectMenuPath("Component", "Data source", "Items", "5"); + assertItems(5); + + selectItems("Item 1", "Item 2", "Item 4"); + + assertSelected("Item 1", "Item 2", "Item 4"); + + assertOptionTexts("Item 0", "Item 3"); + + deselectItems("Item 1", "Item 4"); + + assertSelected("Item 2"); + + assertOptionTexts("Item 0", "Item 1", "Item 3", "Item 4"); + + selectItems("Item 0"); + + assertSelected("Item 0", "Item 2"); + assertOptionTexts("Item 1", "Item 3", "Item 4"); + } + + @Test + public void clickToSelect() { + selectMenuPath("Component", "Listeners", "Selection listener"); + + selectItems("Item 4"); + Assert.assertEquals("1. Selected: [Item 4]", getLogRow(0)); + assertSelected("Item 4"); + + // the previous item stays selected + selectItems("Item 2"); + // Selection order (most recently selected is last) + Assert.assertEquals("2. Selected: [Item 4, Item 2]", getLogRow(0)); + assertSelected("Item 2", "Item 4"); + + deselectItems("Item 4"); + Assert.assertEquals("3. Selected: [Item 2]", getLogRow(0)); + assertSelected("Item 2"); + + selectItems("Item 10", "Item 0", "Item 9", "Item 4"); + + Assert.assertEquals( + "4. Selected: [Item 2, Item 0, Item 4, Item 10, Item 9]", + getLogRow(0)); + assertSelected("Item 0", "Item 2", "Item 4", "Item 9", "Item 10"); + + deselectItems("Item 0", "Item 2", "Item 9"); + Assert.assertEquals("5. Selected: [Item 4, Item 10]", getLogRow(0)); + assertSelected("Item 4", "Item 10"); + } + + @Test + public void disabled_clickToSelect() { + selectMenuPath("Component", "State", "Enabled"); + + Assert.assertTrue(getTwinColSelect().findElements(By.tagName("input")) + .stream() + .allMatch(element -> element.getAttribute("disabled") != null)); + + selectMenuPath("Component", "Listeners", "Selection listener"); + + String lastLogRow = getLogRow(0); + + selectItems("Item 4"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + assertNothingSelected(); + + selectItems("Item 2"); + // Selection order (most recently selected is last) + Assert.assertEquals(lastLogRow, getLogRow(0)); + assertNothingSelected(); + + selectItems("Item 4"); + Assert.assertEquals(lastLogRow, getLogRow(0)); + assertNothingSelected(); + } + + @Test + public void clickToSelect_reenable() { + selectMenuPath("Component", "State", "Enabled"); + selectMenuPath("Component", "Listeners", "Selection listener"); + + selectItems("Item 4"); + assertNothingSelected(); + + selectMenuPath("Component", "State", "Enabled"); + + selectItems("Item 5"); + Assert.assertEquals("3. Selected: [Item 5]", getLogRow(0)); + assertSelected("Item 5"); + + selectItems("Item 2"); + Assert.assertEquals("4. Selected: [Item 5, Item 2]", getLogRow(0)); + assertSelected("Item 2", "Item 5"); + + deselectItems("Item 5"); + Assert.assertEquals("5. Selected: [Item 2]", getLogRow(0)); + assertSelected("Item 2"); + } + + @Test + public void itemCaptionProvider() { + selectMenuPath("Component", "Item Generator", + "Use Item Caption Generator"); + assertItems(20, " Caption"); + } + + @Test + public void selectProgramatically() { + selectMenuPath("Component", "Listeners", "Selection listener"); + + selectMenuPath("Component", "Selection", "Toggle Item 5"); + Assert.assertEquals("2. Selected: [Item 5]", getLogRow(0)); + assertSelected("Item 5"); + + selectMenuPath("Component", "Selection", "Toggle Item 1"); + // Selection order (most recently selected is last) + Assert.assertEquals("4. Selected: [Item 5, Item 1]", getLogRow(0)); + // DOM order + assertSelected("Item 1", "Item 5"); + + selectMenuPath("Component", "Selection", "Toggle Item 5"); + Assert.assertEquals("6. Selected: [Item 1]", getLogRow(0)); + assertSelected("Item 1"); + + selectMenuPath("Component", "Selection", + "Toggle items 0, 1, 5, 10, 25"); + + // currently non-existing items are added to selection! + Assert.assertEquals( + "8. Selected: [Item 1, Item 0, Item 5, Item 10, Item 25]", + getLogRow(0)); + assertSelected("Item 0", "Item 1", "Item 5", "Item 10"); + } + + private void assertSelected(String... expectedSelection) { + Assert.assertEquals(Arrays.asList(expectedSelection), + getTwinColSelect().getValues()); + } + + private void assertNothingSelected() { + Assert.assertEquals(0, getTwinColSelect().getValues().size()); + } + + @Override + protected Class<?> getUIClass() { + return TwinColSelectTestUI.class; + } + + protected TwinColSelectElement getTwinColSelect() { + return $(TwinColSelectElement.class).first(); + } + + protected Select getOptionsElement() { + return new Select(getTwinColSelect().findElement(By.tagName("select"))); + } + + protected Select getSelectedOptionsElement() { + return new Select( + getTwinColSelect().findElements(By.tagName("select")).get(1)); + } + + protected WebElement getSelectButton() { + return getTwinColSelect().findElements(By.className("v-button")).get(0); + } + + protected WebElement getDeselectButton() { + return getTwinColSelect().findElements(By.className("v-button")).get(1); + } + + protected void selectItems(String... items) { + Select options = getOptionsElement(); + options.deselectAll(); + Stream.of(items).forEach(text -> options.selectByVisibleText(text)); + getSelectButton().click(); + } + + protected void deselectItems(String... items) { + Select options = getSelectedOptionsElement(); + options.deselectAll(); + Stream.of(items).forEach(text -> options.selectByVisibleText(text)); + getDeselectButton().click(); + } + + protected void assertItems(int count) { + assertItems(count, ""); + } + + protected void assertItems(int count, String suffix) { + int i = 0; + for (String text : getTwinColSelect().getOptions()) { + assertEquals("Item " + i + suffix, text); + i++; + } + assertEquals("Number of items", count, i); + } + + protected void assertItemSuffices(int count) { + int i = 0; + for (String text : getTwinColSelect().getOptions()) { + assertTrue(text.endsWith("Item " + i)); + i++; + } + assertEquals("Number of items", count, i); + } + + protected void assertOptionTexts(String... items) { + List<String> optionTexts = getOptionsElement().getOptions().stream() + .map(element -> element.getText()).collect(Collectors.toList()); + Assert.assertArrayEquals(items, optionTexts.toArray()); + } + +} |