private void bindEventBuses() { // Make event buses injectable. Use EventBusProxyScope to automatically wrap the event buses injected across // scopes with ResettableEventBus final Provider<EventBus> eventBusProvider = () -> new ResettableEventBus(new SimpleEventBus()); final Function<String, Key<EventBus>> createEventBusKey = (name) -> Key.get(EventBus.class, Names.named(name)); final Consumer<String> bindEventBus = name -> bind(createEventBusKey.apply(name)) .toProvider(eventBusProvider) .in(eventBusScopes.get(name)); bindEventBus.accept(AdmincentralEventBus.NAME); bindEventBus.accept(AppEventBus.NAME); bindEventBus.accept(SubAppEventBus.NAME); bindEventBus.accept(ChooseDialogEventBus.NAME); bindEventBus.accept("mediaeditor"); }
@Override protected void configure() { bind(EventBus.class).annotatedWith(Names.named(SystemEventBus.NAME)).toProvider(Providers.of(new SimpleEventBus())); } }
@Override public void setUp() throws Exception { super.setUp(); workbenchPresenter = mock(WorkbenchPresenter.class); eventBus = new SimpleEventBus(); itemIds = new HashSet<>(); // make sure that workbench view registers a content view so that restore selection doesn't fail. WorkbenchView workbenchView = mock(WorkbenchView.class); doReturn(mock(Component.class)).when(workbenchView).asVaadinComponent(); doReturn(workbenchView).when(workbenchPresenter).start(any(WorkbenchDefinition.class), any(ImageProviderDefinition.class), any(EventBus.class)); }
private void initWorkbenchPresenter() throws Exception { SimpleTranslator i18n = mock(SimpleTranslator.class); contentConnector = mock(ContentConnector.class); StatusBarView view = mock(StatusBarViewImpl.class); activeContentPresenter = mock(ContentPresenter.class); eventBus = new SimpleEventBus(); barPresenter = new WorkbenchStatusBarPresenter(view, contentConnector, i18n); barPresenter.start(eventBus, activeContentPresenter); }
@Override @Before public void setUp() throws Exception { super.setUp(); // Init this.chooseDialogEventBus = new SimpleEventBus(); this.availabilityChecker = mock(AvailabilityChecker.class); this.actionDefinition = mock(ActionDefinition.class); this.actionListener = mock(ActionListener.class); ConfiguredAvailabilityDefinition availability = mock(ConfiguredAvailabilityDefinition.class); this.renderer = new SelectionSensitiveActionRenderer(chooseDialogEventBus, availabilityChecker); this.itemId = new HashSet<Object>(); itemId.add("one"); this.event = new SelectionChangedEvent(itemId); when(actionDefinition.getAvailability()).thenReturn(availability); when(availabilityChecker.isAvailable(any(ConfiguredAvailabilityDefinition.class), anyList())).thenReturn(true); }
private void assertCallsLocationControllerWhenCurrentHistoryIs(String fragment, Location expectedLocation) { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); Shell shell = mock(Shell.class); when(shell.getFragment()).thenReturn(fragment); LocationController locationController = mock(LocationController.class); LocationHistoryMapper locationHistoryMapper = new MockLocationHistoryMapper(); LocationHistoryHandler locationHistoryHandler = new LocationHistoryHandler(locationHistoryMapper, shell); locationHistoryHandler.register(locationController, eventBus, Location.NOWHERE); // WHEN locationHistoryHandler.handleCurrentFragment(); // THEN Mockito.verify(locationController).goTo(eq(expectedLocation)); }
@Before public void setUp() { this.context = mock(Context.class); User usr = mock(User.class); when(context.getUser()).thenReturn(usr); MgnlContext.setInstance(context); this.componentProvider = mock(ComponentProvider.class); when(componentProvider.getComponent(eq(SystemContext.class))).thenReturn(new MockContext()); this.eventBus = new SimpleEventBus(); this.events = new ArrayList<>(); final UI ui = mock(UI.class); doReturn(true).when(ui).isAttached(); doAnswer(invocation -> { final Runnable task = invocation.getArgumentAt(0, Runnable.class); task.run(); return null; }).when(ui).access(any(Runnable.class)); this.dispatcher = new LocalTaskDispatcher(eventBus, ui, context, componentProvider); eventBus.addHandler(TaskEvent.class, new CollectingTaskEventHandler(events)); }
@Override @Before public void setUp() throws Exception { super.setUp(); ComponentsTestUtil.setImplementation(AccessDefinition.class, ConfiguredAccessDefinition.class); ComponentsTestUtil.setImplementation(AvailabilityDefinition.class, ConfiguredAvailabilityDefinition.class); definition = new RestorePreviousVersionActionDefinition(); Session webSiteSession = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE); node = webSiteSession.getRootNode().addNode("node", NodeTypes.Page.NAME); NodeTypes.Created.set(node); node.setProperty("property", "property"); node.addNode("subNode", NodeTypes.Page.NAME); node.getNode("subNode").setProperty("property_subNode", "property_subNode"); webSiteSession.save(); locationEventBus = new SimpleEventBus(); locationController = new LocationController(locationEventBus, mock(Shell.class)); subAppContext = mock(SubAppContext.class); eventBus = mock(EventBus.class); i18n = mock(SimpleTranslator.class); }
private void assertSetsFragmentToOnLocationChange(Location newLocation, String expectedFragment) { // GIVEN Shell shell = mock(Shell.class); SimpleEventBus eventBus = new SimpleEventBus(); LocationController locationController = mock(LocationController.class); LocationHistoryMapper locationHistoryMapper = new MockLocationHistoryMapper(); LocationHistoryHandler locationHistoryHandler = new LocationHistoryHandler(locationHistoryMapper, shell); locationHistoryHandler.register(locationController, eventBus, Location.NOWHERE); // WHEN eventBus.fireEvent(new LocationChangedEvent(newLocation)); // THEN Mockito.verify(shell).setFragment(expectedFragment); }
private void assertFragmentChangeCausesLocationChangeTo(String fragment, Location expectedLocation) { // GIVEN Shell shell = mock(Shell.class); final Reference<FragmentChangedHandler> handler = new Reference<FragmentChangedHandler>(); doAnswer(new Answer<HandlerRegistration>() { @Override public HandlerRegistration answer(InvocationOnMock invocation) throws Throwable { handler.value = (FragmentChangedHandler) invocation.getArguments()[0]; return null; } }).when(shell).addFragmentChangedHandler(any(FragmentChangedHandler.class)); SimpleEventBus eventBus = new SimpleEventBus(); LocationController locationController = mock(LocationController.class); LocationHistoryMapper locationHistoryMapper = new MockLocationHistoryMapper(); LocationHistoryHandler locationHistoryHandler = new LocationHistoryHandler(locationHistoryMapper, shell); locationHistoryHandler.register(locationController, eventBus, Location.NOWHERE); // WHEN handler.value.onFragmentChanged(new FragmentChangedEvent(fragment)); // THEN Mockito.verify(locationController).goTo(eq(expectedLocation)); }
@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 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); } }
@Before public void setUp() throws Exception { // INIT EventBus eventBus = new SimpleEventBus(); ComponentsTestUtil.setImplementation(AppDescriptor.class, ConfiguredAppDescriptor.class); session = SessionTestUtil.createSession(RepositoryConstants.CONFIG, "/modules/aModule/apps/app1.name=appNameA", "/modules/bModule/apps/app1.name=appNameB" ); MockUtil.initMockContext(); MockUtil.setSystemContextSessionAndHierarchyManager(session); Set<String> moduleNames = new LinkedHashSet<String>(); moduleNames.add("aModule"); moduleNames.add("bModule"); moduleRegistry = mock(ModuleRegistry.class); when(moduleRegistry.getModuleNames()).thenReturn(moduleNames); appRegistry = new AppDescriptorRegistry(eventBus); TypeMappingImpl typeMapping = new TypeMappingImpl(); Node2BeanTransformerImpl transformer = new Node2BeanTransformerImpl(); ComponentsTestUtil.setInstance(Node2BeanProcessor.class, new Node2BeanProcessorImpl(typeMapping, transformer)); }
ResettableEventBus eventBus = new ResettableEventBus(new SimpleEventBus());
@Before public void setUp() throws Exception { EventBus eventBus = new SimpleEventBus(); eventHandler = new EventCollectingAppRegistryEventHandler(); eventBus.addHandler(AppRegistryEvent.class, eventHandler); appDescriptorRegistry = new AppDescriptorRegistry(eventBus, mock(ModuleRegistry.class)); TypeMappingImpl typeMapping = new TypeMappingImpl(); Node2BeanTransformerImpl transformer = new Node2BeanTransformerImpl(new PreConfiguredBeanUtils()); ComponentsTestUtil.setInstance(Node2BeanProcessor.class, new Node2BeanProcessorImpl(typeMapping, transformer)); ComponentsTestUtil.setImplementation(AppDescriptor.class, ConfiguredAppDescriptor.class); }
@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()); }
@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 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()); }
@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); }
@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()); }