From 96bc581eb60746b00c22b6c65e7dce7858befd92 Mon Sep 17 00:00:00 2001 From: Yevhenii Date: Wed, 11 Nov 2026 18:29:20 +0200 Subject: [PATCH 1/4] Make getInitialURL() answer the link that relaunches a reclaimed Android app When Android ends a backgrounded app's process but keeps its task, a link relaunches the app into that task: Android recreates the activity from the task's first intent and hands the link to onNewIntent before React Native can emit it as a url event. getIntent() kept answering the task's first intent, so Linking.getInitialURL() returned the first link and the new one never reached JavaScript. ReactActivity.onNewIntent now calls setIntent(intent) first, so getIntent(), and getInitialURL() through it, answer the newest link. --- .../com/facebook/react/ReactActivity.java | 3 + .../com/facebook/react/ReactActivityTest.kt | 84 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index b375d1859dbc..3b5ca546ef37 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -132,6 +132,9 @@ public void invokeDefaultOnBackPressed() { @Override public void onNewIntent(Intent intent) { + // Linking.getInitialURL() reads getIntent(), and a link that relaunches a reclaimed task + // arrives here before React Native can emit it as a url event. + setIntent(intent); if (!mDelegate.onNewIntent(intent)) { super.onNewIntent(intent); } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt new file mode 100644 index 000000000000..967eb35d64a6 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt @@ -0,0 +1,84 @@ +/* + * 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.react + +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import com.facebook.react.bridge.Promise +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.modules.intent.IntentModule +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.android.controller.ActivityController + +@RunWith(RobolectricTestRunner::class) +class ReactActivityTest { + + @Test + fun onNewIntent_makesTheDeliveredIntentTheActivityIntent() { + val controller = createActivity(FIRST_LINK) + val link = viewIntent(SECOND_LINK) + + controller.newIntent(link) + + assertThat(controller.get().intent).isSameAs(link) + } + + @Test + fun getInitialURL_answersTheLinkDeliveredAfterLaunch() { + val controller = createActivity(FIRST_LINK) + controller.newIntent(viewIntent(SECOND_LINK)) + + verify(requestInitialURL(controller.get())).resolve(SECOND_LINK) + } + + @Test + fun getInitialURL_answersTheLaunchLinkWhenNoOtherArrives() { + val controller = createActivity(FIRST_LINK) + + verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) + } + + private fun createActivity(link: String): ActivityController { + val controller = Robolectric.buildActivity(TestReactActivity::class.java, viewIntent(link)) + controller.get().setTheme(R.style.Theme_ReactNative_AppCompat_Light) + return controller.create() + } + + private fun requestInitialURL(activity: ReactActivity): Promise { + val context = mock() + whenever(context.currentActivity).thenReturn(activity) + val promise = mock() + IntentModule(context).getInitialURL(promise) + return promise + } + + private fun viewIntent(link: String): Intent = Intent(Intent.ACTION_VIEW, Uri.parse(link)) + + /** Hands every new intent to a React host that is not ready yet, as a relaunch does. */ + class TestReactActivity : ReactActivity() { + override fun createReactActivityDelegate(): ReactActivityDelegate = + object : ReactActivityDelegate(this, "Test") { + override fun onCreate(savedInstanceState: Bundle?) = Unit + + override fun onNewIntent(intent: Intent?): Boolean = true + } + } + + private companion object { + const val FIRST_LINK = "rntester://first" + const val SECOND_LINK = "rntester://second" + } +} From df9993f72dc4188f0ec261a688ea62b01a738024 Mon Sep 17 00:00:00 2001 From: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:54:48 +0200 Subject: [PATCH 2/4] Keep getInitialURL() at the launch link once React Native has a context A link that reaches a running app is delivered as a url event, and getInitialURL() keeps answering the link that launched it, as on iOS. ReactActivity.onNewIntent now calls setIntent(intent) only while React Native has no context: then no url event can carry the link, which is the one JavaScript starts with. --- .../com/facebook/react/ReactActivity.java | 13 +++++-- .../com/facebook/react/ReactActivityTest.kt | 36 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index 3b5ca546ef37..174510da8052 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -132,14 +132,21 @@ public void invokeDefaultOnBackPressed() { @Override public void onNewIntent(Intent intent) { - // Linking.getInitialURL() reads getIntent(), and a link that relaunches a reclaimed task - // arrives here before React Native can emit it as a url event. - setIntent(intent); + // Before React Native has a context, no url event can carry the link: it is the one JavaScript + // starts with, and Linking.getInitialURL() reads it from getIntent(). + if (!hasReactContext()) { + setIntent(intent); + } if (!mDelegate.onNewIntent(intent)) { super.onNewIntent(intent); } } + private boolean hasReactContext() { + ReactDelegate reactDelegate = getReactDelegate(); + return reactDelegate != null && reactDelegate.getCurrentReactContext() != null; + } + @Override public void onUserLeaveHint() { super.onUserLeaveHint(); diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt index 967eb35d64a6..cc18a0c16ac0 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt @@ -12,6 +12,7 @@ import android.net.Uri import android.os.Bundle import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContext import com.facebook.react.modules.intent.IntentModule import org.assertj.core.api.Assertions.assertThat import org.junit.Test @@ -27,8 +28,8 @@ import org.robolectric.android.controller.ActivityController class ReactActivityTest { @Test - fun onNewIntent_makesTheDeliveredIntentTheActivityIntent() { - val controller = createActivity(FIRST_LINK) + fun onNewIntent_beforeTheReactDelegateExists_makesTheDeliveredIntentTheActivityIntent() { + val controller = createActivity(FIRST_LINK, reactDelegate = null) val link = viewIntent(SECOND_LINK) controller.newIntent(link) @@ -37,26 +38,44 @@ class ReactActivityTest { } @Test - fun getInitialURL_answersTheLinkDeliveredAfterLaunch() { - val controller = createActivity(FIRST_LINK) + fun getInitialURL_answersTheLinkDeliveredBeforeTheReactContextExists() { + val controller = createActivity(FIRST_LINK, reactDelegateWith(context = null)) controller.newIntent(viewIntent(SECOND_LINK)) verify(requestInitialURL(controller.get())).resolve(SECOND_LINK) } + @Test + fun getInitialURL_keepsTheLaunchLinkWhenALinkArrivesOnceTheReactContextExists() { + val controller = createActivity(FIRST_LINK, reactDelegateWith(mock())) + controller.newIntent(viewIntent(SECOND_LINK)) + + verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) + } + @Test fun getInitialURL_answersTheLaunchLinkWhenNoOtherArrives() { - val controller = createActivity(FIRST_LINK) + val controller = createActivity(FIRST_LINK, reactDelegate = null) verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) } - private fun createActivity(link: String): ActivityController { + private fun createActivity( + link: String, + reactDelegate: ReactDelegate?, + ): ActivityController { val controller = Robolectric.buildActivity(TestReactActivity::class.java, viewIntent(link)) controller.get().setTheme(R.style.Theme_ReactNative_AppCompat_Light) + controller.get().testReactDelegate = reactDelegate return controller.create() } + private fun reactDelegateWith(context: ReactContext?): ReactDelegate { + val reactDelegate = mock() + whenever(reactDelegate.currentReactContext).thenReturn(context) + return reactDelegate + } + private fun requestInitialURL(activity: ReactActivity): Promise { val context = mock() whenever(context.currentActivity).thenReturn(activity) @@ -67,12 +86,15 @@ class ReactActivityTest { private fun viewIntent(link: String): Intent = Intent(Intent.ACTION_VIEW, Uri.parse(link)) - /** Hands every new intent to a React host that is not ready yet, as a relaunch does. */ class TestReactActivity : ReactActivity() { + var testReactDelegate: ReactDelegate? = null + override fun createReactActivityDelegate(): ReactActivityDelegate = object : ReactActivityDelegate(this, "Test") { override fun onCreate(savedInstanceState: Bundle?) = Unit + override fun getReactDelegate(): ReactDelegate? = testReactDelegate + override fun onNewIntent(intent: Intent?): Boolean = true } } From 9c0e677c3756bc16d41e49a754ab15b1014400ab Mon Sep 17 00:00:00 2001 From: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:18:59 +0200 Subject: [PATCH 3/4] Read the legacy React context without creating an instance ReactDelegate.currentReactContext read ReactNativeHost.reactInstanceManager, whose getter creates the ReactInstanceManager when there is none, so ReactActivity.onNewIntent's context check could create one before loadApp runs. It now answers null when the host has no instance, as the delegate's other Legacy Architecture paths check hasInstance() first. --- .../java/com/facebook/react/ReactDelegate.kt | 4 +- .../com/facebook/react/ReactDelegateTest.kt | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt index 39eb047a5a5b..07fcaa3436c7 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt @@ -433,8 +433,10 @@ public open class ReactDelegate { } else { null } - } else { + } else if (reactNativeHost?.hasInstance() == true) { getReactInstanceManager().currentReactContext + } else { + null } } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt new file mode 100644 index 000000000000..6835a127b4a4 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt @@ -0,0 +1,76 @@ +/* + * 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. + */ + +@file:Suppress("DEPRECATION") // Suppressing as we want to test the Legacy Architecture path here + +package com.facebook.react + +import android.app.Activity +import com.facebook.react.bridge.ReactContext +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class ReactDelegateTest { + + @Before + fun setUp() { + ReactNativeFeatureFlagsForTests.setUp() + ReactNativeFeatureFlags.override( + object : ReactNativeFeatureFlagsDefaults() { + override fun enableBridgelessArchitecture(): Boolean = false + }, + ) + } + + @After + fun tearDown() { + ReactNativeFeatureFlags.dangerouslyReset() + } + + @Test + fun currentReactContext_withoutAnInstance_isNullAndCreatesNone() { + val reactNativeHost = mock() + whenever(reactNativeHost.hasInstance()).thenReturn(false) + + assertThat(createReactDelegate(reactNativeHost).currentReactContext).isNull() + verify(reactNativeHost, never()).reactInstanceManager + } + + @Test + fun currentReactContext_withAnInstance_answersItsContext() { + val context = mock() + val reactInstanceManager = mock() + whenever(reactInstanceManager.currentReactContext).thenReturn(context) + val reactNativeHost = mock() + whenever(reactNativeHost.hasInstance()).thenReturn(true) + whenever(reactNativeHost.reactInstanceManager).thenReturn(reactInstanceManager) + + assertThat(createReactDelegate(reactNativeHost).currentReactContext).isSameAs(context) + } + + private fun createReactDelegate(reactNativeHost: ReactNativeHost): ReactDelegate = + ReactDelegate( + Robolectric.buildActivity(Activity::class.java).get(), + reactNativeHost, + "Test", + null, + false, + ) +} From 84aa8856625cb31dd98cbd64ba761d1f5e8bd515 Mon Sep 17 00:00:00 2001 From: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:56:01 +0200 Subject: [PATCH 4/4] Set the relaunch link as the intent until the activity first resumes In a release build ReactHostImpl creates its context as soon as the bundle loader resolves, which can be before the relaunch link reaches onNewIntent, so gating setIntent on the context left getInitialURL() at the task's first link. Android delivers that link between the recreated activity's onStart and its first onResume, so ReactActivity.onNewIntent now calls setIntent(intent) only until then; once the activity has resumed, a link arrives as a url event and getInitialURL() keeps the launch link. The ReactDelegate change is no longer needed and is dropped. --- .../com/facebook/react/ReactActivity.java | 13 ++-- .../java/com/facebook/react/ReactDelegate.kt | 4 +- .../com/facebook/react/ReactActivityTest.kt | 33 +++----- .../com/facebook/react/ReactDelegateTest.kt | 76 ------------------- 4 files changed, 17 insertions(+), 109 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index 174510da8052..0f1c1a1a3971 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -25,6 +25,7 @@ public abstract class ReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, PermissionAwareActivity { private final ReactActivityDelegate mDelegate; + private boolean mHasResumed = false; // Due to enforced predictive back on targetSdk 36, 'onBackPressed()' is disabled by default. // Using a workaround to trigger it manually. @@ -74,6 +75,7 @@ protected void onPause() { @Override protected void onResume() { super.onResume(); + mHasResumed = true; mDelegate.onResume(); } @@ -132,9 +134,9 @@ public void invokeDefaultOnBackPressed() { @Override public void onNewIntent(Intent intent) { - // Before React Native has a context, no url event can carry the link: it is the one JavaScript - // starts with, and Linking.getInitialURL() reads it from getIntent(). - if (!hasReactContext()) { + // Until the activity first resumes, a new intent belongs to its launch, as when a link + // relaunches a reclaimed task: Linking.getInitialURL() reads it from getIntent(). + if (!mHasResumed) { setIntent(intent); } if (!mDelegate.onNewIntent(intent)) { @@ -142,11 +144,6 @@ public void onNewIntent(Intent intent) { } } - private boolean hasReactContext() { - ReactDelegate reactDelegate = getReactDelegate(); - return reactDelegate != null && reactDelegate.getCurrentReactContext() != null; - } - @Override public void onUserLeaveHint() { super.onUserLeaveHint(); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt index 07fcaa3436c7..39eb047a5a5b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.kt @@ -433,10 +433,8 @@ public open class ReactDelegate { } else { null } - } else if (reactNativeHost?.hasInstance() == true) { - getReactInstanceManager().currentReactContext } else { - null + getReactInstanceManager().currentReactContext } } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt index cc18a0c16ac0..1b2bc0a79edb 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt @@ -12,7 +12,6 @@ import android.net.Uri import android.os.Bundle import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.bridge.ReactContext import com.facebook.react.modules.intent.IntentModule import org.assertj.core.api.Assertions.assertThat import org.junit.Test @@ -28,8 +27,8 @@ import org.robolectric.android.controller.ActivityController class ReactActivityTest { @Test - fun onNewIntent_beforeTheReactDelegateExists_makesTheDeliveredIntentTheActivityIntent() { - val controller = createActivity(FIRST_LINK, reactDelegate = null) + fun onNewIntent_beforeTheFirstResume_makesTheDeliveredIntentTheActivityIntent() { + val controller = createActivity(FIRST_LINK) val link = viewIntent(SECOND_LINK) controller.newIntent(link) @@ -38,16 +37,16 @@ class ReactActivityTest { } @Test - fun getInitialURL_answersTheLinkDeliveredBeforeTheReactContextExists() { - val controller = createActivity(FIRST_LINK, reactDelegateWith(context = null)) + fun getInitialURL_answersTheLinkDeliveredBeforeTheFirstResume() { + val controller = createActivity(FIRST_LINK) controller.newIntent(viewIntent(SECOND_LINK)) verify(requestInitialURL(controller.get())).resolve(SECOND_LINK) } @Test - fun getInitialURL_keepsTheLaunchLinkWhenALinkArrivesOnceTheReactContextExists() { - val controller = createActivity(FIRST_LINK, reactDelegateWith(mock())) + fun getInitialURL_keepsTheLaunchLinkWhenALinkArrivesAfterTheActivityResumed() { + val controller = createActivity(FIRST_LINK).start().resume().pause().stop() controller.newIntent(viewIntent(SECOND_LINK)) verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) @@ -55,27 +54,17 @@ class ReactActivityTest { @Test fun getInitialURL_answersTheLaunchLinkWhenNoOtherArrives() { - val controller = createActivity(FIRST_LINK, reactDelegate = null) + val controller = createActivity(FIRST_LINK) verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) } - private fun createActivity( - link: String, - reactDelegate: ReactDelegate?, - ): ActivityController { + private fun createActivity(link: String): ActivityController { val controller = Robolectric.buildActivity(TestReactActivity::class.java, viewIntent(link)) controller.get().setTheme(R.style.Theme_ReactNative_AppCompat_Light) - controller.get().testReactDelegate = reactDelegate return controller.create() } - private fun reactDelegateWith(context: ReactContext?): ReactDelegate { - val reactDelegate = mock() - whenever(reactDelegate.currentReactContext).thenReturn(context) - return reactDelegate - } - private fun requestInitialURL(activity: ReactActivity): Promise { val context = mock() whenever(context.currentActivity).thenReturn(activity) @@ -87,13 +76,13 @@ class ReactActivityTest { private fun viewIntent(link: String): Intent = Intent(Intent.ACTION_VIEW, Uri.parse(link)) class TestReactActivity : ReactActivity() { - var testReactDelegate: ReactDelegate? = null - override fun createReactActivityDelegate(): ReactActivityDelegate = object : ReactActivityDelegate(this, "Test") { override fun onCreate(savedInstanceState: Bundle?) = Unit - override fun getReactDelegate(): ReactDelegate? = testReactDelegate + override fun onResume() = Unit + + override fun onPause() = Unit override fun onNewIntent(intent: Intent?): Boolean = true } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt deleted file mode 100644 index 6835a127b4a4..000000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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. - */ - -@file:Suppress("DEPRECATION") // Suppressing as we want to test the Legacy Architecture path here - -package com.facebook.react - -import android.app.Activity -import com.facebook.react.bridge.ReactContext -import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags -import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults -import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests -import org.assertj.core.api.Assertions.assertThat -import org.junit.After -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.kotlin.mock -import org.mockito.kotlin.never -import org.mockito.kotlin.verify -import org.mockito.kotlin.whenever -import org.robolectric.Robolectric -import org.robolectric.RobolectricTestRunner - -@RunWith(RobolectricTestRunner::class) -class ReactDelegateTest { - - @Before - fun setUp() { - ReactNativeFeatureFlagsForTests.setUp() - ReactNativeFeatureFlags.override( - object : ReactNativeFeatureFlagsDefaults() { - override fun enableBridgelessArchitecture(): Boolean = false - }, - ) - } - - @After - fun tearDown() { - ReactNativeFeatureFlags.dangerouslyReset() - } - - @Test - fun currentReactContext_withoutAnInstance_isNullAndCreatesNone() { - val reactNativeHost = mock() - whenever(reactNativeHost.hasInstance()).thenReturn(false) - - assertThat(createReactDelegate(reactNativeHost).currentReactContext).isNull() - verify(reactNativeHost, never()).reactInstanceManager - } - - @Test - fun currentReactContext_withAnInstance_answersItsContext() { - val context = mock() - val reactInstanceManager = mock() - whenever(reactInstanceManager.currentReactContext).thenReturn(context) - val reactNativeHost = mock() - whenever(reactNativeHost.hasInstance()).thenReturn(true) - whenever(reactNativeHost.reactInstanceManager).thenReturn(reactInstanceManager) - - assertThat(createReactDelegate(reactNativeHost).currentReactContext).isSameAs(context) - } - - private fun createReactDelegate(reactNativeHost: ReactNativeHost): ReactDelegate = - ReactDelegate( - Robolectric.buildActivity(Activity::class.java).get(), - reactNativeHost, - "Test", - null, - false, - ) -}