Skip to content

Commit dfa2d87

Browse files
rvandermeulenjonalmeida
authored andcommitted
Fix CustomTabsTest intent resolution with ActivityScenario
ActivityScenario.launch resolves intents through normal Android routing, so an ACTION_VIEW intent with an HTTP URL resolved to Chrome rather than IntentReceiverActivity. Set the component explicitly before launching to force the activity within the test process. Improve error message when BrowserActivityTestRule.activity is unavailable
1 parent 680a52f commit dfa2d87

2 files changed

Lines changed: 22 additions & 25 deletions

File tree

app/src/androidTest/java/org/mozilla/reference/browser/helpers/BrowserActivityTestRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class BrowserActivityTestRule : ExternalResource() {
2929
get() {
3030
var result: BrowserActivity? = null
3131
scenario.onActivity { result = it }
32-
return result!!
32+
return checkNotNull(result) { "Activity unavailable — scenario may be in a terminal state" }
3333
}
3434

3535
override fun before() {

app/src/androidTest/java/org/mozilla/reference/browser/ui/CustomTabsTest.kt

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
package org.mozilla.reference.browser.ui
66

7+
import android.content.ComponentName
78
import androidx.test.core.app.ActivityScenario
9+
import androidx.test.platform.app.InstrumentationRegistry
810
import mockwebserver3.MockWebServer
911
import org.junit.After
1012
import org.junit.Before
@@ -28,6 +30,19 @@ class CustomTabsTest {
2830
@JvmField
2931
val retryTestRule = RetryTestRule(3)
3032

33+
// ActivityScenario.launch resolves intents via normal Android routing, so an ACTION_VIEW
34+
// intent with an HTTP URL would resolve to the browser rather than IntentReceiverActivity.
35+
// Setting the component explicitly forces it to launch within the test process.
36+
private fun launchCustomTab(pageUrl: String) =
37+
ActivityScenario.launch<IntentReceiverActivity>(
38+
createCustomTabIntent(pageUrl).apply {
39+
component = ComponentName(
40+
InstrumentationRegistry.getInstrumentation().targetContext,
41+
IntentReceiverActivity::class.java,
42+
)
43+
},
44+
)
45+
3146
@Before
3247
fun setUp() {
3348
mockWebServer = MockWebServer().apply {
@@ -45,10 +60,7 @@ class CustomTabsTest {
4560
fun openCustomTabTest() {
4661
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
4762

48-
ActivityScenario
49-
.launch<IntentReceiverActivity>(
50-
createCustomTabIntent(customTabPage.url.toString()),
51-
).use {
63+
launchCustomTab(customTabPage.url.toString()).use {
5264
customTabScreen {
5365
verifyCloseButton()
5466
verifyTrackingProtectionIcon()
@@ -65,10 +77,7 @@ class CustomTabsTest {
6577
fun verifyCustomTabMenuItemsTest() {
6678
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
6779

68-
ActivityScenario
69-
.launch<IntentReceiverActivity>(
70-
createCustomTabIntent(customTabPage.url.toString()),
71-
).use {
80+
launchCustomTab(customTabPage.url.toString()).use {
7281
customTabScreen {
7382
}.openMainMenu {
7483
verifyForwardButton()
@@ -87,10 +96,7 @@ class CustomTabsTest {
8796
val pageLinks = TestAssetHelper.getGenericAsset(mockWebServer, 4)
8897
val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)
8998

90-
ActivityScenario
91-
.launch<IntentReceiverActivity>(
92-
createCustomTabIntent(pageLinks.url.toString()),
93-
).use {
99+
launchCustomTab(pageLinks.url.toString()).use {
94100
customTabScreen {
95101
clickGenericLink("Link 1")
96102
verifyPageTitle(genericURL.title)
@@ -110,10 +116,7 @@ class CustomTabsTest {
110116
fun customTabShareTest() {
111117
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
112118

113-
ActivityScenario
114-
.launch<IntentReceiverActivity>(
115-
createCustomTabIntent(customTabPage.url.toString()),
116-
).use {
119+
launchCustomTab(customTabPage.url.toString()).use {
117120
customTabScreen {
118121
}.openMainMenu {
119122
}.clickShareButton {
@@ -126,10 +129,7 @@ class CustomTabsTest {
126129
fun customTabRequestDesktopSiteTest() {
127130
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
128131

129-
ActivityScenario
130-
.launch<IntentReceiverActivity>(
131-
createCustomTabIntent(customTabPage.url.toString()),
132-
).use {
132+
launchCustomTab(customTabPage.url.toString()).use {
133133
customTabScreen {
134134
}.openMainMenu {
135135
switchRequestDesktopSiteToggle()
@@ -146,10 +146,7 @@ class CustomTabsTest {
146146
fun customTabOpenInBrowserTest() {
147147
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
148148

149-
ActivityScenario
150-
.launch<IntentReceiverActivity>(
151-
createCustomTabIntent(customTabPage.url.toString()),
152-
).use {
149+
launchCustomTab(customTabPage.url.toString()).use {
153150
customTabScreen {
154151
}.openMainMenu {
155152
}.clickOpenInBrowserButton {

0 commit comments

Comments
 (0)