Search in sources :

Example 11 with Registration

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);
    }
}
Also used : Registration(com.vaadin.flow.shared.Registration)

Example 12 with Registration

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);
}
Also used : UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 13 with Registration

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);
}
Also used : UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Registration(com.vaadin.flow.shared.Registration) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 14 with Registration

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());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Registration(com.vaadin.flow.shared.Registration) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 15 with Registration

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());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Registration(com.vaadin.flow.shared.Registration) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Aggregations

Registration (com.vaadin.flow.shared.Registration)25 Test (org.junit.Test)20 UI (com.vaadin.flow.component.UI)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)4 Element (com.vaadin.flow.dom.Element)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 StreamRegistration (com.vaadin.flow.server.StreamRegistration)3 JsonObject (elemental.json.JsonObject)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)2 Set (java.util.Set)2 Assert (org.junit.Assert)2 Component (com.vaadin.flow.component.Component)1 Html (com.vaadin.flow.component.Html)1 Tag (com.vaadin.flow.component.Tag)1 Text (com.vaadin.flow.component.Text)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1