use of com.vaadin.flow.component.page.ExtendedClientDetails in project flow by vaadin.
the class VaadinRouteScopeTest method refresh_uiWithTheSameWindowName_beanInScopeIsDestroyedAfterRefresh.
@Test
public void refresh_uiWithTheSameWindowName_beanInScopeIsDestroyedAfterRefresh() {
UI ui = mockUI();
UI anotherUI = makeAnotherUI(ui);
ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
Mockito.when(details.getWindowName()).thenReturn("bar");
ui.getInternals().setExtendedClientDetails(details);
anotherUI.getInternals().setExtendedClientDetails(details);
ui.getSession().addUI(ui);
ui.getSession().addUI(anotherUI);
mockServletContext(ui);
VaadinRouteScope scope = initScope(ui);
AtomicInteger count = new AtomicInteger();
scope.registerDestructionCallback("foo", () -> count.getAndIncrement());
scope.uiInit(new UIInitEvent(ui, ui.getSession().getService()));
navigateTo(ui, new NavigationTarget());
putObjectIntoScope(scope);
// close the first UI
ui.getSession().removeUI(ui);
// the bean is not removed since there is a "preserved" UI
Assert.assertEquals(0, count.get());
UI.setCurrent(anotherUI);
scope = initScope(anotherUI);
// the bean is not removed since there is a "preserved" UI
Assert.assertEquals(0, count.get());
navigateTo(anotherUI, new AnotherNavigationTarget());
// the bean is removed since navigation away from it's owner navigation
// target
Assert.assertEquals(1, count.get());
}
use of com.vaadin.flow.component.page.ExtendedClientDetails in project flow by vaadin.
the class AbstractNavigationStateRenderer method setPreservedChain.
/**
* Invoke this method with the chain that needs to be preserved after
* {@link #handle(NavigationEvent)} method created it.
*/
private void setPreservedChain(ArrayList<HasElement> chain, NavigationEvent event) {
final Location location = event.getLocation();
final UI ui = event.getUI();
final VaadinSession session = ui.getSession();
final ExtendedClientDetails extendedClientDetails = ui.getInternals().getExtendedClientDetails();
if (extendedClientDetails == null) {
// We need first to retrieve the window name in order to cache the
// component chain for later potential refreshes.
ui.getPage().retrieveExtendedClientDetails(details -> setPreservedChain(session, details.getWindowName(), location, chain));
} else {
final String windowName = extendedClientDetails.getWindowName();
setPreservedChain(session, windowName, location, chain);
}
}
use of com.vaadin.flow.component.page.ExtendedClientDetails 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.component.page.ExtendedClientDetails 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());
}
use of com.vaadin.flow.component.page.ExtendedClientDetails 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());
}
Aggregations