From e254474d4cbaebdb38ef5fdb5e69ca45257ce6ef Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Fri, 23 Mar 2018 16:46:09 -0700 Subject: [PATCH] iOS: added fabric/core ShadowNode tests Summary: basic tests for shadow nodes and props for fabric Reviewed By: shergin Differential Revision: D7377867 fbshipit-source-id: dc8e5bb369aeb32b4790fd8b56f333376bc1578c --- ReactCommon/fabric/core/shadownode/Props.cpp | 4 + ReactCommon/fabric/core/shadownode/Props.h | 2 + .../fabric/core/shadownode/ShadowNode.cpp | 8 ++ .../fabric/core/shadownode/ShadowNode.h | 2 + .../fabric/core/tests/ShadowNodeTest.cpp | 115 ++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 ReactCommon/fabric/core/tests/ShadowNodeTest.cpp diff --git a/ReactCommon/fabric/core/shadownode/Props.cpp b/ReactCommon/fabric/core/shadownode/Props.cpp index fb527d62b..cd5655ee5 100644 --- a/ReactCommon/fabric/core/shadownode/Props.cpp +++ b/ReactCommon/fabric/core/shadownode/Props.cpp @@ -25,5 +25,9 @@ void Props::apply(const RawProps &rawProps) { } } +const std::string &Props::getNativeId() const { + return nativeId_; +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/core/shadownode/Props.h b/ReactCommon/fabric/core/shadownode/Props.h index 8a6a73834..5760f3491 100644 --- a/ReactCommon/fabric/core/shadownode/Props.h +++ b/ReactCommon/fabric/core/shadownode/Props.h @@ -32,6 +32,8 @@ public: virtual void apply(const RawProps &rawProps); + const std::string &getNativeId() const; + private: std::string nativeId_ {""}; }; diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp index 3f7aee7be..0dfb51e1e 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp @@ -58,6 +58,14 @@ Tag ShadowNode::getTag() const { return tag_; } +Tag ShadowNode::getRootTag() const { + return rootTag_; +} + +InstanceHandle ShadowNode::getInstanceHandle() const { + return instanceHandle_; +} + SharedShadowNode ShadowNode::getSourceNode() const { return sourceNode_; } diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.h b/ReactCommon/fabric/core/shadownode/ShadowNode.h index 7fcb9d04c..1deb4d520 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.h +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.h @@ -55,6 +55,8 @@ public: SharedShadowNodeSharedList getChildren() const; SharedProps getProps() const; Tag getTag() const; + Tag getRootTag() const; + InstanceHandle getInstanceHandle() const; SharedShadowNode getSourceNode() const; void sealRecursive() const; diff --git a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp new file mode 100644 index 000000000..75bc82e80 --- /dev/null +++ b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp @@ -0,0 +1,115 @@ +/** + * 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 +#include +#include + +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"; + + auto props = std::make_shared(); + props->apply(raw); + + // Props are not sealed after applying raw props. + ASSERT_FALSE(props->getSealed()); + + ASSERT_STREQ(props->getNativeId().c_str(), "abc"); +} + +TEST(ShadowNodeTest, handleShadowNodeCreation) { + auto node = std::make_shared(9, 1, (void *)NULL); + + ASSERT_FALSE(node->getSealed()); + ASSERT_STREQ(node->getComponentName().c_str(), "Test"); + ASSERT_EQ(node->getTag(), 9); + ASSERT_EQ(node->getRootTag(), 1); + ASSERT_EQ(node->getInstanceHandle(), (void *)NULL); + TestShadowNode *nodePtr = node.get(); + ASSERT_EQ(node->getComponentHandle(), typeid(*nodePtr).hash_code()); + ASSERT_EQ(node->getSourceNode(), nullptr); + ASSERT_EQ(node->getChildren()->size(), 0); + + // TODO(#27369757): getProps() doesn't work + // ASSERT_STREQ(node->getProps()->getNativeId().c_str(), "testNativeID"); + + node->sealRecursive(); + ASSERT_TRUE(node->getSealed()); + + // TODO(#27369757): verify Props are also sealed. + // ASSERT_TRUE(node->getProps()->getSealed()); +} + +TEST(ShadowNodeTest, handleShadowNodeSimpleCloning) { + auto node = std::make_shared(9, 1, (void *)NULL); + auto node2 = std::make_shared(node); + + ASSERT_STREQ(node->getComponentName().c_str(), "Test"); + ASSERT_EQ(node->getTag(), 9); + ASSERT_EQ(node->getRootTag(), 1); + ASSERT_EQ(node->getInstanceHandle(), (void *)NULL); + ASSERT_EQ(node2->getSourceNode(), node); +} + +TEST(ShadowNodeTest, handleShadowNodeMutation) { + auto node1 = std::make_shared(1, 1, (void *)NULL); + auto node2 = std::make_shared(2, 1, (void *)NULL); + auto node3 = std::make_shared(3, 1, (void *)NULL); + + node1->appendChild(node2); + node1->appendChild(node3); + SharedShadowNodeSharedList node1Children = node1->getChildren(); + ASSERT_EQ(node1Children->size(), 2); + ASSERT_EQ(node1Children->at(0), node2); + ASSERT_EQ(node1Children->at(1), node3); + + auto node4 = std::make_shared(node2); + node1->replaceChild(node2, node4); + node1Children = node1->getChildren(); + ASSERT_EQ(node1Children->size(), 2); + ASSERT_EQ(node1Children->at(0), node4); + ASSERT_EQ(node1Children->at(1), node3); + + // Seal the entire tree. + node1->sealRecursive(); + ASSERT_TRUE(node1->getSealed()); + ASSERT_TRUE(node3->getSealed()); + ASSERT_TRUE(node4->getSealed()); + + // No more mutation after sealing. + EXPECT_THROW(node4->clearSourceNode(), std::runtime_error); + + auto node5 = std::make_shared(node4); + node5->clearSourceNode(); + ASSERT_EQ(node5->getSourceNode(), nullptr); +}