use of com.vaadin.tests.util.AlwaysLockedVaadinSession 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());
}
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 UITest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException {
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
VaadinResponse response = Mockito.mock(VaadinResponse.class);
String pathInfo;
if (initialLocation.isEmpty()) {
pathInfo = null;
} else {
Assert.assertFalse(initialLocation.startsWith("/"));
pathInfo = "/" + initialLocation;
}
Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
VaadinService service = new MockVaadinServletService() {
@Override
public VaadinContext getContext() {
return new MockVaadinContext();
}
};
service.setCurrentInstances(request, response);
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
session.lock();
session.setConfiguration(config);
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
Arrays.asList(RootNavigationTarget.class, FooBarNavigationTarget.class, Parameterized.class, FooBarParamNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
});
ui.doInit(request, 0);
ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
session.unlock();
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
}
use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.
the class UIInternalsTest method init.
@Before
public void init() {
MockitoAnnotations.initMocks(this);
Mockito.when(ui.getUI()).thenReturn(Optional.of(ui));
Element body = new Element("body");
Mockito.when(ui.getElement()).thenReturn(body);
internals = new UIInternals(ui);
AlwaysLockedVaadinSession session = new AlwaysLockedVaadinSession(vaadinService);
VaadinContext context = Mockito.mock(VaadinContext.class);
Mockito.when(vaadinService.getContext()).thenReturn(context);
Mockito.when(vaadinService.getInstantiator()).thenReturn(new DefaultInstantiator(vaadinService));
internals.setSession(session);
Mockito.when(ui.getSession()).thenReturn(session);
}
use of com.vaadin.tests.util.AlwaysLockedVaadinSession in project flow by vaadin.
the class InvalidUrlTest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException, ServiceException {
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
VaadinResponse response = Mockito.mock(VaadinResponse.class);
String pathInfo;
if (initialLocation.isEmpty()) {
pathInfo = null;
} else {
Assert.assertFalse(initialLocation.startsWith("/"));
pathInfo = "/" + initialLocation;
}
Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
VaadinService service = new MockVaadinServletService() {
@Override
public VaadinContext getContext() {
return new MockVaadinContext();
}
};
service.setCurrentInstances(request, response);
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
session.lock();
session.setConfiguration(config);
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
Arrays.asList(UITest.RootNavigationTarget.class, UITest.FooBarNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
});
ui.doInit(request, 0);
ui.getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
session.unlock();
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
}
Aggregations