Extend Yoga to be able clone Yoga Node with new children

Summary:
This diff includes the following changes:

1 ) I extended the Java implementation of YogaNode to be able to get a full copy of a YogaNode object without copying the List of children of the original YogaNode. In other words, the new copy of the YogaNode will have the same data of the original YogaNode, but with an empty list of children.

2 ) We created a new method in Yoga.cpp called YGNodeInsertSharedChild. This new method is going to be used by Fabric in order to temporarily share a YogaNode between two "Yoga Trees" (the "current Yoga" tree and a partial "clone of the current Yoga tree"). We exposed this new functionality in the java implementation of Yoga (method addSharedChildAt)

I'm including sebmarkbage for more context.

Reviewed By: emilsjolander

Differential Revision: D7245421

fbshipit-source-id: 72578c8261f29e4a12fc6c72a91f2f891cd58d48
This commit is contained in:
David Vacca
2018-04-01 18:27:08 -07:00
committed by Facebook Github Bot
parent f0edefdbb7
commit 5e3ffb39a2
5 changed files with 155 additions and 32 deletions

View File

@@ -10,6 +10,7 @@ package com.facebook.yoga;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -251,6 +252,48 @@ public class YogaNodeTest {
assertEquals(1, clonedChild.getChildCount());
}
@Test
public void testCloneWithNewChildren() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = new YogaNode(config);
YogaNode child = new YogaNode(config);
YogaNode grandChild = new YogaNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
YogaNode clonedChild = child.cloneWithNewChildren();
assertNotSame(clonedChild, child);
assertEquals(YogaFlexDirection.ROW, clonedChild.getFlexDirection());
assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection());
assertEquals(0, clonedChild.getChildCount());
assertEquals(1, child.getChildCount());
}
@Test
public void testAddSharedChildCloneWithNewChildren() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = new YogaNode(config);
YogaNode child = new YogaNode(config);
YogaNode grandChild = new YogaNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
YogaNode clonedChild = child.cloneWithNewChildren();
assertNotSame(clonedChild, child);
assertEquals(YogaFlexDirection.ROW, clonedChild.getFlexDirection());
assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection());
assertEquals(0, clonedChild.getChildCount());
assertEquals(1, child.getChildCount());
clonedChild.addSharedChildAt(grandChild, 0);
assertEquals(1, clonedChild.getChildCount());
assertNull(grandChild.getOwner());
}
@Test
public void testCloneNodeListener() throws Exception {
final AtomicBoolean onNodeClonedExecuted = new AtomicBoolean(false);
@@ -259,13 +302,8 @@ public class YogaNodeTest {
new YogaNodeCloneFunction() {
@Override
public YogaNode cloneNode(YogaNode oldNode, YogaNode owner, int childIndex) {
try {
onNodeClonedExecuted.set(true);
return oldNode.clone();
} catch (CloneNotSupportedException ex) {
// DO nothing
return null;
}
onNodeClonedExecuted.set(true);
return oldNode.clone();
}
});
YogaNode root = new YogaNode(config);
@@ -296,12 +334,7 @@ public class YogaNodeTest {
new YogaNodeCloneFunction() {
@Override
public YogaNode cloneNode(YogaNode oldNode, YogaNode owner, int childIndex) {
try {
return oldNode.clone();
} catch (CloneNotSupportedException ex) {
// DO nothing
return null;
}
return oldNode.clone();
}
});
config.setOnCloneNode(null);