use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.
the class WebComponentWrapperTest method constructWebComponentUI.
private static WebComponentUI constructWebComponentUI(Element wrapperElement) {
WebComponentUI ui = mock(WebComponentUI.class);
when(ui.getUI()).thenReturn(Optional.of(ui));
Element body = new Element("body");
when(ui.getElement()).thenReturn(body);
UIInternals internals = new UIInternals(ui);
internals.setSession(new AlwaysLockedVaadinSession(mock(VaadinService.class)));
when(ui.getInternals()).thenReturn(internals);
Component parent = new Parent();
parent.getElement().appendVirtualChild(wrapperElement);
VaadinSession session = mock(VaadinSession.class);
DeploymentConfiguration configuration = mock(DeploymentConfiguration.class);
when(ui.getSession()).thenReturn(session);
when(session.getConfiguration()).thenReturn(configuration);
when(configuration.getWebComponentDisconnect()).thenReturn(1);
return ui;
}
use of com.vaadin.tests.util.AlwaysLockedVaadinSession 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.tests.util.AlwaysLockedVaadinSession 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.tests.util.AlwaysLockedVaadinSession 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.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.
the class NavigationStateRendererTest method handle_RouterLinkTrigger_scrollPositionHandlerAfterServerNavigationIsInvoked.
@Test
public void handle_RouterLinkTrigger_scrollPositionHandlerAfterServerNavigationIsInvoked() {
// 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 JS invocations
AtomicBoolean jsInvoked = new AtomicBoolean(false);
List<String> jsExpressions = new ArrayList<>();
MockUI ui = new MockUI(session) {
final Page page = new Page(this) {
@Override
public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
jsInvoked.set(true);
jsExpressions.add(expression);
return super.executeJs(expression, params);
}
};
@Override
public Page getPage() {
return page;
}
};
JsonObject state = Json.createObject();
state.put("href", "view/regular");
state.put("scrollPositionX", 0.0);
state.put("scrollPositionY", 0.0);
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui, NavigationTrigger.ROUTER_LINK, state, false));
// then client-side JS was invoked
Assert.assertTrue("Expected JS invocation", jsInvoked.get());
Assert.assertTrue(jsExpressions.stream().anyMatch(expression -> expression.contains("scrollPositionHandlerAfterServerNavigation")));
}
Aggregations