Search in sources :

Example 26 with Subscription

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]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) Autowired(org.springframework.beans.factory.annotation.Autowired) SearchField(com.haulmont.cuba.gui.components.SearchField) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) Group(com.haulmont.cuba.core.model.common.Group) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) HasValue(io.jmix.ui.component.HasValue) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) UiComponents(com.haulmont.cuba.gui.UiComponents) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) User(com.haulmont.cuba.core.model.common.User) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.core.model.common.Group) SearchField(com.haulmont.cuba.gui.components.SearchField) User(com.haulmont.cuba.core.model.common.User) UUID(java.util.UUID) Subscription(io.jmix.core.common.event.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

Subscription (io.jmix.core.common.event.Subscription)26 Datasource (com.haulmont.cuba.gui.data.Datasource)24 Consumer (java.util.function.Consumer)24 Disabled (org.junit.jupiter.api.Disabled)24 Test (org.junit.jupiter.api.Test)24 HasValue (io.jmix.ui.component.HasValue)22 User (com.haulmont.cuba.core.model.common.User)20 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)18 Assertions (org.junit.jupiter.api.Assertions)10 Group (com.haulmont.cuba.core.model.common.Group)9 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)7 UUID (java.util.UUID)7 ArrayList (java.util.ArrayList)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 UiComponents (com.haulmont.cuba.gui.UiComponents)5 Date (java.util.Date)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Role (com.haulmont.cuba.core.model.common.Role)4