Search in sources :

Example 1 with ElementData

use of com.vaadin.flow.internal.nodefeature.ElementData 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 2 with ElementData

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

the class AttachTemplateChildRpcHandlerTest method assertHandleNode.

private void assertHandleNode(int assignedId, JsonValue id) {
    AttachTemplateChildRpcHandler handler = new AttachTemplateChildRpcHandler();
    int requestedId = 1;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, assignedId);
    object.put(JsonConstants.RPC_ATTACH_ID, id);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode parentNode = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(node.getParent()).thenReturn(parentNode);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(node);
    ElementData data = new ElementData(node);
    data.setTag("foo");
    Mockito.when(node.getFeature(ElementData.class)).thenReturn(data);
    Mockito.when(parentNode.getId()).thenReturn(3);
    handler.handleNode(node, object);
}
Also used : AttachTemplateChildRpcHandler(com.vaadin.flow.server.communication.rpc.AttachTemplateChildRpcHandler) StateTree(com.vaadin.flow.internal.StateTree) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementData(com.vaadin.flow.internal.nodefeature.ElementData)

Example 3 with ElementData

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

the class BasicElementStateProvider method visit.

@Override
public void visit(StateNode node, NodeVisitor visitor) {
    Element element = Element.get(node);
    ElementData data = node.getFeature(ElementData.class);
    JsonValue payload = data.getPayload();
    boolean visitDescendants;
    if (payload instanceof JsonObject) {
        JsonObject object = (JsonObject) payload;
        String type = object.getString(NodeProperties.TYPE);
        if (NodeProperties.IN_MEMORY_CHILD.equals(type)) {
            visitDescendants = visitor.visit(NodeVisitor.ElementType.VIRTUAL, element);
        } else if (NodeProperties.INJECT_BY_ID.equals(type) || NodeProperties.TEMPLATE_IN_TEMPLATE.equals(type)) {
            visitDescendants = visitor.visit(NodeVisitor.ElementType.VIRTUAL_ATTACHED, element);
        } else {
            throw new IllegalStateException("Unexpected payload type : " + type);
        }
    } else if (payload == null) {
        visitDescendants = visitor.visit(NodeVisitor.ElementType.REGULAR, element);
    } else {
        throw new IllegalStateException("Unexpected payload in element data : " + payload.toJson());
    }
    if (visitDescendants) {
        visitDescendants(element, visitor);
        element.getShadowRoot().ifPresent(root -> root.accept(visitor));
    }
}
Also used : Element(com.vaadin.flow.dom.Element) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) ElementData(com.vaadin.flow.internal.nodefeature.ElementData)

Example 4 with ElementData

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

the class StateNodeTest method nodeContainsDefinedFeatures.

@Test
public void nodeContainsDefinedFeatures() {
    StateNode node = new StateNode(ElementData.class);
    Assert.assertTrue("Should have feature defined in constructor", node.hasFeature(ElementData.class));
    ElementData feature = node.getFeature(ElementData.class);
    Assert.assertNotNull("Existing feature should also be available", feature);
    Assert.assertFalse("Should not have a feature that wasn't defined in constructor", node.hasFeature(ElementPropertyMap.class));
}
Also used : ElementData(com.vaadin.flow.internal.nodefeature.ElementData) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

ElementData (com.vaadin.flow.internal.nodefeature.ElementData)4 Element (com.vaadin.flow.dom.Element)2 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)2 JsonObject (elemental.json.JsonObject)2 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 ElementFactory (com.vaadin.flow.dom.ElementFactory)1 StateNode (com.vaadin.flow.internal.StateNode)1 StateTree (com.vaadin.flow.internal.StateTree)1 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)1 NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)1 NodeChange (com.vaadin.flow.internal.change.NodeChange)1 NodeDetachChange (com.vaadin.flow.internal.change.NodeDetachChange)1 ElementAttributeMap (com.vaadin.flow.internal.nodefeature.ElementAttributeMap)1 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)1 ElementClassList (com.vaadin.flow.internal.nodefeature.ElementClassList)1 NodeFeature (com.vaadin.flow.internal.nodefeature.NodeFeature)1 VisibilityData (com.vaadin.flow.internal.nodefeature.VisibilityData)1 AttachTemplateChildRpcHandler (com.vaadin.flow.server.communication.rpc.AttachTemplateChildRpcHandler)1 Registration (com.vaadin.flow.shared.Registration)1