Search in sources :

Example 66 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class ModelDescriptorTest method assertComplexModeType.

private <T extends Serializable> void assertComplexModeType(Class<T> clazz, T wrappedValue, T value) {
    Optional<ComplexModelType<?>> type = BasicComplexModelType.get(clazz);
    StateNode stateNode = type.get().applicationToModel(wrappedValue, null);
    Assert.assertEquals(wrappedValue, stateNode.getFeature(BasicTypeValue.class).getValue());
    stateNode.getFeature(BasicTypeValue.class).setValue(value);
    Object modelValue = type.get().modelToApplication(stateNode);
    Assert.assertEquals(value, modelValue);
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) BasicTypeValue(com.vaadin.flow.internal.nodefeature.BasicTypeValue)

Example 67 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class ModelDescriptorTest method basicComplexModelType_intAndInteger_numberFromClient.

@Test
public void basicComplexModelType_intAndInteger_numberFromClient() {
    ComplexModelType<?> type = BasicComplexModelType.get(Integer.class).get();
    StateNode stateNode = type.applicationToModel(2.0d, null);
    Object applicationValue = type.modelToApplication(stateNode);
    Assert.assertEquals(Integer.valueOf(2), applicationValue);
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 68 with StateNode

use of com.vaadin.flow.internal.StateNode 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 69 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class BeanModelTypeTest method applicationToModel.

@Test
public void applicationToModel() {
    BeanModelType<Bean> beanType = new BeanModelType<>(Bean.class, PropertyFilter.ACCEPT_ALL, false);
    Bean bean = new Bean(3);
    StateNode applicationToModel = beanType.applicationToModel(bean, PropertyFilter.ACCEPT_ALL);
    ElementPropertyMap model = ElementPropertyMap.getModel(applicationToModel);
    assertThreeBean(model);
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 70 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class TemplateModelTest method beanModelType_emptySubBeanAsInitialValue.

@Test
public void beanModelType_emptySubBeanAsInitialValue() {
    SubBeansTemplate template = new SubBeansTemplate();
    // Check that even before calling any model method the properties are
    // available via features
    Serializable bean = template.getElement().getNode().getFeature(ElementPropertyMap.class).getProperty("bean");
    StateNode node = (StateNode) bean;
    Serializable subBean = node.getFeature(ElementPropertyMap.class).getProperty("bean");
    assertTrue(subBean instanceof StateNode);
    // Now check properties via API
    Assert.assertNotNull(template.getModel().getBean().getBean());
}
Also used : Serializable(java.io.Serializable) StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)196 Test (org.junit.Test)122 Element (com.vaadin.flow.dom.Element)32 JsonObject (elemental.json.JsonObject)24 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)22 UI (com.vaadin.flow.component.UI)19 StateTree (com.vaadin.flow.internal.StateTree)18 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)18 ArrayList (java.util.ArrayList)18 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)17 Serializable (java.io.Serializable)17 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)16 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)10 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)9 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)9 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)9 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)9 Bean (com.vaadin.flow.templatemodel.Bean)9 HashMap (java.util.HashMap)9 Collectors (java.util.stream.Collectors)9