Search in sources :

Example 21 with Element

use of com.vaadin.flow.dom.Element in project flow by vaadin.

the class ViewRendererTest method showNestedView.

@Test
public void showNestedView() {
    new TestViewRenderer(TestView.class, ParentView.class, AnotherParentView.class).handle(dummyEvent);
    List<View> viewChain = ui.getInternals().getActiveViewChain();
    Assert.assertEquals(3, viewChain.size());
    Assert.assertEquals(Arrays.asList(TestView.class, ParentView.class, AnotherParentView.class), viewChain.stream().map(Object::getClass).collect(Collectors.toList()));
    Element element = null;
    for (View view : viewChain) {
        Assert.assertEquals(Arrays.asList(dummyEvent.getLocation()), ((TestView) view).locations);
        Element viewElement = view.getElement();
        if (element != null) {
            Assert.assertEquals(viewElement, element.getParent());
        }
        element = viewElement;
    }
    Assert.assertEquals(ui.getElement(), element.getParent());
    Assert.assertEquals(1, ui.getElement().getChildCount());
}
Also used : TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) Element(com.vaadin.flow.dom.Element) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Test(org.junit.Test)

Example 22 with Element

use of com.vaadin.flow.dom.Element in project flow by vaadin.

the class ViewRendererTest method showNestedView.

@Test
public void showNestedView() {
    new TestViewRenderer(TestView.class, ParentView.class, AnotherParentView.class).handle(dummyEvent);
    List<View> viewChain = ui.getInternals().getActiveViewChain();
    Assert.assertEquals(3, viewChain.size());
    Assert.assertEquals(Arrays.asList(TestView.class, ParentView.class, AnotherParentView.class), viewChain.stream().map(Object::getClass).collect(Collectors.toList()));
    Element element = null;
    for (View view : viewChain) {
        Assert.assertEquals(Arrays.asList(dummyEvent.getLocation()), ((TestView) view).locations);
        Element viewElement = view.getElement();
        if (element != null) {
            Assert.assertEquals(viewElement, element.getParent());
        }
        element = viewElement;
    }
    Assert.assertEquals(ui.getElement(), element.getParent());
    Assert.assertEquals(1, ui.getElement().getChildCount());
}
Also used : Element(com.vaadin.flow.dom.Element) HasChildView(com.vaadin.flow.router.legacy.HasChildView) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) View(com.vaadin.flow.router.legacy.View) Test(org.junit.Test)

Example 23 with Element

use of com.vaadin.flow.dom.Element in project flow by vaadin.

the class AbstractRpcInvocationHandlerTest method handleVisibleNode_nodeIsHandled.

@Test
public void handleVisibleNode_nodeIsHandled() {
    UI ui = new UI();
    Element element = createRpcInvocationData(ui, null);
    Assert.assertSame(element.getNode(), handler.node);
}
Also used : UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 24 with Element

use of com.vaadin.flow.dom.Element 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 25 with Element

use of com.vaadin.flow.dom.Element 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

Element (com.vaadin.flow.dom.Element)377 Test (org.junit.Test)322 UI (com.vaadin.flow.component.UI)42 StateNode (com.vaadin.flow.internal.StateNode)32 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)29 Style (com.vaadin.flow.dom.Style)25 StreamResource (com.vaadin.flow.server.StreamResource)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 ArrayList (java.util.ArrayList)14 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)13 JsonObject (elemental.json.JsonObject)12 WeakReference (java.lang.ref.WeakReference)12 URI (java.net.URI)11 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)10 Registration (com.vaadin.flow.shared.Registration)10 JsonValue (elemental.json.JsonValue)10 ShadowRoot (com.vaadin.flow.dom.ShadowRoot)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)8 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)8