Search in sources :

Example 11 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent 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)

Example 12 with NavigationEvent

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

the class NavigationStateRendererTest method handle_preserveOnRefreshAndWindowNameNotKnown_clientSideCallTriggered.

@Test
public void handle_preserveOnRefreshAndWindowNameNotKnown_clientSideCallTriggered() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    // given a NavigationStateRenderer mapping to PreservedView
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
    // given the session has a cache of something at this location
    AbstractNavigationStateRenderer.setPreservedChain(session, "", new Location("preserved"), new ArrayList<>(Arrays.asList(Mockito.mock(Component.class))));
    // given a UI that contain no window name with an instrumented Page
    // that records JS invocations
    AtomicBoolean jsInvoked = new AtomicBoolean(false);
    MockUI ui = new MockUI(session) {

        final Page page = new Page(this) {

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

        @Override
        public Page getPage() {
            return page;
        }
    };
    // when a navigation event reaches the renderer
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui, NavigationTrigger.PAGE_LOAD));
    // then client-side JS was invoked
    Assert.assertTrue("Expected JS invocation", jsInvoked.get());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) Serializable(java.io.Serializable) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Router(com.vaadin.flow.router.Router) Page(com.vaadin.flow.component.page.Page) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 13 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent 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 14 with NavigationEvent

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

the class ErrorStateRendererTest method handle_openNPEView_infiniteReroute_noStackOverflow_throws.

@Test(expected = ExceptionsTrace.class)
public void handle_openNPEView_infiniteReroute_noStackOverflow_throws() {
    UI ui = configureMocks();
    NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(InfiniteLoopNPEView.class).build();
    NavigationStateRenderer renderer = new NavigationStateRenderer(state);
    RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(InfiniteLoopNPEView.class);
    ((ApplicationRouteRegistry) ui.getInternals().getRouter().getRegistry()).setErrorNavigationTargets(Collections.singleton(InfiniteLoopErrorTarget.class));
    NavigationEvent event = new NavigationEvent(ui.getInternals().getRouter(), new Location("npe"), ui, NavigationTrigger.CLIENT_SIDE);
    // event should route to ErrorTarget whose layout forwards to NPEView
    // which reroute to ErrorTarget and this is an infinite loop
    renderer.handle(event);
    JsonObject routerLinkState = Json.createObject();
    routerLinkState.put("href", "router_link");
    routerLinkState.put("scrollPositionX", 0d);
    routerLinkState.put("scrollPositionY", 0d);
    event = new NavigationEvent(ui.getInternals().getRouter(), new Location("npe"), ui, NavigationTrigger.ROUTER_LINK, routerLinkState, false);
    // event should route to ErrorTarget whose layout forwards to NPEView
    // which reroute to ErrorTarget and this is an infinite loop
    renderer.handle(event);
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationState(com.vaadin.flow.router.NavigationState) JsonObject(elemental.json.JsonObject) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 15 with NavigationEvent

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

the class ViewRendererTest method testViewInstantiationCustomization.

@Test
public void testViewInstantiationCustomization() {
    // override default implementation of reusing the views if possible
    ViewRenderer renderer = new TestViewRenderer(TestView.class, ParentView.class) {

        @Override
        protected <T extends View> T getView(Class<T> viewType, NavigationEvent event) {
            // always return a new view
            return ReflectTools.createInstance(viewType);
        }
    };
    renderer.handle(dummyEvent);
    View view1 = dummyEvent.getUI().getInternals().getActiveViewChain().get(0);
    View parentView1 = dummyEvent.getUI().getInternals().getActiveViewChain().get(1);
    renderer.handle(dummyEvent);
    View view2 = dummyEvent.getUI().getInternals().getActiveViewChain().get(0);
    View parentView2 = dummyEvent.getUI().getInternals().getActiveViewChain().get(1);
    Assert.assertNotSame(view1, view2);
    Assert.assertNotSame(parentView1, parentView2);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) ViewRenderer(com.vaadin.flow.router.legacy.ViewRenderer) TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Test(org.junit.Test)

Aggregations

NavigationEvent (com.vaadin.flow.router.NavigationEvent)25 Location (com.vaadin.flow.router.Location)19 Test (org.junit.Test)15 Router (com.vaadin.flow.router.Router)11 UI (com.vaadin.flow.component.UI)10 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)10 MockUI (com.vaadin.tests.util.MockUI)10 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)9 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)8 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)7 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)7 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)7 Component (com.vaadin.flow.component.Component)6 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)6 HasElement (com.vaadin.flow.component.HasElement)5 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)5 NavigationState (com.vaadin.flow.router.NavigationState)5 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)5 Page (com.vaadin.flow.component.page.Page)4 Element (com.vaadin.flow.dom.Element)4