mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-03 22:48:25 +08:00
Include instanceHandle in cloning mechanism
Reviewed By: shergin, achen1 Differential Revision: D8072075 fbshipit-source-id: 2fcfdfa5116850ce0bac6c2c86d87e5bf00fd7f0
This commit is contained in:
committed by
Facebook Github Bot
parent
40c7248345
commit
23fbd312aa
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class InstanceHandleHelper {
|
||||
|
||||
private static final Random random = new Random();
|
||||
|
||||
public static long randomInstanceHandle() {
|
||||
return random.nextLong();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
package com.facebook.react.fabric;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static com.facebook.react.bridge.InstanceHandleHelper.randomInstanceHandle;
|
||||
|
||||
import com.facebook.react.ReactRootView;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
@@ -28,6 +29,7 @@ import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
|
||||
/** Tests {@link FabricUIManager} */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class FabricUIManagerTest {
|
||||
@@ -35,7 +37,6 @@ public class FabricUIManagerTest {
|
||||
private FabricUIManager mFabricUIManager;
|
||||
private ThemedReactContext mThemedReactContext;
|
||||
private int mNextReactTag;
|
||||
private int mNextInstanceHandle;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
@@ -58,10 +59,9 @@ public class FabricUIManagerTest {
|
||||
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
|
||||
int rootTag = mFabricUIManager.addRootView(rootView);
|
||||
int reactTag = mNextReactTag++;
|
||||
int instanceHandle = mNextInstanceHandle++;
|
||||
String viewClass = ReactViewManager.REACT_CLASS;
|
||||
ReactShadowNode node =
|
||||
mFabricUIManager.createNode(reactTag, viewClass, rootTag, null, instanceHandle);
|
||||
mFabricUIManager.createNode(reactTag, viewClass, rootTag, null, randomInstanceHandle());
|
||||
|
||||
assertThat(reactTag).isEqualTo(node.getReactTag());
|
||||
assertThat(viewClass).isEqualTo(node.getViewClass());
|
||||
@@ -79,10 +79,9 @@ public class FabricUIManagerTest {
|
||||
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
|
||||
int rootTag = mFabricUIManager.addRootView(rootView);
|
||||
int reactTag = mNextReactTag++;
|
||||
int instanceHandle = mNextInstanceHandle++;
|
||||
String viewClass = ReactViewManager.REACT_CLASS;
|
||||
ReactShadowNode node =
|
||||
mFabricUIManager.createNode(reactTag, viewClass, rootTag, null, instanceHandle);
|
||||
mFabricUIManager.createNode(reactTag, viewClass, rootTag, null, randomInstanceHandle());
|
||||
|
||||
List<ReactShadowNode> childSet = mFabricUIManager.createChildSet(rootTag);
|
||||
mFabricUIManager.appendChildToSet(childSet, node);
|
||||
@@ -106,6 +105,21 @@ public class FabricUIManagerTest {
|
||||
assertThat(clonedNode.getChildAt(0)).isEqualTo(child);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCloneWithInstanceHandle() {
|
||||
ReactShadowNode node = createViewNode();
|
||||
|
||||
long oldInstanceHandle = node.getInstanceHandle();
|
||||
long newInstanceHandle = oldInstanceHandle + 1;
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNode(node, newInstanceHandle);
|
||||
|
||||
assertThat(clonedNode).isNotSameAs(node);
|
||||
assertThat(clonedNode.getInstanceHandle()).isSameAs(newInstanceHandle);
|
||||
assertThat(node.getInstanceHandle()).isSameAs(oldInstanceHandle);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDefaultSpacingCloning() {
|
||||
ReactShadowNode node = createViewNode();
|
||||
@@ -124,7 +138,7 @@ public class FabricUIManagerTest {
|
||||
node.setText("test");
|
||||
assertThat(node.isVirtual()).isTrue();
|
||||
|
||||
ReactRawTextShadowNode clonedNode = (ReactRawTextShadowNode) node.mutableCopy();
|
||||
ReactRawTextShadowNode clonedNode = (ReactRawTextShadowNode) node.mutableCopy(randomInstanceHandle());
|
||||
|
||||
assertThat(clonedNode.getText()).isEqualTo("test");
|
||||
assertThat(clonedNode).isNotEqualTo(node);
|
||||
@@ -134,7 +148,7 @@ public class FabricUIManagerTest {
|
||||
public void testLayoutProgressBarAfterClonning() {
|
||||
ProgressBarShadowNode node = new ProgressBarShadowNode();
|
||||
node.setThemedContext(mThemedReactContext);
|
||||
ProgressBarShadowNode clone = (ProgressBarShadowNode) node.mutableCopy();
|
||||
ProgressBarShadowNode clone = (ProgressBarShadowNode) node.mutableCopy(randomInstanceHandle());
|
||||
clone.calculateLayout();
|
||||
}
|
||||
|
||||
@@ -144,7 +158,7 @@ public class FabricUIManagerTest {
|
||||
ReactShadowNode child = createViewNode();
|
||||
node.addChildAt(child, 0);
|
||||
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewChildren(node, 0);
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewChildren(node, randomInstanceHandle());
|
||||
|
||||
assertThat(clonedNode.getChildCount()).isZero();
|
||||
assertSameFields(clonedNode, node);
|
||||
@@ -155,7 +169,7 @@ public class FabricUIManagerTest {
|
||||
ReactShadowNode node = createViewNode();
|
||||
ReadableNativeMap props = null; // TODO(ayc): Figure out how to create a Native map from tests.
|
||||
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewProps(node, props, 0);
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewProps(node, props, randomInstanceHandle());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,7 +177,7 @@ public class FabricUIManagerTest {
|
||||
ReactShadowNode node = createViewNode();
|
||||
ReadableNativeMap props = null;
|
||||
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewChildrenAndProps(node, props, 0);
|
||||
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewChildrenAndProps(node, props, randomInstanceHandle());
|
||||
|
||||
assertThat(clonedNode.getChildCount()).isZero();
|
||||
}
|
||||
@@ -221,10 +235,10 @@ public class FabricUIManagerTest {
|
||||
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
|
||||
int rootTag = mFabricUIManager.addRootView(rootView);
|
||||
ReactShadowNode text =
|
||||
mFabricUIManager.createNode(0, ReactTextViewManager.REACT_CLASS, rootTag, null, mNextInstanceHandle++);
|
||||
mFabricUIManager.createNode(0, ReactTextViewManager.REACT_CLASS, rootTag, null, randomInstanceHandle());
|
||||
assertThat(text.isMeasureDefined()).isTrue();
|
||||
|
||||
ReactShadowNode textCopy = text.mutableCopy();
|
||||
ReactShadowNode textCopy = text.mutableCopy(randomInstanceHandle());
|
||||
assertThat(textCopy.isMeasureDefined()).isTrue();
|
||||
|
||||
textCopy.setStyleWidth(200);
|
||||
@@ -247,13 +261,13 @@ public class FabricUIManagerTest {
|
||||
int rootTag = mFabricUIManager.addRootView(rootView);
|
||||
String viewClass = ReactViewManager.REACT_CLASS;
|
||||
|
||||
ReactShadowNode aa = mFabricUIManager.createNode(2, viewClass, rootTag, null, mNextInstanceHandle++);
|
||||
ReactShadowNode a = mFabricUIManager.createNode(3, viewClass, rootTag, null, mNextInstanceHandle++);
|
||||
ReactShadowNode aa = mFabricUIManager.createNode(2, viewClass, rootTag, null, randomInstanceHandle());
|
||||
ReactShadowNode a = mFabricUIManager.createNode(3, viewClass, rootTag, null, randomInstanceHandle());
|
||||
mFabricUIManager.appendChild(a, aa);
|
||||
ReactShadowNode bb = mFabricUIManager.createNode(4, viewClass, rootTag, null, mNextInstanceHandle++);
|
||||
ReactShadowNode b = mFabricUIManager.createNode(5, viewClass, rootTag, null, mNextInstanceHandle++);
|
||||
ReactShadowNode bb = mFabricUIManager.createNode(4, viewClass, rootTag, null, randomInstanceHandle());
|
||||
ReactShadowNode b = mFabricUIManager.createNode(5, viewClass, rootTag, null, randomInstanceHandle());
|
||||
mFabricUIManager.appendChild(b, bb);
|
||||
ReactShadowNode container = mFabricUIManager.createNode(6, viewClass, rootTag, null, mNextInstanceHandle++);
|
||||
ReactShadowNode container = mFabricUIManager.createNode(6, viewClass, rootTag, null, randomInstanceHandle());
|
||||
mFabricUIManager.appendChild(container, a);
|
||||
mFabricUIManager.appendChild(container, b);
|
||||
List<ReactShadowNode> childSet = mFabricUIManager.createChildSet(rootTag);
|
||||
|
||||
@@ -13,7 +13,7 @@ public class ReactShadowNodeTest {
|
||||
@Test(expected = AssertionError.class)
|
||||
public void testClonedInstance() {
|
||||
TestReactShadowNode node = new TestReactShadowNode();
|
||||
node.mutableCopy();
|
||||
node.mutableCopy(node.getInstanceHandle());
|
||||
}
|
||||
|
||||
private static class TestReactShadowNode extends ReactShadowNodeImpl {}
|
||||
|
||||
Reference in New Issue
Block a user