Skip to content

Commit

Permalink
Fabric: Tests for traitCast<>
Browse files Browse the repository at this point in the history
Summary:
Subject.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D20462834

fbshipit-source-id: 13e3c3b45537e22002b948f967a2576bc52145e8
  • Loading branch information
shergin authored and facebook-github-bot committed Mar 16, 2020
1 parent c6e224e commit e602b22
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ReactCommon/fabric/core/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ fb_xplat_cxx_test(
deps = [
"fbsource//xplat/folly:molly",
"fbsource//xplat/js/react-native-github/ReactCommon/fabric/element:element",
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/components/scrollview:scrollview"),
react_native_xplat_target("fabric/components/text:text"),
"fbsource//xplat/third-party/gmock:gtest",
react_native_xplat_target("fabric/components/view:view"),
":core",
Expand Down
105 changes: 105 additions & 0 deletions ReactCommon/fabric/core/tests/traitCastTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <gtest/gtest.h>

#include <react/components/scrollview/ScrollViewComponentDescriptor.h>
#include <react/components/text/ParagraphComponentDescriptor.h>
#include <react/components/text/RawTextComponentDescriptor.h>
#include <react/components/text/TextComponentDescriptor.h>
#include <react/components/view/ViewComponentDescriptor.h>

#include <react/element/Element.h>
#include <react/element/testUtils.h>

using namespace facebook::react;

TEST(traitCastTest, testOne) {
auto builder = simpleComponentBuilder();

auto viewShadowNode = std::shared_ptr<ViewShadowNode>{};
auto scrollViewShadowNode = std::shared_ptr<ScrollViewShadowNode>{};
auto paragraphShadowNode = std::shared_ptr<ParagraphShadowNode>{};
auto textShadowNode = std::shared_ptr<TextShadowNode>{};
auto rawTextShadowNode = std::shared_ptr<RawTextShadowNode>{};

// clang-format off
auto element =
Element<ScrollViewShadowNode>()
.reference(scrollViewShadowNode)
.children({
Element<ParagraphShadowNode>()
.reference(paragraphShadowNode)
.children({
Element<TextShadowNode>()
.reference(textShadowNode),
Element<RawTextShadowNode>()
.reference(rawTextShadowNode)
}),
Element<ViewShadowNode>()
.reference(viewShadowNode),
});
// clang-format on

auto rootShadowNode = builder.build(element);

// Casting `nullptr` returns `nullptrs`.
EXPECT_FALSE(traitCast<LayoutableShadowNode const *>(nullptr));
EXPECT_FALSE(traitCast<YogaLayoutableShadowNode const *>(nullptr));

// `ViewShadowNode` is `LayoutableShadowNode` and `YogaLayoutableShadowNode`.
EXPECT_TRUE(traitCast<LayoutableShadowNode const *>(viewShadowNode.get()));
EXPECT_TRUE(
traitCast<YogaLayoutableShadowNode const *>(viewShadowNode.get()));
EXPECT_NO_FATAL_FAILURE(
traitCast<LayoutableShadowNode const &>(*viewShadowNode));
EXPECT_NO_FATAL_FAILURE(
traitCast<YogaLayoutableShadowNode const &>(*viewShadowNode));

// `ScrollViewShadowNode` is `LayoutableShadowNode` and
// `YogaLayoutableShadowNode`.
EXPECT_TRUE(
traitCast<LayoutableShadowNode const *>(scrollViewShadowNode.get()));
EXPECT_TRUE(
traitCast<YogaLayoutableShadowNode const *>(scrollViewShadowNode.get()));
EXPECT_NO_FATAL_FAILURE(
traitCast<LayoutableShadowNode const &>(*scrollViewShadowNode));
EXPECT_NO_FATAL_FAILURE(
traitCast<YogaLayoutableShadowNode const &>(*scrollViewShadowNode));

// `ParagraphShadowNode` is `LayoutableShadowNode` and
// `YogaLayoutableShadowNode`.
EXPECT_TRUE(
traitCast<LayoutableShadowNode const *>(paragraphShadowNode.get()));
EXPECT_TRUE(
traitCast<YogaLayoutableShadowNode const *>(paragraphShadowNode.get()));
EXPECT_NO_FATAL_FAILURE(
traitCast<LayoutableShadowNode const &>(*paragraphShadowNode));
EXPECT_NO_FATAL_FAILURE(
traitCast<YogaLayoutableShadowNode const &>(*paragraphShadowNode));

// `TextShadowNode` is *not* `LayoutableShadowNode` nor
// `YogaLayoutableShadowNode`.
EXPECT_FALSE(traitCast<LayoutableShadowNode const *>(textShadowNode.get()));
EXPECT_FALSE(
traitCast<YogaLayoutableShadowNode const *>(textShadowNode.get()));
EXPECT_DEATH_IF_SUPPORTED(
traitCast<LayoutableShadowNode const &>(*textShadowNode), "");
EXPECT_DEATH_IF_SUPPORTED(
traitCast<YogaLayoutableShadowNode const &>(*textShadowNode), "");

// `RawTextShadowNode` is *not* `LayoutableShadowNode` nor
// `YogaLayoutableShadowNode`.
EXPECT_FALSE(
traitCast<LayoutableShadowNode const *>(rawTextShadowNode.get()));
EXPECT_FALSE(
traitCast<YogaLayoutableShadowNode const *>(rawTextShadowNode.get()));
EXPECT_DEATH_IF_SUPPORTED(
traitCast<LayoutableShadowNode const &>(*rawTextShadowNode), "");
EXPECT_DEATH_IF_SUPPORTED(
traitCast<YogaLayoutableShadowNode const &>(*rawTextShadowNode), "");
}
1 change: 1 addition & 0 deletions ReactCommon/fabric/element/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ rn_xplat_cxx_library(
react_native_xplat_target("fabric/components/root:root"),
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/components/scrollview:scrollview"),
react_native_xplat_target("fabric/components/text:text"),
react_native_xplat_target("fabric/uimanager:uimanager"),
react_native_xplat_target("fabric/core:core"),
react_native_xplat_target("fabric/debug:debug"),
Expand Down
9 changes: 9 additions & 0 deletions ReactCommon/fabric/element/testUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include <react/components/root/RootComponentDescriptor.h>
#include <react/components/scrollview/ScrollViewComponentDescriptor.h>
#include <react/components/text/ParagraphComponentDescriptor.h>
#include <react/components/text/RawTextComponentDescriptor.h>
#include <react/components/text/TextComponentDescriptor.h>
#include <react/components/view/ViewComponentDescriptor.h>
#include <react/element/ComponentBuilder.h>
#include <react/uimanager/ComponentDescriptorProviderRegistry.h>
Expand All @@ -29,6 +32,12 @@ inline ComponentBuilder simpleComponentBuilder() {
concreteComponentDescriptorProvider<ViewComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<ScrollViewComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<ParagraphComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<TextComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<RawTextComponentDescriptor>());

return ComponentBuilder{componentDescriptorRegistry};
}
Expand Down

0 comments on commit e602b22

Please sign in to comment.