public void resetEventBuses() { for (ResettableEventBus resettableEventBus : resettableEventBuses) { resettableEventBus.reset(); } } }
@Override public void removeHandler() { remove(handler); } };
@Override public HandlerRegistration addFragmentChangedHandler(final FragmentChangedHandler handler) { return handlers.add(handler); }
@Test public void testDoesNotDispatchToRemovedHandler() { // GIVEN ResettableEventBus eventBus = new ResettableEventBus(new SimpleEventBus()); InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); HandlerRegistration registration = eventBus.addHandler(TestEvent.class, handler); registration.removeHandler(); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(0, handler.getInvocationCount()); }
@Test public void testHandlerRemovedWhileDispatchingIsCalled() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); RemoveEventHandler handler1 = new RemoveEventHandler(); InvocationCountingTestEventHandler handler2 = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler1); handler1.handlerRegistration = eventBus.addHandler(TestEvent.class, handler2); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler1.getInvocationCount()); assertEquals(1, handler2.getInvocationCount()); }
@Test public void testMultipleRegistrationOfTheSameHandlerWillNotResultInMultipleInvocations() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler.getInvocationCount()); }
@Override public <H extends EventHandler> void fireEvent(Event<H> event) { wrapped.fireEvent(event); }
@Test public void testCanRemoveHandlerViaReturnedRegistrationObject() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); InvocationCountingTestEventHandler eventHandler = new InvocationCountingTestEventHandler(); HandlerRegistration registration = collection.add(eventHandler); collection.dispatch(new TestEvent()); assertEquals(1, eventHandler.getInvocationCount()); registration.removeHandler(); // WHEN collection.dispatch(new TestEvent()); // THEN (still 1) assertEquals(1, eventHandler.getInvocationCount()); }
@Test public void testCanRemoveHandler() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); InvocationCountingTestEventHandler eventHandler = new InvocationCountingTestEventHandler(); collection.add(eventHandler); collection.dispatch(new TestEvent()); assertEquals(1, eventHandler.getInvocationCount()); collection.remove(eventHandler); // WHEN collection.dispatch(new TestEvent()); // THEN (still 1) assertEquals(1, eventHandler.getInvocationCount()); }
@Test public void testCanDispatchEvent() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); InvocationCountingTestEventHandler eventHandler = new InvocationCountingTestEventHandler(); assertTrue(collection.isEmpty()); assertEquals(0, collection.size()); collection.add(eventHandler); assertFalse(collection.isEmpty()); assertEquals(1, collection.size()); // WHEN collection.dispatch(new TestEvent()); // THEN assertEquals(1, eventHandler.getInvocationCount()); }
@Inject public AppEventTestImpl(AppContext ctx, AppView view, @Named(AppEventBus.NAME) EventBus eventBus, @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus) { super(ctx, view); this.eventBus = eventBus; this.admincentralEventBus = admincentralEventBus; // Register handler = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler); }
@Override public void handleEvent(TestEvent event) { super.handleEvent(event); handlerRegistration.removeHandler(); } }
@Override public synchronized void handleEvent(TestEvent event) { super.handleEvent(event); collection.add(secondHandler); } };
@Override public synchronized void handleEvent(TestEvent event) { super.handleEvent(event); collection.remove(secondHandler); } };
@Override public void handleEvent(TestEvent event) { super.handleEvent(event); eventBus.addHandler(TestEvent.class, handler2); } };
@Override public <H extends EventHandler> HandlerRegistration addHandler(final Class<? extends Event<H>> eventClass, final H handler) { log.debug("Adding handler {} for events of class {}", handler, eventClass); internalAddHandler(eventClass, handler); return new HandlerRegistration() { @Override public void removeHandler() { internalRemoveHandler(eventClass, handler); } }; }
@Override public void dispatch(TestEventHandler handler) { handler.handleEvent(this); } }
protected void goToWithoutChecks(Location newLocation) { this.where = newLocation; eventBus.fireEvent(new LocationChangedEvent(newLocation)); } }
@Override protected void after() { this.eventBus.reset(); } }
/** * Send an event to the system event bus. */ private void sendEvent(AppRegistryEventType eventType, Collection<DefinitionMetadata> appDescriptors) { for (DefinitionMetadata appMetadata : appDescriptors) { systemEventBus.fireEvent(new AppRegistryEvent(appMetadata, eventType)); } } }