Search in sources :

Example 11 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class ElementListenersTest method addEventDataElement_eventTarget_usesMapEventTargetInstead.

@Test
public void addEventDataElement_eventTarget_usesMapEventTargetInstead() {
    Element parent = new Element("parent");
    Element child = new Element("child");
    parent.appendChild(child);
    new StateTree(new UI().getInternals(), ElementChildrenList.class).getUI().getElement().appendChild(parent);
    final String eventType = "click";
    AtomicReference<DomEvent> capturedTarget = new AtomicReference<>();
    final DomListenerRegistration registration = parent.addEventListener(eventType, capturedTarget::set);
    final ElementListenerMap listenerMap = parent.getNode().getFeature(ElementListenerMap.class);
    registration.addEventDataElement("event.target");
    Set<String> expressions = getExpressions(listenerMap, eventType);
    Assert.assertEquals(1, expressions.size());
    Assert.assertEquals(JsonConstants.MAP_STATE_NODE_EVENT_DATA, expressions.iterator().next());
    final JsonObject eventData = Json.createObject();
    eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, child.getNode().getId());
    listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
    Assert.assertEquals(child, capturedTarget.get().getEventTarget().get());
    Assert.assertEquals(child, capturedTarget.get().getEventDataElement("event.target").get());
}
Also used : Element(com.vaadin.flow.dom.Element) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 12 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class ElementListenersTest method disabledElement_listenerWithAlwaysUpdateModeReceivesEvent.

@Test
public void disabledElement_listenerWithAlwaysUpdateModeReceivesEvent() {
    AtomicInteger eventCount = new AtomicInteger();
    ns.add("foo", e -> eventCount.incrementAndGet()).setDisabledUpdateMode(DisabledUpdateMode.ALWAYS);
    Assert.assertEquals(0, eventCount.get());
    DomEvent event = createEvent("foo");
    event.getSource().setEnabled(false);
    ns.fireEvent(event);
    Assert.assertEquals(1, eventCount.get());
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 13 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class ElementListenersTest method disabledElement_listenerDoesntReceiveEvent.

@Test
public void disabledElement_listenerDoesntReceiveEvent() {
    AtomicInteger eventCount = new AtomicInteger();
    ns.add("foo", e -> eventCount.incrementAndGet());
    Assert.assertEquals(0, eventCount.get());
    DomEvent event = createEvent("foo");
    event.getSource().setEnabled(false);
    ns.fireEvent(event);
    Assert.assertEquals(0, eventCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Aggregations

DomEvent (com.vaadin.flow.dom.DomEvent)13 Test (org.junit.Test)11 Element (com.vaadin.flow.dom.Element)9 JsonObject (elemental.json.JsonObject)8 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 UI (com.vaadin.flow.component.UI)4 DomEventListener (com.vaadin.flow.dom.DomEventListener)4 StateTree (com.vaadin.flow.internal.StateTree)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Registration (com.vaadin.flow.shared.Registration)3 Component (com.vaadin.flow.component.Component)1 HtmlComponent (com.vaadin.flow.component.HtmlComponent)1 Label (com.vaadin.flow.component.html.Label)1 DisabledUpdateMode (com.vaadin.flow.dom.DisabledUpdateMode)1 ElementListenerMap (com.vaadin.flow.internal.nodefeature.ElementListenerMap)1 Route (com.vaadin.flow.router.Route)1 JsonConstants (com.vaadin.flow.shared.JsonConstants)1 ViewTestLayout (com.vaadin.flow.uitest.servlet.ViewTestLayout)1 Json (elemental.json.Json)1