Search in sources :

Example 21 with Subscription

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

the class DateFieldDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    DateField dateField = uiComponents.create(DateField.NAME);
    Datasource<User> userDs = createTestDatasource(User.class);
    User user = userDs.getItem();
    Date updateTs = new Date();
    user.setUpdateTs(updateTs);
    dateField.setDatasource(userDs, "updateTs");
    // unbind
    dateField.setDatasource(null, null);
    // datasource before listener
    dateField.setDatasource(userDs, "updateTs");
    dateField.setResolution(DateField.Resolution.SEC);
    assertEquals(updateTs, dateField.getValue());
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    Subscription subscription = dateField.addValueChangeListener(listener);
    Date updateTs2 = new Date();
    user.setUpdateTs(updateTs2);
    assertTrue(valueWasChanged[0]);
    assertEquals(updateTs2, dateField.getValue());
    // reset state
    subscription.remove();
    dateField.setDatasource(null, null);
    valueWasChanged[0] = false;
    dateField.setValue(updateTs);
    // listener before datasource
    dateField.addValueChangeListener(listener);
    dateField.setDatasource(userDs, "updateTs");
    dateField.setResolution(DateField.Resolution.SEC);
    assertTrue(valueWasChanged[0]);
// assertEquals(updateTs2, dateField.getValue()); todo web tests
}
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) DateField(com.haulmont.cuba.gui.components.DateField) Subscription(io.jmix.core.common.event.Subscription) Date(java.util.Date) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.core.model.common.User) User(com.haulmont.cuba.core.model.common.User) DateField(com.haulmont.cuba.gui.components.DateField) Subscription(io.jmix.core.common.event.Subscription) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 22 with Subscription

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

the class LabelDsTest method testUnsubscribeSubscribeComponentListener.

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

Example 23 with Subscription

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

the class LookupPickerFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    LookupPickerField component = create(LookupPickerField.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) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) 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) 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) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) Group(com.haulmont.cuba.core.model.common.Group) User(com.haulmont.cuba.core.model.common.User) 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 24 with Subscription

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

the class OptionsGroupDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    OptionsGroup optionsGroup = uiComponents.create(OptionsGroup.NAME);
    // listener before datasource
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    Subscription subscription = optionsGroup.addValueChangeListener(listener);
    Datasource<Role> roleDs = createTestDatasource(Role.class);
    roleDs.getItem().setType(RoleType.READONLY);
    optionsGroup.setDatasource(roleDs, "type");
    assertTrue(valueWasChanged[0]);
    // reset state
    valueWasChanged[0] = false;
    subscription.remove();
    optionsGroup.setDatasource(null, null);
    // datasource before listener
    optionsGroup.setDatasource(roleDs, "type");
    optionsGroup.addValueChangeListener(listener);
    roleDs.getItem().setType(RoleType.DENYING);
    assertTrue(valueWasChanged[0]);
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) OptionsGroup(com.haulmont.cuba.gui.components.OptionsGroup) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) 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) Role(com.haulmont.cuba.core.model.common.Role) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) Assert.assertEquals(org.junit.Assert.assertEquals) Role(com.haulmont.cuba.core.model.common.Role) OptionsGroup(com.haulmont.cuba.gui.components.OptionsGroup) Subscription(io.jmix.core.common.event.Subscription) Test(org.junit.jupiter.api.Test)

Example 25 with Subscription

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

the class PickerFieldDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    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);
    // datasource before listener
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    Subscription subscription = pickerField.addValueChangeListener(listener);
    pickerField.setDatasource(userDs, "group");
    assertTrue(valueWasChanged[0]);
    // reset state
    pickerField.setDatasource(null, null);
    subscription.remove();
    valueWasChanged[0] = false;
    pickerField.setValue(null);
    // 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)

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