use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.
the class ElementListenersTest method testEventDataInEvent.
@Test
public void testEventDataInEvent() {
AtomicReference<JsonObject> eventDataReference = new AtomicReference<>();
ns.add("foo", e -> {
Assert.assertNull(eventDataReference.get());
eventDataReference.set(e.getEventData());
}, new String[0]);
Assert.assertNull(eventDataReference.get());
JsonObject eventData = Json.createObject();
eventData.put("baz", true);
ns.fireEvent(new DomEvent(new Element("element"), "foo", eventData));
JsonObject capturedJson = eventDataReference.get();
Assert.assertNotNull(capturedJson);
Assert.assertEquals(1, capturedJson.keys().length);
Assert.assertEquals("true", capturedJson.get("baz").toJson());
}
use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.
the class EventRpcHandler method handleNode.
@Override
public Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
assert invocationJson.hasKey(JsonConstants.RPC_EVENT_TYPE);
String eventType = invocationJson.getString(JsonConstants.RPC_EVENT_TYPE);
JsonObject eventData = invocationJson.getObject(JsonConstants.RPC_EVENT_DATA);
if (eventData == null) {
eventData = Json.createObject();
}
DomEvent event = new DomEvent(Element.get(node), eventType, eventData);
node.getFeature(ElementListenerMap.class).fireEvent(event);
return Optional.empty();
}
use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.
the class ElementListenersTest method implicitlyDisabledElement_listenerDoesntReceiveEvent.
@Test
public void implicitlyDisabledElement_listenerDoesntReceiveEvent() {
AtomicInteger eventCount = new AtomicInteger();
ns.add("foo", e -> eventCount.incrementAndGet());
Assert.assertEquals(0, eventCount.get());
DomEvent event = createEvent("foo");
Element parent = new Element("parent");
parent.appendChild(event.getSource());
parent.setEnabled(false);
ns.fireEvent(event);
Assert.assertEquals(0, eventCount.get());
}
use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.
the class ElementListenersTest method addEventDataElement_targetNodeInJsonData_elementMapped.
@Test
public void addEventDataElement_targetNodeInJsonData_elementMapped() {
Element parent = new Element("parent");
Element child = new Element("child");
Element sibling = new Element("sibling");
parent.appendChild(child);
new StateTree(new UI().getInternals(), ElementChildrenList.class).getUI().getElement().appendChild(parent, sibling);
final String eventType = "click";
final String expression = "expression";
final String key = JsonConstants.MAP_STATE_NODE_EVENT_DATA + expression;
AtomicReference<DomEvent> capturedTarget = new AtomicReference<>();
final DomListenerRegistration registration = parent.addEventListener(eventType, capturedTarget::set);
final ElementListenerMap listenerMap = parent.getNode().getFeature(ElementListenerMap.class);
Set<String> expressions = getExpressions(listenerMap, eventType);
Assert.assertEquals(0, expressions.size());
registration.addEventDataElement(expression);
expressions = getExpressions(listenerMap, eventType);
Assert.assertEquals(1, expressions.size());
Assert.assertEquals(key, expressions.iterator().next());
final JsonObject eventData = Json.createObject();
eventData.put(key, child.getNode().getId());
listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
Assert.assertEquals(child, capturedTarget.get().getEventDataElement(expression).get());
// nothing reported -> empty optional
listenerMap.fireEvent(new DomEvent(parent, eventType, Json.createObject()));
Assert.assertFalse("no element should be reported", capturedTarget.get().getEventDataElement(expression).isPresent());
// sibling
eventData.put(key, sibling.getNode().getId());
listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
Assert.assertEquals(sibling, capturedTarget.get().getEventDataElement(expression).get());
}
use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.
the class ElementListenersTest method eventDataKeyNotPresentNotFail.
@Test
public void eventDataKeyNotPresentNotFail() {
AtomicInteger eventCount = new AtomicInteger();
DomListenerRegistration registration = ns.add("foo", e -> eventCount.incrementAndGet());
registration.setFilter("filterKey");
ns.fireEvent(createEvent("foo"));
Assert.assertEquals(0, eventCount.get());
JsonObject eventData = Json.createObject();
eventData.put("filterKey", true);
ns.fireEvent(new DomEvent(new Element("element"), "foo", eventData));
Assert.assertEquals(1, eventCount.get());
}
Aggregations