Skip to content

Commit d05191e

Browse files
committed
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 d05191e

2 files changed

Lines changed: 21 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: 20 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,18 @@ 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) = ActivityScenario.launch<IntentReceiverActivity>(
37+
createCustomTabIntent(pageUrl).apply {
38+
component = ComponentName(
39+
InstrumentationRegistry.getInstrumentation().targetContext,
40+
IntentReceiverActivity::class.java,
41+
)
42+
},
43+
)
44+
3145
@Before
3246
fun setUp() {
3347
mockWebServer = MockWebServer().apply {
@@ -45,10 +59,7 @@ class CustomTabsTest {
4559
fun openCustomTabTest() {
4660
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
4761

48-
ActivityScenario
49-
.launch<IntentReceiverActivity>(
50-
createCustomTabIntent(customTabPage.url.toString()),
51-
).use {
62+
launchCustomTab(customTabPage.url.toString()).use {
5263
customTabScreen {
5364
verifyCloseButton()
5465
verifyTrackingProtectionIcon()
@@ -65,10 +76,7 @@ class CustomTabsTest {
6576
fun verifyCustomTabMenuItemsTest() {
6677
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
6778

68-
ActivityScenario
69-
.launch<IntentReceiverActivity>(
70-
createCustomTabIntent(customTabPage.url.toString()),
71-
).use {
79+
launchCustomTab(customTabPage.url.toString()).use {
7280
customTabScreen {
7381
}.openMainMenu {
7482
verifyForwardButton()
@@ -87,10 +95,7 @@ class CustomTabsTest {
8795
val pageLinks = TestAssetHelper.getGenericAsset(mockWebServer, 4)
8896
val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)
8997

90-
ActivityScenario
91-
.launch<IntentReceiverActivity>(
92-
createCustomTabIntent(pageLinks.url.toString()),
93-
).use {
98+
launchCustomTab(pageLinks.url.toString()).use {
9499
customTabScreen {
95100
clickGenericLink("Link 1")
96101
verifyPageTitle(genericURL.title)
@@ -110,10 +115,7 @@ class CustomTabsTest {
110115
fun customTabShareTest() {
111116
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
112117

113-
ActivityScenario
114-
.launch<IntentReceiverActivity>(
115-
createCustomTabIntent(customTabPage.url.toString()),
116-
).use {
118+
launchCustomTab(customTabPage.url.toString()).use {
117119
customTabScreen {
118120
}.openMainMenu {
119121
}.clickShareButton {
@@ -126,10 +128,7 @@ class CustomTabsTest {
126128
fun customTabRequestDesktopSiteTest() {
127129
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
128130

129-
ActivityScenario
130-
.launch<IntentReceiverActivity>(
131-
createCustomTabIntent(customTabPage.url.toString()),
132-
).use {
131+
launchCustomTab(customTabPage.url.toString()).use {
133132
customTabScreen {
134133
}.openMainMenu {
135134
switchRequestDesktopSiteToggle()
@@ -146,10 +145,7 @@ class CustomTabsTest {
146145
fun customTabOpenInBrowserTest() {
147146
val customTabPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
148147

149-
ActivityScenario
150-
.launch<IntentReceiverActivity>(
151-
createCustomTabIntent(customTabPage.url.toString()),
152-
).use {
148+
launchCustomTab(customTabPage.url.toString()).use {
153149
customTabScreen {
154150
}.openMainMenu {
155151
}.clickOpenInBrowserButton {

0 commit comments

Comments
 (0)