Search in sources :

Example 1 with PendingJavaScriptResult

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")));
}
Also used : Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) Json(elemental.json.Json) Router(com.vaadin.flow.router.Router) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Route(com.vaadin.flow.router.Route) History(com.vaadin.flow.component.page.History) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) JsonValue(elemental.json.JsonValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) NotThreadSafe(net.jcip.annotations.NotThreadSafe) List(java.util.List) NavigationState(com.vaadin.flow.router.NavigationState) ServiceException(com.vaadin.flow.server.ServiceException) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ArrayList(java.util.ArrayList) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Tag(com.vaadin.flow.component.Tag) Location(com.vaadin.flow.router.Location) MockInstantiator(com.vaadin.flow.server.MockInstantiator) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) QueryParameters(com.vaadin.flow.router.QueryParameters) Text(com.vaadin.flow.component.Text) Page(com.vaadin.flow.component.page.Page) RouterLayout(com.vaadin.flow.router.RouterLayout) HasElement(com.vaadin.flow.component.HasElement) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Test(org.junit.Test) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) PreserveOnRefresh(com.vaadin.flow.router.PreserveOnRefresh) JsonObject(elemental.json.JsonObject) Assert(org.junit.Assert) Collections(java.util.Collections) NavigationEvent(com.vaadin.flow.router.NavigationEvent) ParentLayout(com.vaadin.flow.router.ParentLayout) NavigationEvent(com.vaadin.flow.router.NavigationEvent) Serializable(java.io.Serializable) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject) Router(com.vaadin.flow.router.Router) Page(com.vaadin.flow.component.page.Page) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 2 with PendingJavaScriptResult

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());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) Serializable(java.io.Serializable) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Router(com.vaadin.flow.router.Router) Page(com.vaadin.flow.component.page.Page) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 3 with PendingJavaScriptResult

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());
}
Also used : PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) Test(org.junit.Test)

Example 4 with PendingJavaScriptResult

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());
}
Also used : PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) Test(org.junit.Test)

Example 5 with PendingJavaScriptResult

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;
}
Also used : IntStream(java.util.stream.IntStream) Component(com.vaadin.flow.component.Component) VirtualChildrenList(com.vaadin.flow.internal.nodefeature.VirtualChildrenList) ComponentUtil(com.vaadin.flow.component.ComponentUtil) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) HashMap(java.util.HashMap) AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource) BasicTextElementStateProvider(com.vaadin.flow.dom.impl.BasicTextElementStateProvider) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) JsonValue(elemental.json.JsonValue) Locale(java.util.Locale) Map(java.util.Map) JsonCodec(com.vaadin.flow.internal.JsonCodec) Command(com.vaadin.flow.server.Command) Page(com.vaadin.flow.component.page.Page) TextNodeMap(com.vaadin.flow.internal.nodefeature.TextNodeMap) StateNode(com.vaadin.flow.internal.StateNode) BasicElementStateProvider(com.vaadin.flow.dom.impl.BasicElementStateProvider) Predicate(java.util.function.Predicate) Set(java.util.Set) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) CustomAttribute(com.vaadin.flow.dom.impl.CustomAttribute) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) List(java.util.List) JsonUtils(com.vaadin.flow.internal.JsonUtils) Stream(java.util.stream.Stream) ThemeListImpl(com.vaadin.flow.dom.impl.ThemeListImpl) Document(org.jsoup.nodes.Document) Optional(java.util.Optional) StreamResource(com.vaadin.flow.server.StreamResource) JavaScriptSemantics(com.vaadin.flow.internal.JavaScriptSemantics) Serializable(java.io.Serializable) StateNode(com.vaadin.flow.internal.StateNode) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation)

Aggregations

PendingJavaScriptResult (com.vaadin.flow.component.page.PendingJavaScriptResult)5 Test (org.junit.Test)4 Component (com.vaadin.flow.component.Component)3 Page (com.vaadin.flow.component.page.Page)3 Serializable (java.io.Serializable)3 Location (com.vaadin.flow.router.Location)2 NavigationEvent (com.vaadin.flow.router.NavigationEvent)2 Router (com.vaadin.flow.router.Router)2 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)2 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)2 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)2 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)2 MockUI (com.vaadin.tests.util.MockUI)2 Json (elemental.json.Json)2 JsonValue (elemental.json.JsonValue)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ComponentUtil (com.vaadin.flow.component.ComponentUtil)1 HasElement (com.vaadin.flow.component.HasElement)1 Tag (com.vaadin.flow.component.Tag)1 Text (com.vaadin.flow.component.Text)1