Search in sources :

Example 1 with ElementChildrenList

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

the class StateTreeTest method reattachedNodeRetainsId.

@Test
public void reattachedNodeRetainsId() throws InterruptedException {
    StateNode child = new StateNode(ElementChildrenList.class);
    StateNode grandChild = new StateNode(ElementChildrenList.class);
    child.getFeature(ElementChildrenList.class).add(0, grandChild);
    ElementChildrenList children = tree.getRootNode().getFeature(ElementChildrenList.class);
    children.add(0, child);
    int childId = child.getId();
    int grandChildId = grandChild.getId();
    Assert.assertTrue(child.isAttached());
    Assert.assertTrue(grandChild.isAttached());
    Assert.assertSame(child, tree.getNodeById(childId));
    Assert.assertSame(grandChild, tree.getNodeById(grandChildId));
    children.remove(0);
    Assert.assertFalse(child.isAttached());
    Assert.assertFalse(grandChild.isAttached());
    Assert.assertNull(tree.getNodeById(childId));
    Assert.assertNull(tree.getNodeById(grandChildId));
    children.add(0, child);
    Assert.assertTrue(child.isAttached());
    Assert.assertTrue(grandChild.isAttached());
    Assert.assertEquals(childId, child.getId());
    Assert.assertEquals(grandChildId, grandChild.getId());
    Assert.assertSame(child, tree.getNodeById(childId));
    Assert.assertSame(grandChild, tree.getNodeById(grandChildId));
}
Also used : ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test)

Example 2 with ElementChildrenList

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

the class StateNodeTest method collectChanges_inertChildMoved_inertStateInherited.

@Test
public void collectChanges_inertChildMoved_inertStateInherited() {
    StateNode inertParent = createTestNode("Inert parent", ElementChildrenList.class, InertData.class);
    StateNode child = createTestNode("Child", InertData.class);
    StateNode parent = createTestNode("Non-inert parent", ElementChildrenList.class, InertData.class);
    final ElementChildrenList feature = new StateTree(new UI().getInternals(), ElementChildrenList.class, InertData.class).getRootNode().getFeature(ElementChildrenList.class);
    feature.add(0, parent);
    feature.add(1, inertParent);
    inertParent.getFeature(ElementChildrenList.class).add(0, child);
    inertParent.getFeature(InertData.class).setInertSelf(true);
    inertParent.collectChanges(node -> {
    });
    Assert.assertTrue(inertParent.isInert());
    Assert.assertTrue(child.isInert());
    Assert.assertFalse(parent.isInert());
    inertParent.getFeature(ElementChildrenList.class).remove(0);
    parent.getFeature(ElementChildrenList.class).add(0, child);
    Assert.assertTrue(inertParent.isInert());
    Assert.assertFalse(child.isInert());
    Assert.assertFalse(parent.isInert());
    parent.getFeature(ElementChildrenList.class).remove(0);
    inertParent.getFeature(ElementChildrenList.class).add(0, child);
    Assert.assertTrue(inertParent.isInert());
    Assert.assertTrue(child.isInert());
    Assert.assertFalse(parent.isInert());
}
Also used : UI(com.vaadin.flow.component.UI) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) InertData(com.vaadin.flow.internal.nodefeature.InertData) Test(org.junit.Test)

Example 3 with ElementChildrenList

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

the class StateNodeTest method detachParent_insertChildAsFirstOnDetach_noEvents.

@Test
public void detachParent_insertChildAsFirstOnDetach_noEvents() {
    TestStateTree tree = new TestStateTree();
    StateNode child = createEmptyNode("a");
    StateNode parent = createParentNode("parent");
    addChild(parent, child);
    addChild(tree.getRootNode(), parent);
    AtomicInteger events = new AtomicInteger();
    child.addDetachListener(() -> {
        StateNode b = createEmptyNode("b");
        b.addAttachListener(events::incrementAndGet);
        b.addDetachListener(events::incrementAndGet);
        ElementChildrenList list = parent.getFeature(ElementChildrenList.class);
        list.add(0, b);
    });
    removeFromParent(parent);
    Assert.assertEquals(0, events.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test)

Example 4 with ElementChildrenList

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

the class StateTreeTest method detachedNodeGarbageCollected.

@Test
public void detachedNodeGarbageCollected() throws InterruptedException {
    StateNode child = new StateNode(ElementChildrenList.class);
    StateNode grandChild = new StateNode(ElementChildrenList.class);
    child.getFeature(ElementChildrenList.class).add(0, grandChild);
    ElementChildrenList children = tree.getRootNode().getFeature(ElementChildrenList.class);
    children.add(0, child);
    WeakReference<StateNode> childRef = new WeakReference<>(child);
    child = null;
    WeakReference<StateNode> grandChildRef = new WeakReference<>(grandChild);
    grandChild = null;
    children.remove(0);
    tree.collectChanges(c -> {
    // nop
    });
    Assert.assertTrue(TestUtil.isGarbageCollected(childRef));
    Assert.assertTrue(TestUtil.isGarbageCollected(grandChildRef));
}
Also used : WeakReference(java.lang.ref.WeakReference) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test)

Example 5 with ElementChildrenList

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

the class AbstractNodeStateProvider method removeChild.

@Override
public void removeChild(StateNode node, Element child) {
    ElementChildrenList childrenFeature = getChildrenFeature(node);
    int pos = childrenFeature.indexOf(child.getNode());
    if (pos == -1) {
        throw new IllegalArgumentException("Trying to detach an element from parent that does not have it.");
    }
    childrenFeature.remove(pos);
}
Also used : ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList)

Aggregations

ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)7 Test (org.junit.Test)4 UI (com.vaadin.flow.component.UI)1 InertData (com.vaadin.flow.internal.nodefeature.InertData)1 WeakReference (java.lang.ref.WeakReference)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1