Search in sources :

Example 1 with DomEventListener

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

Example 2 with DomEventListener

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
}
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 3 with DomEventListener

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;
}
Also used : Consumer(java.util.function.Consumer) DomEvent(com.vaadin.flow.dom.DomEvent) DomEventListener(com.vaadin.flow.dom.DomEventListener) Component(com.vaadin.flow.component.Component) HtmlComponent(com.vaadin.flow.component.HtmlComponent) Element(com.vaadin.flow.dom.Element) Label(com.vaadin.flow.component.html.Label) ViewTestLayout(com.vaadin.flow.uitest.servlet.ViewTestLayout) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) Route(com.vaadin.flow.router.Route) Element(com.vaadin.flow.dom.Element) Label(com.vaadin.flow.component.html.Label) DomEventListener(com.vaadin.flow.dom.DomEventListener) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEvent(com.vaadin.flow.dom.DomEvent)

Example 4 with DomEventListener

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"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) SerializationUtils(org.apache.commons.lang3.SerializationUtils) StateTree(com.vaadin.flow.internal.StateTree) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) UI(com.vaadin.flow.component.UI) Before(org.junit.Before) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DisabledUpdateMode(com.vaadin.flow.dom.DisabledUpdateMode) DomEvent(com.vaadin.flow.dom.DomEvent) JsonConstants(com.vaadin.flow.shared.JsonConstants) Set(java.util.Set) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Serializable(java.io.Serializable) Mockito(org.mockito.Mockito) DomEventListener(com.vaadin.flow.dom.DomEventListener) JsonObject(elemental.json.JsonObject) Assert(org.junit.Assert) Collections(java.util.Collections) Registration(com.vaadin.flow.shared.Registration) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEventListener(com.vaadin.flow.dom.DomEventListener) Test(org.junit.Test)

Example 5 with DomEventListener

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"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) SerializationUtils(org.apache.commons.lang3.SerializationUtils) StateTree(com.vaadin.flow.internal.StateTree) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) UI(com.vaadin.flow.component.UI) Before(org.junit.Before) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DisabledUpdateMode(com.vaadin.flow.dom.DisabledUpdateMode) DomEvent(com.vaadin.flow.dom.DomEvent) JsonConstants(com.vaadin.flow.shared.JsonConstants) Set(java.util.Set) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Serializable(java.io.Serializable) Mockito(org.mockito.Mockito) DomEventListener(com.vaadin.flow.dom.DomEventListener) JsonObject(elemental.json.JsonObject) Assert(org.junit.Assert) Collections(java.util.Collections) Serializable(java.io.Serializable) Registration(com.vaadin.flow.shared.Registration) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEventListener(com.vaadin.flow.dom.DomEventListener) Test(org.junit.Test)

Aggregations

DomEvent (com.vaadin.flow.dom.DomEvent)5 DomEventListener (com.vaadin.flow.dom.DomEventListener)5 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)4 Registration (com.vaadin.flow.shared.Registration)4 Test (org.junit.Test)4 Element (com.vaadin.flow.dom.Element)3 UI (com.vaadin.flow.component.UI)2 DisabledUpdateMode (com.vaadin.flow.dom.DisabledUpdateMode)2 StateTree (com.vaadin.flow.internal.StateTree)2 JsonConstants (com.vaadin.flow.shared.JsonConstants)2 Json (elemental.json.Json)2 JsonObject (elemental.json.JsonObject)2 Serializable (java.io.Serializable)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SerializationUtils (org.apache.commons.lang3.SerializationUtils)2 Assert (org.junit.Assert)2