use of com.vaadin.flow.internal.nodefeature.VisibilityData in project flow by vaadin.
the class StateNodeTest method collectChanges_initiallyActiveElement_sendOnlyDisalowFeatureChangesWhenInactive.
@Test
public void collectChanges_initiallyActiveElement_sendOnlyDisalowFeatureChangesWhenInactive() {
StateNode stateNode = createTestNode("Active node", ElementPropertyMap.class, VisibilityData.class);
VisibilityData visibility = stateNode.getFeature(VisibilityData.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, stateNode);
assertCollectChanges_initiallyVisible(stateNode, properties, isVisible -> {
visibility.setVisible(isVisible);
stateNode.updateActiveState();
});
}
use of com.vaadin.flow.internal.nodefeature.VisibilityData 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, VisibilityData.class);
StateNode parent = createTestNode("Parent node", ElementPropertyMap.class, VisibilityData.class, ElementChildrenList.class);
parent.getFeature(ElementChildrenList.class).add(0, stateNode);
VisibilityData visibility = parent.getFeature(VisibilityData.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();
});
}
use of com.vaadin.flow.internal.nodefeature.VisibilityData 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());
}
use of com.vaadin.flow.internal.nodefeature.VisibilityData in project flow by vaadin.
the class StateNodeTest method collectChanges_initiallyInactiveViaParentElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive.
@Test
public void collectChanges_initiallyInactiveViaParentElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive() {
Element element = ElementFactory.createAnchor();
StateNode stateNode = element.getNode();
StateNode parent = createTestNode("Parent node", ElementPropertyMap.class, VisibilityData.class, ElementChildrenList.class);
parent.getFeature(ElementChildrenList.class).add(0, stateNode);
VisibilityData visibility = parent.getFeature(VisibilityData.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_initiallyInactive(stateNode, properties, isVisible -> {
visibility.setVisible(isVisible);
parent.updateActiveState();
});
}
use of com.vaadin.flow.internal.nodefeature.VisibilityData in project flow by vaadin.
the class StateNodeTest method collectChanges_initiallyInactiveElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive.
@Test
public void collectChanges_initiallyInactiveElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive() {
Element element = ElementFactory.createAnchor();
StateNode stateNode = element.getNode();
VisibilityData visibility = stateNode.getFeature(VisibilityData.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, stateNode);
assertCollectChanges_initiallyInactive(stateNode, properties, isVisible -> {
visibility.setVisible(isVisible);
stateNode.updateActiveState();
});
}
Aggregations