Search in sources :

Example 1 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 2 with AlwaysLockedVaadinSession

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

the class NavigationStateRendererTest method handle_variousInputs_checkPushStateShouldBeCalledOrNot.

@Test
public // - navigation trigger is PAGE_LOAD, HISTORY, or PROGRAMMATIC
void handle_variousInputs_checkPushStateShouldBeCalledOrNot() {
    // 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 RegularView
    new NavigationStateBuilder(router).withTarget(RegularView.class).build();
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(RegularView.class));
    // given a UI with an instrumented Page that records
    // getHistory().pushState calls
    AtomicBoolean pushStateCalled = new AtomicBoolean(false);
    List<Location> pushStateLocations = new ArrayList<>();
    MockUI ui = new MockUI(session) {

        final Page page = new Page(this) {

            final History history = new History(getUI().get()) {

                @Override
                public void pushState(JsonValue state, Location location) {
                    pushStateCalled.set(true);
                    pushStateLocations.add(location);
                }
            };

            @Override
            public History getHistory() {
                return history;
            }
        };

        @Override
        public Page getPage() {
            return page;
        }
    };
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE, null, true));
    Assert.assertFalse("No pushState invocation is expected when forwardTo is true.", pushStateCalled.get());
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.PROGRAMMATIC));
    Assert.assertFalse("No pushState invocation is expected when navigation trigger is PROGRAMMATIC.", pushStateCalled.get());
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.HISTORY));
    Assert.assertFalse("No pushState invocation is expected when navigation trigger is HISTORY.", pushStateCalled.get());
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.PAGE_LOAD));
    Assert.assertFalse("No pushState invocation is expected when navigation trigger is PAGE_LOAD.", pushStateCalled.get());
    pushStateCalled.set(false);
    pushStateLocations.clear();
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE));
    Assert.assertTrue("pushState invocation is expected.", pushStateCalled.get());
    Assert.assertTrue(pushStateLocations.stream().anyMatch(location -> location.getPath().equals("regular")));
    pushStateCalled.set(false);
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE));
    Assert.assertFalse("No pushState invocation is expected when navigating to the current location.", pushStateCalled.get());
}
Also used : Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) Json(elemental.json.Json) Router(com.vaadin.flow.router.Router) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Route(com.vaadin.flow.router.Route) History(com.vaadin.flow.component.page.History) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) JsonValue(elemental.json.JsonValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) NotThreadSafe(net.jcip.annotations.NotThreadSafe) List(java.util.List) NavigationState(com.vaadin.flow.router.NavigationState) ServiceException(com.vaadin.flow.server.ServiceException) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ArrayList(java.util.ArrayList) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Tag(com.vaadin.flow.component.Tag) Location(com.vaadin.flow.router.Location) MockInstantiator(com.vaadin.flow.server.MockInstantiator) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) QueryParameters(com.vaadin.flow.router.QueryParameters) Text(com.vaadin.flow.component.Text) Page(com.vaadin.flow.component.page.Page) RouterLayout(com.vaadin.flow.router.RouterLayout) HasElement(com.vaadin.flow.component.HasElement) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Test(org.junit.Test) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) PreserveOnRefresh(com.vaadin.flow.router.PreserveOnRefresh) JsonObject(elemental.json.JsonObject) Assert(org.junit.Assert) Collections(java.util.Collections) NavigationEvent(com.vaadin.flow.router.NavigationEvent) ParentLayout(com.vaadin.flow.router.ParentLayout) NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue) Router(com.vaadin.flow.router.Router) Page(com.vaadin.flow.component.page.Page) History(com.vaadin.flow.component.page.History) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 3 with AlwaysLockedVaadinSession

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

the class NavigationStateRendererTest method handle_preserveOnRefreshAndWindowNameKnown_componentIsCachedRetrievedAndFlushed.

@Test
public void handle_preserveOnRefreshAndWindowNameKnown_componentIsCachedRetrievedAndFlushed() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    session.setConfiguration(new MockDeploymentConfiguration());
    // given a UI that contain a window name ROOT.123
    MockUI ui1 = new MockUI(session);
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("ROOT.123");
    ui1.getInternals().setExtendedClientDetails(details);
    // given a NavigationStateRenderer mapping to PreservedView
    NavigationStateRenderer renderer1 = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
    // when a navigation event reaches the renderer
    renderer1.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui1, NavigationTrigger.PAGE_LOAD));
    // then the session has a cached record of the view
    Assert.assertTrue("Session expected to have cached view", AbstractNavigationStateRenderer.getPreservedChain(session, "ROOT.123", new Location("preserved")).isPresent());
    // given the recently instantiated view
    final Component view = (Component) ui1.getInternals().getActiveRouterTargetsChain().get(0);
    // given a new UI with the same window name
    MockUI ui2 = new MockUI(session);
    ui2.getInternals().setExtendedClientDetails(details);
    // given a new NavigationStateRenderer mapping to PreservedView
    NavigationStateRenderer renderer2 = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
    // when another navigation targets the same location
    renderer2.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui2, NavigationTrigger.PAGE_LOAD));
    // then the same view is routed to
    Assert.assertEquals("Expected same view", view, ui1.getInternals().getActiveRouterTargetsChain().get(0));
    // given yet another new UI with the same window name
    MockUI ui3 = new MockUI(session);
    ui3.getInternals().setExtendedClientDetails(details);
    // given a new NavigationStateRenderer mapping to another location
    NavigationStateRenderer renderer3 = new NavigationStateRenderer(navigationStateFromTarget(RegularView.class));
    // when a navigation event targets that other location
    renderer3.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui2, NavigationTrigger.PAGE_LOAD));
    // then session no longer has a cached record at location "preserved"
    Assert.assertFalse("Session expected to not have cached view", AbstractNavigationStateRenderer.hasPreservedChainOfLocation(session, new Location("preserved")));
}
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) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) 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) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 4 with AlwaysLockedVaadinSession

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

