123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- package com.vaadin.tests.components.grid;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertSame;
- import static org.junit.Assert.assertTrue;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.LinkedHashMap;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.Set;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.stream.IntStream;
-
- import org.easymock.Capture;
- import org.junit.Before;
- import org.junit.Test;
- import org.mockito.Mockito;
-
- import com.vaadin.data.provider.DataProvider;
- import com.vaadin.data.provider.bov.Person;
- import com.vaadin.event.selection.MultiSelectionEvent;
- import com.vaadin.event.selection.MultiSelectionListener;
- import com.vaadin.shared.Registration;
- import com.vaadin.tests.util.MockUI;
- import com.vaadin.ui.Grid;
- import com.vaadin.ui.Grid.SelectionMode;
- import com.vaadin.ui.UI;
- import com.vaadin.ui.components.grid.GridSelectionModel;
- import com.vaadin.ui.components.grid.MultiSelectionModel;
- import com.vaadin.ui.components.grid.MultiSelectionModel.SelectAllCheckBoxVisibility;
- import com.vaadin.ui.components.grid.MultiSelectionModelImpl;
-
- import elemental.json.JsonObject;
-
- public class GridMultiSelectionModelTest {
-
- public static final Person PERSON_C = new Person("c", 3);
- public static final Person PERSON_B = new Person("b", 2);
- public static final Person PERSON_A = new Person("a", 1);
-
- private Grid<Person> grid;
- private MultiSelectionModelImpl<Person> selectionModel;
- private Capture<List<Person>> currentSelectionCapture;
- private Capture<List<Person>> oldSelectionCapture;
- private AtomicInteger events;
-
- public static class CustomMultiSelectionModel
- extends MultiSelectionModelImpl<String> {
- public final Map<String, Boolean> generatedData = new LinkedHashMap<>();
-
- @Override
- public void generateData(String item, JsonObject jsonObject) {
- super.generateData(item, jsonObject);
- // capture updated row
- generatedData.put(item, isSelected(item));
- }
-
- }
-
- public static class CustomSelectionModelGrid extends Grid<String> {
- public CustomSelectionModelGrid() {
- this(new CustomMultiSelectionModel());
- }
-
- public CustomSelectionModelGrid(
- GridSelectionModel<String> selectionModel) {
- super();
- setSelectionModel(selectionModel);
- }
- }
-
- private static class TestMultiSelectionModel
- extends MultiSelectionModelImpl<Object> {
-
- public TestMultiSelectionModel() {
- getState(false).selectionAllowed = false;
- }
-
- @Override
- protected void updateSelection(Set<Object> addedItems,
- Set<Object> removedItems, boolean userOriginated) {
- super.updateSelection(addedItems, removedItems, userOriginated);
- }
-
- }
-
- @Before
- public void setUp() {
- grid = new Grid<>();
- selectionModel = (MultiSelectionModelImpl<Person>) grid
- .setSelectionMode(SelectionMode.MULTI);
-
- grid.setItems(PERSON_A, PERSON_B, PERSON_C);
-
- currentSelectionCapture = new Capture<>();
- oldSelectionCapture = new Capture<>();
- events = new AtomicInteger();
-
- selectionModel.addMultiSelectionListener(event -> {
- currentSelectionCapture
- .setValue(new ArrayList<>(event.getNewSelection()));
- oldSelectionCapture
- .setValue(new ArrayList<>(event.getOldSelection()));
- events.incrementAndGet();
- });
- }
-
- @Test(expected = IllegalStateException.class)
- public void throwExcpetionWhenSelectionIsDisallowed() {
- TestMultiSelectionModel model = new TestMultiSelectionModel();
- model.updateSelection(Collections.emptySet(), Collections.emptySet(),
- true);
- }
-
- @Test(expected = IllegalStateException.class)
- public void selectionModelChanged_usingPreviousSelectionModel_throws() {
- grid.setSelectionMode(SelectionMode.SINGLE);
-
- selectionModel.select(PERSON_A);
- }
-
- @Test
- public void changingSelectionModel_firesSelectionEvent() {
- Grid<String> customGrid = new Grid<>();
- customGrid.setSelectionMode(SelectionMode.MULTI);
- customGrid.setItems("Foo", "Bar", "Baz");
-
- List<String> selectionChanges = new ArrayList<>();
- Capture<List<String>> oldSelectionCapture = new Capture<>();
- ((MultiSelectionModelImpl<String>) customGrid.getSelectionModel())
- .addMultiSelectionListener(event -> {
- selectionChanges.addAll(event.getValue());
- oldSelectionCapture
- .setValue(new ArrayList<>(event.getOldSelection()));
- });
-
- customGrid.getSelectionModel().select("Foo");
- assertEquals(Arrays.asList("Foo"), selectionChanges);
- selectionChanges.clear();
-
- customGrid.getSelectionModel().select("Bar");
- assertEquals("Foo",
- customGrid.getSelectionModel().getFirstSelectedItem().get());
- assertEquals(Arrays.asList("Foo", "Bar"), selectionChanges);
- selectionChanges.clear();
-
- customGrid.setSelectionMode(SelectionMode.SINGLE);
- assertFalse(customGrid.getSelectionModel().getFirstSelectedItem()
- .isPresent());
- assertEquals(Arrays.asList(), selectionChanges);
- assertEquals(Arrays.asList("Foo", "Bar"),
- oldSelectionCapture.getValue());
- }
-
- @Test
- public void serverSideSelection_GridChangingSelectionModel_sendsUpdatedRowsToClient() {
- Grid<String> customGrid = new CustomSelectionModelGrid();
- CustomMultiSelectionModel customModel = (CustomMultiSelectionModel) customGrid
- .getSelectionModel();
- customGrid.setItems("Foo", "Bar", "Baz");
-
- customGrid.getDataCommunicator().beforeClientResponse(true);
-
- assertFalse("Item should have been updated as selected",
- customModel.generatedData.get("Foo"));
- assertFalse("Item should have been updated as NOT selected",
- customModel.generatedData.get("Bar"));
- assertFalse("Item should have been updated as NOT selected",
- customModel.generatedData.get("Baz"));
-
- customModel.generatedData.clear();
-
- customGrid.getSelectionModel().select("Foo");
- customGrid.getDataCommunicator().beforeClientResponse(false);
-
- assertTrue("Item should have been updated as selected",
- customModel.generatedData.get("Foo"));
- assertFalse("Item should have NOT been updated",
- customModel.generatedData.containsKey("Bar"));
- assertFalse("Item should have NOT been updated",
- customModel.generatedData.containsKey("Baz"));
-
- customModel.generatedData.clear();
-
- customModel.updateSelection(asSet("Bar"), asSet("Foo"));
- customGrid.getDataCommunicator().beforeClientResponse(false);
-
- assertFalse("Item should have been updated as NOT selected",
- customModel.generatedData.get("Foo"));
- assertTrue("Item should have been updated as selected",
- customModel.generatedData.get("Bar"));
- assertFalse("Item should have NOT been updated",
- customModel.generatedData.containsKey("Baz"));
-
- // switch to single to cause event
- customModel.generatedData.clear();
- customGrid.setSelectionMode(SelectionMode.SINGLE);
- customGrid.getDataCommunicator().beforeClientResponse(false);
-
- // changing selection model should trigger row updates, but the old
- // selection model is not triggered as it has been removed
- assertTrue(customModel.generatedData.isEmpty()); // not triggered
- }
-
- @Test
- public void select_gridWithStrings() {
- Grid<String> gridWithStrings = new Grid<>();
- gridWithStrings.setSelectionMode(SelectionMode.MULTI);
- gridWithStrings.setItems("Foo", "Bar", "Baz");
-
- GridSelectionModel<String> model = gridWithStrings.getSelectionModel();
- assertFalse(model.isSelected("Foo"));
-
- model.select("Foo");
- assertTrue(model.isSelected("Foo"));
- assertEquals(Optional.of("Foo"), model.getFirstSelectedItem());
-
- model.select("Bar");
- assertTrue(model.isSelected("Foo"));
- assertTrue(model.isSelected("Bar"));
- assertEquals(Arrays.asList("Foo", "Bar"),
- new ArrayList<>(model.getSelectedItems()));
-
- model.deselect("Bar");
- assertFalse(model.isSelected("Bar"));
- assertTrue(model.getFirstSelectedItem().isPresent());
- assertEquals(Arrays.asList("Foo"),
- new ArrayList<>(model.getSelectedItems()));
- }
-
- @Test
- public void select() {
- selectionModel.select(PERSON_B);
-
- assertEquals(PERSON_B,
- selectionModel.getFirstSelectedItem().orElse(null));
- assertEquals(Optional.of(PERSON_B),
- selectionModel.getFirstSelectedItem());
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(PERSON_B),
- currentSelectionCapture.getValue());
-
- selectionModel.select(PERSON_A);
- assertEquals(PERSON_B,
- selectionModel.getFirstSelectedItem().orElse(null));
-
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(PERSON_B, PERSON_A),
- currentSelectionCapture.getValue());
- assertEquals(2, events.get());
- }
-
- @Test
- public void deselect() {
- selectionModel.select(PERSON_B);
- selectionModel.deselect(PERSON_B);
-
- assertFalse(selectionModel.getFirstSelectedItem().isPresent());
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(2, events.get());
- }
-
- @Test
- public void selectItems() {
- selectionModel.selectItems(PERSON_C, PERSON_B);
-
- assertEquals(PERSON_C,
- selectionModel.getFirstSelectedItem().orElse(null));
- assertEquals(Optional.of(PERSON_C),
- selectionModel.getFirstSelectedItem());
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(PERSON_C, PERSON_B),
- currentSelectionCapture.getValue());
-
- selectionModel.selectItems(PERSON_A, PERSON_C); // partly NOOP
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(PERSON_C, PERSON_B, PERSON_A),
- currentSelectionCapture.getValue());
- assertEquals(2, events.get());
- }
-
- @Test
- public void deselectItems() {
- selectionModel.selectItems(PERSON_C, PERSON_A, PERSON_B);
-
- selectionModel.deselectItems(PERSON_A);
- assertEquals(PERSON_C,
- selectionModel.getFirstSelectedItem().orElse(null));
- assertEquals(Optional.of(PERSON_C),
- selectionModel.getFirstSelectedItem());
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(PERSON_C, PERSON_B),
- currentSelectionCapture.getValue());
-
- selectionModel.deselectItems(PERSON_A, PERSON_B, PERSON_C);
- assertNull(selectionModel.getFirstSelectedItem().orElse(null));
- assertEquals(Optional.empty(), selectionModel.getFirstSelectedItem());
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(3, events.get());
- }
-
- @Test
- public void selectionEvent_newSelection_oldSelection() {
- selectionModel.selectItems(PERSON_C, PERSON_A, PERSON_B);
-
- assertEquals(Arrays.asList(PERSON_C, PERSON_A, PERSON_B),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(), oldSelectionCapture.getValue());
-
- selectionModel.deselect(PERSON_A);
-
- assertEquals(Arrays.asList(PERSON_C, PERSON_B),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_C, PERSON_A, PERSON_B),
- oldSelectionCapture.getValue());
-
- selectionModel.deselectItems(PERSON_A, PERSON_B, PERSON_C);
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_C, PERSON_B),
- oldSelectionCapture.getValue());
-
- selectionModel.selectItems(PERSON_A);
- assertEquals(Arrays.asList(PERSON_A),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(), oldSelectionCapture.getValue());
-
- selectionModel.updateSelection(
- new LinkedHashSet<>(Arrays.asList(PERSON_B, PERSON_C)),
- new LinkedHashSet<>(Arrays.asList(PERSON_A)));
- assertEquals(Arrays.asList(PERSON_B, PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_A), oldSelectionCapture.getValue());
-
- selectionModel.deselectAll();
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_B, PERSON_C),
- oldSelectionCapture.getValue());
-
- selectionModel.select(PERSON_C);
- assertEquals(Arrays.asList(PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(), oldSelectionCapture.getValue());
-
- selectionModel.deselect(PERSON_C);
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_C), oldSelectionCapture.getValue());
- }
-
- @Test
- public void deselectAll() {
- selectionModel.selectItems(PERSON_A, PERSON_C, PERSON_B);
-
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_A, PERSON_C, PERSON_B),
- currentSelectionCapture.getValue());
- assertEquals(1, events.get());
-
- selectionModel.deselectAll();
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_A, PERSON_C, PERSON_B),
- oldSelectionCapture.getValue());
- assertEquals(2, events.get());
-
- selectionModel.select(PERSON_C);
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(), oldSelectionCapture.getValue());
- assertEquals(3, events.get());
-
- selectionModel.deselectAll();
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_C), oldSelectionCapture.getValue());
- assertEquals(4, events.get());
-
- selectionModel.deselectAll();
- assertEquals(4, events.get());
- }
-
- @Test
- public void selectAll() {
- selectionModel.selectAll();
-
- assertTrue(selectionModel.isAllSelected());
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_A, PERSON_B, PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(1, events.get());
-
- selectionModel.deselectItems(PERSON_A, PERSON_C);
-
- assertFalse(selectionModel.isAllSelected());
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_A, PERSON_B, PERSON_C),
- oldSelectionCapture.getValue());
-
- selectionModel.selectAll();
-
- assertTrue(selectionModel.isAllSelected());
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_B, PERSON_A, PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_B), oldSelectionCapture.getValue());
- assertEquals(3, events.get());
- }
-
- @Test
- public void updateSelection() {
- selectionModel.updateSelection(asSet(PERSON_A), Collections.emptySet());
-
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_A),
- currentSelectionCapture.getValue());
- assertEquals(1, events.get());
-
- selectionModel.updateSelection(asSet(PERSON_B), asSet(PERSON_A));
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_B),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_A), oldSelectionCapture.getValue());
- assertEquals(2, events.get());
-
- selectionModel.updateSelection(asSet(PERSON_B), asSet(PERSON_A)); // NOOP
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_B),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_A), oldSelectionCapture.getValue());
- assertEquals(2, events.get());
-
- selectionModel.updateSelection(asSet(PERSON_A, PERSON_C),
- asSet(PERSON_A)); // partly NOOP
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_B, PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_B), oldSelectionCapture.getValue());
- assertEquals(3, events.get());
-
- selectionModel.updateSelection(asSet(PERSON_B, PERSON_A),
- asSet(PERSON_B)); // partly NOOP
-
- assertTrue(selectionModel.isSelected(PERSON_A));
- assertTrue(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(PERSON_B, PERSON_C, PERSON_A),
- currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_B, PERSON_C),
- oldSelectionCapture.getValue());
- assertEquals(4, events.get());
-
- selectionModel.updateSelection(asSet(),
- asSet(PERSON_B, PERSON_A, PERSON_C));
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(Arrays.asList(PERSON_B, PERSON_C, PERSON_A),
- oldSelectionCapture.getValue());
- assertEquals(5, events.get());
- }
-
- private <T> Set<T> asSet(@SuppressWarnings("unchecked") T... people) {
- return new LinkedHashSet<>(Arrays.asList(people));
- }
-
- @Test
- public void selectTwice() {
- selectionModel.select(PERSON_C);
- selectionModel.select(PERSON_C);
-
- assertEquals(PERSON_C,
- selectionModel.getFirstSelectedItem().orElse(null));
-
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertTrue(selectionModel.isSelected(PERSON_C));
-
- assertEquals(Optional.of(PERSON_C),
- selectionModel.getFirstSelectedItem());
-
- assertEquals(Arrays.asList(PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(1, events.get());
- }
-
- @Test
- public void deselectTwice() {
- selectionModel.select(PERSON_C);
- assertEquals(Arrays.asList(PERSON_C),
- currentSelectionCapture.getValue());
- assertEquals(1, events.get());
-
- selectionModel.deselect(PERSON_C);
-
- assertFalse(selectionModel.getFirstSelectedItem().isPresent());
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(2, events.get());
-
- selectionModel.deselect(PERSON_C);
-
- assertFalse(selectionModel.getFirstSelectedItem().isPresent());
- assertFalse(selectionModel.isSelected(PERSON_A));
- assertFalse(selectionModel.isSelected(PERSON_B));
- assertFalse(selectionModel.isSelected(PERSON_C));
- assertEquals(Arrays.asList(), currentSelectionCapture.getValue());
- assertEquals(2, events.get());
- }
-
- @SuppressWarnings({ "serial" })
- @Test
- public void addValueChangeListener() {
- String value = "foo";
-
- AtomicReference<MultiSelectionListener<String>> selectionListener = new AtomicReference<>();
- Registration registration = Mockito.mock(Registration.class);
- MultiSelectionModelImpl<String> model = new MultiSelectionModelImpl<String>() {
- @Override
- public Registration addMultiSelectionListener(
- MultiSelectionListener<String> listener) {
- selectionListener.set(listener);
- return registration;
- }
-
- @Override
- public Set<String> getSelectedItems() {
- return new LinkedHashSet<>(Arrays.asList(value));
- }
- };
-
- Grid<String> grid = new CustomSelectionModelGrid(model);
-
- grid.setItems("foo", "bar");
-
- AtomicReference<MultiSelectionEvent<String>> event = new AtomicReference<>();
- Registration actualRegistration = model
- .addMultiSelectionListener(evt -> {
- assertNull(event.get());
- event.set(evt);
- });
- assertSame(registration, actualRegistration);
-
- selectionListener.get().selectionChange(new MultiSelectionEvent<>(grid,
- model.asMultiSelect(), Collections.emptySet(), true));
-
- assertEquals(grid, event.get().getComponent());
- assertEquals(new LinkedHashSet<>(Arrays.asList(value)),
- event.get().getValue());
- assertTrue(event.get().isUserOriginated());
- }
-
- @Test
- public void selectAllCheckboxVisible__inMemoryDataProvider() {
- UI ui = new MockUI();
-
- Grid<String> grid = new Grid<>();
- MultiSelectionModel<String> model = (MultiSelectionModel<String>) grid
- .setSelectionMode(SelectionMode.MULTI);
-
- ui.setContent(grid);
- // no items yet, default data provider is empty not in memory one
- assertFalse(model.isSelectAllCheckBoxVisible());
- assertEquals(SelectAllCheckBoxVisibility.DEFAULT,
- model.getSelectAllCheckBoxVisibility());
-
- grid.setItems("Foo", "Bar", "Baz");
-
- // in-memory container keeps default
- assertTrue(model.isSelectAllCheckBoxVisible());
- assertEquals(SelectAllCheckBoxVisibility.DEFAULT,
- model.getSelectAllCheckBoxVisibility());
-
- // change to explicit NO
- model.setSelectAllCheckBoxVisibility(
- SelectAllCheckBoxVisibility.HIDDEN);
-
- assertEquals(SelectAllCheckBoxVisibility.HIDDEN,
- model.getSelectAllCheckBoxVisibility());
- assertFalse(model.isSelectAllCheckBoxVisible());
-
- // change to explicit YES
- model.setSelectAllCheckBoxVisibility(
- SelectAllCheckBoxVisibility.VISIBLE);
-
- assertEquals(SelectAllCheckBoxVisibility.VISIBLE,
- model.getSelectAllCheckBoxVisibility());
- assertTrue(model.isSelectAllCheckBoxVisible());
- }
-
- @Test
- public void selectAllCheckboxVisible__lazyDataProvider() {
- Grid<String> grid = new Grid<>();
- UI ui = new MockUI();
- ui.setContent(grid);
-
- MultiSelectionModel<String> model = (MultiSelectionModel<String>) grid
- .setSelectionMode(SelectionMode.MULTI);
-
- // no items yet, default data provider is empty not in memory one
- assertFalse(model.isSelectAllCheckBoxVisible());
- assertEquals(SelectAllCheckBoxVisibility.DEFAULT,
- model.getSelectAllCheckBoxVisibility());
-
- grid.setDataProvider(
- DataProvider
- .fromCallbacks(
- query -> IntStream
- .range(query.getOffset(),
- Math.max(query.getOffset()
- + query.getLimit() + 1,
- 1000))
- .mapToObj(i -> "Item " + i),
- query -> 1000));
-
- // not in-memory -> checkbox is hidden
- assertFalse(model.isSelectAllCheckBoxVisible());
- assertEquals(SelectAllCheckBoxVisibility.DEFAULT,
- model.getSelectAllCheckBoxVisibility());
-
- // change to explicit YES
- model.setSelectAllCheckBoxVisibility(
- SelectAllCheckBoxVisibility.VISIBLE);
-
- assertEquals(SelectAllCheckBoxVisibility.VISIBLE,
- model.getSelectAllCheckBoxVisibility());
- assertTrue(model.isSelectAllCheckBoxVisible());
-
- // change to explicit NO
- model.setSelectAllCheckBoxVisibility(
- SelectAllCheckBoxVisibility.HIDDEN);
-
- assertEquals(SelectAllCheckBoxVisibility.HIDDEN,
- model.getSelectAllCheckBoxVisibility());
- assertFalse(model.isSelectAllCheckBoxVisible());
-
- // change back to depends on data provider
- model.setSelectAllCheckBoxVisibility(
- SelectAllCheckBoxVisibility.DEFAULT);
-
- assertFalse(model.isSelectAllCheckBoxVisible());
- assertEquals(SelectAllCheckBoxVisibility.DEFAULT,
- model.getSelectAllCheckBoxVisibility());
-
- }
- }
|