Search in sources :

Example 16 with Registration

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

the class AbstractDataProviderTest method removeListener_listenerIsNotNotified.

@Test
public void removeListener_listenerIsNotNotified() {
    TestDataProvider dataProvider = new TestDataProvider();
    AtomicReference<DataChangeEvent<Object>> event = new AtomicReference<>();
    Registration registration = dataProvider.addDataProviderListener(event::set);
    registration.remove();
    dataProvider.refreshAll();
    Assert.assertNull(event.get());
}
Also used : Registration(com.vaadin.flow.shared.Registration) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataChangeEvent(com.vaadin.flow.data.provider.DataChangeEvent) Test(org.junit.Test)

Example 17 with Registration

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

the class CompositeDataGeneratorTest method removeDataGenerator_dataIsDestroyed.

@Test
public void removeDataGenerator_dataIsDestroyed() {
    CompositeDataGenerator<String> composite = new CompositeDataGenerator<>();
    MockDataGenerator mock1 = new MockDataGenerator("mock", "value1");
    MockDataGenerator mock2 = new MockDataGenerator("mock", "value1");
    Registration registration = composite.addDataGenerator(mock1);
    composite.addDataGenerator(mock2);
    composite.generateData("item1", Json.createObject());
    Assert.assertThat(mock1.getProcessed(), CoreMatchers.hasItem("item1"));
    Assert.assertThat(mock2.getProcessed(), CoreMatchers.hasItem("item1"));
    registration.remove();
    Assert.assertThat(mock1.getProcessed(), CoreMatchers.not(CoreMatchers.hasItem("item1")));
    Assert.assertThat(mock2.getProcessed(), CoreMatchers.hasItem("item1"));
    composite.removeDataGenerator(mock2);
    Assert.assertThat(mock2.getProcessed(), CoreMatchers.not(CoreMatchers.hasItem("item1")));
}
Also used : CompositeDataGenerator(com.vaadin.flow.data.provider.CompositeDataGenerator) Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 18 with Registration

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

the class ShadowRootTest method detachListener_parentDetach_childListenersTriggered.

@Test
public void detachListener_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.attachShadow().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) Test(org.junit.Test)

Example 19 with Registration

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

the class StateNodeTest method testAttachListener_listenerRemoved_listenerNotTriggered.

@Test
public void testAttachListener_listenerRemoved_listenerNotTriggered() {
    StateNode root = new RootStateNode();
    TestStateNode child = new TestStateNode();
    Assert.assertFalse(child.isAttached());
    AtomicBoolean triggered = new AtomicBoolean(false);
    Registration registrationHandle = child.addAttachListener(() -> triggered.set(true));
    registrationHandle.remove();
    setParent(child, root);
    Assert.assertFalse(triggered.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 20 with Registration

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

the class ElementListenersTest method testAddedListenerGetsEvent.

@Test
public void testAddedListenerGetsEvent() {
    AtomicInteger eventCount = new AtomicInteger();
    Registration handle = ns.add("foo", e -> eventCount.incrementAndGet(), new String[0]);
    Assert.assertEquals(0, eventCount.get());
    ns.fireEvent(createEvent("foo"));
    Assert.assertEquals(1, eventCount.get());
    handle.remove();
    ns.fireEvent(createEvent("foo"));
    Assert.assertEquals(1, eventCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Registration(com.vaadin.flow.shared.Registration) 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