use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.
the class AbstractNavigationStateRenderer method getRouteTarget.
/**
* Gets the component instance to use for the given type and the
* corresponding navigation event.
* <p>
* Override this method to control the creation of view instances.
* <p>
* By default always creates new instances.
*
* @param <T>
* the route target type
* @param routeTargetType
* the class of the route target component
* @param event
* the navigation event that uses the route target
* @return an instance of the route target component
*/
@SuppressWarnings("unchecked")
static <// Non-private for testing purposes
T extends HasElement> T getRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
UI ui = event.getUI();
Optional<HasElement> currentInstance = ui.getInternals().getActiveRouterTargetsChain().stream().filter(component -> component.getClass().equals(routeTargetType)).findAny();
return (T) currentInstance.orElseGet(() -> Instantiator.get(ui).createRouteTarget(routeTargetType, event));
}
use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.
the class BootstrapUtils method createInitialPageSettingsObject.
private static InitialPageSettings createInitialPageSettingsObject(BootstrapHandler.BootstrapContext context) {
UI ui = context.getUI();
WebBrowser browser = context.getSession().getBrowser();
Router router = ui.getInternals().getRouter();
NavigationEvent navigationEvent = new NavigationEvent(router, context.getRoute(), ui, NavigationTrigger.PAGE_LOAD);
List<HasElement> components = ui.getChildren().map(component -> (HasElement) component).collect(Collectors.toList());
AfterNavigationEvent afterNavigationEvent = new AfterNavigationEvent(new LocationChangeEvent(navigationEvent.getSource(), navigationEvent.getUI(), navigationEvent.getTrigger(), navigationEvent.getLocation(), components));
return new InitialPageSettings(context.getRequest(), ui, afterNavigationEvent, browser);
}
use of com.vaadin.flow.router.NavigationEvent 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());
}
use of com.vaadin.flow.router.NavigationEvent 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")));
}
use of com.vaadin.flow.router.NavigationEvent 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());
}
Aggregations