Compile the GTest suite with CMake (#36477)

Summary:
That's a first step to make sure our C++ tests can effectively run.
Here I'm setting up the pipeline to at least compile those tests and make sure they can be executed on a Android Emulator

Changelog:
[Internal] [Changed] - Setup CMake to run the GTest suite

Pull Request resolved: https://github.com/facebook/react-native/pull/36477

Test Plan: CI needs to be green.

Reviewed By: rshest

Differential Revision: D44066754

Pulled By: cortinico

fbshipit-source-id: f963e0d7d4d9473eada53d7feecfabc21f3c3196
This commit is contained in:
Nicola Corti
2023-03-17 13:40:57 -07:00
committed by Facebook GitHub Bot
parent 790df10fa9
commit 346de2d16a
9 changed files with 168 additions and 21 deletions
@@ -518,6 +518,17 @@ android {
}
}
buildTypes {
debug {
externalNativeBuild {
cmake {
// We want to build Gtest suite only for the debug variant.
targets "reactnative_unittest"
}
}
}
}
preBuild.dependsOn(prepareJSC, prepareBoost, prepareDoubleConversion, prepareFmt, prepareFolly, prepareGlog, prepareLibevent)
preBuild.dependsOn("generateCodegenArtifactsFromSchema")
preBuild.dependsOn(preparePrefab)
@@ -78,6 +78,7 @@ add_react_common_subdir(react/renderer/telemetry)
add_react_common_subdir(react/renderer/templateprocessor)
add_react_common_subdir(react/renderer/uimanager)
add_react_common_subdir(react/renderer/core)
add_react_common_subdir(react/renderer/element)
add_react_common_subdir(react/renderer/graphics)
add_react_common_subdir(react/renderer/debug)
add_react_common_subdir(react/renderer/imagemanager)
@@ -116,3 +117,109 @@ add_react_android_subdir(src/main/jni/react/fabric)
add_react_android_subdir(src/main/jni/react/newarchdefaults)
add_react_android_subdir(src/main/jni/react/hermes/reactexecutor)
add_react_android_subdir(src/main/jni/react/hermes/instrumentation/)
# GTest setup
set(GOOGLETEST_ROOT ${ANDROID_NDK}/sources/third_party/googletest)
add_library(gtest STATIC ${GOOGLETEST_ROOT}/src/gtest_main.cc ${GOOGLETEST_ROOT}/src/gtest-all.cc)
target_include_directories(gtest PRIVATE ${GOOGLETEST_ROOT})
target_include_directories(gtest PUBLIC ${GOOGLETEST_ROOT}/include)
# GTest dependencies
add_executable(reactnative_unittest
${REACT_COMMON_DIR}/cxxreact/tests/jsarg_helpers.cpp
${REACT_COMMON_DIR}/cxxreact/tests/jsbigstring.cpp
${REACT_COMMON_DIR}/cxxreact/tests/methodcall.cpp
${REACT_COMMON_DIR}/cxxreact/tests/RecoverableErrorTest.cpp
${REACT_COMMON_DIR}/hermes/inspector/chrome/tests/MessageTests.cpp
${REACT_COMMON_DIR}/hermes/inspector/chrome/tests/RemoteObjectsTableTest.cpp
${REACT_COMMON_DIR}/react/bridging/tests/BridgingTest.cpp
${REACT_COMMON_DIR}/react/renderer/attributedstring/tests/AttributedStringBoxTest.cpp
${REACT_COMMON_DIR}/react/renderer/attributedstring/tests/AttributedStringTest.cpp
${REACT_COMMON_DIR}/react/renderer/attributedstring/tests/ParagraphAttributesTest.cpp
${REACT_COMMON_DIR}/react/renderer/attributedstring/tests/TextAttributesTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/image/tests/ImageTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/root/tests/RootShadowNodeTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/scrollview/tests/ScrollViewTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/text/tests/ParagraphLocalDataTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/text/tests/TextTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/view/tests/LayoutTest.cpp
${REACT_COMMON_DIR}/react/renderer/components/view/tests/ViewTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/DynamicPropsUtilitiesTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/EventQueueProcessorTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/FindNodeAtPointTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/LayoutableShadowNodeTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/PrimitivesTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/RawPropsTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/ShadowNodeFamilyTest.cpp
${REACT_COMMON_DIR}/react/renderer/core/tests/traitCastTest.cpp
${REACT_COMMON_DIR}/react/renderer/debug/tests/DebugStringConvertibleTest.cpp
${REACT_COMMON_DIR}/react/renderer/element/tests/ElementTest.cpp
${REACT_COMMON_DIR}/react/renderer/graphics/tests/GraphicsTest.cpp
${REACT_COMMON_DIR}/react/renderer/graphics/tests/TransformTest.cpp
${REACT_COMMON_DIR}/react/renderer/imagemanager/tests/ImageManagerTest.cpp
${REACT_COMMON_DIR}/react/renderer/mapbuffer/tests/MapBufferTest.cpp
${REACT_COMMON_DIR}/react/renderer/mounting/tests/StackingContextTest.cpp
${REACT_COMMON_DIR}/react/renderer/mounting/tests/StateReconciliationTest.cpp
${REACT_COMMON_DIR}/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp
${REACT_COMMON_DIR}/react/renderer/runtimescheduler/tests/SchedulerPriorityTest.cpp
${REACT_COMMON_DIR}/react/renderer/telemetry/tests/TransactionTelemetryTest.cpp
${REACT_COMMON_DIR}/react/renderer/templateprocessor/tests/UITemplateProcessorTest.cpp
${REACT_COMMON_DIR}/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp
${REACT_COMMON_DIR}/react/renderer/uimanager/tests/FabricUIManagerTest.cpp
########## (COMPILE BUT FAIL ON ASSERTS) ###########
# ${REACT_COMMON_DIR}/react/renderer/animations/tests/LayoutAnimationTest.cpp
# ${REACT_COMMON_DIR}/react/renderer/mounting/tests/MountingTest.cpp
# ${REACT_COMMON_DIR}/react/renderer/mounting/tests/ShadowTreeLifeCycleTest.cpp
########## (COMPILE BUT FAIL WITH RUNTIME EXCEPTIONS) ###########
# ${REACT_COMMON_DIR}/hermes/inspector/chrome/tests/ConnectionDemuxTests.cpp
# ${REACT_COMMON_DIR}/hermes/inspector/detail/tests/SerialExecutorTests.cpp
# ${REACT_COMMON_DIR}/hermes/inspector/tests/InspectorTests.cpp
########## (DO NOT COMPILE) ###########
# ${REACT_COMMON_DIR}/react/renderer/core/tests/ShadowNodeTest.cpp
# ${REACT_COMMON_DIR}/react/renderer/core/tests/ConcreteShadowNodeTest.cpp
# ${REACT_COMMON_DIR}/react/renderer/core/tests/ComponentDescriptorTest.cpp
# ${REACT_COMMON_DIR}/hermes/inspector/chrome/tests/ConnectionTests.cpp
)
target_compile_options(reactnative_unittest
PRIVATE
-Wall
-Werror
-fexceptions
-frtti
-std=c++17
-DANDROID
-DHERMES_ENABLE_DEBUGGER)
target_link_libraries(reactnative_unittest
folly_runtime
folly_futures
glog
glog_init
gtest
hermes-engine::libhermes
hermes_inspector
jsi
react_codegen_rncore
react_debug
react_render_animations
react_render_attributedstring
react_render_core
react_render_debug
react_render_element
react_render_graphics
react_render_mapbuffer
react_render_mounting
react_render_templateprocessor
react_render_textlayoutmanager
react_render_uimanager
react_utils
reactnative
rrc_modal
rrc_scrollview
rrc_text
rrc_view
yoga
)
@@ -420,7 +420,7 @@ TEST_F(BridgingTest, pointerTest) {
auto str = "hi"s;
auto unique = std::make_unique<std::string>(str);
auto shared = std::make_shared<std::string>(str);
auto weak = std::weak_ptr(shared);
auto weak = std::weak_ptr<std::string>(shared);
EXPECT_EQ(str, bridging::toJs(rt, unique, invoker).asString(rt).utf8(rt));
EXPECT_EQ(str, bridging::toJs(rt, shared, invoker).asString(rt).utf8(rt));
@@ -9,7 +9,6 @@
#include <assert.h>
#include <gtest/gtest.h>
#include <react/debug/react_native_assert.h>
#include <react/renderer/attributedstring/AttributedString.h>
#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/attributedstring/primitives.h>
@@ -34,21 +33,19 @@ TEST(ParagraphLocalDataTest, testSomething) {
text.fontWeight = FontWeight::Thin;
text.fontVariant = FontVariant::TabularNums;
fragment.textAttributes = text;
attString.prependFragment(fragment);
attributedString.prependFragment(fragment);
auto paragraphState = ParagraphState{};
paragraphLocalData.attributedString = attributedString;
paragraphState.attributedString = attributedString;
auto result = toDynamic(paragraphState)["attributedString"];
react_native_assert(result["string"] == fragment.string);
EXPECT_EQ(result["string"], fragment.string);
auto textAttribute = result["fragments"][0]["textAttributes"];
react_native_assert(
textAttribute["foregroundColor"] == toDynamic(text.foregroundColor));
react_native_assert(textAttribute["opacity"] == text.opacity);
react_native_assert(textAttribute["fontStyle"] == toString(*text.fontStyle));
react_native_assert(
textAttribute["fontWeight"] == toString(*text.fontWeight));
EXPECT_EQ(textAttribute["foregroundColor"], toDynamic(text.foregroundColor));
EXPECT_EQ(textAttribute["opacity"], text.opacity);
EXPECT_EQ(textAttribute["fontStyle"], toString(*text.fontStyle));
EXPECT_EQ(textAttribute["fontWeight"], toString(*text.fontWeight));
}
#endif
@@ -57,6 +57,9 @@ inline void fromRawValue(
inline int toAndroidRepr(const SharedColor &color) {
return *color;
}
inline folly::dynamic toDynamic(const SharedColor &color) {
return *color;
}
#endif
inline std::string toString(const SharedColor &value) {
@@ -6,7 +6,7 @@
*/
#include <gtest/gtest.h>
#include <hermes/API/hermes/hermes.h>
#include <hermes/hermes.h>
#include <jsi/jsi.h>
#include <react/renderer/core/EventPipe.h>
#include <react/renderer/core/EventQueueProcessor.h>
@@ -0,0 +1,29 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
add_compile_options(
-fexceptions
-frtti
-std=c++17
-Wall
-Wpedantic
-Wno-gnu-zero-variadic-macro-arguments
-DLOG_TAG=\"Fabric\")
file(GLOB react_render_element_SRC CONFIGURE_DEPENDS *.cpp)
add_library(react_render_element SHARED ${react_render_element_SRC})
target_include_directories(react_render_element PUBLIC ${REACT_COMMON_DIR})
target_link_libraries(react_render_element
folly_runtime
glog
react_render_core
react_render_componentregistry
)
@@ -769,16 +769,16 @@ TEST_F(StackingContextTest, zIndexAndFlattenedNodes) {
testViewTree_([](StubViewTree const &viewTree) {
// 5 views in total.
EXPECT_EQ(viewTree.size(), 5);
EXPECT_EQ(viewTree.size(), 8);
// The root view has all 4 subviews.
EXPECT_EQ(viewTree.getRootStubView().children.size(), 4);
// The root view has all 5 subviews.
EXPECT_EQ(viewTree.getRootStubView().children.size(), 5);
// The root view subviews are [10, 9, 5, 3].
EXPECT_EQ(viewTree.getRootStubView().children.at(0)->tag, 10);
EXPECT_EQ(viewTree.getRootStubView().children.at(1)->tag, 9);
EXPECT_EQ(viewTree.getRootStubView().children.at(2)->tag, 5);
EXPECT_EQ(viewTree.getRootStubView().children.at(3)->tag, 3);
// The root view subviews are [6, 10, 9, 5].
EXPECT_EQ(viewTree.getRootStubView().children.at(0)->tag, 6);
EXPECT_EQ(viewTree.getRootStubView().children.at(1)->tag, 10);
EXPECT_EQ(viewTree.getRootStubView().children.at(2)->tag, 9);
EXPECT_EQ(viewTree.getRootStubView().children.at(3)->tag, 5);
});
}
@@ -6,7 +6,7 @@
*/
#include <gtest/gtest.h>
#include <hermes/API/hermes/hermes.h>
#include <hermes/hermes.h>
#include <jsi/jsi.h>
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
#include <memory>