use of com.vaadin.flow.component.page.Page 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")));
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class NavigationStateRendererTest method handle_preserveOnRefreshAndWindowNameNotKnown_clientSideCallTriggered.
@Test
public void handle_preserveOnRefreshAndWindowNameNotKnown_clientSideCallTriggered() {
// given a service with instantiator
MockVaadinServletService service = createMockServiceWithInstantiator();
// given a locked session
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
// given a NavigationStateRenderer mapping to PreservedView
NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
// given the session has a cache of something at this location
AbstractNavigationStateRenderer.setPreservedChain(session, "", new Location("preserved"), new ArrayList<>(Arrays.asList(Mockito.mock(Component.class))));
// given a UI that contain no window name with an instrumented Page
// that records JS invocations
AtomicBoolean jsInvoked = new AtomicBoolean(false);
MockUI ui = new MockUI(session) {
final Page page = new Page(this) {
@Override
public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
jsInvoked.set(true);
return super.executeJs(expression, params);
}
};
@Override
public Page getPage() {
return page;
}
};
// when a navigation event reaches the renderer
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("preserved"), ui, NavigationTrigger.PAGE_LOAD));
// then client-side JS was invoked
Assert.assertTrue("Expected JS invocation", jsInvoked.get());
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class FragmentLinkView method onAttach.
@Override
protected void onAttach(AttachEvent attachEvent) {
Page page = attachEvent.getUI().getPage();
page.executeJs("var i = 0;" + "window.addEventListener('hashchange', function(event) {" + "var x = document.createElement('span');" + "x.textContent = ' ' + i;" + "i++;" + "x.class = 'hashchange';" + "document.getElementById('placeholder').appendChild(x);}," + " false);");
HistoryStateChangeHandler current = page.getHistory().getHistoryStateChangeHandler();
page.getHistory().setHistoryStateChangeHandler(event -> {
if (event.getLocation().getPath().equals("override")) {
event.getSource().replaceState(null, "overridden#Scroll_Target2");
} else {
current.onHistoryStateChange(event);
}
});
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class ViewAccessCheckerTest method setupRequest.
private Result setupRequest(Class navigationTarget, User user, boolean productionMode) {
CurrentInstance.clearAll();
Principal principal;
String[] roles;
if (user == User.USER_NO_ROLES) {
principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
roles = new String[0];
} else if (user == User.NORMAL_USER) {
principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
roles = new String[] { "user" };
} else if (user == User.ADMIN) {
principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
roles = new String[] { "admin" };
} else {
principal = null;
roles = new String[0];
}
VaadinServletRequest vaadinServletRequest = Mockito.mock(VaadinServletRequest.class);
HttpServletRequest httpServletRequest = AccessAnnotationCheckerTest.createRequest(principal, roles);
Mockito.when(vaadinServletRequest.getHttpServletRequest()).thenReturn(httpServletRequest);
CurrentInstance.set(VaadinRequest.class, vaadinServletRequest);
Router router = Mockito.mock(Router.class);
UI ui = Mockito.mock(UI.class);
Page page = Mockito.mock(Page.class);
Mockito.when(ui.getPage()).thenReturn(page);
VaadinSession vaadinSession = Mockito.mock(VaadinSession.class);
Mockito.when(ui.getSession()).thenReturn(vaadinSession);
DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(vaadinSession.getConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isProductionMode()).thenReturn(productionMode);
UIInternals uiInternals = Mockito.mock(UIInternals.class);
Mockito.when(ui.getInternals()).thenReturn(uiInternals);
Mockito.when(uiInternals.getRouter()).thenReturn(router);
Mockito.when(router.getErrorNavigationTarget(Mockito.any())).thenAnswer(invocation -> {
Class<?> exceptionClass = invocation.getArguments()[0].getClass();
if (exceptionClass == NotFoundException.class) {
return Optional.of(new ErrorTargetEntry(RouteNotFoundError.class, NotFoundException.class));
} else {
return Optional.empty();
}
});
Location location = new Location(getRoute(navigationTarget));
NavigationEvent navigationEvent = new NavigationEvent(router, location, ui, NavigationTrigger.ROUTER_LINK);
BeforeEnterEvent event = new BeforeEnterEvent(navigationEvent, navigationTarget, new ArrayList<>());
RouteRegistry routeRegistry = Mockito.mock(RouteRegistry.class);
Mockito.when(router.getRegistry()).thenReturn(routeRegistry);
Mockito.when(routeRegistry.getNavigationTarget(Mockito.anyString())).thenAnswer(invocation -> {
String url = (String) invocation.getArguments()[0];
if (location.getPath().equals(url)) {
return Optional.of(navigationTarget);
} else {
return Optional.empty();
}
});
HttpSession session = Mockito.mock(HttpSession.class);
Map<String, Object> sessionAttributes = new HashMap<>();
Mockito.when(httpServletRequest.getSession()).thenReturn(session);
Mockito.doAnswer(invocation -> {
String key = (String) invocation.getArguments()[0];
Object value = invocation.getArguments()[1];
sessionAttributes.put(key, value);
return null;
}).when(session).setAttribute(Mockito.anyString(), Mockito.any());
Result info = new Result();
info.event = event;
info.sessionAttributes = sessionAttributes;
Mockito.doAnswer(invocation -> {
info.redirectUsingPageLocation = (String) invocation.getArguments()[0];
return null;
}).when(page).setLocation(Mockito.anyString());
return info;
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class JavaScriptBootstrapUITest method mockPage.
private Page mockPage() {
Page page = Mockito.mock(Page.class);
Mockito.when(ui.getPage()).thenReturn(page);
History history = new History(ui);
Mockito.when(page.getHistory()).thenReturn(history);
return page;
}
Aggregations