@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())); }
@SuppressWarnings({"serial"}) @Test public void addValueChangeListener() { AtomicReference<SingleSelectionListener<String>> selectionListener = new AtomicReference<>(); Registration registration = Mockito.mock(Registration.class); Grid<String> grid = new Grid<>(); grid.setItems("foo", "bar"); String value = "foo"; SingleSelectionModelImpl<String> select = new SingleSelectionModelImpl<String>() { @Override public Registration addSingleSelectionListener(SingleSelectionListener<String> listener) { selectionListener.set(listener); return registration; } @Override public Optional<String> getSelectedItem() { return Optional.of(value); } }; AtomicReference<ValueChangeEvent<?>> event = new AtomicReference<>(); Registration actualRegistration = select.addSingleSelectionListener( evt -> { Assert.assertNull(event.get()); event.set(evt); }); Assert.assertSame(registration, actualRegistration); selectionListener .get() .selectionChange(new SingleSelectionEvent<>(grid, select.asSingleSelect(), true)); Assert.assertEquals(grid, event.get().getComponent()); Assert.assertEquals(value, event.get().getValue()); Assert.assertTrue(event.get().isUserOriginated()); }