Search in sources :

Example 1 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class Page method executeJs.

// When updating JavaDocs here, keep in sync with Element.executeJavaScript
/**
 * Asynchronously runs the given JavaScript expression in the browser.
 * <p>
 * The returned <code>PendingJavaScriptResult</code> can be used to retrieve
 * any <code>return</code> value from the JavaScript expression. If no
 * return value handler is registered, the return value will be ignored.
 * <p>
 * The given parameters will be available to the expression as variables
 * named <code>$0</code>, <code>$1</code>, and so on. Supported parameter
 * types are:
 * <ul>
 * <li>{@link String}
 * <li>{@link Integer}
 * <li>{@link Double}
 * <li>{@link Boolean}
 * <li>{@link JsonValue}
 * <li>{@link Element} (will be sent as <code>null</code> if the server-side
 * element instance is not attached when the invocation is sent to the
 * client)
 * </ul>
 * Note that the parameter variables can only be used in contexts where a
 * JavaScript variable can be used. You should for instance do
 * <code>'prefix' + $0</code> instead of <code>'prefix$0'</code> and
 * <code>value[$0]</code> instead of <code>value.$0</code> since JavaScript
 * variables aren't evaluated inside strings or property names.
 *
 * @param expression
 *            the JavaScript expression to invoke
 * @param parameters
 *            parameters to pass to the expression
 * @return a pending result that can be used to get a value returned from
 *         the expression
 */
public PendingJavaScriptResult executeJs(String expression, Serializable... parameters) {
    JavaScriptInvocation invocation = new JavaScriptInvocation(expression, parameters);
    PendingJavaScriptInvocation execution = new PendingJavaScriptInvocation(ui.getInternals().getStateTree().getRootNode(), invocation);
    ui.getInternals().addJavaScriptInvocation(execution);
    return execution;
}
Also used : PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation)

Example 2 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class ElementTest method assertPendingJs.

private void assertPendingJs(UI ui, String js, Serializable... arguments) {
    List<PendingJavaScriptInvocation> pendingJs = ui.getInternals().dumpPendingJavaScriptInvocations();
    JavaScriptInvocation expected = new JavaScriptInvocation(js, arguments);
    Assert.assertEquals(1, pendingJs.size());
    assertEquals(expected, pendingJs.get(0).getInvocation());
}
Also used : PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)

Example 3 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class ElementTest method callFunctionBeforeDetach.

@Test
public void callFunctionBeforeDetach() {
    UI ui = new MockUI();
    Element element = ElementFactory.createDiv();
    ui.getElement().appendChild(element);
    element.callJsFunction("noArgsMethod");
    ui.getElement().removeAllChildren();
    ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
    List<PendingJavaScriptInvocation> invocations = ui.getInternals().dumpPendingJavaScriptInvocations();
    Assert.assertTrue(invocations.isEmpty());
}
Also used : MockUI(com.vaadin.tests.util.MockUI) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Test(org.junit.Test) ElementListenersTest(com.vaadin.flow.internal.nodefeature.ElementListenersTest)

Example 4 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class UidlWriterTest method testEncodeExecuteJavaScript_npmMode.

