Search in sources :

Example 11 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class OptionsListDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    OptionsList optionsList = uiComponents.create(OptionsList.NAME);
    Datasource<Role> roleDs = createTestDatasource(Role.class);
    roleDs.getItem().setType(RoleType.DENYING);
    optionsList.setDatasource(roleDs, "type");
    optionsList.setDatasource(null, null);
    // datasource before listener
    optionsList.setDatasource(roleDs, "type");
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    Subscription subscription = optionsList.addValueChangeListener(listener);
    roleDs.getItem().setType(RoleType.READONLY);
    assertTrue(valueWasChanged[0]);
    // reset state
    valueWasChanged[0] = false;
    subscription.remove();
    optionsList.setDatasource(null, null);
    optionsList.setValue(null);
    // listener before datasource
    optionsList.addValueChangeListener(listener);
    optionsList.setDatasource(roleDs, "type");
    assertTrue(valueWasChanged[0]);
}
Also used : Role(com.haulmont.cuba.core.model.common.Role) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) Autowired(org.springframework.beans.factory.annotation.Autowired) Disabled(org.junit.jupiter.api.Disabled) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) HasValue(io.jmix.ui.component.HasValue) RoleType(com.haulmont.cuba.core.model.common.RoleType) OptionsList(com.haulmont.cuba.gui.components.OptionsList) Role(com.haulmont.cuba.core.model.common.Role) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) UiComponents(com.haulmont.cuba.gui.UiComponents) Assert.assertEquals(org.junit.Assert.assertEquals) Subscription(io.jmix.core.common.event.Subscription) OptionsList(com.haulmont.cuba.gui.components.OptionsList) Test(org.junit.jupiter.api.Test)

Example 12 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class LookupFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    LookupField component = create(LookupField.class);
    final AtomicInteger counter = new AtomicInteger(0);
    // noinspection unchecked
    Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getFetchPlan(User.class, FetchPlan.LOCAL)).buildDatasource();
    testDs.setItem(new User());
    ((DatasourceImpl) testDs).valid();
    assertNull(component.getValue());
    final Group g = new Group();
    testDs.getItem().setGroup(g);
    // noinspection unchecked
    CollectionDatasource<Group, UUID> groupsDs = new DsBuilder().setId("testDs").setJavaClass(Group.class).setView(viewRepository.getFetchPlan(Group.class, FetchPlan.LOCAL)).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setAllowCommit(false).buildCollectionDatasource();
    groupsDs.includeItem(g);
    Group g1 = new Group();
    groupsDs.includeItem(g1);
    Group g2 = new Group();
    groupsDs.includeItem(g2);
    component.setOptionsDatasource(groupsDs);
    Consumer<HasValue.ValueChangeEvent> listener1 = e -> {
        assertNull(e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    Subscription subscription = component.addValueChangeListener(listener1);
    component.setValue(g2);
    subscription.remove();
    assertEquals(1, counter.get());
    Consumer<HasValue.ValueChangeEvent> listener2 = e -> {
        assertEquals(g2, e.getPrevValue());
        assertEquals(g, e.getValue());
        counter.addAndGet(1);
    };
    subscription = component.addValueChangeListener(listener2);
    component.setDatasource(testDs, "group");
    assertEquals(g, component.getValue());
    assertEquals(2, counter.get());
    subscription.remove();
    component.setValue(g1);
    assertEquals(g1, testDs.getItem().getGroup());
    assertEquals(2, counter.get());
    Consumer<HasValue.ValueChangeEvent> listener3 = e -> {
        assertEquals(g1, e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener3);
    testDs.getItem().setGroup(g2);
    assertEquals(g2, component.getValue());
    assertEquals(3, counter.get());
}
Also used : Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) FetchPlan(io.jmix.core.FetchPlan) 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) Component(io.jmix.ui.component.Component) HasValue(io.jmix.ui.component.HasValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Assertions(org.junit.jupiter.api.Assertions) LookupField(com.haulmont.cuba.gui.components.LookupField) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) User(com.haulmont.cuba.core.model.common.User) Group(com.haulmont.cuba.core.model.common.Group) User(com.haulmont.cuba.core.model.common.User) LookupField(com.haulmont.cuba.gui.components.LookupField) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) Subscription(io.jmix.core.common.event.Subscription) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.jupiter.api.Test)

