diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt index 4d04e7e20d..864a3692ce 100644 --- a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.customtabs import android.content.Intent +import androidx.annotation.VisibleForTesting import androidx.navigation.NavDestination import androidx.navigation.NavDirections import kotlinx.android.synthetic.main.activity_home.* @@ -111,16 +112,19 @@ open class ExternalAppBrowserActivity : HomeActivity() { } } - private fun hasExternalTab(): Boolean { + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + internal fun hasExternalTab(): Boolean { return getExternalTab() != null } - private fun getExternalTab(): SessionState? { + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + internal fun getExternalTab(): SessionState? { val id = getExternalTabId() ?: return null return components.core.store.state.findCustomTab(id) } - private fun getExternalTabId(): String? { + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + internal fun getExternalTabId(): String? { return getIntentSessionId(SafeIntent(intent)) } } diff --git a/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarMenuControllerTest.kt b/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarMenuControllerTest.kt index a5c2d6c314..4082077ae6 100644 --- a/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarMenuControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarMenuControllerTest.kt @@ -513,7 +513,7 @@ class DefaultBrowserToolbarMenuControllerTest { verify { currentSession.customTabConfig = null } verify { sessionManager.select(currentSession) } verify { activity.startActivity(openInFenixIntent) } - verify { activity.finish() } + verify { activity.finishAndRemoveTask() } } @Test diff --git a/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt b/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt index 19d562c9ef..280f40e80f 100644 --- a/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt @@ -12,18 +12,28 @@ import io.mockk.every import io.mockk.mockk import io.mockk.spyk import io.mockk.verify +import mozilla.components.browser.state.state.BrowserState +import mozilla.components.browser.state.state.createCustomTab +import mozilla.components.browser.state.state.createTab +import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.feature.intent.ext.putSessionId import mozilla.components.support.utils.toSafeIntent import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Test +import org.junit.runner.RunWith import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.components +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.utils.Settings +@RunWith(FenixRobolectricTestRunner::class) class ExternalAppBrowserActivityTest { @Test @@ -95,4 +105,68 @@ class ExternalAppBrowserActivityTest { assertNull(directions) verify { activity.finishAndRemoveTask() } } + + @Test + fun `ExternalAppBrowserActivity with matching external tab`() { + val store = BrowserStore(BrowserState( + customTabs = listOf( + createCustomTab( + url = "https://www.mozilla.org", + id = "mozilla" + ) + ) + )) + + val intent = Intent(Intent.ACTION_VIEW).apply { putSessionId("mozilla") } + + val activity = spyk(ExternalAppBrowserActivity()) + every { activity.components.core.store } returns store + every { activity.intent } returns intent + + assertTrue(activity.hasExternalTab()) + + assertEquals("mozilla", activity.getExternalTabId()) + + val tab = activity.getExternalTab() + assertNotNull(tab!!) + assertEquals("https://www.mozilla.org", tab.content.url) + } + + @Test + fun `ExternalAppBrowserActivity without matching external tab`() { + val store = BrowserStore() + + val intent = Intent(Intent.ACTION_VIEW).apply { putSessionId("mozilla") } + + val activity = spyk(ExternalAppBrowserActivity()) + every { activity.components.core.store } returns store + every { activity.intent } returns intent + + assertFalse(activity.hasExternalTab()) + assertEquals("mozilla", activity.getExternalTabId()) + assertNull(activity.getExternalTab()) + } + + @Test + fun `ExternalAppBrowserActivity with matching regular tab`() { + val store = BrowserStore(BrowserState( + tabs = listOf( + createTab( + url = "https://www.mozilla.org", + id = "mozilla" + ) + ) + )) + + val intent = Intent(Intent.ACTION_VIEW).apply { putSessionId("mozilla") } + + val activity = spyk(ExternalAppBrowserActivity()) + every { activity.components.core.store } returns store + every { activity.intent } returns intent + + // Even though we have a matching regular tab we do not care about it in ExternalAppBrowserActivity + assertFalse(activity.hasExternalTab()) + assertEquals("mozilla", activity.getExternalTabId()) + assertNull(activity.getExternalTab()) + } }