@Override public void onFragmentChanged(String fragment) { handlers.dispatch(new FragmentChangedEvent(fragment)); }
@Test public void testHandlersAreInvokedInOrder() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); 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); collection.add(handler); } // WHEN collection.dispatch(new TestEvent()); // THEN for (InvocationOrderTestingEventHandler handler : handlers) { assertEquals(1, handler.getInvocationCount()); assertEquals(handler.expectedOrder, handler.actualOrder); } }
@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 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 testHandlersAddedWhileDispatchedAreNotInvoked() { // GIVEN final EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); final InvocationCountingTestEventHandler secondHandler = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler firstHandler = new InvocationCountingTestEventHandler() { @Override public synchronized void handleEvent(TestEvent event) { super.handleEvent(event); collection.add(secondHandler); } }; collection.add(firstHandler); // WHEN collection.dispatch(new TestEvent()); // THEN assertEquals(1, firstHandler.getInvocationCount()); assertEquals(0, secondHandler.getInvocationCount()); }
@Test public void testHandlersRemovedWhileDispatchedAreStillInvoked() { // GIVEN final EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); final InvocationCountingTestEventHandler secondHandler = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler firstHandler = new InvocationCountingTestEventHandler() { @Override public synchronized void handleEvent(TestEvent event) { super.handleEvent(event); collection.remove(secondHandler); } }; collection.add(firstHandler); collection.add(secondHandler); // WHEN collection.dispatch(new TestEvent()); // THEN assertEquals(1, collection.size()); assertEquals(1, firstHandler.getInvocationCount()); assertEquals(1, secondHandler.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()); }