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);
}
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));
}
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"));
}
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"));
}
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());
}
Aggregations