Search in sources :

Example 1 with ExtendedClientDetails

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());
}
Also used : UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) UIInitEvent(com.vaadin.flow.server.UIInitEvent) Test(org.junit.Test)

Example 2 with ExtendedClientDetails

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);
    }
}
Also used : UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Location(com.vaadin.flow.router.Location)

Example 3 with ExtendedClientDetails

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")));
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) 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) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 4 with ExtendedClientDetails

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());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Router(com.vaadin.flow.router.Router) QueryParameters(com.vaadin.flow.router.QueryParameters) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 5 with ExtendedClientDetails

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());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Router(com.vaadin.flow.router.Router) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) 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)

Aggregations

ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)8 Test (org.junit.Test)7 Location (com.vaadin.flow.router.Location)5 NavigationEvent (com.vaadin.flow.router.NavigationEvent)4 Router (com.vaadin.flow.router.Router)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)4 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)4 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)4 MockUI (com.vaadin.tests.util.MockUI)4 UI (com.vaadin.flow.component.UI)3 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 UIInitEvent (com.vaadin.flow.server.UIInitEvent)2 Component (com.vaadin.flow.component.Component)1 HasElement (com.vaadin.flow.component.HasElement)1 Element (com.vaadin.flow.dom.Element)1 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)1 QueryParameters (com.vaadin.flow.router.QueryParameters)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1