Search in sources :

Example 6 with ExtendedClientDetails

use of com.vaadin.flow.component.page.ExtendedClientDetails 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));
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Element(com.vaadin.flow.dom.Element) HasElement(com.vaadin.flow.component.HasElement) 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) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 7 with ExtendedClientDetails

use of com.vaadin.flow.component.page.ExtendedClientDetails in project flow by vaadin.

the class RouterTest method event_listeners_are_invoked_starting_with_parent_component_when_preserved_on_refresh.

// #4595
@Test
public void event_listeners_are_invoked_starting_with_parent_component_when_preserved_on_refresh() throws InvalidRouteConfigurationException {
    ProcessEventsBase.clear();
    // This is null by default.
    ExtendedClientDetails previousClientDetails = ui.getInternals().getExtendedClientDetails();
    // Used with PreserveOnRefresh.
    ExtendedClientDetails clientDetails = Mockito.mock(ExtendedClientDetails.class);
    ui.getInternals().setExtendedClientDetails(clientDetails);
    Mockito.when(clientDetails.getWindowName()).thenReturn("mock");
    setNavigationTargets(ProcessEventsFruit.class);
    router.navigate(ui, new Location("event/fruit"), NavigationTrigger.PROGRAMMATIC);
    ProcessEventsBase.clear();
    router.navigate(ui, new Location("event/fruit"), NavigationTrigger.PROGRAMMATIC);
    assertExistingChainEventOrder(getProcessEventsBranchChainNames("ProcessEventsFruit"));
    // Set back the previous client details.
    ui.getInternals().setExtendedClientDetails(previousClientDetails);
}
Also used : ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Test(org.junit.Test)

Example 8 with ExtendedClientDetails

use of com.vaadin.flow.component.page.ExtendedClientDetails in project flow by vaadin.

the class VaadinRouteScopeTest method detachUI_uiWithDifferentWindowName_beanInScopeIsDestroyedwhenUIIsDetached.

@Test
public void detachUI_uiWithDifferentWindowName_beanInScopeIsDestroyedwhenUIIsDetached() {
    UI ui = mockUI();
    UI anotherUI = makeAnotherUI(ui);
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("bar");
    ui.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 removed since there is no UI with the window name "bar"
    // present.
    Assert.assertEquals(1, count.get());
    count.set(0);
    UI.setCurrent(anotherUI);
    scope = initScope(anotherUI);
    navigateTo(anotherUI, new AnotherNavigationTarget());
    // the bean is not removed since it's already has been removed when the
    // first UI is detached.
    Assert.assertEquals(0, 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)

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