Search in sources :

Example 6 with AlwaysLockedVaadinSession

use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.

the class NavigationStateRendererTest method handle_preserveOnRefreshView_routerLayoutIsPreserved_oldUiIsClosed.

@Test
public void handle_preserveOnRefreshView_routerLayoutIsPreserved_oldUiIsClosed() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    session.setConfiguration(new MockDeploymentConfiguration());
    // given a NavigationStateRenderer mapping to PreservedNestedView
    Router router = session.getService().getRouter();
    NavigationStateRenderer renderer = new NavigationStateRenderer(new NavigationStateBuilder(router).withTarget(PreservedNestedView.class).withPath("preservedNested").build());
    router.getRegistry().setRoute("preservedNested", PreservedNestedView.class, Arrays.asList(PreservedLayout.class));
    // given the session has a cache of PreservedNestedView at this location
    final PreservedLayout layout = new PreservedLayout();
    final PreservedNestedView nestedView = new PreservedNestedView();
    MockUI previousUi = new MockUI(session);
    previousUi.add(nestedView);
    AbstractNavigationStateRenderer.setPreservedChain(session, "ROOT.123", new Location("preservedNested"), new ArrayList<>(Arrays.asList(nestedView, layout)));
    // given a UI that contain a window name ROOT.123
    MockUI ui = new MockUI(session);
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("ROOT.123");
    ui.getInternals().setExtendedClientDetails(details);
    // when a navigation event reaches the renderer
    renderer.handle(new NavigationEvent(router, new Location("preservedNested"), ui, NavigationTrigger.PAGE_LOAD));
    // then the view and the router layout are preserved
    Assert.assertEquals("Expected same view", nestedView, ui.getInternals().getActiveRouterTargetsChain().get(0));
    Assert.assertEquals("Expected same router layout", layout, ui.getInternals().getActiveRouterTargetsChain().get(1));
    Assert.assertTrue(previousUi.isClosing());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Router(com.vaadin.flow.router.Router) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 7 with AlwaysLockedVaadinSession

use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.

the class WebComponentWrapperTest method constructWebComponentUI.

private static WebComponentUI constructWebComponentUI(Element wrapperElement) {
    WebComponentUI ui = mock(WebComponentUI.class);
    when(ui.getUI()).thenReturn(Optional.of(ui));
    Element body = new Element("body");
    when(ui.getElement()).thenReturn(body);
    UIInternals internals = new UIInternals(ui);
    internals.setSession(new AlwaysLockedVaadinSession(mock(VaadinService.class)));
    when(ui.getInternals()).thenReturn(internals);
    Component parent = new Parent();
    parent.getElement().appendVirtualChild(wrapperElement);
    VaadinSession session = mock(VaadinSession.class);
    DeploymentConfiguration configuration = mock(DeploymentConfiguration.class);
    when(ui.getSession()).thenReturn(session);
    when(session.getConfiguration()).thenReturn(configuration);
    when(configuration.getWebComponentDisconnect()).thenReturn(1);
    return ui;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Element(com.vaadin.flow.dom.Element) UIInternals(com.vaadin.flow.component.internal.UIInternals) Component(com.vaadin.flow.component.Component) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 8 with AlwaysLockedVaadinSession

use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.

the class UITest method initUI.

private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException {
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    String pathInfo;
    if (initialLocation.isEmpty()) {
        pathInfo = null;
    } else {
        Assert.assertFalse(initialLocation.startsWith("/"));
        pathInfo = "/" + initialLocation;
    }
    Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
    VaadinService service = new MockVaadinServletService() {

        @Override
        public VaadinContext getContext() {
            return new MockVaadinContext();
        }
    };
    service.setCurrentInstances(request, response);
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(config.isProductionMode()).thenReturn(false);
    session.lock();
    session.setConfiguration(config);
    ui.getInternals().setSession(session);
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        Arrays.asList(RootNavigationTarget.class, FooBarNavigationTarget.class, Parameterized.class, FooBarParamNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
    });
    ui.doInit(request, 0);
    ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
    session.unlock();
    if (statusCodeCaptor != null) {
        Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    }
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 9 with AlwaysLockedVaadinSession

use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.

the class UIInternalsTest method init.

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    Mockito.when(ui.getUI()).thenReturn(Optional.of(ui));
    Element body = new Element("body");
    Mockito.when(ui.getElement()).thenReturn(body);
    internals = new UIInternals(ui);
    AlwaysLockedVaadinSession session = new AlwaysLockedVaadinSession(vaadinService);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(vaadinService.getContext()).thenReturn(context);
    Mockito.when(vaadinService.getInstantiator()).thenReturn(new DefaultInstantiator(vaadinService));
    internals.setSession(session);
    Mockito.when(ui.getSession()).thenReturn(session);
}
Also used : DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator) VaadinContext(com.vaadin.flow.server.VaadinContext) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Element(com.vaadin.flow.dom.Element) HasElement(com.vaadin.flow.component.HasElement) Before(org.junit.Before)

Example 10 with AlwaysLockedVaadinSession

use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.

the class InvalidUrlTest method initUI.

private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException, ServiceException {
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    String pathInfo;
    if (initialLocation.isEmpty()) {
        pathInfo = null;
    } else {
        Assert.assertFalse(initialLocation.startsWith("/"));
        pathInfo = "/" + initialLocation;
    }
    Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
    VaadinService service = new MockVaadinServletService() {

        @Override
        public VaadinContext getContext() {
            return new MockVaadinContext();
        }
    };
    service.setCurrentInstances(request, response);
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(config.isProductionMode()).thenReturn(false);
    session.lock();
    session.setConfiguration(config);
    ui.getInternals().setSession(session);
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getRouter().getRegistry());
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        Arrays.asList(UITest.RootNavigationTarget.class, UITest.FooBarNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
    });
    ui.doInit(request, 0);
    ui.getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
    session.unlock();
    if (statusCodeCaptor != null) {
        Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    }
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Aggregations

AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)16 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)14 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)11 MockUI (com.vaadin.tests.util.MockUI)11 Location (com.vaadin.flow.router.Location)8 NavigationEvent (com.vaadin.flow.router.NavigationEvent)8 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)8 Test (org.junit.Test)8 Router (com.vaadin.flow.router.Router)7 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)6 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)6 Component (com.vaadin.flow.component.Component)5 Element (com.vaadin.flow.dom.Element)5 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 HasElement (com.vaadin.flow.component.HasElement)4 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)4 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)4 VaadinService (com.vaadin.flow.server.VaadinService)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 UI (com.vaadin.flow.component.UI)3