Search in sources :

Example 1 with NodeChange

use of com.vaadin.flow.internal.change.NodeChange in project flow by vaadin.

the class TemplateModelTest method testSetterSameValue_noUpdates.

@Test
public void testSetterSameValue_noUpdates() {
    BasicTypeModelTemplate template = new BasicTypeModelTemplate();
    BasicTypeModel model = template.getModel();
    // Initial populate properties model changes. Clear them out.
    template.getElement().getNode().clearChanges();
    model.setString("foobar");
    Assert.assertEquals("foobar", model.getString());
    List<NodeChange> changes = new ArrayList<>();
    ModelMap modelMap = template.getElement().getNode().getFeature(ModelMap.class);
    modelMap.collectChanges(changes::add);
    Assert.assertEquals(1, changes.size());
    Assert.assertEquals(template.getElement().getNode(), changes.get(0).getNode());
    changes.clear();
    template.getElement().getNode().clearChanges();
    model.setString("foobar");
    Assert.assertEquals("foobar", model.getString());
    modelMap.collectChanges(changes::add);
    Assert.assertEquals(0, changes.size());
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with NodeChange

use of com.vaadin.flow.internal.change.NodeChange in project flow by vaadin.

the class TemplateModelTest method testSetterSameValue_noUpdates.

@Test
public void testSetterSameValue_noUpdates() {
    BasicTypeModelTemplate template = new BasicTypeModelTemplate();
    BasicTypeModel model = template.getModel();
    // Initial populate properties model changes. Clear them out.
    template.getElement().getNode().clearChanges();
    model.setString("foobar");
    Assert.assertEquals("foobar", model.getString());
    List<NodeChange> changes = new ArrayList<>();
    ElementPropertyMap modelMap = template.getElement().getNode().getFeature(ElementPropertyMap.class);
    modelMap.collectChanges(changes::add);
    Assert.assertEquals(1, changes.size());
    Assert.assertEquals(template.getElement().getNode(), changes.get(0).getNode());
    changes.clear();
    template.getElement().getNode().clearChanges();
    model.setString("foobar");
    Assert.assertEquals("foobar", model.getString());
    modelMap.collectChanges(changes::add);
    Assert.assertEquals(0, changes.size());
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ArrayList(java.util.ArrayList) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 3 with NodeChange

use of com.vaadin.flow.internal.change.NodeChange in project flow by vaadin.

the class StateNodeTest method assertCollectChanges_initiallyInactive.

private void assertCollectChanges_initiallyInactive(StateNode stateNode, ElementPropertyMap properties, Consumer<Boolean> activityUpdater) {
    VisibilityData visibility = stateNode.getFeature(VisibilityData.class);
    activityUpdater.accept(false);
    // 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();
    properties.setProperty("foo", "bar");
    TestStateTree tree = (TestStateTree) stateNode.getOwner();
    tree.dirtyNodes.clear();
    List<NodeChange> changes = new ArrayList<>();
    stateNode.collectChanges(changes::add);
    if (visibilityChanged) {
        Assert.assertEquals(0, tree.dirtyNodes.size());
    } else {
        // the target node should be marked as dirty because it's visible
        // but its parent is inactive
        Assert.assertEquals(1, tree.dirtyNodes.size());
        tree.dirtyNodes.contains(stateNode);
    }
    Assert.assertEquals(visibilityChanged ? 3 : 2, changes.size());
    // node is attached event
    Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(NodeAttachChange.class));
    // tag update (ElementData is reported feature) and possible visibility
    // update
    Assert.assertThat(changes.get(1), CoreMatchers.instanceOf(MapPutChange.class));
    MapPutChange change = (MapPutChange) changes.get(1);
    MapPutChange tagChange;
    if (visibilityChanged) {
        Assert.assertThat(changes.get(2), CoreMatchers.instanceOf(MapPutChange.class));
        tagChange = change.getFeature().equals(ElementData.class) ? change : (MapPutChange) changes.get(2);
        change = tagChange.equals(change) ? (MapPutChange) changes.get(2) : change;
    } else {
        tagChange = (MapPutChange) changes.get(1);
    }
    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
    Assert.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(VisibilityData.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) VisibilityData(com.vaadin.flow.internal.nodefeature.VisibilityData) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) Registration(com.vaadin.flow.shared.Registration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) 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) VisibilityData(com.vaadin.flow.internal.nodefeature.VisibilityData)

Example 4 with NodeChange

use of com.vaadin.flow.internal.change.NodeChange in project flow by vaadin.

the class ElementDataTest method collectChanges_setPayloadOnly_onlyOneChange.

@Test
public void collectChanges_setPayloadOnly_onlyOneChange() {
    JsonObject object = Json.createObject();
    elementData.setPayload(object);
    List<NodeChange> changes = new ArrayList<>();
    elementData.collectChanges(changes::add);
    Assert.assertEquals(1, changes.size());
    Assert.assertTrue(changes.get(0) instanceof MapPutChange);
    MapPutChange change = (MapPutChange) changes.get(0);
    Assert.assertEquals(NodeProperties.PAYLOAD, change.getKey());
    Assert.assertEquals(elementData.getNode(), change.getNode());
    Assert.assertEquals(object, change.getValue());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 5 with NodeChange

use of com.vaadin.flow.internal.change.NodeChange in project flow by vaadin.

the class ElementDataTest method collectChanges_setBothTagAndPayload_twoChanges.

@Test
public void collectChanges_setBothTagAndPayload_twoChanges() {
    JsonObject object = Json.createObject();
    elementData.setPayload(object);
    elementData.setTag("foo");
    List<NodeChange> changes = new ArrayList<>();
    elementData.collectChanges(changes::add);
    Assert.assertEquals(2, changes.size());
    Assert.assertTrue(changes.get(0) instanceof MapPutChange);
    Assert.assertTrue(changes.get(1) instanceof MapPutChange);
    MapPutChange change = (MapPutChange) changes.get(0);
    Assert.assertEquals(NodeProperties.TAG, change.getKey());
    Assert.assertEquals(elementData.getNode(), change.getNode());
    Assert.assertEquals("foo", change.getValue());
    change = (MapPutChange) changes.get(1);
    Assert.assertEquals(NodeProperties.PAYLOAD, change.getKey());
    Assert.assertEquals(elementData.getNode(), change.getNode());
    Assert.assertEquals(object, change.getValue());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Aggregations

NodeChange (com.vaadin.flow.internal.change.NodeChange)19 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)8 StateNode (com.vaadin.flow.internal.StateNode)5 ListAddChange (com.vaadin.flow.internal.change.ListAddChange)4 NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)4 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ListRemoveChange (com.vaadin.flow.internal.change.ListRemoveChange)3 EmptyChange (com.vaadin.flow.internal.change.EmptyChange)2 ListClearChange (com.vaadin.flow.internal.change.ListClearChange)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)2 VisibilityData (com.vaadin.flow.internal.nodefeature.VisibilityData)2 JsonObject (elemental.json.JsonObject)2 Serializable (java.io.Serializable)2 List (java.util.List)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2