BootstrapHandler extends SynchronizedRequestHandler { protected BootstrapContext createAndInitUI(Class<? extends UI> uiClass, VaadinRequest request, VaadinResponse response, VaadinSession session) { UI ui = ReflectTools.createInstance(uiClass); ui.getInternals().setContextRoot( request.getService().getContextRootRelativePath(request)); PushConfiguration pushConfiguration = ui.getPushConfiguration(); ui.getInternals().setSession(session); ui.setLocale(session.getLocale()); BootstrapContext context = createBootstrapContext(request, response, ui, request.getService()::getContextRootRelativePath); Optional<Push> push = context .getPageConfigurationAnnotation(Push.class); DeploymentConfiguration deploymentConfiguration = context.getSession() .getService().getDeploymentConfiguration(); PushMode pushMode = push.map(Push::value) .orElseGet(deploymentConfiguration::getPushMode); setupPushConnectionFactory(pushConfiguration, context); pushConfiguration.setPushMode(pushMode); pushConfiguration.setPushUrl(deploymentConfiguration.getPushURL()); push.map(Push::transport).ifPresent(pushConfiguration::setTransport); UI.setCurrent(ui); ui.doInit(request, session.getNextUIid()); session.addUI(ui); session.getService().fireUIInitListeners(ui); initializeUIWithRouter(request, ui); return context; } BootstrapHandler(); protected  BootstrapHandler(PageBuilder pageBuilder); @Override boolean synchronizedHandleRequest(VaadinSession session,
            VaadinRequest request, VaadinResponse response);  }
@Test public void uiInitialization_allRegisteredListenersAreNotified() { BootstrapHandler bootstrapHandler = new BootstrapHandler(); VaadinResponse response = Mockito.mock(VaadinResponse.class); AtomicReference<UI> uiReference = new AtomicReference<>(); Registration registration = service.addUIInitListener( event -> Assert.assertTrue("Atomic reference was not empty.", uiReference.compareAndSet(null, event.getUI()))); final BootstrapContext context = bootstrapHandler.createAndInitUI( TestUI.class, createVaadinRequest(), response, session); Assert.assertEquals("Event UI didn't match initialized UI instance.", context.getUI(), uiReference.get()); registration.remove(); AtomicReference<UI> secondListenerReference = new AtomicReference<>(); service.addUIInitListener(event -> Assert.assertTrue( "Atomic reference did not contain previous UI.", uiReference.compareAndSet(context.getUI(), event.getUI()))); service.addUIInitListener(event -> Assert.assertTrue( "Atomic reference was not empty.", secondListenerReference.compareAndSet(null, event.getUI()))); final BootstrapContext secondInit = bootstrapHandler.createAndInitUI( TestUI.class, createVaadinRequest(), response, session); Assert.assertEquals("Event UI didn't match initialized UI instance.", secondInit.getUI(), uiReference.get()); Assert.assertEquals( "Second event UI didn't match initialized UI instance.", secondInit.getUI(), secondListenerReference.get()); }
@Test public void uiInitialization_changingListenersOnEventWorks() { BootstrapHandler bootstrapHandler = new BootstrapHandler(); VaadinResponse response = Mockito.mock(VaadinResponse.class); AtomicReference<UI> uiReference = new AtomicReference<>(); Registration registration = service.addUIInitListener( event -> service.addUIInitListener(laterEvent -> uiReference .compareAndSet(null, laterEvent.getUI()))); bootstrapHandler.createAndInitUI(TestUI.class, createVaadinRequest(), response, session); Assert.assertEquals("Event UI didn't match initialized UI instance.", null, uiReference.get()); registration.remove(); service.addUIInitListener(event -> registration.remove()); final BootstrapContext secondInit = bootstrapHandler.createAndInitUI( TestUI.class, createVaadinRequest(), response, session); Assert.assertEquals("Event UI didn't match initialized UI instance.", secondInit.getUI(), uiReference.get()); }