Search in sources :

Example 11 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class GwtPolymerModelTest method setupSetMethod.

private void setupSetMethod(Element element, String prefix) {
    NativeFunction function = NativeFunction.create("this['" + prefix + "'+arguments[0]]=arguments[1]");
    WidgetUtil.setJsProperty(element, "set", function);
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Example 12 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class GwtEventHandlerTest method testPolymerMockedEventHandlerWithEventData.

public void testPolymerMockedEventHandlerWithEventData() {
    String methodName = "eventHandler";
    String methodId = "handlerId";
    String eventData = "event.button";
    node.getList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS).add(0, methodName);
    node.getMap(NodeFeatures.POLYMER_EVENT_LISTENERS).getProperty(methodName).setValue(methodId);
    JsonObject json = Json.createObject();
    JsonArray array = Json.createArray();
    array.set(0, eventData);
    json.put(methodId, array);
    node.getTree().getRegistry().getConstantPool().importFromJson(json);
    Binder.bind(node, element);
    Reactive.flush();
    NativeFunction mockedFunction = new NativeFunction("this." + methodName + "({button: 2, altKey: false, clientX: 50, clientY: 100})");
    mockedFunction.apply(element, JsCollections.array());
    assertEquals("The amount of server methods was not as expected", 1, serverMethods.size());
    assertEquals("Expected method did not match", methodName, serverMethods.keySet().iterator().next());
    assertEquals("Wrong amount of method arguments", 1, serverMethods.get(methodName).length());
    assertEquals("Gotten argument wasn't as expected", "2", serverMethods.get(methodName).get(0).toString());
    assertEquals("Method node did not match the expected node.", node, serverRpcNodes.get(methodName));
}
Also used : JsonArray(elemental.json.JsonArray) NativeFunction(com.vaadin.client.flow.util.NativeFunction) JsonObject(elemental.json.JsonObject)

Example 13 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class GwtEventHandlerTest method testPolymerMockedEventHandlerWithDefaultImplementation.

public void testPolymerMockedEventHandlerWithDefaultImplementation() {
    String methodName = "eventHandler";
    node.getList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS).add(0, methodName);
    Binder.bind(node, element);
    Reactive.flush();
    setPrototypeEventHandler(element, methodName);
    NativeFunction mockedFunction = new NativeFunction("this." + methodName + "({button: 2}, 'Extra String argument')");
    mockedFunction.apply(element, JsCollections.array());
    JsonObject expectedResult = Json.createObject();
    expectedResult.put("button", 2);
    expectedResult.put("result", "Extra String argument");
    assertEquals("The amount of server methods was not as expected", 1, serverMethods.size());
    assertEquals("Expected method did not match", methodName, serverMethods.keySet().iterator().next());
    assertEquals("Wrong amount of method arguments", 2, serverMethods.get(methodName).length());
    assertEquals("Gotten argument wasn't as expected", WidgetUtil.toPrettyJson(expectedResult), WidgetUtil.toPrettyJson(WidgetUtil.crazyJsoCast(serverMethods.get(methodName).get(0))));
    assertEquals("Method node did not match the expected node.", node, serverRpcNodes.get(methodName));
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) JsonObject(elemental.json.JsonObject)

Example 14 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class GwtEventHandlerTest method testEventHandlerModelItem.

public void testEventHandlerModelItem() {
    String methodName = "eventHandlerModelItem";
    String methodId = "handlerModelId";
    String eventData = "event.model.item";
    node.getList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS).add(0, methodName);
    node.getMap(NodeFeatures.POLYMER_EVENT_LISTENERS).getProperty(methodName).setValue(methodId);
    JsonObject json = Json.createObject();
    JsonArray array = Json.createArray();
    array.set(0, eventData);
    json.put(methodId, array);
    node.getTree().getRegistry().getConstantPool().importFromJson(json);
    Binder.bind(node, element);
    Reactive.flush();
    NativeFunction mockedFunction = new NativeFunction("this." + methodName + "({button: 0, model:{item: {nodeId: 2, value: 'test value'}}})");
    mockedFunction.apply(element, JsCollections.array());
    assertEquals("The amount of server methods was not as expected", 1, serverMethods.size());
    assertEquals("Expected method did not match", methodName, serverMethods.keySet().iterator().next());
    assertEquals("Wrong amount of method arguments", 1, serverMethods.get(methodName).length());
    assertTrue("Received value was not a JsonObject", serverMethods.get(methodName).get(0) instanceof JsonObject);
    JsonObject expectedResult = Json.createObject();
    expectedResult.put("nodeId", 2);
    assertEquals("Gotten argument wasn't as expected", expectedResult.toJson(), ((JsonObject) serverMethods.get(methodName).get(0)).toJson());
    assertEquals("Method node did not match the expected node.", node, serverRpcNodes.get(methodName));
}
Also used : JsonArray(elemental.json.JsonArray) NativeFunction(com.vaadin.client.flow.util.NativeFunction) JsonObject(elemental.json.JsonObject)

Example 15 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class GwtEventHandlerTest method testEventHandlerModelItemSingleItem.

public void testEventHandlerModelItemSingleItem() {
    String methodName = "eventHandlerSingleModelItem";
    String methodId = "handlerSingleModelId";
    String eventData = "item";
    node.getList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS).add(0, methodName);
    node.getMap(NodeFeatures.POLYMER_EVENT_LISTENERS).getProperty(methodName).setValue(methodId);
    JsonObject json = Json.createObject();
    JsonArray array = Json.createArray();
    array.set(0, eventData);
    json.put(methodId, array);
    node.getTree().getRegistry().getConstantPool().importFromJson(json);
    node.setDomNode(element);
    Binder.bind(node, element);
    // Add the node property for getPolymerPropertyObject functionality
    setNodeProperty(node.getDomNode(), eventData, "nodeId", "1");
    Reactive.flush();
    NativeFunction mockedFunction = new NativeFunction("this." + methodName + "({button: 0})");
    mockedFunction.apply(element, JsCollections.array());
    assertEquals("The amount of server methods was not as expected", 1, serverMethods.size());
    assertEquals("Expected method did not match", methodName, serverMethods.keySet().iterator().next());
    assertEquals("Wrong amount of method arguments", 1, serverMethods.get(methodName).length());
    assertTrue("Received value was not a JsonObject", serverMethods.get(methodName).get(0) instanceof JsonObject);
    JsonObject expectedResult = Json.createObject();
    expectedResult.put("nodeId", 1);
    assertEquals("Gotten argument wasn't as expected", expectedResult.toJson(), ((JsonObject) serverMethods.get(methodName).get(0)).toJson());
    assertEquals("Method node did not match the expected node.", node, serverRpcNodes.get(methodName));
}
Also used : JsonArray(elemental.json.JsonArray) NativeFunction(com.vaadin.client.flow.util.NativeFunction) JsonObject(elemental.json.JsonObject)

Aggregations

NativeFunction (com.vaadin.client.flow.util.NativeFunction)19 JsonObject (elemental.json.JsonObject)6 StateNode (com.vaadin.client.flow.StateNode)3 JsonArray (elemental.json.JsonArray)3 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)2 Element (elemental.dom.Element)2 ServerEventObject (com.vaadin.client.flow.binding.ServerEventObject)1 DomNode (com.vaadin.client.flow.dom.DomNode)1 BeanModelType (com.vaadin.client.flow.model.BeanModelType)1 Node (elemental.dom.Node)1