Search in sources :

Example 6 with NodeChange

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

the class ElementDataTest method collectChanges_setTagOnly_onlyOneChange.

@Test
public void collectChanges_setTagOnly_onlyOneChange() {
    elementData.setTag("foo");
    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.TAG, change.getKey());
    Assert.assertEquals(elementData.getNode(), change.getNode());
    Assert.assertEquals("foo", change.getValue());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with NodeChange

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

the class NodeListAddRemoveTest method verifyCleared.

private void verifyCleared(List<NodeChange> changes) {
    Assert.assertEquals(1, changes.size());
    NodeChange nodeChange = changes.get(0);
    Assert.assertThat(nodeChange, CoreMatchers.instanceOf(ListClearChange.class));
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ListClearChange(com.vaadin.flow.internal.change.ListClearChange)

Example 8 with NodeChange

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

the class NodeListAddRemoveTest method verifyAdded.

private void verifyAdded(List<NodeChange> changes, List<String> items, Integer... indexes) {
    for (int i = 0; i < indexes.length; i++) {
        NodeChange nodeChange = changes.get(i);
        Assert.assertThat(nodeChange, CoreMatchers.instanceOf(ListAddChange.class));
        ListAddChange<?> change = (ListAddChange<?>) nodeChange;
        Assert.assertEquals(indexes[i].intValue(), change.getIndex());
        Assert.assertEquals(1, change.getNewItems().size());
        Assert.assertEquals(items.get(i), change.getNewItems().get(0));
    }
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ListAddChange(com.vaadin.flow.internal.change.ListAddChange)

Example 9 with NodeChange

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

the class ElementData method collectChanges.

@Override
public void collectChanges(Consumer<NodeChange> collector) {
    List<NodeChange> changes = new ArrayList<>(1);
    super.collectChanges(changes::add);
    Serializable[] previousValue;
    Serializable tracker = getNode().getChangeTracker(this, () -> null);
    if (tracker instanceof Serializable[]) {
        previousValue = (Serializable[]) tracker;
    } else {
        previousValue = new Serializable[2];
    }
    NodeChange change = changes.get(0);
    if (change instanceof MapPutChange) {
        if (!Objects.equals(previousValue[0], getTag())) {
            collector.accept(new MapPutChange(this, getKey(), getTag()));
        }
        if (!Objects.equals(previousValue[1], getPayload())) {
            collector.accept(new MapPutChange(this, NodeProperties.PAYLOAD, getPayload()));
        }
    } else if (change instanceof EmptyChange) {
        collector.accept(change);
    }
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) EmptyChange(com.vaadin.flow.internal.change.EmptyChange)

Example 10 with NodeChange

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

the class StateNodeTest method assertCollectChanges_initiallyVisible.

private void assertCollectChanges_initiallyVisible(StateNode stateNode, ElementPropertyMap properties, Consumer<Boolean> activityUpdater) {
    VisibilityData visibility = stateNode.getFeature(VisibilityData.class);
    // check that normal flow works as it should (without any inactivity)
    properties.setProperty("foo", "bar");
    List<NodeChange> changes = new ArrayList<>();
    stateNode.collectChanges(changes::add);
    Assert.assertEquals(2, changes.size());
    // node is attached event
    Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(NodeAttachChange.class));
    // the property update event
    Assert.assertThat(changes.get(1), CoreMatchers.instanceOf(MapPutChange.class));
    changes.clear();
    // now make the node inactive via the VisibiltyData
    activityUpdater.accept(false);
    // now the node becomes inactive and should send only changes for
    // VisibiltyData, but don't loose changes for other features
    properties.setProperty("foo", "baz");
    TestStateTree tree = (TestStateTree) stateNode.getOwner();
    tree.dirtyNodes.clear();
    stateNode.collectChanges(changes::add);
    // 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();
    // The only possible change is visibility value change
    Assert.assertEquals(visibilityChanged ? 1 : 0, changes.size());
    MapPutChange change;
    if (visibilityChanged) {
        Assert.assertEquals(0, tree.dirtyNodes.size());
        Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(MapPutChange.class));
        change = (MapPutChange) changes.get(0);
        Assert.assertEquals(VisibilityData.class, change.getFeature());
    } 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);
    }
    changes.clear();
    // make the node active again
    activityUpdater.accept(true);
    stateNode.collectChanges(changes::add);
    // Two possible changes: probable visibility value change and property
    // update change
    Assert.assertEquals(visibilityChanged ? 2 : 1, changes.size());
    Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(MapPutChange.class));
    change = (MapPutChange) changes.get(0);
    MapPutChange propertyChange;
    if (visibilityChanged) {
        MapPutChange visibilityChange = VisibilityData.class.equals(change.getFeature()) ? change : (MapPutChange) changes.get(1);
        propertyChange = change.equals(visibilityChange) ? (MapPutChange) changes.get(1) : change;
    } else {
        propertyChange = change;
    }
    Assert.assertEquals(ElementPropertyMap.class, propertyChange.getFeature());
    Assert.assertEquals("baz", propertyChange.getValue());
    // 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) 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)

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