@Override public void handleEvent(TestEvent event) { super.handleEvent(event); eventBus.addHandler(TestEvent.class, handler2); } };
@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 void run() { try { startSignal.await(); for (int i = 0; i < numberOfIterations; i++) { List<InvocationCountingTestEventHandler> handlers = new ArrayList<InvocationCountingTestEventHandler>(); List<HandlerRegistration> registrations = new ArrayList<HandlerRegistration>(); // Add handlers for (int j = 0; j < numberOfHandlers; j++) { InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); handlers.add(handler); registrations.add(eventBus.addHandler(TestEvent.class, handler)); } // Fire event eventBus.fireEvent(new TestEvent()); // Remove handlers for (HandlerRegistration handler : registrations) { handler.removeHandler(); } // Make sure that every handler was invoked at least once for (InvocationCountingTestEventHandler handler : handlers) { Assert.assertTrue(handler.getInvocationCount() > 0); } } } catch (Throwable e) { threadEncounteredException.set(true); e.printStackTrace(); } } });
@Test public void testAreHandlersInvokedInOrder() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); List<InvocationOrderTestingEventHandler> handlers = new ArrayList<InvocationOrderTestingEventHandler>(); AtomicInteger counter = new AtomicInteger(0); for (int i = 0; i < 1000; i++) { InvocationOrderTestingEventHandler handler = new InvocationOrderTestingEventHandler(i, counter); handlers.add(handler); eventBus.addHandler(TestEvent.class, handler); } // WHEN eventBus.fireEvent(new TestEvent()); // THEN for (InvocationOrderTestingEventHandler handler : handlers) { assertEquals(1, handler.getInvocationCount()); assertEquals(handler.expectedOrder, handler.actualOrder); } }
@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 testGoToSameLocationDoesNothing() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); LocationController locationController = new LocationController(eventBus, mock(Shell.class)); assertEquals(Location.NOWHERE, locationController.getWhere()); Location newLocation = getNewEmptyLocation(); locationController.goTo(newLocation); assertSame(newLocation, locationController.getWhere()); LocationChangeRequestedHandler requestHandler = new LocationChangeRequestedHandler(); LocationChangedHandler changeHandler = new LocationChangedHandler(); eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler); // WHEN locationController.goTo(newLocation); // THEN assertNull(requestHandler.event); assertNull(changeHandler.event); }
eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler);
eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler);
@Test public void testGoToWithoutWarning() { // GIVEN LocationChangeRequestedHandler requestHandler = new LocationChangeRequestedHandler(); LocationChangedHandler changeHandler = new LocationChangedHandler(); SimpleEventBus eventBus = new SimpleEventBus(); eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler); LocationController locationController = new LocationController(eventBus, mock(Shell.class)); assertEquals(Location.NOWHERE, locationController.getWhere()); Location newLocation = getNewEmptyLocation(); // WHEN locationController.goTo(newLocation); // THEN assertSame(newLocation, locationController.getWhere()); assertNotNull(requestHandler.event); assertSame(newLocation, requestHandler.event.getNewLocation()); assertNotNull(changeHandler.event); assertSame(newLocation, changeHandler.event.getNewLocation()); }
@Test public void testHandlerAddedWhileDispatchingIsNotCalled() { // GIVEN final SimpleEventBus eventBus = new SimpleEventBus(); final InvocationCountingTestEventHandler handler2 = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler handler1 = new InvocationCountingTestEventHandler() { @Override public void handleEvent(TestEvent event) { super.handleEvent(event); eventBus.addHandler(TestEvent.class, handler2); } }; eventBus.addHandler(TestEvent.class, handler1); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler1.getInvocationCount()); assertEquals(0, handler2.getInvocationCount()); }