Search in sources :

Example 21 with Registration

use of com.vaadin.flow.shared.Registration 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
}
Also used : Registration(com.vaadin.flow.shared.Registration) DomEventListener(com.vaadin.flow.dom.DomEventListener) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 22 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class RouterTest method manual_before_listeners_are_fired_before_observers.

// #2754
@Test
public void manual_before_listeners_are_fired_before_observers() throws InvalidRouteConfigurationException {
    ManualNavigationTarget.events.clear();
    router.getRegistry().setNavigationTargets(Stream.of(ManualNavigationTarget.class, FooNavigationTarget.class).collect(Collectors.toSet()));
    Registration beforeEnter = ui.addBeforeEnterListener(event -> ManualNavigationTarget.events.add("Manual event"));
    router.navigate(ui, new Location("manual"), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("not enough events", 2, ManualNavigationTarget.events.size());
    Assert.assertEquals("Manual event", ManualNavigationTarget.events.get(0));
    Assert.assertEquals("Before enter", ManualNavigationTarget.events.get(1));
    // Deactivate before enter and add beforeLeave listener
    beforeEnter.remove();
    ui.addBeforeLeaveListener(event -> ManualNavigationTarget.events.add("Manual event"));
    router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("not enough events", 4, ManualNavigationTarget.events.size());
    Assert.assertEquals("Manual event", ManualNavigationTarget.events.get(2));
    Assert.assertEquals("Before leave", ManualNavigationTarget.events.get(3));
}
Also used : Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 23 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class ComponentEventBusTest method nonDomEvent_removeListener.

@Test
public void nonDomEvent_removeListener() {
    TestComponent component = new TestComponent();
    EventTracker<ServerEvent> eventTracker = new EventTracker<>();
    Registration remover = component.addListener(ServerEvent.class, eventTracker);
    remover.remove();
    component.fireEvent(new ServerEvent(component, new BigDecimal("12.2")));
    eventTracker.assertEventNotCalled();
    assertNoListeners(component.getEventBus());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Registration(com.vaadin.flow.shared.Registration) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 24 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class ElementAttributeMap method deferRegistration.

private void deferRegistration(String attribute, AbstractStreamResource resource) {
    ensurePendingRegistrations();
    assert !pendingRegistrations.containsKey(attribute);
    Registration handle = getNode().addAttachListener(() -> registerResource(attribute, resource));
    pendingRegistrations.put(attribute, handle);
}
Also used : Registration(com.vaadin.flow.shared.Registration) StreamRegistration(com.vaadin.flow.server.StreamRegistration)

Example 25 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class ElementTest method testDetachListener_parentDetach_childListenersTriggered.

@Test
public void testDetachListener_parentDetach_childListenersTriggered() {
    Element body = new UI().getElement();
    Element parent = ElementFactory.createDiv();
    Element child = ElementFactory.createDiv();
    Element grandChild = ElementFactory.createDiv();
    AtomicInteger triggered = new AtomicInteger();
    Registration registrationHandle = child.addDetachListener(event -> {
        triggered.addAndGet(1);
        Assert.assertEquals(child, event.getSource());
    });
    grandChild.addDetachListener(event -> {
        triggered.addAndGet(1);
        Assert.assertEquals(grandChild, event.getSource());
    });
    child.appendChild(grandChild);
    parent.appendChild(child);
    body.appendChild(parent);
    Assert.assertEquals(triggered.get(), 0);
    body.removeAllChildren();
    Assert.assertEquals(triggered.get(), 2);
    body.appendChild(parent);
    body.removeAllChildren();
    Assert.assertEquals(triggered.get(), 4);
    body.appendChild(parent);
    registrationHandle.remove();
    body.removeAllChildren();
    Assert.assertEquals(triggered.get(), 5);
}
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)

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