diff --git a/app/src/main/java/org/mozilla/fenix/ext/Activity.kt b/app/src/main/java/org/mozilla/fenix/ext/Activity.kt index 35c6b6253..ea884be7d 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/Activity.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/Activity.kt @@ -212,18 +212,15 @@ fun Activity.setNavigationIcon( * @param customTabSessionId Optional custom tab session ID if navigating from a custom tab. * * @return the [NavDirections] for the given [Activity]. + * @throws IllegalArgumentException if the given [Activity] is not supported. */ fun Activity.getNavDirections( from: BrowserDirection, customTabSessionId: String? = null, ): NavDirections? = when (this) { - is ExternalAppBrowserActivity -> { - getExternalAppBrowserNavDirections(from, customTabSessionId) - } - - else -> { - getHomeNavDirections(from) - } + is ExternalAppBrowserActivity -> getExternalAppBrowserNavDirections(from, customTabSessionId) + is HomeActivity -> getHomeNavDirections(from) + else -> throw IllegalArgumentException("$this is not supported") } private fun Activity.getExternalAppBrowserNavDirections( @@ -321,10 +318,12 @@ const val EXTERNAL_APP_BROWSER_INTENT_SOURCE = "CUSTOM_TAB" * Depending on the [Activity], maybe derive the source of the given [intent]. * * @param intent the [SafeIntent] to derive the source from. + * @throws IllegalArgumentException if the given [Activity] is not supported. */ fun Activity.getIntentSource(intent: SafeIntent): String? = when (this) { is ExternalAppBrowserActivity -> EXTERNAL_APP_BROWSER_INTENT_SOURCE - else -> getHomeIntentSource(intent) + is HomeActivity -> getHomeIntentSource(intent) + else -> throw IllegalArgumentException("$this is not supported") } private fun getHomeIntentSource(intent: SafeIntent): String? { @@ -339,10 +338,12 @@ private fun getHomeIntentSource(intent: SafeIntent): String? { * Depending on the [Activity], maybe derive the session ID of the given [intent]. * * @param intent the [SafeIntent] to derive the session ID from. + * @throws IllegalArgumentException if the given [Activity] is not supported. */ fun Activity.getIntentSessionId(intent: SafeIntent): String? = when (this) { is ExternalAppBrowserActivity -> getExternalAppBrowserIntentSessionId(intent) - else -> null + is HomeActivity -> null + else -> throw IllegalArgumentException("$this is not supported") } private fun getExternalAppBrowserIntentSessionId(intent: SafeIntent) = intent.getSessionId() @@ -351,10 +352,12 @@ private fun getExternalAppBrowserIntentSessionId(intent: SafeIntent) = intent.ge * Get the breadcrumb message for the [Activity]. * * @param destination the [NavDestination] required to provide the destination ID. + * @throws IllegalArgumentException if the given [Activity] is not supported. */ fun Activity.getBreadcrumbMessage(destination: NavDestination): String = when (this) { is ExternalAppBrowserActivity -> getExternalAppBrowserBreadcrumbMessage(destination.id) - else -> getHomeBreadcrumbMessage(destination.id) + is HomeActivity -> getHomeBreadcrumbMessage(destination.id) + else -> throw IllegalArgumentException("$this is not supported") } private fun Activity.getExternalAppBrowserBreadcrumbMessage(destinationId: Int): String { diff --git a/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt b/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt index 884e5bd5f..75621e391 100644 --- a/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt @@ -7,10 +7,13 @@ package org.mozilla.fenix.ext import android.app.Activity import android.view.View import android.view.WindowManager +import io.mockk.mockk import org.junit.Assert.assertEquals +import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith +import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.robolectric.Robolectric import org.robolectric.Shadows.shadowOf @@ -41,4 +44,36 @@ class ActivityTest { for (f in flags) assertEquals(f, window.decorView.systemUiVisibility and f) assertTrue(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)) } + + @Test + fun `WHEN Activity is not supported THEN getNavDirections throws IllegalArgument exception`() { + val activity = mockk() + + assertThrows(IllegalArgumentException::class.java) { + activity.getNavDirections( + BrowserDirection.FromGlobal, + ) + } + } + + @Test + fun `WHEN Activity is not supported THEN getIntentSource throws IllegalArgument exception`() { + val activity = mockk() + + assertThrows(IllegalArgumentException::class.java) { activity.getIntentSource(mockk()) } + } + + @Test + fun `WHEN Activity is not supported THEN getIntentSessionId throws IllegalArgument exception`() { + val activity = mockk() + + assertThrows(IllegalArgumentException::class.java) { activity.getIntentSessionId(mockk()) } + } + + @Test + fun `WHEN Activity is not supported THEN getBreadcrumbMessage throws IllegalArgument exception`() { + val activity = mockk() + + assertThrows(IllegalArgumentException::class.java) { activity.getBreadcrumbMessage(mockk()) } + } }