From 408a5f264b4f67873e6763c3a718bafe40fd48b3 Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Fri, 23 Mar 2018 16:46:10 -0700 Subject: [PATCH] iOS: added fabric/core ComponentDescriptor tests Summary: Basic test for ComponentDescriptor mechanism. Reviewed By: shergin Differential Revision: D7388297 fbshipit-source-id: 3b0c625656e31df03d71a2e036388621a5e2e21d --- ReactCommon/fabric/core/BUCK | 1 + .../ConcreteComponentDescriptor.h | 3 +- .../fabric/core/shadownode/ShadowNode.cpp | 1 + .../core/tests/ComponentDescriptorTest.cpp | 66 +++++++++++++++++++ .../fabric/core/tests/ShadowNodeTest.cpp | 23 +------ ReactCommon/fabric/core/tests/TestComponent.h | 50 ++++++++++++++ 6 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp create mode 100644 ReactCommon/fabric/core/tests/TestComponent.h diff --git a/ReactCommon/fabric/core/BUCK b/ReactCommon/fabric/core/BUCK index f394579fc0..d9ee58fae8 100644 --- a/ReactCommon/fabric/core/BUCK +++ b/ReactCommon/fabric/core/BUCK @@ -61,6 +61,7 @@ if not IS_OSS_BUILD: cxx_test( name = "tests", srcs = glob(["tests/**/*.cpp"]), + headers = glob(["tests/**/*.h"]), contacts = ["oncall+react_native@xmail.facebook.com"], compiler_flags = [ "-fexceptions", diff --git a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h index 0a521bd499..95de5e1753 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h @@ -9,8 +9,9 @@ #include -#include +#include #include +#include namespace facebook { namespace react { diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp index 0dfb51e1eb..04fb5d30f8 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp @@ -39,6 +39,7 @@ ShadowNode::ShadowNode( SharedShadowNodeSharedList children ): tag_(shadowNode->tag_), + rootTag_(shadowNode->rootTag_), instanceHandle_(shadowNode->instanceHandle_), props_(props ? props : shadowNode->props_), children_(children ? children : shadowNode->children_), diff --git a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp new file mode 100644 index 0000000000..699642ce7f --- /dev/null +++ b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp @@ -0,0 +1,66 @@ +/** + * 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. + */ + +#include + +#include "TestComponent.h" + +using namespace facebook::react; + +TEST(ComponentDescriptorTest, createShadowNode) { + SharedComponentDescriptor descriptor = std::make_shared(); + + ASSERT_EQ(descriptor->getComponentHandle(), typeid(TestShadowNode).hash_code()); + ASSERT_STREQ(descriptor->getComponentName().c_str(), "Test"); + + RawProps raw; + raw["nativeID"] = "abc"; + SharedShadowNode node = descriptor->createShadowNode(9, 1, (void *)NULL, raw); + + ASSERT_EQ(node->getComponentHandle(), typeid(TestShadowNode).hash_code()); + ASSERT_STREQ(node->getComponentName().c_str(), "Test"); + ASSERT_EQ(node->getTag(), 9); + ASSERT_EQ(node->getRootTag(), 1); + + // TODO(#27369757): getProps() doesn't work + // ASSERT_STREQ(node->getProps()->getNativeId().c_str(), "testNativeID"); +} + +TEST(ComponentDescriptorTest, cloneShadowNode) { + SharedComponentDescriptor descriptor = std::make_shared(); + + RawProps raw; + raw["nativeID"] = "abc"; + SharedShadowNode node = descriptor->createShadowNode(9, 1, (void *)NULL, raw); + SharedShadowNode cloned = descriptor->cloneShadowNode(node); + + ASSERT_EQ(cloned->getComponentHandle(), typeid(TestShadowNode).hash_code()); + ASSERT_STREQ(cloned->getComponentName().c_str(), "Test"); + ASSERT_EQ(cloned->getTag(), 9); + ASSERT_EQ(cloned->getRootTag(), 1); + + // TODO(#27369757): getProps() doesn't work + // ASSERT_STREQ(cloned->getProps()->getNativeId().c_str(), "testNativeID"); +} + +TEST(ComponentDescriptorTest, appendChild) { + SharedComponentDescriptor descriptor = std::make_shared(); + + RawProps raw; + raw["nativeID"] = "abc"; + SharedShadowNode node1 = descriptor->createShadowNode(1, 1, (void *)NULL, raw); + SharedShadowNode node2 = descriptor->createShadowNode(2, 1, (void *)NULL, raw); + SharedShadowNode node3 = descriptor->createShadowNode(3, 1, (void *)NULL, raw); + + descriptor->appendChild(node1, node2); + descriptor->appendChild(node1, node3); + + SharedShadowNodeSharedList node1Children = node1->getChildren(); + ASSERT_EQ(node1Children->size(), 2); + ASSERT_EQ(node1Children->at(0), node2); + ASSERT_EQ(node1Children->at(1), node3); +} diff --git a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp index 75bc82e803..418938a497 100644 --- a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp +++ b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp @@ -11,29 +11,10 @@ #include #include +#include "TestComponent.h" + using namespace facebook::react; -class TestProps : public Props { -public: - TestProps() { - RawProps raw; - raw["nativeID"] = "testNativeID"; - apply(raw); - } -}; -using SharedTestProps = std::shared_ptr; - -class TestShadowNode; -using SharedTestShadowNode = std::shared_ptr; -class TestShadowNode : public ConcreteShadowNode { -public: - using ConcreteShadowNode::ConcreteShadowNode; - - ComponentName getComponentName() const override { - return ComponentName("Test"); - } -}; - TEST(ShadowNodeTest, handleProps) { RawProps raw; raw["nativeID"] = "abc"; diff --git a/ReactCommon/fabric/core/tests/TestComponent.h b/ReactCommon/fabric/core/tests/TestComponent.h new file mode 100644 index 0000000000..18890daeb0 --- /dev/null +++ b/ReactCommon/fabric/core/tests/TestComponent.h @@ -0,0 +1,50 @@ +/** + * 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. + */ + +#pragma once + +#include + +#include +#include +#include + +using namespace facebook::react; + +/** + * This defines a set of TestComponent classes: Props, ShadowNode, ComponentDescriptor. + * To be used for testing purpose. + */ + +class TestProps : public Props { +public: + TestProps() { + RawProps raw; + raw["nativeID"] = "testNativeID"; + apply(raw); + } +}; +using SharedTestProps = std::shared_ptr; + +class TestShadowNode; +using SharedTestShadowNode = std::shared_ptr; +class TestShadowNode : public ConcreteShadowNode { +public: + using ConcreteShadowNode::ConcreteShadowNode; + + ComponentName getComponentName() const override { + return ComponentName("Test"); + } +}; + +class TestComponentDescriptor: public ConcreteComponentDescriptor { +public: + // TODO (shergin): Why does this gets repeated here and the shadow node class? + ComponentName getComponentName() const override { + return "Test"; + } +};