@Test public void gridChangingSelectionModel_firesSelectionChangeEvent() { Grid<String> customGrid = new Grid<>(); customGrid.setItems("Foo", "Bar", "Baz"); List<String> selectionChanges = new ArrayList<>(); ((SingleSelectionModelImpl<String>) customGrid.getSelectionModel()) .addSingleSelectionListener(e -> selectionChanges.add(e.getValue())); customGrid.getSelectionModel().select("Foo"); assertEquals("Foo", customGrid.getSelectionModel().getFirstSelectedItem().get()); assertEquals(Arrays.asList("Foo"), selectionChanges); customGrid.setSelectionMode(SelectionMode.MULTI); assertEquals(Arrays.asList("Foo", null), selectionChanges); }
@Before public void setUp() { grid = new Grid<>(); grid.setItems(PERSON_A, PERSON_B, PERSON_C); selectionModel = (SingleSelectionModelImpl<Person>) grid.getSelectionModel(); selectionChanges = new ArrayList<>(); selectionModel.addSingleSelectionListener(e -> selectionChanges.add(e.getValue())); }
@Test public void serverSideSelection_GridChangingSelectionModel_sendsUpdatedRowsToClient() { CustomSingleSelectionModel customModel = new CustomSingleSelectionModel(); Grid<String> customGrid = new Grid<String>() { { setSelectionModel(customModel); } }; customGrid.setItems("Foo", "Bar", "Baz"); customGrid.getDataCommunicator().beforeClientResponse(true); Assert.assertFalse( "Item should have been updated as selected", customModel.generatedData.get("Foo")); Assert.assertFalse( "Item should have been updated as NOT selected", customModel.generatedData.get("Bar")); Assert.assertFalse( "Item should have been updated as NOT selected", customModel.generatedData.get("Baz")); customModel.generatedData.clear(); customGrid.getSelectionModel().select("Foo"); customGrid.getDataCommunicator().beforeClientResponse(false); Assert.assertTrue( "Item should have been updated as selected", customModel.generatedData.get("Foo")); Assert.assertFalse( "Item should have NOT been updated", customModel.generatedData.containsKey("Bar")); Assert.assertFalse( "Item should have NOT been updated", customModel.generatedData.containsKey("Baz")); // switch to another selection model to cause event customModel.generatedData.clear(); customGrid.setSelectionMode(SelectionMode.MULTI); customGrid.getDataCommunicator().beforeClientResponse(false); // since the selection model has been removed, it is no longer a data // generator for the data communicator, would need to verify somehow // that row is not marked as selected anymore ? (done in UI tests) Assert.assertTrue(customModel.generatedData.isEmpty()); // at least // removed // selection // model is not // triggered }
@Test public void testGridWithSingleSelection() { Grid<String> gridWithStrings = new Grid<>(); gridWithStrings.setItems("Foo", "Bar", "Baz"); GridSelectionModel<String> model = gridWithStrings.getSelectionModel(); Assert.assertFalse(model.isSelected("Foo")); model.select("Foo"); Assert.assertTrue(model.isSelected("Foo")); Assert.assertEquals(Optional.of("Foo"), model.getFirstSelectedItem()); model.select("Bar"); Assert.assertFalse(model.isSelected("Foo")); Assert.assertTrue(model.isSelected("Bar")); model.deselect("Bar"); Assert.assertFalse(model.isSelected("Bar")); Assert.assertFalse(model.getFirstSelectedItem().isPresent()); }