Search in sources :

Example 26 with StateNode

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

the class StateNodeNodeListTest method testIndexOf.

@Test
public void testIndexOf() {
    StateNode one = StateNodeTest.createEmptyNode("one");
    StateNode two = StateNodeTest.createEmptyNode("two");
    StateNode three = StateNodeTest.createEmptyNode("three");
    nodeList.add(one);
    nodeList.add(two);
    Assert.assertEquals(0, nodeList.indexOf(one));
    Assert.assertEquals(1, nodeList.indexOf(two));
    Assert.assertEquals(-1, nodeList.indexOf(three));
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 27 with StateNode

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

the class VisibilityDataTest method setVisible.

@Test
public void setVisible() {
    StateNode node = new StateNode(VisibilityData.class);
    VisibilityData data = node.getFeature(VisibilityData.class);
    Assert.assertNull(data.getValue());
    Assert.assertTrue(data.isVisible());
    data.setValue(true);
    Assert.assertTrue(data.isVisible());
    data.setValue(false);
    Assert.assertFalse(data.isVisible());
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 28 with StateNode

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

the class AttachExistingElementRpcHandlerTest method handleNode_requestedIdAndAssignedIdAreDifferent.

@Test
public void handleNode_requestedIdAndAssignedIdAreDifferent() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    int assignedId = 2;
    int index = 3;
    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_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, index);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateNode assigned = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    Mockito.when(tree.getNodeById(assignedId)).thenReturn(assigned);
    Mockito.when(assigned.hasFeature(Mockito.any())).thenReturn(true);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    assertNodeIsUnregistered(node, requested, feature);
    Mockito.verify(parentNode, Mockito.times(0)).insertChild(index, Element.get(assigned));
    Mockito.verify(consumer).accept(Element.get(assigned));
}
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) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) AttachExistingElementRpcHandler(com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler) Test(org.junit.Test)

Example 29 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);
    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 30 with StateNode

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

the class MapSyncRpcHandlerTest method syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy.

@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ModelList modelList = propertyMap.resolveModelList("foo");
    // fake StateNode has been created for the model
    StateNode item = new StateNode(ElementPropertyMap.class);
    modelList.add(item);
    item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", item.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);
    // Now the model node should be copied and available as the
    // TEST_PROPERTY value
    Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertNotEquals(item.getId(), newNode.getId());
    Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class).getProperty("bar"));
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)160 Test (org.junit.Test)99 Element (com.vaadin.flow.dom.Element)29 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)19 JsonObject (elemental.json.JsonObject)19 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)18 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)16 ArrayList (java.util.ArrayList)14 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)13 Serializable (java.io.Serializable)13 StateTree (com.vaadin.flow.internal.StateTree)10 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)10 UI (com.vaadin.flow.component.UI)9 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)9 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)9 Bean (com.vaadin.flow.templatemodel.Bean)9 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)8 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)8 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)8 TemplateOverridesMap (com.vaadin.flow.internal.nodefeature.TemplateOverridesMap)6