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