Search in sources :

Example 21 with NavigationEvent

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

the class NavigationStateRendererTest method handle_preserveOnRefresh_otherUIChildrenAreMoved.

@Test
public void handle_preserveOnRefresh_otherUIChildrenAreMoved() {
    // 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 PreservedView
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
    // given the session has a cache of PreservedView at this location
    final PreservedView view = new PreservedView();
    AbstractNavigationStateRenderer.setPreservedChain(session, "ROOT.123", new Location("preserved"), new ArrayList<>(Arrays.asList(view)));
    // given an old UI that contains the component and an extra element
    MockUI ui0 = new MockUI(session);
    ui0.add(view);
    final Element otherElement = new Element("div");
    ui0.getElement().insertChild(1, otherElement);
    // given a new UI after a refresh with the same window name
    MockUI ui1 = new MockUI(session);
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("ROOT.123");
    ui1.getInternals().setExtendedClientDetails(details);
    // when a navigation event reaches the renderer
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui1, NavigationTrigger.PAGE_LOAD));
    // then both the view element and the other element are expected to be
    // transferred from the previous UI to the new UI
    final Set<Element> uiChildren = ui1.getElement().getChildren().collect(Collectors.toSet());
    Assert.assertEquals(2, uiChildren.size());
    Assert.assertTrue("Component element expected transferred", uiChildren.contains(view.getElement()));
    Assert.assertTrue("Extra element expected transferred", uiChildren.contains(otherElement));
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Element(com.vaadin.flow.dom.Element) HasElement(com.vaadin.flow.component.HasElement) 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) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 22 with NavigationEvent

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

the class ViewAccessCheckerTest method setupRequest.

private Result setupRequest(Class navigationTarget, User user, boolean productionMode) {
    CurrentInstance.clearAll();
    Principal principal;
    String[] roles;
    if (user == User.USER_NO_ROLES) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[0];
    } else if (user == User.NORMAL_USER) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[] { "user" };
    } else if (user == User.ADMIN) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[] { "admin" };
    } else {
        principal = null;
        roles = new String[0];
    }
    VaadinServletRequest vaadinServletRequest = Mockito.mock(VaadinServletRequest.class);
    HttpServletRequest httpServletRequest = AccessAnnotationCheckerTest.createRequest(principal, roles);
    Mockito.when(vaadinServletRequest.getHttpServletRequest()).thenReturn(httpServletRequest);
    CurrentInstance.set(VaadinRequest.class, vaadinServletRequest);
    Router router = Mockito.mock(Router.class);
    UI ui = Mockito.mock(UI.class);
    Page page = Mockito.mock(Page.class);
    Mockito.when(ui.getPage()).thenReturn(page);
    VaadinSession vaadinSession = Mockito.mock(VaadinSession.class);
    Mockito.when(ui.getSession()).thenReturn(vaadinSession);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(vaadinSession.getConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.isProductionMode()).thenReturn(productionMode);
    UIInternals uiInternals = Mockito.mock(UIInternals.class);
    Mockito.when(ui.getInternals()).thenReturn(uiInternals);
    Mockito.when(uiInternals.getRouter()).thenReturn(router);
    Mockito.when(router.getErrorNavigationTarget(Mockito.any())).thenAnswer(invocation -> {
        Class<?> exceptionClass = invocation.getArguments()[0].getClass();
        if (exceptionClass == NotFoundException.class) {
            return Optional.of(new ErrorTargetEntry(RouteNotFoundError.class, NotFoundException.class));
        } else {
            return Optional.empty();
        }
    });
    Location location = new Location(getRoute(navigationTarget));
    NavigationEvent navigationEvent = new NavigationEvent(router, location, ui, NavigationTrigger.ROUTER_LINK);
    BeforeEnterEvent event = new BeforeEnterEvent(navigationEvent, navigationTarget, new ArrayList<>());
    RouteRegistry routeRegistry = Mockito.mock(RouteRegistry.class);
    Mockito.when(router.getRegistry()).thenReturn(routeRegistry);
    Mockito.when(routeRegistry.getNavigationTarget(Mockito.anyString())).thenAnswer(invocation -> {
        String url = (String) invocation.getArguments()[0];
        if (location.getPath().equals(url)) {
            return Optional.of(navigationTarget);
        } else {
            return Optional.empty();
        }
    });
    HttpSession session = Mockito.mock(HttpSession.class);
    Map<String, Object> sessionAttributes = new HashMap<>();
    Mockito.when(httpServletRequest.getSession()).thenReturn(session);
    Mockito.doAnswer(invocation -> {
        String key = (String) invocation.getArguments()[0];
        Object value = invocation.getArguments()[1];
        sessionAttributes.put(key, value);
        return null;
    }).when(session).setAttribute(Mockito.anyString(), Mockito.any());
    Result info = new Result();
    info.event = event;
    info.sessionAttributes = sessionAttributes;
    Mockito.doAnswer(invocation -> {
        info.redirectUsingPageLocation = (String) invocation.getArguments()[0];
        return null;
    }).when(page).setLocation(Mockito.anyString());
    return info;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) RouteRegistry(com.vaadin.flow.server.RouteRegistry) HashMap(java.util.HashMap) NotFoundException(com.vaadin.flow.router.NotFoundException) Page(com.vaadin.flow.component.page.Page) HttpServletRequest(javax.servlet.http.HttpServletRequest) UI(com.vaadin.flow.component.UI) NavigationEvent(com.vaadin.flow.router.NavigationEvent) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Router(com.vaadin.flow.router.Router) UIInternals(com.vaadin.flow.component.internal.UIInternals) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) RouteNotFoundError(com.vaadin.flow.router.RouteNotFoundError) Principal(java.security.Principal) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Location(com.vaadin.flow.router.Location)

