diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-12-05 13:11:59 +0200 |
---|---|---|
committer | Denis <denis@vaadin.com> | 2016-12-05 14:11:59 +0300 |
commit | 0cef38513b58f4b486dfb1c0e3c154eed6039743 (patch) | |
tree | 062df7cbcdb34c0047e01464662bf2a773990367 /server/src/test/java | |
parent | 154a6a01148e84344e9730f9654ca3fc0190d907 (diff) | |
download | vaadin-framework-0cef38513b58f4b486dfb1c0e3c154eed6039743.tar.gz vaadin-framework-0cef38513b58f4b486dfb1c0e3c154eed6039743.zip |
Add a SelectionMode shorthand for Grid. (#83)
* Add a SelectionMode shorthand for Grid.
Hides setSelectionModel(...) by making it protected.
Refactores the usage of constructor / extend method for Abstract-, Single- and MultiSelectionModelImpl.
Fixes vaadin/framework8-issues#519
Change-Id: I48c30886450506639be9ee6e21c45b0c06755c88
Diffstat (limited to 'server/src/test/java')
7 files changed, 122 insertions, 79 deletions
diff --git a/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinder.java b/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinder.java index e31b2ab188..703ed9e170 100644 --- a/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinder.java +++ b/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinder.java @@ -24,9 +24,9 @@ import com.vaadin.tests.data.bean.BeanWithEnums; import com.vaadin.tests.data.bean.Sex; import com.vaadin.tests.data.bean.TestEnum; import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.MultiSelect; import com.vaadin.ui.components.grid.MultiSelectionModelImpl; -import com.vaadin.ui.components.grid.SingleSelectionModelImpl; public class GridAsMultiSelectInBinder extends BinderTestBase<Binder<BeanWithEnums>, BeanWithEnums> { @@ -50,10 +50,6 @@ public class GridAsMultiSelectInBinder private class CustomMultiSelectModel extends MultiSelectionModelImpl<Sex> { - public CustomMultiSelectModel(Grid<Sex> grid) { - super(grid); - } - @Override public void updateSelection(Set<Sex> addedItems, Set<Sex> removedItems, boolean userOriginated) { @@ -72,7 +68,7 @@ public class GridAsMultiSelectInBinder item = new BeanWithEnums(); grid = new Grid<>(); grid.setItems(TestEnum.values()); - grid.setSelectionModel(new MultiSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.MULTI); select = grid.asMultiSelect(); converterBinder.forField(select) @@ -82,7 +78,7 @@ public class GridAsMultiSelectInBinder @Test(expected = IllegalStateException.class) public void boundGridInBinder_selectionModelChanged_throws() { - grid.setSelectionModel(new SingleSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.SINGLE); select.select(TestEnum.ONE); } @@ -204,9 +200,12 @@ public class GridAsMultiSelectInBinder @Test public void addValueChangeListener_selectionUpdated_eventTriggeredForMultiSelect() { - Grid<Sex> grid = new Grid<>(); - CustomMultiSelectModel model = new CustomMultiSelectModel(grid); - grid.setSelectionModel(model); + CustomMultiSelectModel model = new CustomMultiSelectModel(); + Grid<Sex> grid = new Grid<Sex>() { + { + setSelectionModel(model); + } + }; grid.setItems(Sex.values()); MultiSelect<Sex> select = grid.asMultiSelect(); diff --git a/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinder.java b/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinder.java index cf649384e0..e1fc394fe6 100644 --- a/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinder.java +++ b/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinder.java @@ -15,8 +15,8 @@ import org.junit.Test; import com.vaadin.tests.data.bean.Person; import com.vaadin.tests.data.bean.Sex; import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.SingleSelect; -import com.vaadin.ui.components.grid.MultiSelectionModelImpl; import com.vaadin.ui.components.grid.SingleSelectionModelImpl; public class GridAsSingleSelectInBinder @@ -30,11 +30,8 @@ public class GridAsSingleSelectInBinder } } - private class CustomSingleSelectModel extends SingleSelectionModelImpl<Sex> { - - public CustomSingleSelectModel(Grid<Sex> grid) { - super(grid); - } + private class CustomSingleSelectModel + extends SingleSelectionModelImpl<Sex> { public void setSelectedFromClient(Sex item) { setSelectedFromClient(itemToKey(item)); @@ -55,7 +52,7 @@ public class GridAsSingleSelectInBinder @Test(expected = IllegalStateException.class) public void boundGridInBinder_selectionModelChanged_throws() { - grid.setSelectionModel(new MultiSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.MULTI); select.setValue(Sex.MALE); } @@ -126,7 +123,7 @@ public class GridAsSingleSelectInBinder @Test public void addValueChangeListener_selectionUpdated_eventTriggeredForSelect() { GridWithCustomSingleSelectionModel grid = new GridWithCustomSingleSelectionModel(); - CustomSingleSelectModel model = new CustomSingleSelectModel(grid); + CustomSingleSelectModel model = new CustomSingleSelectModel(); grid.setSelectionModel(model); grid.setItems(Sex.values()); select = grid.asSingleSelect(); diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java index 4eb7170fed..3a57311bd7 100644 --- a/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java @@ -32,10 +32,10 @@ import com.vaadin.shared.Registration; import com.vaadin.tests.util.MockUI; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.GridSelectionModel; +import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.UI; import com.vaadin.ui.components.grid.MultiSelectionModelImpl; import com.vaadin.ui.components.grid.MultiSelectionModelImpl.SelectAllCheckBoxVisible; -import com.vaadin.ui.components.grid.SingleSelectionModelImpl; import elemental.json.JsonObject; @@ -55,10 +55,6 @@ public class GridMultiSelectionModelTest { extends MultiSelectionModelImpl<String> { public final Map<String, Boolean> generatedData = new LinkedHashMap<>(); - public CustomMultiSelectionModel(Grid<String> grid) { - super(grid); - } - @Override public void generateData(String item, JsonObject jsonObject) { super.generateData(item, jsonObject); @@ -68,11 +64,24 @@ public class GridMultiSelectionModelTest { } + public static class CustomSelectionModelGrid extends Grid<String> { + public CustomSelectionModelGrid() { + this(new CustomMultiSelectionModel()); + } + + public CustomSelectionModelGrid( + GridSelectionModel<String> selectionModel) { + super(); + setSelectionModel(selectionModel); + } + } + @Before public void setUp() { grid = new Grid<>(); - selectionModel = new MultiSelectionModelImpl<>(grid); - grid.setSelectionModel(selectionModel); + selectionModel = (MultiSelectionModelImpl<Person>) grid + .setSelectionMode(SelectionMode.MULTI); + grid.setItems(PERSON_A, PERSON_B, PERSON_C); currentSelectionCapture = new Capture<>(); @@ -90,7 +99,7 @@ public class GridMultiSelectionModelTest { @Test(expected = IllegalStateException.class) public void selectionModelChanged_usingPreviousSelectionModel_throws() { - grid.setSelectionModel(new SingleSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.SINGLE); selectionModel.select(PERSON_A); } @@ -98,7 +107,7 @@ public class GridMultiSelectionModelTest { @Test public void changingSelectionModel_firesSelectionEvent() { Grid<String> customGrid = new Grid<>(); - customGrid.setSelectionModel(new MultiSelectionModelImpl<>(customGrid)); + customGrid.setSelectionMode(SelectionMode.MULTI); customGrid.setItems("Foo", "Bar", "Baz"); List<String> selectionChanges = new ArrayList<>(); @@ -120,8 +129,7 @@ public class GridMultiSelectionModelTest { assertEquals(Arrays.asList("Foo", "Bar"), selectionChanges); selectionChanges.clear(); - customGrid - .setSelectionModel(new SingleSelectionModelImpl<>(customGrid)); + customGrid.setSelectionMode(SelectionMode.SINGLE); assertFalse(customGrid.getSelectionModel().getFirstSelectedItem() .isPresent()); assertEquals(Arrays.asList(), selectionChanges); @@ -131,12 +139,11 @@ public class GridMultiSelectionModelTest { @Test public void serverSideSelection_GridChangingSelectionModel_sendsUpdatedRowsToClient() { - Grid<String> customGrid = new Grid<>(); + Grid<String> customGrid = new CustomSelectionModelGrid(); + CustomMultiSelectionModel customModel = (CustomMultiSelectionModel) customGrid + .getSelectionModel(); customGrid.setItems("Foo", "Bar", "Baz"); - CustomMultiSelectionModel customModel = new CustomMultiSelectionModel( - customGrid); - customGrid.setSelectionModel(customModel); customGrid.getDataCommunicator().beforeClientResponse(true); Assert.assertFalse("Item should have been updated as selected", @@ -172,8 +179,7 @@ public class GridMultiSelectionModelTest { // switch to single to cause event customModel.generatedData.clear(); - customGrid - .setSelectionModel(new SingleSelectionModelImpl<>(customGrid)); + customGrid.setSelectionMode(SelectionMode.SINGLE); customGrid.getDataCommunicator().beforeClientResponse(false); // changing selection model should trigger row updates, but the old @@ -184,8 +190,7 @@ public class GridMultiSelectionModelTest { @Test public void select_gridWithStrings() { Grid<String> gridWithStrings = new Grid<>(); - gridWithStrings.setSelectionModel( - new MultiSelectionModelImpl<>(gridWithStrings)); + gridWithStrings.setSelectionMode(SelectionMode.MULTI); gridWithStrings.setItems("Foo", "Bar", "Baz"); GridSelectionModel<String> model = gridWithStrings.getSelectionModel(); @@ -545,13 +550,11 @@ public class GridMultiSelectionModelTest { @SuppressWarnings({ "serial" }) @Test public void addValueChangeListener() { + String value = "foo"; + AtomicReference<MultiSelectionListener<String>> selectionListener = new AtomicReference<>(); Registration registration = Mockito.mock(Registration.class); - Grid<String> grid = new Grid<>(); - grid.setItems("foo", "bar"); - String value = "foo"; - MultiSelectionModelImpl<String> select = new MultiSelectionModelImpl<String>( - grid) { + MultiSelectionModelImpl<String> model = new MultiSelectionModelImpl<String>() { @Override public Registration addSelectionListener( MultiSelectionListener<String> listener) { @@ -565,15 +568,19 @@ public class GridMultiSelectionModelTest { } }; + Grid<String> grid = new CustomSelectionModelGrid(model); + + grid.setItems("foo", "bar"); + AtomicReference<MultiSelectionEvent<String>> event = new AtomicReference<>(); - Registration actualRegistration = select.addSelectionListener(evt -> { + Registration actualRegistration = model.addSelectionListener(evt -> { Assert.assertNull(event.get()); event.set(evt); }); Assert.assertSame(registration, actualRegistration); selectionListener.get().accept(new MultiSelectionEvent<>(grid, - select.asMultiSelect(), Collections.emptySet(), true)); + model.asMultiSelect(), Collections.emptySet(), true)); Assert.assertEquals(grid, event.get().getComponent()); Assert.assertEquals(new LinkedHashSet<>(Arrays.asList(value)), @@ -583,14 +590,13 @@ public class GridMultiSelectionModelTest { @Test public void selectAllCheckboxVisible__inMemoryDataProvider() { - Grid<String> grid = new Grid<>(); UI ui = new MockUI(); - ui.setContent(grid); - MultiSelectionModelImpl<String> model = new MultiSelectionModelImpl<>( - grid); - grid.setSelectionModel(model); + Grid<String> grid = new Grid<>(); + MultiSelectionModelImpl<String> model = (MultiSelectionModelImpl<String>) grid + .setSelectionMode(SelectionMode.MULTI); + ui.setContent(grid); // no items yet, default data provider is empty not in memory one Assert.assertFalse(model.isSelectAllCheckBoxVisible()); Assert.assertEquals(SelectAllCheckBoxVisible.DEFAULT, @@ -624,9 +630,8 @@ public class GridMultiSelectionModelTest { UI ui = new MockUI(); ui.setContent(grid); - MultiSelectionModelImpl<String> model = new MultiSelectionModelImpl<>( - grid); - grid.setSelectionModel(model); + MultiSelectionModelImpl<String> model = (MultiSelectionModelImpl<String>) grid + .setSelectionMode(SelectionMode.MULTI); // no items yet, default data provider is empty not in memory one Assert.assertFalse(model.isSelectAllCheckBoxVisible()); @@ -662,8 +667,7 @@ public class GridMultiSelectionModelTest { Assert.assertFalse(model.isSelectAllCheckBoxVisible()); // change back to depends on data provider - model.setSelectAllCheckBoxVisible( - SelectAllCheckBoxVisible.DEFAULT); + model.setSelectAllCheckBoxVisible(SelectAllCheckBoxVisible.DEFAULT); Assert.assertFalse(model.isSelectAllCheckBoxVisible()); Assert.assertEquals(SelectAllCheckBoxVisible.DEFAULT, diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java index 15456bd950..8ae829fb56 100644 --- a/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java @@ -11,9 +11,7 @@ import org.junit.Test; import com.vaadin.server.data.provider.bov.Person; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.GridSelectionModel; -import com.vaadin.ui.components.grid.MultiSelectionModelImpl; -import com.vaadin.ui.components.grid.NoSelectionModel; -import com.vaadin.ui.components.grid.SingleSelectionModelImpl; +import com.vaadin.ui.Grid.SelectionMode; public class GridNoSelectionModelTest { @@ -29,8 +27,7 @@ public class GridNoSelectionModelTest { grid = new Grid<>(); grid.setItems(PERSON_A, PERSON_B, PERSON_C); - model = new NoSelectionModel<>(grid); - grid.setSelectionModel(model); + model = grid.setSelectionMode(SelectionMode.NONE); } @Test @@ -50,7 +47,7 @@ public class GridNoSelectionModelTest { @Test public void changingToSingleSelectionModel() { - grid.setSelectionModel(new SingleSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.SINGLE); grid.getSelectionModel().select(PERSON_B); Assert.assertEquals(PERSON_B, @@ -59,7 +56,7 @@ public class GridNoSelectionModelTest { @Test public void changingToMultiSelectionModel() { - grid.setSelectionModel(new MultiSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.MULTI); grid.getSelectionModel().select(PERSON_B); Assert.assertEquals(new LinkedHashSet<>(Arrays.asList(PERSON_B)), diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridSelectionModeTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridSelectionModeTest.java new file mode 100644 index 0000000000..e9515be789 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridSelectionModeTest.java @@ -0,0 +1,49 @@ +package com.vaadin.tests.components.grid; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; +import com.vaadin.ui.components.grid.MultiSelectionModelImpl; +import com.vaadin.ui.components.grid.NoSelectionModel; +import com.vaadin.ui.components.grid.SingleSelectionModelImpl; + +public class GridSelectionModeTest { + + private Grid<String> grid; + + @Before + public void setup() { + grid = new Grid<>(); + grid.setItems("foo", "bar", "baz"); + } + + @Test + public void testSelectionModes() { + Assert.assertEquals(SingleSelectionModelImpl.class, + grid.getSelectionModel().getClass()); + + Assert.assertEquals(MultiSelectionModelImpl.class, + grid.setSelectionMode(SelectionMode.MULTI).getClass()); + Assert.assertEquals(MultiSelectionModelImpl.class, + grid.getSelectionModel().getClass()); + + Assert.assertEquals(NoSelectionModel.class, + grid.setSelectionMode(SelectionMode.NONE).getClass()); + Assert.assertEquals(NoSelectionModel.class, + grid.getSelectionModel().getClass()); + + Assert.assertEquals(SingleSelectionModelImpl.class, + grid.setSelectionMode(SelectionMode.SINGLE).getClass()); + Assert.assertEquals(SingleSelectionModelImpl.class, + grid.getSelectionModel().getClass()); + } + + @Test(expected = NullPointerException.class) + public void testNullSelectionMode() { + grid.setSelectionMode(null); + } + +} diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java index 17b409c1a6..06ceb8bfc3 100644 --- a/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java @@ -24,7 +24,7 @@ import com.vaadin.server.data.provider.bov.Person; import com.vaadin.shared.Registration; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.GridSelectionModel; -import com.vaadin.ui.components.grid.MultiSelectionModelImpl; +import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.components.grid.SingleSelectionModelImpl; import elemental.json.JsonObject; @@ -39,10 +39,6 @@ public class GridSingleSelectionModelTest { extends SingleSelectionModelImpl<String> { public final Map<String, Boolean> generatedData = new LinkedHashMap<>(); - public CustomSingleSelectionModel(Grid<String> grid) { - super(grid); - } - @Override public void generateData(String item, JsonObject jsonObject) { super.generateData(item, jsonObject); @@ -70,7 +66,7 @@ public class GridSingleSelectionModelTest { @Test(expected = IllegalStateException.class) public void selectionModelChanged_usingPreviousSelectionModel_throws() { - grid.setSelectionModel(new MultiSelectionModelImpl<>(grid)); + grid.setSelectionMode(SelectionMode.MULTI); selectionModel.select(PERSON_A); } @@ -89,19 +85,20 @@ public class GridSingleSelectionModelTest { customGrid.getSelectionModel().getFirstSelectedItem().get()); assertEquals(Arrays.asList("Foo"), selectionChanges); - customGrid - .setSelectionModel(new CustomSingleSelectionModel(customGrid)); + customGrid.setSelectionMode(SelectionMode.MULTI); assertEquals(Arrays.asList("Foo", null), selectionChanges); } @Test public void serverSideSelection_GridChangingSelectionModel_sendsUpdatedRowsToClient() { - Grid<String> customGrid = new Grid<>(); - customGrid.setItems("Foo", "Bar", "Baz"); - CustomSingleSelectionModel customModel = new CustomSingleSelectionModel( - customGrid); - customGrid.setSelectionModel(customModel); + CustomSingleSelectionModel customModel = new CustomSingleSelectionModel(); + Grid<String> customGrid = new Grid<String>() { + { + setSelectionModel(customModel); + } + }; + customGrid.setItems("Foo", "Bar", "Baz"); customGrid.getDataCommunicator().beforeClientResponse(true); @@ -126,7 +123,7 @@ public class GridSingleSelectionModelTest { // switch to another selection model to cause event customModel.generatedData.clear(); - customGrid.setSelectionModel(new SingleSelectionModelImpl<>(customGrid)); + customGrid.setSelectionMode(SelectionMode.MULTI); customGrid.getDataCommunicator().beforeClientResponse(false); // since the selection model has been removed, it is no longer a data @@ -274,8 +271,7 @@ public class GridSingleSelectionModelTest { Grid<String> grid = new Grid<>(); grid.setItems("foo", "bar"); String value = "foo"; - SingleSelectionModelImpl<String> select = new SingleSelectionModelImpl<String>( - grid) { + SingleSelectionModelImpl<String> select = new SingleSelectionModelImpl<String>() { @Override public Registration addSelectionListener( SingleSelectionListener<String> listener) { diff --git a/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java b/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java index 87986c1b5c..7a11cf4df6 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java @@ -30,6 +30,7 @@ public class StateGetDoesNotMarkDirtyTest { excludedMethods.add(Label.class.getName() + "getDataProviderValue"); excludedMethods.add("getConnectorId"); excludedMethods.add("getContent"); + excludedMethods.add("com.vaadin.ui.Grid:getSelectAllCheckBoxVisible"); } @Test @@ -62,8 +63,8 @@ public class StateGetDoesNotMarkDirtyTest { // we still wouldnt know what to put into continue; } - if (excludedMethods - .contains(clazz.getName() + method.getName())) { + if (excludedMethods.contains( + clazz.getName() + ":" + method.getName())) { // blacklisted method for specific classes continue; } |