Search in sources :

Example 1 with RouterInterface

use of com.vaadin.flow.router.RouterInterface in project flow by vaadin.

the class VaadinUIScopeTest method mockUI.

private UI mockUI() {
    VaadinSession session = mockSession();
    RouterInterface routerIface = mock(RouterInterface.class);
    VaadinService service = session.getService();
    when(service.getRouter()).thenReturn(routerIface);
    ImmutableRouterConfiguration config = mock(ImmutableRouterConfiguration.class);
    when(routerIface.getConfiguration()).thenReturn(config);
    when(config.isConfigured()).thenReturn(false);
    UI ui = new UI();
    ui.getInternals().setSession(session);
    ui.doInit(null, 1);
    UI.setCurrent(ui);
    // prevent UI from being GCed.
    this.ui = ui;
    return ui;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) RouterInterface(com.vaadin.flow.router.RouterInterface)

Example 2 with RouterInterface

use of com.vaadin.flow.router.RouterInterface in project flow by vaadin.

the class UidlWriterTest method initializeUIForDependenciesTest.

private UI initializeUIForDependenciesTest(UI ui) {
    ServletContext context = Mockito.mock(ServletContext.class);
    VaadinServletService service = new VaadinServletService(new VaadinServlet() {

        @Override
        public ServletContext getServletContext() {
            return context;
        }
    }, new MockDeploymentConfiguration()) {

        RouterInterface router = new com.vaadin.flow.router.legacy.Router();

        @Override
        public RouterInterface getRouter() {
            return router;
        }

        @Override
        public Iterable<DependencyFilter> getDependencyFilters() {
            return Collections.emptyList();
        }
    };
    service.getRouter().reconfigure(conf -> {
        conf.setRoute("", BaseClass.class);
        conf.setParentView(BaseClass.class, ParentClass.class);
        conf.setParentView(ParentClass.class, SuperParentClass.class);
    });
    MockVaadinSession session = new MockVaadinSession(service);
    session.lock();
    ui.getInternals().setSession(session);
    when(service.getResourceAsStream(anyString())).thenAnswer(invocation -> new ByteArrayInputStream(((String) invocation.getArguments()[0]).getBytes()));
    HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
    VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
    when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
    service.setCurrentInstances(vaadinRequestMock, mock(VaadinResponse.class));
    ui.doInit(vaadinRequestMock, 1);
    factory = mock(VaadinUriResolverFactory.class);
    VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
    Mockito.when(factory.getUriResolver(any())).thenReturn(vaadinUriResolver);
    Mockito.when(vaadinUriResolver.resolveVaadinUri(anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);
    doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(any(), anyString());
    session.setAttribute(VaadinUriResolverFactory.class, factory);
    VaadinSession.setCurrent(session);
    return ui;
}
Also used : MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Matchers.anyString(org.mockito.Matchers.anyString) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletContext(javax.servlet.ServletContext) RouterInterface(com.vaadin.flow.router.RouterInterface) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver)

Example 3 with RouterInterface

use of com.vaadin.flow.router.RouterInterface in project flow by vaadin.

the class InternalRedirectHandler method handle.

@Override
public int handle(NavigationEvent event) {
    UI ui = event.getUI();
    RouterInterface router = event.getSource();
    ui.getPage().getHistory().replaceState(null, target);
    return router.navigate(ui, target, event.getTrigger());
}
Also used : UI(com.vaadin.flow.component.UI) RouterInterface(com.vaadin.flow.router.RouterInterface)

Example 4 with RouterInterface

use of com.vaadin.flow.router.RouterInterface in project flow by vaadin.

the class UIInternals method setSession.

/**
 * Sets the session to which the related UI is assigned.
 * <p>
 * This method is for internal use by the framework. To explicitly close a
 * UI, see {@link UI#close()}.
 *
 * @param session
 *            the session to set
 *
 * @throws IllegalStateException
 *             if the session has already been set
 *
 * @see #getSession()
 */
public void setSession(VaadinSession session) {
    if (session == null && this.session == null) {
        throw new IllegalStateException("Session should never be set to null when UI.session is already null");
    } else if (session != null && this.session != null) {
        throw new IllegalStateException("Session has already been set. Old session: " + getSessionDetails(this.session) + ". New session: " + getSessionDetails(session) + ".");
    } else {
        if (session == null) {
            try {
                ComponentUtil.onComponentDetach(ui);
            } catch (Exception e) {
                getLogger().warn("Error while detaching UI from session", e);
            }
            // Disable push when the UI is detached. Otherwise the
            // push connection and possibly VaadinSession will live on.
            ui.getPushConfiguration().setPushMode(PushMode.DISABLED);
            setPushConnection(null);
        }
        this.session = session;
    }
    if (session != null) {
        VaadinService service = getSession().getService();
        if (service != null) {
            // Allow null service to simplify testing mocks
            RouterInterface serviceRouter = service.getRouter();
            if (serviceRouter != null && serviceRouter.getConfiguration().isConfigured()) {
                router = serviceRouter;
            }
        }
        ComponentUtil.onComponentAttach(ui, true);
    }
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) RouterInterface(com.vaadin.flow.router.RouterInterface)

Aggregations

RouterInterface (com.vaadin.flow.router.RouterInterface)4 UI (com.vaadin.flow.component.UI)2 VaadinService (com.vaadin.flow.server.VaadinService)2 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)1 DependencyFilter (com.vaadin.flow.server.DependencyFilter)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinServlet (com.vaadin.flow.server.VaadinServlet)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 VaadinUriResolverFactory (com.vaadin.flow.server.VaadinUriResolverFactory)1 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)1 SpringVaadinSession (com.vaadin.flow.spring.SpringVaadinSession)1 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Matchers.anyString (org.mockito.Matchers.anyString)1