Example 23 with NavigationEvent

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

the class AbstractNavigationStateRenderer method reroute.

private int reroute(NavigationEvent event, BeforeEvent beforeNavigation) {
    NavigationHandler handler = beforeNavigation.getRerouteTarget();
    NavigationEvent newNavigationEvent = getNavigationEvent(event, beforeNavigation);
    return handler.handle(newNavigationEvent);
}
Also used : ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) NavigationHandler(com.vaadin.flow.router.NavigationHandler)

Example 24 with NavigationEvent

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

the class AbstractNavigationStateRenderer method getNavigationEvent.

private NavigationEvent getNavigationEvent(NavigationEvent event, BeforeEvent beforeNavigation) {
    if (beforeNavigation.hasErrorParameter()) {
        ErrorParameter<?> errorParameter = beforeNavigation.getErrorParameter();
        return new ErrorNavigationEvent(event.getSource(), event.getLocation(), event.getUI(), NavigationTrigger.PROGRAMMATIC, errorParameter);
    }
    String url;
    final boolean isForward = beforeNavigation.hasForwardTarget();
    if (isForward) {
        url = beforeNavigation.getForwardUrl();
    } else {
        url = beforeNavigation.getRerouteUrl();
    }
    if (url == null) {
        final String redirectType;
        final Class<? extends Component> redirectTarget;
        final RouteParameters redirectParameters;
        if (isForward) {
            redirectType = "forward";
            redirectTarget = beforeNavigation.getForwardTargetType();
            redirectParameters = beforeNavigation.getForwardTargetRouteParameters();
        } else {
            redirectType = "reroute";
            redirectTarget = beforeNavigation.getRerouteTargetType();
            redirectParameters = beforeNavigation.getRerouteTargetRouteParameters();
        }
        throw new IllegalStateException(String.format("Attempting to %s to unresolved location target %s with route parameters %s", redirectType, redirectTarget, redirectParameters));
    }
    Location location = new Location(url, event.getLocation().getQueryParameters());
    return new NavigationEvent(event.getSource(), location, event.getUI(), NavigationTrigger.PROGRAMMATIC, null, true);
}
Also used : ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouteParameters(com.vaadin.flow.router.RouteParameters) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) Location(com.vaadin.flow.router.Location)

Example 25 with NavigationEvent

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

the class JavaScriptBootstrapUI method handleNavigation.

private void handleNavigation(Location location, NavigationState navigationState, NavigationTrigger trigger) {
    try {
        NavigationEvent navigationEvent = new NavigationEvent(getInternals().getRouter(), location, this, trigger);
        JavaScriptNavigationStateRenderer clientNavigationStateRenderer = new JavaScriptNavigationStateRenderer(navigationState);
        clientNavigationStateRenderer.handle(navigationEvent);
        forwardToClientUrl = clientNavigationStateRenderer.getClientForwardRoute();
        adjustPageTitle();
    } catch (Exception exception) {
        handleExceptionNavigation(location, exception);
    } finally {
        getInternals().clearLastHandledNavigation();
    }
}
Also used : ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) NotFoundException(com.vaadin.flow.router.NotFoundException)

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