use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.
the class SearchFieldDsTest method testUnsubscribeSubscribeComponentListener.
@Test
public void testUnsubscribeSubscribeComponentListener() {
SearchField searchField = uiComponents.create(SearchField.class);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
searchField.setOptionsDatasource(groupsDs);
Datasource<User> userDs = createTestDatasource(User.class);
Group group = groupsDs.getItems().iterator().next();
User user = userDs.getItem();
user.setGroup(group);
searchField.setDatasource(userDs, "group");
// unbind
searchField.setDatasource(null, null);
// setup
boolean[] valueWasChanged = { false };
Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
// datasource before listener
searchField.setDatasource(userDs, "group");
Subscription subscription = searchField.addValueChangeListener(listener);
user.setGroup(metadata.create(Group.class));
assertTrue(valueWasChanged[0]);
// reset state
valueWasChanged[0] = false;
searchField.setDatasource(null, null);
subscription.remove();
searchField.setValue(null);
// listener before datasource
searchField.addValueChangeListener(listener);
searchField.setDatasource(userDs, "group");
assertTrue(valueWasChanged[0]);
}
Aggregations