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