Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -74,6 +75,7 @@ protected void onPause() {
@Override
protected void onResume() {
super.onResume();
mHasResumed = true;
mDelegate.onResume();
}

Expand Down Expand Up @@ -132,6 +134,11 @@ public void invokeDefaultOnBackPressed() {

@Override
public void onNewIntent(Intent intent) {
// 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)) {
super.onNewIntent(intent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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_beforeTheFirstResume_makesTheDeliveredIntentTheActivityIntent() {
val controller = createActivity(FIRST_LINK)
val link = viewIntent(SECOND_LINK)

controller.newIntent(link)

assertThat(controller.get().intent).isSameAs(link)
}

@Test
fun getInitialURL_answersTheLinkDeliveredBeforeTheFirstResume() {
val controller = createActivity(FIRST_LINK)
controller.newIntent(viewIntent(SECOND_LINK))

verify(requestInitialURL(controller.get())).resolve(SECOND_LINK)
}

@Test
fun getInitialURL_keepsTheLaunchLinkWhenALinkArrivesAfterTheActivityResumed() {
val controller = createActivity(FIRST_LINK).start().resume().pause().stop()
controller.newIntent(viewIntent(SECOND_LINK))

verify(requestInitialURL(controller.get())).resolve(FIRST_LINK)
}

@Test
fun getInitialURL_answersTheLaunchLinkWhenNoOtherArrives() {
val controller = createActivity(FIRST_LINK)

verify(requestInitialURL(controller.get())).resolve(FIRST_LINK)
}

private fun createActivity(link: String): ActivityController<TestReactActivity> {
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<ReactApplicationContext>()
whenever(context.currentActivity).thenReturn(activity)
val promise = mock<Promise>()
IntentModule(context).getInitialURL(promise)
return promise
}

private fun viewIntent(link: String): Intent = Intent(Intent.ACTION_VIEW, Uri.parse(link))

class TestReactActivity : ReactActivity() {
override fun createReactActivityDelegate(): ReactActivityDelegate =
object : ReactActivityDelegate(this, "Test") {
override fun onCreate(savedInstanceState: Bundle?) = Unit

override fun onResume() = Unit

override fun onPause() = Unit

override fun onNewIntent(intent: Intent?): Boolean = true
}
}

private companion object {
const val FIRST_LINK = "rntester://first"
const val SECOND_LINK = "rntester://second"
}
}
Loading