Search in sources :

Example 51 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class AttachExistingElementRpcHandler method handleNode.

@Override
protected Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_TAG_NAME);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_INDEX);
    int requestedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    int assignedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    String tag = invocationJson.getString(JsonConstants.RPC_ATTACH_TAG_NAME);
    int index = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_INDEX);
    AttachExistingElementFeature feature = node.getFeature(AttachExistingElementFeature.class);
    StateTree tree = (StateTree) node.getOwner();
    StateNode requestedNode = tree.getNodeById(requestedId);
    if (assignedId == -1) {
        // handle an error
        assert index == -1;
        ChildElementConsumer callback = feature.getCallback(requestedNode);
        assert callback != null;
        callback.onError(feature.getParent(requestedNode), tag, feature.getPreviousSibling(requestedNode));
        feature.unregister(requestedNode);
    } else {
        Element element = Element.get(tree.getNodeById(assignedId));
        attachElement(feature, element, index, tree.getNodeById(requestedId), requestedId == assignedId);
    }
    return Optional.empty();
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)

Example 52 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class ReturnChannelHandlerTest method noReturnChannelMap_invocationIgnored.

@Test
public void noReturnChannelMap_invocationIgnored() {
    StateNode nodeWithoutMap = new StateNode();
    ui.getElement().getNode().getFeature(ElementChildrenList.class).add(0, nodeWithoutMap);
    handleMessage(nodeWithoutMap.getId(), 0);
// Nothing to assert, just checking that no exception is thrown
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test)

Example 53 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class MapSyncRpcHandlerTest method handleNode_callsElementPropertyMapDeferredUpdateFromClient.

@Test
public void handleNode_callsElementPropertyMapDeferredUpdateFromClient() {
    AtomicInteger deferredUpdateInvocations = new AtomicInteger();
    AtomicReference<String> deferredKey = new AtomicReference<>();
    StateNode node = new StateNode(ElementPropertyMap.class) {

        private ElementPropertyMap map = new ElementPropertyMap(this) {

            @Override
            public Runnable deferredUpdateFromClient(String key, Serializable value) {
                deferredUpdateInvocations.incrementAndGet();
                deferredKey.set(key);
                return () -> {
                };
            }
        };

        @Override
        public <F extends NodeFeature> F getFeature(Class<F> featureType) {
            if (featureType.equals(ElementPropertyMap.class)) {
                return featureType.cast(map);
            }
            return super.getFeature(featureType);
        }
    };
    new MapSyncRpcHandler().handleNode(node, createSyncPropertyInvocation(node, TEST_PROPERTY, NEW_VALUE));
    Assert.assertEquals(1, deferredUpdateInvocations.get());
    Assert.assertEquals(TEST_PROPERTY, deferredKey.get());
}
Also used : Serializable(java.io.Serializable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) StateNode(com.vaadin.flow.internal.StateNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 54 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class MapSyncRpcHandlerTest method propertyIsNotExplicitlyAllowed_subproperty_throwsWithComponentInfo.

@Test
public void propertyIsNotExplicitlyAllowed_subproperty_throwsWithComponentInfo() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage(CoreMatchers.allOf(CoreMatchers.containsString("Component " + TestComponent.class.getName()), CoreMatchers.containsString("'" + TEST_PROPERTY + "'")));
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    StateNode node = element.getNode();
    ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class).resolveModelMap("foo.bar");
    new MapSyncRpcHandler().handleNode(propertyMap.getNode(), createSyncPropertyInvocation(propertyMap.getNode(), TEST_PROPERTY, NEW_VALUE));
}
Also used : Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 55 with StateNode

use of com.vaadin.flow.internal.StateNode 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);
    Mockito.when(node.getChangeTracker(Mockito.any(), Mockito.any())).thenReturn(new HashMap<>());
    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 : StateTree(com.vaadin.flow.internal.StateTree) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementData(com.vaadin.flow.internal.nodefeature.ElementData)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)196 Test (org.junit.Test)122 Element (com.vaadin.flow.dom.Element)32 JsonObject (elemental.json.JsonObject)24 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)22 UI (com.vaadin.flow.component.UI)19 StateTree (com.vaadin.flow.internal.StateTree)18 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)18 ArrayList (java.util.ArrayList)18 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)17 Serializable (java.io.Serializable)17 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)16 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)10 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)9 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)9 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)9 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)9 Bean (com.vaadin.flow.templatemodel.Bean)9 HashMap (java.util.HashMap)9 Collectors (java.util.stream.Collectors)9