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));
}
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;
}
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);
}
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);
}
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();
}
}
Aggregations