Search in sources :

Example 16 with MapProperty

use of com.vaadin.client.flow.nodefeature.MapProperty in project flow by vaadin.

the class GwtBasicElementBinderTest method testBindVirtualChild_withDeferredElementInShadowRoot_byIndicesPath.

public void testBindVirtualChild_withDeferredElementInShadowRoot_byIndicesPath() {
    String childId = "childElement";
    StateNode childNode = createChildNode(childId, element.getTagName());
    NodeMap properties = childNode.getMap(NodeFeatures.ELEMENT_PROPERTIES);
    MapProperty fooProperty = properties.getProperty("foo");
    fooProperty.setValue("bar");
    WidgetUtil.setJsProperty(element, "ready", NativeFunction.create(""));
    Binder.bind(node, element);
    JsonArray path = Json.createArray();
    path.set(0, 0);
    addVirtualChild(node, childNode, NodeProperties.TEMPLATE_IN_TEMPLATE, path);
    Element shadowRoot = Browser.getDocument().createElement("div");
    List<Integer> expectedAfterBindingFeatures = Arrays.asList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS, NodeFeatures.ELEMENT_CHILDREN);
    Reactive.flush();
    expectedAfterBindingFeatures.forEach(notExpectedFeature -> assertFalse("Child node should not have any features from list " + expectedAfterBindingFeatures + " before binding, but got feature " + notExpectedFeature, childNode.hasFeature(notExpectedFeature)));
    WidgetUtil.setJsProperty(element, "root", shadowRoot);
    Element addressedElement = createAndAppendElementToShadowRoot(shadowRoot, childId, element.getTagName());
    // add flush listener which register the property to revert its initial
    // value back if it has been changed during binding "from the client
    // side" and do update the property emulating client side update
    // The property value should be reverted back in the end
    Reactive.addFlushListener(() -> {
        tree.getRegistry().getInitialPropertiesHandler().handlePropertyUpdate(fooProperty);
        fooProperty.setValue("baz");
    });
    PolymerUtils.fireReadyEvent(element);
    // the property value should be the same as initially
    assertEquals("bar", fooProperty.getValue());
    expectedAfterBindingFeatures.forEach(expectedFeature -> assertTrue("Child node should have all features from list " + expectedAfterBindingFeatures + " before binding, but missing feature " + expectedFeature, childNode.hasFeature(expectedFeature)));
    // nothing has changed: no new child
    assertEquals("No new child should be added to the element after attach", 0, element.getChildElementCount());
    assertEquals("No new child should be added to the shadow root after attach", 1, shadowRoot.getChildElementCount());
    Element childElement = shadowRoot.getFirstElementChild();
    assertSame("Existing element should be the same as element in the StateNode object", addressedElement, childElement);
}
Also used : JsonArray(elemental.json.JsonArray) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) Element(elemental.dom.Element) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 17 with MapProperty

use of com.vaadin.client.flow.nodefeature.MapProperty in project flow by vaadin.

the class GwtPolymerModelTest method testInitialUpdateModelProperty_propertyIsUpdatableAndSchedulerIsNotExecuted_propertyIsNotSync.

public void testInitialUpdateModelProperty_propertyIsUpdatableAndSchedulerIsNotExecuted_propertyIsNotSync() {
    addMockMethods(element);
    String propertyName = "black";
    String propertyValue = "coffee";
    setModelProperty(node, propertyName, propertyValue);
    initScheduler(new SchedulerImpl() {

        @Override
        public void scheduleDeferred(ScheduledCommand cmd) {
        }
    });
    node.setNodeData(new UpdatableModelProperties(JsCollections.array("black")));
    Binder.bind(node, element);
    Reactive.flush();
    assertEquals("Expected to have property with name " + propertyName + " defined after initial binding", propertyValue, WidgetUtil.getJsProperty(element, propertyName));
    String newPropertyValue = "bubblegum";
    emulatePolymerPropertyChange(element, propertyName, newPropertyValue);
    Reactive.flush();
    assertEquals("Expected to have property with name " + propertyName + " updated from client side", propertyValue, WidgetUtil.getJsProperty(element, propertyName));
    MapProperty property = node.getMap(NodeFeatures.ELEMENT_PROPERTIES).getProperty(propertyName);
    assertEquals(propertyValue, property.getValue());
    assertFalse(tree.synchronizedProperties.has(node));
}
Also used : SchedulerImpl(com.google.gwt.core.client.impl.SchedulerImpl) UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty)

Example 18 with MapProperty

use of com.vaadin.client.flow.nodefeature.MapProperty in project flow by vaadin.

the class GwtPolymerModelTest method testLatePolymerInit.

