use of com.vaadin.flow.shared.Registration 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
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class RouterTest method manual_before_listeners_are_fired_before_observers.
// #2754
@Test
public void manual_before_listeners_are_fired_before_observers() throws InvalidRouteConfigurationException {
ManualNavigationTarget.events.clear();
router.getRegistry().setNavigationTargets(Stream.of(ManualNavigationTarget.class, FooNavigationTarget.class).collect(Collectors.toSet()));
Registration beforeEnter = ui.addBeforeEnterListener(event -> ManualNavigationTarget.events.add("Manual event"));
router.navigate(ui, new Location("manual"), NavigationTrigger.PROGRAMMATIC);
Assert.assertEquals("not enough events", 2, ManualNavigationTarget.events.size());
Assert.assertEquals("Manual event", ManualNavigationTarget.events.get(0));
Assert.assertEquals("Before enter", ManualNavigationTarget.events.get(1));
// Deactivate before enter and add beforeLeave listener
beforeEnter.remove();
ui.addBeforeLeaveListener(event -> ManualNavigationTarget.events.add("Manual event"));
router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC);
Assert.assertEquals("not enough events", 4, ManualNavigationTarget.events.size());
Assert.assertEquals("Manual event", ManualNavigationTarget.events.get(2));
Assert.assertEquals("Before leave", ManualNavigationTarget.events.get(3));
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentEventBusTest method nonDomEvent_removeListener.
@Test
public void nonDomEvent_removeListener() {
TestComponent component = new TestComponent();
EventTracker<ServerEvent> eventTracker = new EventTracker<>();
Registration remover = component.addListener(ServerEvent.class, eventTracker);
remover.remove();
component.fireEvent(new ServerEvent(component, new BigDecimal("12.2")));
eventTracker.assertEventNotCalled();
assertNoListeners(component.getEventBus());
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ElementAttributeMap method deferRegistration.
private void deferRegistration(String attribute, AbstractStreamResource resource) {
ensurePendingRegistrations();
assert !pendingRegistrations.containsKey(attribute);
Registration handle = getNode().addAttachListener(() -> registerResource(attribute, resource));
pendingRegistrations.put(attribute, handle);
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ElementTest method testDetachListener_parentDetach_childListenersTriggered.
@Test
public void testDetachListener_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.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);
}
Aggregations