Search in sources :

Example 16 with ElementPropertyMap

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

the class TemplateModelTest method setBeanIncludeProperties.

@Test
public void setBeanIncludeProperties() {
    TemplateWithInclude template = new TemplateWithInclude();
    template.getModel().setBean(new Bean(123));
    ElementPropertyMap modelMap = getModelMap(template, "bean");
    Set<String> mapKeys = getKeys(modelMap);
    assertTrue("Model should contain included 'doubleValue'", mapKeys.remove("doubleValue"));
    assertTrue("Model should contain included 'booleanObject'", mapKeys.remove("booleanObject"));
    assertTrue("model should be empty but contains: " + mapKeys, mapKeys.isEmpty());
}
Also used : ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 17 with ElementPropertyMap

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

the class TemplateModelTest method setListWithSubBeanInclude.

@Test
public void setListWithSubBeanInclude() {
    TemplateWithIncludeOnListSubBean template = new TemplateWithIncludeOnListSubBean();
    List<BeanContainingBeans> beanContainingBeans = new ArrayList<>();
    beanContainingBeans.add(new BeanContainingBeans(new Bean(11), new Bean(12)));
    beanContainingBeans.add(new BeanContainingBeans(null, new Bean(22)));
    template.getModel().setBeanContainingBeans(beanContainingBeans);
    ModelList modelList = getModelList(template, "beanContainingBeans");
    Assert.assertEquals(2, modelList.size());
    ElementPropertyMap container1Map = ElementPropertyMap.getModel(modelList.get(0));
    ElementPropertyMap container2Map = ElementPropertyMap.getModel(modelList.get(1));
    Set<String> bean1bean2 = new HashSet<>();
    bean1bean2.add("bean1");
    bean1bean2.add("bean2");
    Assert.assertEquals(bean1bean2, container1Map.getPropertyNames().collect(Collectors.toSet()));
    Assert.assertEquals(bean1bean2, container2Map.getPropertyNames().collect(Collectors.toSet()));
    Set<String> container1Bean1Properties = getKeys(container1Map.resolveModelMap("bean1"));
    assertTrue(container1Bean1Properties.remove("intValue"));
    Assert.assertEquals(0, container1Bean1Properties.size());
    Set<String> container1Bean2Properties = getKeys(container1Map.resolveModelMap("bean2"));
    assertTrue(container1Bean2Properties.remove("booleanValue"));
    Assert.assertEquals(0, container1Bean2Properties.size());
    Set<String> container2Bean1Properties = getKeys(container2Map.resolveModelMap("bean1"));
    // Null value in the initial bean implies not imported or created
    Assert.assertEquals(0, container2Bean1Properties.size());
    Set<String> container2Bean2Properties = getKeys(container2Map.resolveModelMap("bean2"));
    assertTrue(container2Bean2Properties.remove("booleanValue"));
    Assert.assertEquals(0, container2Bean2Properties.size());
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ArrayList(java.util.ArrayList) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with ElementPropertyMap

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

the class TemplateModelTest method setBeanIncludeAndExcludeProperties.

@Test
public void setBeanIncludeAndExcludeProperties() {
    TemplateWithExcludeAndIncludeImpl template = new TemplateWithExcludeAndIncludeImpl();
    template.getModel().setBean(new Bean(123));
    ElementPropertyMap modelMap = getModelMap(template, "bean");
    assertTrue(modelMap.hasProperty("booleanObject"));
    Assert.assertEquals(1, modelMap.getPropertyNames().count());
}
Also used : ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 19 with ElementPropertyMap

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

the class StateNodeTest method assertCollectChanges_initiallyInactive.

private void assertCollectChanges_initiallyInactive(StateNode stateNode, ElementPropertyMap properties, Consumer<Boolean> activityUpdater) {
    TestStateTree tree = (TestStateTree) stateNode.getOwner();
    tree.dirtyNodes.clear();
    ElementData visibility = stateNode.getFeature(ElementData.class);
    activityUpdater.accept(false);
    properties.setProperty("foo", "bar");
    // activity updater may modify visibility of the node itself or its
    // ancestor. The number of changes will depend on whether the subject
    // node is visible or not
    boolean visibilityChanged = !visibility.isVisible();
    List<NodeChange> changes = new ArrayList<>();
    stateNode.collectChanges(changes::add);
    if (visibilityChanged) {
        Assert.assertEquals(1, tree.dirtyNodes.size());
        MatcherAssert.assertThat(tree.dirtyNodes, CoreMatchers.hasItem(stateNode));
    } else {
        // the target node should be marked as dirty because it's visible
        // but its parent is inactive
        Assert.assertEquals(2, tree.dirtyNodes.size());
        stateNode.visitNodeTree(node -> MatcherAssert.assertThat(tree.dirtyNodes, CoreMatchers.hasItem(node)));
    }
    Assert.assertEquals(visibilityChanged ? 3 : 2, changes.size());
    // node is attached event
    MatcherAssert.assertThat(changes.get(0), CoreMatchers.instanceOf(NodeAttachChange.class));
    // tag update (ElementData is reported feature) and possible active
    // state update
    Optional<MapPutChange> tagFound = changes.stream().filter(MapPutChange.class::isInstance).map(MapPutChange.class::cast).filter(chang -> chang.getKey().equals("tag")).findFirst();
    Assert.assertTrue("No tag change found", tagFound.isPresent());
    MapPutChange tagChange = tagFound.get();
    MapPutChange change = (MapPutChange) changes.get(1);
    if (visibilityChanged) {
        MatcherAssert.assertThat(changes.get(2), CoreMatchers.instanceOf(MapPutChange.class));
        change = tagChange.equals(change) ? (MapPutChange) changes.get(2) : change;
    }
    Assert.assertEquals(Element.get(stateNode).getTag(), tagChange.getValue());
    if (visibilityChanged) {
        Assert.assertEquals(Boolean.FALSE, change.getValue());
    }
    changes.clear();
    // now the node becomes active and should send all values from all
    // features (including values that has not been sent previously).
    activityUpdater.accept(true);
    properties.setProperty("baz", "foo");
    stateNode.collectChanges(changes::add);
    Assert.assertEquals(visibilityChanged ? 3 : 2, changes.size());
    // node is attached event
    // property updates and possible visibility update
    MatcherAssert.assertThat(changes.get(1), CoreMatchers.instanceOf(MapPutChange.class));
    Optional<MapPutChange> visibilityChange = changes.stream().filter(MapPutChange.class::isInstance).map(MapPutChange.class::cast).filter(chang -> chang.getFeature().equals(ElementData.class)).findFirst();
    if (visibilityChanged) {
        Assert.assertTrue(visibilityChange.isPresent());
        Assert.assertTrue((Boolean) visibilityChange.get().getValue());
        changes.remove(visibilityChange.get());
    }
    Optional<MapPutChange> fooUpdate = changes.stream().filter(MapPutChange.class::isInstance).map(MapPutChange.class::cast).filter(chang -> chang.getKey().equals("foo")).findFirst();
    Assert.assertTrue(fooUpdate.isPresent());
    Assert.assertEquals("bar", fooUpdate.get().getValue());
    changes.remove(fooUpdate.get());
    change = (MapPutChange) changes.get(0);
    Assert.assertEquals("foo", change.getValue());
    Assert.assertEquals("baz", change.getKey());
    // Don't make any changes, check that there are no changes collected
    changes.clear();
    stateNode.collectChanges(changes::add);
    Assert.assertEquals(0, changes.size());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) IntStream(java.util.stream.IntStream) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) InertData(com.vaadin.flow.internal.nodefeature.InertData) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) Registration(com.vaadin.flow.shared.Registration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Element(com.vaadin.flow.dom.Element) UI(com.vaadin.flow.component.UI) LinkedList(java.util.LinkedList) ElementAttributeMap(com.vaadin.flow.internal.nodefeature.ElementAttributeMap) NodeDetachChange(com.vaadin.flow.internal.change.NodeDetachChange) ElementFactory(com.vaadin.flow.dom.ElementFactory) ElementClassList(com.vaadin.flow.internal.nodefeature.ElementClassList) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) Consumer(java.util.function.Consumer) NodeChange(com.vaadin.flow.internal.change.NodeChange) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) List(java.util.List) MapPutChange(com.vaadin.flow.internal.change.MapPutChange) Stream(java.util.stream.Stream) MatcherAssert(org.hamcrest.MatcherAssert) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Optional(java.util.Optional) Assert(org.junit.Assert) NodeChange(com.vaadin.flow.internal.change.NodeChange) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) ArrayList(java.util.ArrayList) ElementData(com.vaadin.flow.internal.nodefeature.ElementData)

Example 20 with ElementPropertyMap

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

the class StateNodeTest method collectChanges_inactivateViaParent_initiallyActiveElement_sendOnlyDisalowFeatureChangesWhenInactive.

@Test
public void collectChanges_inactivateViaParent_initiallyActiveElement_sendOnlyDisalowFeatureChangesWhenInactive() {
    StateNode stateNode = createTestNode("Active node", ElementPropertyMap.class, ElementData.class);
    StateNode parent = createTestNode("Parent node", ElementPropertyMap.class, ElementData.class, ElementChildrenList.class);
    parent.getFeature(ElementChildrenList.class).add(0, stateNode);
    ElementData visibility = parent.getFeature(ElementData.class);
    ElementPropertyMap properties = stateNode.getFeature(ElementPropertyMap.class);
    TestStateTree tree = new TestStateTree();
    // attach the node to be able to get changes
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0, parent);
    assertCollectChanges_initiallyVisible(stateNode, properties, isVisible -> {
        visibility.setVisible(isVisible);
        parent.updateActiveState();
    });
}
Also used : ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) 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