use of com.vaadin.flow.internal.StateTree in project flow by vaadin.
the class ElementListenersTest method mapEventTargetToElement_targetNodeIdInJsonData_elementMapped.
@Test
public void mapEventTargetToElement_targetNodeIdInJsonData_elementMapped() {
Element parent = new Element("parent");
Element child = new Element("child");
Element grandChild = new Element("grandChild");
parent.appendChild(child.appendChild(grandChild));
new StateTree(new UI().getInternals(), ElementChildrenList.class).getUI().getElement().appendChild(parent);
final String eventType = "click";
AtomicReference<Element> capturedTarget = new AtomicReference<>();
final DomListenerRegistration registration = parent.addEventListener(eventType, e -> {
capturedTarget.set(e.getEventTarget().orElse(null));
});
final ElementListenerMap listenerMap = parent.getNode().getFeature(ElementListenerMap.class);
Set<String> expressions = getExpressions(listenerMap, eventType);
Assert.assertEquals(0, expressions.size());
registration.mapEventTargetElement();
expressions = getExpressions(listenerMap, eventType);
Assert.assertEquals(1, expressions.size());
Assert.assertEquals(JsonConstants.MAP_STATE_NODE_EVENT_DATA, expressions.iterator().next());
// child
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());
// nothing reported -> empty optional
listenerMap.fireEvent(new DomEvent(parent, eventType, Json.createObject()));
Assert.assertNull("no element should be reported", capturedTarget.get());
// grandchild
eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, grandChild.getNode().getId());
listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
Assert.assertEquals(grandChild, capturedTarget.get());
// -1 -> empty optional
eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, -1);
listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
Assert.assertNull("no element should be reported", capturedTarget.get());
}
use of com.vaadin.flow.internal.StateTree 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());
}
Aggregations