Example 13 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class OptionsGroupTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    OptionsGroup component = create(OptionsGroup.class);
    final AtomicInteger counter = new AtomicInteger(0);
    assertNull(component.getValue());
    Consumer<HasValue.ValueChangeEvent> listener1 = e -> {
        assertNull(e.getPrevValue());
        assertEquals("Two", e.getValue());
        counter.addAndGet(1);
    };
    Subscription subscription = component.addValueChangeListener(listener1);
    component.setOptionsList(new ArrayList<>(Arrays.asList("One", "Two", "Three")));
    component.setValue("Two");
    subscription.remove();
    assertEquals(1, counter.get());
    Consumer<HasValue.ValueChangeEvent> listener2 = e -> {
        assertEquals("Two", e.getPrevValue());
        assertEquals("One", e.getValue());
        counter.addAndGet(1);
    };
    subscription = component.addValueChangeListener(listener2);
    component.setValue("One");
    assertEquals("One", component.getValue());
    assertEquals(2, counter.get());
    subscription.remove();
    Consumer<HasValue.ValueChangeEvent> listener3 = e -> {
        assertEquals("One", e.getPrevValue());
        assertEquals("Three", e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener3);
    component.setValue("Three");
    assertEquals(3, counter.get());
}
Also used : OptionsGroup(com.haulmont.cuba.gui.components.OptionsGroup) Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) FetchPlan(io.jmix.core.FetchPlan) 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) Component(io.jmix.ui.component.Component) HasValue(io.jmix.ui.component.HasValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Assertions(org.junit.jupiter.api.Assertions) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) User(com.haulmont.cuba.core.model.common.User) OptionsGroup(com.haulmont.cuba.gui.components.OptionsGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(io.jmix.core.common.event.Subscription) Test(org.junit.jupiter.api.Test)

Example 14 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class PickerFieldDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    PickerField pickerField = uiComponents.create(PickerField.NAME);
    Datasource<User> userDs = createTestDatasource(User.class);
    User user = userDs.getItem();
    Group group = metadata.create(Group.class);
    group.setName("Test group");
    user.setGroup(group);
    pickerField.setDatasource(userDs, "group");
    // unbind
    pickerField.setDatasource(null, null);
    // setup
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    // datasource before listener
    pickerField.setDatasource(userDs, "group");
    Subscription subscription = pickerField.addValueChangeListener(listener);
    user.setGroup(null);
    assertTrue(valueWasChanged[0]);
    // reset state
    pickerField.setDatasource(null, null);
    subscription.remove();
    valueWasChanged[0] = false;
    user.setGroup(metadata.create(Group.class));
    // listener before datasource
    pickerField.addValueChangeListener(listener);
    pickerField.setDatasource(userDs, "group");
    assertTrue(valueWasChanged[0]);
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) Autowired(org.springframework.beans.factory.annotation.Autowired) Disabled(org.junit.jupiter.api.Disabled) Group(com.haulmont.cuba.core.model.common.Group) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) HasValue(io.jmix.ui.component.HasValue) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PickerField(com.haulmont.cuba.gui.components.PickerField) UiComponents(com.haulmont.cuba.gui.UiComponents) User(com.haulmont.cuba.core.model.common.User) Group(com.haulmont.cuba.core.model.common.Group) User(com.haulmont.cuba.core.model.common.User) PickerField(com.haulmont.cuba.gui.components.PickerField) Subscription(io.jmix.core.common.event.Subscription) Test(org.junit.jupiter.api.Test)

Example 15 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class TextFieldDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    TextField textField = uiComponents.create(TextField.NAME);
    Datasource<User> userDs = createTestDatasource(User.class);
    User user = userDs.getItem();
    user.setName("testName");
    textField.setDatasource(userDs, "name");
    // unbind
    textField.setDatasource(null, null);
    // setup
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    // datasource before listener
    textField.setDatasource(userDs, "name");
    Subscription subscription = textField.addValueChangeListener(listener);
    user.setName("anotherName");
    assertTrue(valueWasChanged[0]);
    // reset state
    subscription.remove();
    textField.setDatasource(null, null);
    valueWasChanged[0] = false;
    textField.setValue("testName");
    // listener before datasource
    textField.addValueChangeListener(listener);
    textField.setDatasource(userDs, "name");
    assertTrue(valueWasChanged[0]);
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) HasValue(io.jmix.ui.component.HasValue) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TextField(com.haulmont.cuba.gui.components.TextField) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.core.model.common.User) User(com.haulmont.cuba.core.model.common.User) TextField(com.haulmont.cuba.gui.components.TextField) 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