the class NavigationStateRendererTest method handle_preserveOnRefresh_sameUI_uiIsNotClosed_childrenAreNotRemoved.

@Test
public void handle_preserveOnRefresh_sameUI_uiIsNotClosed_childrenAreNotRemoved() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    // the path is the same, location params will be different
    String path = "foo";
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    session.setConfiguration(new MockDeploymentConfiguration());
    // given a NavigationStateRenderer mapping to PreservedView
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
    // given the session has a cache of PreservedView at this location
    final PreservedView view = new PreservedView();
    MockUI ui = new MockUI(session);
    ui.add(view);
    AbstractNavigationStateRenderer.setPreservedChain(session, "ROOT.123", new Location(path, new QueryParameters(Collections.singletonMap("a", Collections.emptyList()))), new ArrayList<>(Arrays.asList(view)));
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("ROOT.123");
    ui.getInternals().setExtendedClientDetails(details);
    AtomicInteger count = new AtomicInteger();
    view.addDetachListener(event -> count.getAndIncrement());
    NavigationEvent event = new NavigationEvent(new Router(new TestRouteRegistry()), new Location(path, new QueryParameters(Collections.singletonMap("b", Collections.emptyList()))), ui, NavigationTrigger.ROUTER_LINK, Json.createObject(), false);
    renderer.handle(event);
    Assert.assertFalse(ui.isClosing());
    Assert.assertEquals(0, count.get());
}
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) QueryParameters(com.vaadin.flow.router.QueryParameters) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 5 with AlwaysLockedVaadinSession

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

the class NavigationStateRendererTest method handle_RouterLinkTrigger_scrollPositionHandlerAfterServerNavigationIsInvoked.

@Test
public void handle_RouterLinkTrigger_scrollPositionHandlerAfterServerNavigationIsInvoked() {
    // 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 RegularView
    new NavigationStateBuilder(router).withTarget(RegularView.class).build();
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(RegularView.class));
    // given a UI with an instrumented Page that records JS invocations
    AtomicBoolean jsInvoked = new AtomicBoolean(false);
    List<String> jsExpressions = new ArrayList<>();
    MockUI ui = new MockUI(session) {

        final Page page = new Page(this) {

            @Override
            public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
                jsInvoked.set(true);
                jsExpressions.add(expression);
                return super.executeJs(expression, params);
            }
        };

        @Override
        public Page getPage() {
            return page;
        }
    };
    JsonObject state = Json.createObject();
    state.put("href", "view/regular");
    state.put("scrollPositionX", 0.0);
    state.put("scrollPositionY", 0.0);
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui, NavigationTrigger.ROUTER_LINK, state, false));
    // then client-side JS was invoked
    Assert.assertTrue("Expected JS invocation", jsInvoked.get());
    Assert.assertTrue(jsExpressions.stream().anyMatch(expression -> expression.contains("scrollPositionHandlerAfterServerNavigation")));
}
Also used : Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) Json(elemental.json.Json) Router(com.vaadin.flow.router.Router) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Route(com.vaadin.flow.router.Route) History(com.vaadin.flow.component.page.History) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) JsonValue(elemental.json.JsonValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) NotThreadSafe(net.jcip.annotations.NotThreadSafe) List(java.util.List) NavigationState(com.vaadin.flow.router.NavigationState) ServiceException(com.vaadin.flow.server.ServiceException) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ArrayList(java.util.ArrayList) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Tag(com.vaadin.flow.component.Tag) Location(com.vaadin.flow.router.Location) MockInstantiator(com.vaadin.flow.server.MockInstantiator) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) QueryParameters(com.vaadin.flow.router.QueryParameters) Text(com.vaadin.flow.component.Text) Page(com.vaadin.flow.component.page.Page) RouterLayout(com.vaadin.flow.router.RouterLayout) HasElement(com.vaadin.flow.component.HasElement) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Test(org.junit.Test) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) PreserveOnRefresh(com.vaadin.flow.router.PreserveOnRefresh) JsonObject(elemental.json.JsonObject) Assert(org.junit.Assert) Collections(java.util.Collections) NavigationEvent(com.vaadin.flow.router.NavigationEvent) ParentLayout(com.vaadin.flow.router.ParentLayout) NavigationEvent(com.vaadin.flow.router.NavigationEvent) Serializable(java.io.Serializable) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject) Router(com.vaadin.flow.router.Router) Page(com.vaadin.flow.component.page.Page) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

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