use of com.vaadin.flow.dom.DomEventListener in project flow by vaadin.
the class ElementListenersTest method addAndRemoveEventData.
@Test
public void addAndRemoveEventData() {
ns.add("eventType", noOp).addEventData("data1").addEventData("data2");
Set<String> expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
Assert.assertFalse(expressions.contains("data3"));
Registration handle = ns.add("eventType", new DomEventListener() {
/*
* Can't use the existing noOp instance since there would then not
* be any listeners left after calling remove()
*/
@Override
public void handleEvent(DomEvent event) {
// no op
}
}).addEventData("data3");
expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
Assert.assertTrue(expressions.contains("data3"));
handle.remove();
expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
// due to fix to #5090, data3 won't be present after removal
Assert.assertFalse(expressions.contains("data3"));
}
use of com.vaadin.flow.dom.DomEventListener in project flow by vaadin.
the class ElementListenersTest method testAddAndRemoveEventData.
@Test
public void testAddAndRemoveEventData() {
ns.add("eventType", noOp, new String[] { "data1", "data2" });
Set<String> expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
Assert.assertFalse(expressions.contains("data3"));
Registration handle = ns.add("eventType", new DomEventListener() {
/*
* Can't use the existing noOp instance since there would
* then not be any listeners left after calling remove()
*/
@Override
public void handleEvent(DomEvent event) {
// no op
}
}, new String[] { "data3" });
expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
Assert.assertTrue(expressions.contains("data3"));
handle.remove();
expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
// data3 might still be there, but we don't care
}
use of com.vaadin.flow.dom.DomEventListener in project flow by vaadin.
the class DebounceSynchronizePropertyView method createModeToggle.
private Component createModeToggle(String caption, String id, Consumer<DomListenerRegistration> configurator) {
Element checkbox = new Element("input");
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("id", id);
checkbox.addEventListener("change", new DomEventListener() {
private DomListenerRegistration registration = null;
@Override
public void handleEvent(DomEvent event) {
if (event.getEventData().getBoolean("element.checked")) {
assert registration == null;
registration = inputElement.addPropertyChangeListener("value", "input", propertyChange -> addChangeMessage(propertyChange.getValue()));
configurator.accept(registration);
} else {
registration.remove();
registration = null;
}
}
}).addEventData("element.checked");
Label label = new Label(caption);
label.getElement().insertChild(0, checkbox);
label.getElement().getStyle().set("display", "block");
return label;
}
use of com.vaadin.flow.dom.DomEventListener in project flow by vaadin.
the class ElementListenersTest method addingRemovingAndAddingListenerOfTheSameType.
@Test
public void addingRemovingAndAddingListenerOfTheSameType() {
DomEventListener del1 = event -> {
};
DomEventListener del2 = event -> {
};
Registration handle = ns.add("eventType", del1).addEventData("data1");
Set<String> expressions = getExpressions("eventType");
Assert.assertTrue(expressions.contains("data1"));
handle.remove();
expressions = getExpressions("eventType");
Assert.assertFalse(expressions.contains("data1"));
// re-add a listener for "eventType", using different eventData
handle = ns.add("eventType", del2).addEventData("data2");
expressions = getExpressions("eventType");
Assert.assertFalse(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
handle.remove();
expressions = getExpressions("eventType");
Assert.assertFalse(expressions.contains("data1"));
Assert.assertFalse(expressions.contains("data2"));
}
use of com.vaadin.flow.dom.DomEventListener in project flow by vaadin.
the class ElementListenersTest method settingsAreOnlyUpdated_should_ListenersSharingTheTypeOfRemovedListenerExist.
@Test
public void settingsAreOnlyUpdated_should_ListenersSharingTheTypeOfRemovedListenerExist() {
ns = spy(createFeature());
DomEventListener del1 = event -> {
};
DomEventListener del2 = event -> {
};
DomEventListener del3 = event -> {
};
Registration handle1 = ns.add("eventType", del1).addEventData("data1");
Registration handle2 = ns.add("eventType", del2).addEventData("data2");
Registration handle3 = ns.add("eventTypeOther", del3).addEventData("data3");
Mockito.reset(ns);
Set<String> expressions = getExpressions("eventType");
expressions.addAll(getExpressions("eventTypeOther"));
Assert.assertTrue(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
Assert.assertTrue(expressions.contains("data3"));
handle1.remove();
Mockito.verify(ns, times(1)).put(eq("eventType"), any(Serializable.class));
expressions = getExpressions("eventType");
expressions.addAll(getExpressions("eventTypeOther"));
Assert.assertFalse(expressions.contains("data1"));
Assert.assertTrue(expressions.contains("data2"));
Assert.assertTrue(expressions.contains("data3"));
handle2.remove();
// updating settings does not take place a second time
Mockito.verify(ns, times(1)).put(eq("eventType"), any(Serializable.class));
expressions = getExpressions("eventType");
expressions.addAll(getExpressions("eventTypeOther"));
Assert.assertFalse(expressions.contains("data1"));
Assert.assertFalse(expressions.contains("data2"));
Assert.assertTrue(expressions.contains("data3"));
}
Aggregations