Search in sources :

Example 6 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON.

@Test
public void syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    TestComponent anotherComonent = new TestComponent();
    StateNode anotherNode = anotherComonent.getElement().getNode();
    ElementPropertyMap.getModel(node).setUpdateFromClientFilter(name -> true);
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", anotherNode.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);
    Serializable testPropertyValue = node.getFeature(ElementPropertyMap.class).getProperty("foo");
    Assert.assertNotSame(anotherNode, testPropertyValue);
    Assert.assertTrue(testPropertyValue instanceof JsonValue);
}
Also used : Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 7 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class PolymerTemplateTest method initModel_onlyExplicitelySetPropertiesAreSet.

@Test
public void initModel_onlyExplicitelySetPropertiesAreSet() {
    InitModelTemplate template = new InitModelTemplate();
    template.getModel().setMessage("foo");
    ElementPropertyMap map = template.getElement().getNode().getFeature(ElementPropertyMap.class);
    // message has been explicitly set
    Assert.assertTrue(map.hasProperty("message"));
    Assert.assertNotNull(map.getProperty("message"));
    // "list" is represented by StateNode so it's considered as explicitly
    // set
    Assert.assertTrue(map.hasProperty("list"));
    Assert.assertNotNull(map.getProperty("list"));
    // title has not been
    Assert.assertFalse(map.hasProperty("title"));
}
Also used : ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 8 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class TemplateModelUtil method resolveListAndRun.

/**
 * Resolves a list model type and a model list based on a model instance and
 * passes those to the provided callback.
 *
 * @param <R>
 *            the return type
 * @param model
 *            the model instance for which to resolve a type and a list, not
 *            <code>null</code>
 * @param modelPath
 *            the model path to resolve, not <code>null</code>
 * @param callback
 *            the callback to run with the resolved list type and model
 *            list, not <code>null</code>
 * @return the value returned by the callback
 */
public static <R> R resolveListAndRun(TemplateModel model, String modelPath, BiFunction<ListModelType<?>, ModelList, R> callback) {
    assert model != null;
    assert modelPath != null;
    assert callback != null;
    BeanModelType<?> modelType = TemplateModelProxyHandler.getModelTypeForProxy(model);
    ModelType listType = modelType.resolveType(modelPath);
    if (listType instanceof ListModelType<?>) {
        StateNode stateNode = TemplateModelProxyHandler.getStateNodeForProxy(model);
        ElementPropertyMap modelMap = ElementPropertyMap.getModel(stateNode);
        ModelList modelList = modelMap.resolveModelList(modelPath);
        return callback.apply((ListModelType<?>) listType, modelList);
    } else {
        throw new IllegalArgumentException(modelPath + " does not resolve to a list");
    }
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap)

Example 9 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class BeanModelTypeTest method importBean_incompatibleBean.

@Test(expected = IllegalArgumentException.class)
public void importBean_incompatibleBean() {
    BeanModelType<Bean> beanType = new BeanModelType<>(Bean.class, PropertyFilter.ACCEPT_ALL, false);
    ElementPropertyMap model = createEmptyModel();
    DifferentBean bean = new DifferentBean(3);
    beanType.importProperties(model, bean, PropertyFilter.ACCEPT_ALL);
}
Also used : ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 10 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class BeanModelTypeTest method clientValueToApplication.

@Test
public void clientValueToApplication() {
    BeanModelType<Bean> beanType = new BeanModelType<>(Bean.class, PropertyFilter.ACCEPT_ALL, false);
    ElementPropertyMap model = createEmptyModel();
    model.setProperty("doubleValue", JsonCodec.decodeWithoutTypeInfo(Json.create(3)));
    model.setProperty("doubleObject", JsonCodec.decodeWithoutTypeInfo(Json.create(3)));
    model.setProperty("intValue", JsonCodec.decodeWithoutTypeInfo(Json.create(3)));
    model.setProperty("intObject", JsonCodec.decodeWithoutTypeInfo(Json.create(3)));
    model.setProperty("booleanValue", JsonCodec.decodeWithoutTypeInfo(Json.create(true)));
    model.setProperty("booleanObject", JsonCodec.decodeWithoutTypeInfo(Json.create(true)));
    model.setProperty("string", JsonCodec.decodeWithoutTypeInfo(Json.create("3")));
    Bean bean = beanType.modelToApplication(model.getNode());
    Assert.assertEquals(3.0, bean.getDoubleValue(), 0);
    Assert.assertEquals(3.0, bean.getDoubleObject(), 0);
    Assert.assertEquals(3, bean.getIntValue());
    Assert.assertEquals(3, bean.getIntObject().intValue());
    Assert.assertEquals(Boolean.TRUE, bean.getBooleanObject());
    Assert.assertTrue(bean.isBooleanValue());
    Assert.assertEquals("3", bean.getString());
}
Also used : ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)45 Test (org.junit.Test)35 StateNode (com.vaadin.flow.internal.StateNode)21 HashSet (java.util.HashSet)9 Element (com.vaadin.flow.dom.Element)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)6 Json (elemental.json.Json)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Assert (org.junit.Assert)6 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)5 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)5 JsonObject (elemental.json.JsonObject)5 Serializable (java.io.Serializable)5 Arrays (java.util.Arrays)5 UI (com.vaadin.flow.component.UI)4 JsonCodec (com.vaadin.flow.internal.JsonCodec)4 JsonArray (elemental.json.JsonArray)4