@Test
public void testEncodeExecuteJavaScript_npmMode() {
    Element element = ElementFactory.createDiv();
    JavaScriptInvocation invocation1 = new JavaScriptInvocation("$0.focus()", element);
    JavaScriptInvocation invocation2 = new JavaScriptInvocation("console.log($0, $1)", "Lives remaining:", Integer.valueOf(3));
    List<PendingJavaScriptInvocation> executeJavaScriptList = Stream.of(invocation1, invocation2).map(invocation -> new PendingJavaScriptInvocation(element.getNode(), invocation)).collect(Collectors.toList());
    JsonArray json = UidlWriter.encodeExecuteJavaScriptList(executeJavaScriptList);
    JsonArray expectedJson = JsonUtils.createArray(JsonUtils.createArray(// Null since element is not attached
    Json.createNull(), Json.create("$0.focus()")), JsonUtils.createArray(Json.create("Lives remaining:"), Json.create(3), Json.create("console.log($0, $1)")));
    assertTrue(JsonUtils.jsonEquals(expectedJson, json));
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Route(com.vaadin.flow.router.Route) Map(java.util.Map) After(org.junit.After) Element(com.vaadin.flow.dom.Element) Is.is(org.hamcrest.core.Is.is) Lookup(com.vaadin.flow.di.Lookup) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) RoutePathProvider(com.vaadin.flow.router.RoutePathProvider) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Collectors(java.util.stream.Collectors) NotThreadSafe(net.jcip.annotations.NotThreadSafe) List(java.util.List) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Mockito.mock(org.mockito.Mockito.mock) Dependency(com.vaadin.flow.shared.ui.Dependency) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ElementFactory(com.vaadin.flow.dom.ElementFactory) RouterLayout(com.vaadin.flow.router.RouterLayout) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MockServletServiceSessionSetup(com.vaadin.flow.server.MockServletServiceSessionSetup) Mockito.when(org.mockito.Mockito.when) Mockito(org.mockito.Mockito) JsonUtils(com.vaadin.flow.internal.JsonUtils) RoutePathProviderImpl(com.vaadin.flow.server.MockVaadinContext.RoutePathProviderImpl) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) JsonObject(elemental.json.JsonObject) Collections(java.util.Collections) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) JsonArray(elemental.json.JsonArray) Element(com.vaadin.flow.dom.Element) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 5 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class PublishedServerEventHandlerRpcHandlerTest method promiseFailure.

@Test
public void promiseFailure() {
    int promiseId = 4;
    JsonArray args = Json.createArray();
    args.set(0, -36);
    ComponentWithCompute component = new ComponentWithCompute();
    UI ui = new UI();
    ui.getInternals().setSession(session);
    ui.add(component);
    // Get rid of attach invocations
    ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
    ui.getInternals().dumpPendingJavaScriptInvocations();
    try {
        PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "compute", args, promiseId);
        Assert.fail("Exception should be thrown");
    } catch (RuntimeException e) {
        Assert.assertTrue(e.getCause() instanceof ArithmeticException);
    }
    ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
    List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = ui.getInternals().dumpPendingJavaScriptInvocations();
    Assert.assertEquals(1, pendingJavaScriptInvocations.size());
    JavaScriptInvocation invocation = pendingJavaScriptInvocations.get(0).getInvocation();
    Assert.assertTrue("Invocation does not look like a promise callback", invocation.getExpression().contains(JsonConstants.RPC_PROMISE_CALLBACK_NAME));
    List<Object> parameters = invocation.getParameters();
    Assert.assertEquals("Expected two paramters: promiseId,  target", 2, parameters.size());
    Assert.assertEquals("Promise id should match the value passed to invokeMethod", Integer.valueOf(promiseId), parameters.get(0));
    Assert.assertEquals("Target should be the component's element", component.getElement(), parameters.get(1));
}
Also used : JsonArray(elemental.json.JsonArray) UI(com.vaadin.flow.component.UI) JsonObject(elemental.json.JsonObject) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) Test(org.junit.Test)

Aggregations

PendingJavaScriptInvocation (com.vaadin.flow.component.internal.PendingJavaScriptInvocation)12 Test (org.junit.Test)8 JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)6 UI (com.vaadin.flow.component.UI)4 JsonArray (elemental.json.JsonArray)4 JsonObject (elemental.json.JsonObject)4 Component (com.vaadin.flow.component.Component)2 JsonUtils (com.vaadin.flow.internal.JsonUtils)2 BootstrapHandlerTest (com.vaadin.flow.server.BootstrapHandlerTest)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 MockUI (com.vaadin.tests.util.MockUI)2 Json (elemental.json.Json)2 List (java.util.List)2 Map (java.util.Map)2 ComponentUtil (com.vaadin.flow.component.ComponentUtil)1 Tag (com.vaadin.flow.component.Tag)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 Page (com.vaadin.flow.component.page.Page)1