public void testLatePolymerInit() {
    emulatePolymerNotLoaded();
    addMockMethods(element);
    String propertyName = "black";
    String propertyValue = "coffee";
    setModelProperty(node, propertyName, propertyValue);
    node.setNodeData(new UpdatableModelProperties(JsCollections.array("black")));
    Binder.bind(node, element);
    Reactive.flush();
    assertEquals("Expected to have property with name " + propertyName + " defined after initial binding", propertyValue, WidgetUtil.getJsProperty(element, propertyName));
    String newPropertyValue = "bubblegum";
    emulatePolymerPropertyChange(element, propertyName, newPropertyValue);
    Reactive.flush();
    assertEquals("Expected to have property with name " + propertyName + " updated from client side", newPropertyValue, WidgetUtil.getJsProperty(element, propertyName));
    MapProperty property = node.getMap(NodeFeatures.ELEMENT_PROPERTIES).getProperty(propertyName);
    assertEquals(newPropertyValue, property.getValue());
    assertEquals(newPropertyValue, tree.synchronizedProperties.get(node).get(propertyName));
    assertEquals("`_propertiesChanged` should be triggered exactly once", 1.0, WidgetUtil.getJsProperty(element, "propertiesChangedCallCount"));
    assertEquals("Exactly one `whenDefined.then` callback should be called after element was initialized", 1.0, WidgetUtil.getJsProperty(element, "callbackCallCount"));
}
Also used : UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty)

Example 19 with MapProperty

use of com.vaadin.client.flow.nodefeature.MapProperty in project flow by vaadin.

the class GwtPolymerModelTest method testUpdateModelSubProperty_subpropertyIsUpdatableAndIsNotSetFromServer_subpropertyIsSync.

public void testUpdateModelSubProperty_subpropertyIsUpdatableAndIsNotSetFromServer_subpropertyIsSync() {
    addMockMethods(element);
    setModelProperty(node, "bar", modelNode);
    node.setNodeData(new UpdatableModelProperties(JsCollections.array("bar.foo")));
    Binder.bind(node, element);
    Reactive.flush();
    String newSubPropertyValue = "baz";
    PolymerUtils.setProperty(element, "bar.foo", "baz");
    emulatePolymerPropertyChange(element, "bar.foo", "baz");
    Reactive.flush();
    assertEquals("Expected to have an object 'bar' with a property named 'foo'" + " updated from client side", newSubPropertyValue, WidgetUtil.getJsProperty(WidgetUtil.getJsProperty(element, "bar"), "foo"));
    MapProperty property = modelNode.getMap(NodeFeatures.ELEMENT_PROPERTIES).getProperty("foo");
    assertEquals(newSubPropertyValue, property.getValue());
    assertEquals(newSubPropertyValue, tree.synchronizedProperties.get(modelNode).get("foo"));
}
Also used : UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty)

Example 20 with MapProperty

use of com.vaadin.client.flow.nodefeature.MapProperty in project flow by vaadin.

the class GwtTemplateBinderTest method testServerEventHandlerInNgFor.

public void testServerEventHandlerInNgFor() {
    TestElementTemplateNode parent = TestElementTemplateNode.create("div");
    String operation = "operation";
    // ============= create <li *ngFor> ===============================
    String collectionVar = "items";
    TestForTemplateNode templateNode = TestTemplateNode.create(ForTemplateNode.TYPE);
    int templateId = 42;
    registry.getTemplateRegistry().register(templateId, templateNode);
    TestElementTemplateNode forChild = TestElementTemplateNode.create("li");
    templateNode.setCollectionVariable(collectionVar);
    forChild.addEventHandler("click", "$server." + operation + "()");
    int forChildId = 11;
    registry.getTemplateRegistry().register(forChildId, forChild);
    templateNode.setChildrenIds(new double[] { forChildId });
    parent.setChildrenIds(new double[] { templateId });
    NodeMap model = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    MapProperty property = model.getProperty(collectionVar);
    StateNode modelNode = new StateNode(1, tree);
    property.setValue(modelNode);
    // ================== now modelNode is NG FOR model node ==========
    StateNode varNode = new StateNode(2, tree);
    com.vaadin.client.flow.nodefeature.NodeList modelList = modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST);
    // add one item to the "collection" model
    modelList.add(0, varNode);
    stateNode.getList(NodeFeatures.CLIENT_DELEGATE_HANDLERS).set(0, operation);
    Element element = createElement(parent);
    Reactive.flush();
    MouseEvent event = (MouseEvent) Browser.getDocument().createEvent(Events.MOUSE);
    event.initMouseEvent("click", true, true, Browser.getWindow(), 0, 0, 0, 0, 0, false, false, false, false, 0, element);
    Browser.getDocument().getBody().appendChild(element);
    element.getElementsByTagName("li").item(0).dispatchEvent(event);
    assertEquals(1, serverMethods.size());
    assertNotNull(serverMethods.get(operation));
    assertEquals(stateNode.getId(), serverRpcNodes.get(operation).getId());
}
Also used : MouseEvent(elemental.events.MouseEvent) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Aggregations

MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)40 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)18 StateNode (com.vaadin.client.flow.StateNode)12 Test (org.junit.Test)8 Element (elemental.dom.Element)7 JsonObject (elemental.json.JsonObject)6 UpdatableModelProperties (com.vaadin.client.flow.model.UpdatableModelProperties)5 JsonValue (elemental.json.JsonValue)3 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)2 SchedulerImpl (com.google.gwt.core.client.impl.SchedulerImpl)1 Computation (com.vaadin.client.flow.reactive.Computation)1 FlushListener (com.vaadin.client.flow.reactive.FlushListener)1 Node (elemental.dom.Node)1 NodeList (elemental.dom.NodeList)1 Text (elemental.dom.Text)1 EventRemover (elemental.events.EventRemover)1 MouseEvent (elemental.events.MouseEvent)1 JsonArray (elemental.json.JsonArray)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1