use of com.vaadin.flow.component.page.PendingJavaScriptResult 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.PendingJavaScriptResult 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.PendingJavaScriptResult in project flow by vaadin.
the class PageTest method testJavaScriptExecutionTooLateCancel.
@Test
public void testJavaScriptExecutionTooLateCancel() {
Assert.assertEquals(0, countPendingInvocations());
PendingJavaScriptResult executeJavaScript = page.executeJs("window.alert('$0');", "foobar");
Assert.assertEquals(1, countPendingInvocations());
Assert.assertEquals(1, ui.getInternals().dumpPendingJavaScriptInvocations().size());
Assert.assertEquals(0, countPendingInvocations());
Assert.assertFalse(executeJavaScript.cancelExecution());
}
use of com.vaadin.flow.component.page.PendingJavaScriptResult in project flow by vaadin.
the class PageTest method testJavasScriptExecutionCancel.
@Test
public void testJavasScriptExecutionCancel() {
Assert.assertEquals(0, countPendingInvocations());
PendingJavaScriptResult executeJavaScript = page.executeJs("window.alert('$0');", "foobar");
Assert.assertEquals(1, countPendingInvocations());
Assert.assertTrue(executeJavaScript.cancelExecution());
Assert.assertEquals(0, countPendingInvocations());
}
use of com.vaadin.flow.component.page.PendingJavaScriptResult in project flow by vaadin.
the class Element method scheduleJavaScriptInvocation.
private PendingJavaScriptResult scheduleJavaScriptInvocation(String expression, Stream<Serializable> parameters) {
StateNode node = getNode();
JavaScriptInvocation invocation = new JavaScriptInvocation(expression, parameters.toArray(Serializable[]::new));
PendingJavaScriptInvocation pending = new PendingJavaScriptInvocation(node, invocation);
node.runWhenAttached(ui -> ui.getInternals().getStateTree().beforeClientResponse(node, context -> {
if (!pending.isCanceled()) {
context.getUI().getInternals().addJavaScriptInvocation(pending);
}
}));
return pending;
}
Aggregations