Search in sources :

Example 6 with PendingJavaScriptInvocation

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

the class PublishedServerEventHandlerRpcHandlerTest method promiseSuccess.

@Test
public void promiseSuccess() {
    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();
    PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "compute", args, promiseId);
    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 three paramters: promiseId, value, target", 3, parameters.size());
    Assert.assertEquals("Promise id should match the value passed to invokeMethod", Integer.valueOf(promiseId), parameters.get(0));
    Assert.assertEquals("Promise value should be sqrt(36) = 6", Integer.valueOf(6), parameters.get(1));
    Assert.assertEquals("Target should be the component's element", component.getElement(), parameters.get(2));
}
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)

Example 7 with PendingJavaScriptInvocation

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

the class UITest method localeSet_directionUpdated.

@Test
public void localeSet_directionUpdated() {
    MockUI ui = new MockUI();
    ui.setDirection(Direction.RIGHT_TO_LEFT);
    ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
    List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = ui.dumpPendingJsInvocations();
    Assert.assertEquals(1, pendingJavaScriptInvocations.size());
    Assert.assertEquals("rtl", pendingJavaScriptInvocations.get(0).getInvocation().getParameters().get(0));
}
Also used : MockUI(com.vaadin.tests.util.MockUI) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 8 with PendingJavaScriptInvocation

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

the class ShortcutRegistrationTest method listenOnComponentHasElementLocatorJs_allowBrowserDefault_JsExecutionDoesNotPreventDefault.

@Test
public void listenOnComponentHasElementLocatorJs_allowBrowserDefault_JsExecutionDoesNotPreventDefault() {
    final ElementLocatorTestFixture fixture = new ElementLocatorTestFixture();
    final Key key = Key.KEY_A;
    fixture.createNewShortcut(key).allowBrowserDefault();
    List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = fixture.writeResponse();
    final PendingJavaScriptInvocation js = pendingJavaScriptInvocations.get(0);
    final String expression = js.getInvocation().getExpression();
    Assert.assertFalse("JS execution string should NOT have event.preventDefault() in it" + expression, expression.contains("event.preventDefault();"));
}
Also used : PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Test(org.junit.Test)

Example 9 with PendingJavaScriptInvocation

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

the class ShortcutRegistrationTest method listenOnComponentHasElementLocatorJs_jsExecutionScheduled.

@Test
public void listenOnComponentHasElementLocatorJs_jsExecutionScheduled() {
    final ElementLocatorTestFixture fixture = new ElementLocatorTestFixture();
    final Key key = Key.KEY_A;
    fixture.createNewShortcut(key);
    List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = fixture.writeResponse();
    final PendingJavaScriptInvocation js = pendingJavaScriptInvocations.get(0);
    final String expression = js.getInvocation().getExpression();
    Assert.assertTrue("element locator string " + fixture.elementLocatorJs + " missing from JS execution string " + expression, expression.contains("const delegate=" + fixture.elementLocatorJs + ";"));
    Assert.assertTrue("JS execution string should have event.preventDefault() in it" + expression, expression.contains("event.preventDefault();"));
    Assert.assertTrue("JS execution string should always have event.stopPropagation() in it" + expression, expression.contains("event.stopPropagation();"));
    Assert.assertTrue("JS execution string missing the key" + key, expression.contains(key.getKeys().get(0)));
    fixture.registration.remove();
    fixture.createNewShortcut(Key.KEY_X);
    pendingJavaScriptInvocations = fixture.writeResponse();
    Assert.assertEquals(0, pendingJavaScriptInvocations.size());
}
Also used : PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Test(org.junit.Test)

Example 10 with PendingJavaScriptInvocation

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

the class AbstractDnDUnitTest method testExtension_mobileDnDpolyfillScriptInjected.

@Test
public void testExtension_mobileDnDpolyfillScriptInjected() {
    ui.getInternals().dumpPendingJavaScriptInvocations();
    RouterLink component = new RouterLink();
    ui.add(component);
    runStaticCreateMethodForExtension(component);
    List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = ui.getInternals().dumpPendingJavaScriptInvocations();
    Assert.assertEquals(1, pendingJavaScriptInvocations.size());
    PendingJavaScriptInvocation pendingJavaScriptInvocation = pendingJavaScriptInvocations.get(0);
    // the urls are switched to "" by the mocked service method
    String fake = String.format(DnDUtilHelper.MOBILE_DND_INJECT_SCRIPT, "", "");
    Assert.assertEquals(fake, pendingJavaScriptInvocation.getInvocation().getExpression());
}
Also used : RouterLink(com.vaadin.flow.router.RouterLink) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) 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