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