From 4890d50eddbd9035e567134ac9b7d8570bae60f1 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Fri, 31 Mar 2023 12:47:40 -0700 Subject: [PATCH] introduce shadows helper library for robolectric (#36733) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36733 Changelog: [Android][Added] - Added testing shadow helpers for robolectric in this change, i'm creating a centralized place for test writers to add their shadows in robolectric. as we start deprecating powermock, we can expect that common infra classes will be needed to be stubbed out, so we can leverage this library in order to do so. Reviewed By: javache Differential Revision: D44565806 fbshipit-source-id: 2e322861e8e2f49ede21e4a000495d5f06b911d3 --- .../test/java/com/facebook/react/bridge/BUCK | 3 +++ .../react/bridge/BaseJavaModuleTest.java | 16 +------------ .../java/com/facebook/testutils/shadows/BUCK | 18 ++++++++++++++ .../testutils/shadows/ShadowSoLoader.java | 24 +++++++++++++++++++ 4 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/BUCK create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowSoLoader.java diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK index d86795f20415c7..1f59fb49d13a67 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK @@ -1,5 +1,7 @@ load("//tools/build_defs/oss:rn_defs.bzl", "react_native_android_toplevel_dep", "react_native_dep", "react_native_target", "react_native_tests_target", "rn_android_library", "rn_robolectric_test") +oncall("react_native") + STANDARD_TEST_SRCS = [ "*Test.java", ] @@ -43,5 +45,6 @@ rn_robolectric_test( react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"), react_native_target("java/com/facebook/react/uimanager:uimanager"), react_native_tests_target("java/com/facebook/common/logging:logging"), + react_native_tests_target("java/com/facebook/testutils/shadows:shadows"), ], ) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java index 52919c2a1e7cb8..76a75bb3d67ea8 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java @@ -9,9 +9,8 @@ import static org.mockito.Mockito.when; -import android.content.Context; import com.facebook.react.turbomodule.core.interfaces.TurboModule; -import com.facebook.soloader.SoLoader; +import com.facebook.testutils.shadows.ShadowSoLoader; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -19,19 +18,6 @@ import org.mockito.Mockito; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import org.robolectric.annotation.Implementation; -import org.robolectric.annotation.Implements; - -@Implements(SoLoader.class) -class ShadowSoLoader { - @Implementation - public static void init(Context context, int flags) {} - - @Implementation - public static boolean loadLibrary(String shortName) { - return true; - } -} /** Tests for {@link BaseJavaModule} and {@link JavaModuleWrapper} */ @Config( diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/BUCK b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/BUCK new file mode 100644 index 00000000000000..d5aa6ad6daee43 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/BUCK @@ -0,0 +1,18 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library") + +oncall("react_native") + +rn_android_library( + name = "shadows", + srcs = glob(["**/*.java"]), + autoglob = False, + language = "JAVA", + visibility = [ + "PUBLIC", + ], + deps = [ + react_native_target("java/com/facebook/react/bridge:bridge"), + react_native_dep("third-party/java/robolectric:robolectric"), + react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"), + ], +) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowSoLoader.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowSoLoader.java new file mode 100644 index 00000000000000..712087401fa841 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowSoLoader.java @@ -0,0 +1,24 @@ +/* + * 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. + */ + +package com.facebook.testutils.shadows; + +import android.content.Context; +import com.facebook.soloader.SoLoader; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +@Implements(SoLoader.class) +public class ShadowSoLoader { + @Implementation + public static void init(Context context, int flags) {} + + @Implementation + public static boolean loadLibrary(String shortName) { + return true; + } +}