use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentEventBus method unregisterDomEvent.
/**
* Removes the DOM listener for the given event type.
*
* @param eventType
* the component event type
* @param domEventType
* the DOM event type for the component event type
*/
private void unregisterDomEvent(Class<? extends ComponentEvent<?>> eventType, String domEventType) {
assert eventType != null;
assert domEventType != null && !domEventType.isEmpty();
Registration domEventRemover = componentEventData.get(eventType).domEventRemover;
if (domEventRemover != null) {
domEventRemover.remove();
componentEventData.get(eventType).domEventRemover = null;
} else {
throw new IllegalArgumentException("No remover found when unregistering event type " + eventType.getName() + " from DOM event " + domEventType);
}
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ShadowRootTest method attachListener_parentAttach_childListenersTriggered.
@Test
public void attachListener_parentAttach_childListenersTriggered() {
Element body = new UI().getElement();
Element parent = ElementFactory.createDiv();
Element child = ElementFactory.createDiv();
Element grandChild = ElementFactory.createDiv();
AtomicInteger childTriggered = new AtomicInteger();
AtomicInteger grandChildTriggered = new AtomicInteger();
Registration registrationHandle = child.addAttachListener(event -> {
childTriggered.addAndGet(1);
});
child.addAttachListener(event -> {
Assert.assertEquals(child, event.getSource());
});
grandChild.addAttachListener(event -> {
grandChildTriggered.addAndGet(1);
});
grandChild.addAttachListener(event -> {
Assert.assertEquals(grandChild, event.getSource());
});
parent.attachShadow().appendChild(child);
child.appendChild(grandChild);
Assert.assertEquals(childTriggered.get(), 0);
Assert.assertEquals(grandChildTriggered.get(), 0);
body.appendChild(parent);
Assert.assertEquals(childTriggered.get(), 1);
Assert.assertEquals(grandChildTriggered.get(), 1);
body.removeAllChildren();
parent.getShadowRoot().get().removeAllChildren();
body.appendChild(parent);
parent.getShadowRoot().get().appendChild(child);
Assert.assertEquals(childTriggered.get(), 2);
Assert.assertEquals(grandChildTriggered.get(), 2);
registrationHandle.remove();
body.removeAllChildren();
body.appendChild(child);
Assert.assertEquals(childTriggered.get(), 2);
Assert.assertEquals(grandChildTriggered.get(), 3);
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ElementTest method testAttachListener_parentAttach_childListenersTriggered.
@Test
public void testAttachListener_parentAttach_childListenersTriggered() {
Element body = new UI().getElement();
Element parent = ElementFactory.createDiv();
Element child = ElementFactory.createDiv();
Element grandChild = ElementFactory.createDiv();
AtomicInteger childTriggered = new AtomicInteger();
AtomicInteger grandChildTriggered = new AtomicInteger();
Registration registrationHandle = child.addAttachListener(event -> {
childTriggered.addAndGet(1);
});
child.addAttachListener(event -> {
Assert.assertEquals(child, event.getSource());
});
grandChild.addAttachListener(event -> {
grandChildTriggered.addAndGet(1);
});
grandChild.addAttachListener(event -> {
Assert.assertEquals(grandChild, event.getSource());
});
parent.appendChild(child);
child.appendChild(grandChild);
Assert.assertEquals(childTriggered.get(), 0);
Assert.assertEquals(grandChildTriggered.get(), 0);
body.appendChild(parent);
Assert.assertEquals(childTriggered.get(), 1);
Assert.assertEquals(grandChildTriggered.get(), 1);
body.removeAllChildren();
parent.removeAllChildren();
body.appendChild(parent);
parent.appendChild(child);
Assert.assertEquals(childTriggered.get(), 2);
Assert.assertEquals(grandChildTriggered.get(), 2);
registrationHandle.remove();
body.removeAllChildren();
body.appendChild(child);
Assert.assertEquals(childTriggered.get(), 2);
Assert.assertEquals(grandChildTriggered.get(), 3);
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentEventBusTest method multipleEventsForSameDomEvent_removeListener.
@Test
public void multipleEventsForSameDomEvent_removeListener() {
TestComponent component = new TestComponent();
EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>();
EventTracker<MappedToDomNoDataEvent> eventTracker2 = new EventTracker<>();
Registration remover = component.addListener(MappedToDomEvent.class, eventTracker);
Registration remover2 = component.addListener(MappedToDomNoDataEvent.class, eventTracker2);
remover.remove();
JsonObject eventData = createData("event.someData", 42, "event.moreData", 1);
fireDomEvent(component, "dom-event", eventData);
eventTracker.assertEventNotCalled();
eventTracker2.assertEventCalled(component, true);
remover2.remove();
assertNoListeners(component.getEventBus());
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentEventBusTest method multipleListenersForSameEvent_removeListener.
@Test
public void multipleListenersForSameEvent_removeListener() {
TestComponent component = new TestComponent();
EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>();
EventTracker<MappedToDomEvent> eventTracker2 = new EventTracker<>();
Registration remover = component.addListener(MappedToDomEvent.class, eventTracker);
Registration remover2 = component.addListener(MappedToDomEvent.class, eventTracker2);
remover.remove();
JsonObject eventData = createData("event.someData", 42, "event.moreData", 19);
fireDomEvent(component, "dom-event", eventData);
eventTracker.assertEventNotCalled();
eventTracker2.assertEventCalled(component, true);
Assert.assertEquals("19", eventTracker2.getEvent().getMoreData());
Assert.assertEquals(42, eventTracker2.getEvent().getSomeData());
Assert.assertEquals(component, eventTracker2.getEvent().getSource());
remover2.remove();
assertNoListeners(component.getEventBus());
}
Aggregations