ComponentEventBus implements Serializable { public <T extends ComponentEvent<?>> Registration addListener( Class<T> eventType, ComponentEventListener<T> listener) { return addListenerInternal(eventType, listener, null); } ComponentEventBus(Component component); Registration addListener(
            Class<T> eventType, ComponentEventListener<T> listener); Registration addListener(
            Class<T> eventType, ComponentEventListener<T> listener,
            Consumer<DomListenerRegistration> domListenerConsumer); @SuppressWarnings("rawtypes") boolean hasListener(Class<? extends ComponentEvent> eventType); @SuppressWarnings({ "unchecked", "rawtypes" }) void fireEvent(ComponentEvent event);  }
@Test public void mappedDomEvent_fire_missingData() { TestComponent c = new TestComponent(); EventTracker<MappedToDomEvent> eventListener = new EventTracker<>(); c.addListener(MappedToDomEvent.class, eventListener); fireDomEvent(c, "dom-event", createData("event.someData", 2)); eventListener.assertEventCalled(c, true); Assert.assertEquals(2, eventListener.getEvent().getSomeData()); Assert.assertNull(eventListener.getEvent().getMoreData()); Assert.assertFalse(eventListener.getEvent().getPrimitiveBoolean()); Assert.assertNull(eventListener.getEvent().getObjectBoolean()); }
@Test public void domEvent_removeListener() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); Registration remover = component.addListener(MappedToDomEvent.class, eventTracker); remover.remove(); JsonObject eventData = createData("event.someData", 42, "event.moreData", 1); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventNotCalled(); assertNoListeners(component.getEventBus()); }
@Test public void domEvent_fireClientEvent() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); component.addListener(MappedToDomEvent.class, eventTracker); JsonObject eventData = createData("event.someData", 42, "event.moreData", 1); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventCalled(component, true); MappedToDomEvent event = eventTracker.getEvent(); Assert.assertEquals(42, event.getSomeData()); Assert.assertEquals("1", event.getMoreData()); }
@Test public void domEvent_fireServerEvent() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); component.addListener(MappedToDomEvent.class, eventTracker); JsonObject eventData = Json.createObject(); eventData.put("event.someData", 42); eventData.put("event.moreData", 1); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventCalled(component, true); MappedToDomEvent event = eventTracker.getEvent(); Assert.assertEquals(42, event.getSomeData()); Assert.assertEquals("1", event.getMoreData()); }
@Test public void domEvent_addListenerWithDomListenerConsumer() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); component.getEventBus().addListener(MappedToDomEvent.class, eventTracker, domRegistration -> domRegistration.debounce(200)); }
@Test(expected = IllegalArgumentException.class) public void nonDomEvent_addListenerWithDomListenerConsumer_throws() { TestComponent component = new TestComponent(); EventTracker<ServerEvent> eventTracker = new EventTracker<>(); component.getEventBus().addListener(ServerEvent.class, eventTracker, domRegistration -> domRegistration.debounce(200)); }
@Test public void domEvent_addSameListenerTwice() { TestComponent component = new TestComponent(); ComponentEventListener<MappedToDomEvent> listener = e -> calls++; Registration reg1 = component.addListener(MappedToDomEvent.class, listener); Registration reg2 = component.addListener(MappedToDomEvent.class, listener); Assert.assertEquals(1, component.getEventBus().componentEventData.size()); Assert.assertEquals(2, component.getEventBus().componentEventData .get(MappedToDomEvent.class).size()); fireDomEvent(component, "dom-event", Json.createObject()); Assert.assertEquals(2, calls); reg1.remove(); Assert.assertEquals(1, component.getEventBus().componentEventData.size()); Assert.assertEquals(1, component.getEventBus().componentEventData .get(MappedToDomEvent.class).size()); fireDomEvent(component, "dom-event", Json.createObject()); Assert.assertEquals(3, calls); reg2.remove(); Assert.assertEquals(0, component.getEventBus().componentEventData.size()); fireDomEvent(component, "dom-event", Json.createObject()); Assert.assertEquals(3, calls); }
@Test public void multipleEventsForSameDomEvent_removeListener() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); EventTracker<MappedToDomNoDataEvent> eventTracker2 = new EventTracker<>(); Registration remover = component.addListener(MappedToDomEvent.class, eventTracker); Registration remover2 = component .addListener(MappedToDomNoDataEvent.class, eventTracker2); remover.remove(); JsonObject eventData = createData("event.someData", 42, "event.moreData", 1); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventNotCalled(); eventTracker2.assertEventCalled(component, true); remover2.remove(); assertNoListeners(component.getEventBus()); }
@Test public void multipleEventsForSameDomEvent_fireEvent() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); EventTracker<MappedToDomNoDataEvent> eventTracker2 = new EventTracker<>(); component.addListener(MappedToDomEvent.class, eventTracker); component.addListener(MappedToDomNoDataEvent.class, eventTracker2); JsonObject eventData = createData("event.someData", 42, "event.moreData", 19); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventCalled(component, true); Assert.assertEquals("19", eventTracker.getEvent().getMoreData()); Assert.assertEquals(42, eventTracker.getEvent().getSomeData()); Assert.assertEquals(component, eventTracker.getEvent().getSource()); eventTracker2.assertEventCalled(component, true); }
@Test public void multipleListenersForSameEvent_fireEvent() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); EventTracker<MappedToDomEvent> eventTracker2 = new EventTracker<>(); component.addListener(MappedToDomEvent.class, eventTracker); component.addListener(MappedToDomEvent.class, eventTracker2); JsonObject eventData = createData("event.someData", 42, "event.moreData", 19); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventCalled(component, true); eventTracker2.assertEventCalled(component, true); Assert.assertEquals("19", eventTracker.getEvent().getMoreData()); Assert.assertEquals("19", eventTracker2.getEvent().getMoreData()); Assert.assertEquals(42, eventTracker.getEvent().getSomeData()); Assert.assertEquals(42, eventTracker2.getEvent().getSomeData()); Assert.assertEquals(component, eventTracker.getEvent().getSource()); Assert.assertEquals(component, eventTracker2.getEvent().getSource()); }
@Test public void multipleListenersForSameEvent_removeListener() { TestComponent component = new TestComponent(); EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>(); EventTracker<MappedToDomEvent> eventTracker2 = new EventTracker<>(); Registration remover = component.addListener(MappedToDomEvent.class, eventTracker); Registration remover2 = component.addListener(MappedToDomEvent.class, eventTracker2); remover.remove(); JsonObject eventData = createData("event.someData", 42, "event.moreData", 19); fireDomEvent(component, "dom-event", eventData); eventTracker.assertEventNotCalled(); eventTracker2.assertEventCalled(component, true); Assert.assertEquals("19", eventTracker2.getEvent().getMoreData()); Assert.assertEquals(42, eventTracker2.getEvent().getSomeData()); Assert.assertEquals(component, eventTracker2.getEvent().getSource()); remover2.remove(); assertNoListeners(component.getEventBus()); }
@Test(expected = IllegalArgumentException.class) public void invalidEventConstructor_addListener() { TestComponent c = new TestComponent(); c.addListener(InvalidMappedToDomEvent.class, e -> { }); }
@Test(expected = IllegalArgumentException.class) public void invalidEventDataInConstructor_addListener() { TestComponent c = new TestComponent(); c.addListener(MappedToDomInvalidEventData.class, e -> { }); }
@Test(expected = IllegalArgumentException.class) public void multipleEventDataConstructors_addListener() { TestComponent c = new TestComponent(); c.addListener(MappedToDomEventMultipleConstructors.class, e -> { }); }