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
}
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());
}
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());
}
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]);
}
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]);
}
Aggregations