Bug 1873457 - throw exception when an unsupported Activity is used in Activity ext

fenix/125.0
t-p-white 3 months ago committed by mergify[bot]
parent be13d13683
commit 7d42525477

@ -212,18 +212,15 @@ fun Activity.setNavigationIcon(
* @param customTabSessionId Optional custom tab session ID if navigating from a custom tab. * @param customTabSessionId Optional custom tab session ID if navigating from a custom tab.
* *
* @return the [NavDirections] for the given [Activity]. * @return the [NavDirections] for the given [Activity].
* @throws IllegalArgumentException if the given [Activity] is not supported.
*/ */
fun Activity.getNavDirections( fun Activity.getNavDirections(
from: BrowserDirection, from: BrowserDirection,
customTabSessionId: String? = null, customTabSessionId: String? = null,
): NavDirections? = when (this) { ): NavDirections? = when (this) {
is ExternalAppBrowserActivity -> { is ExternalAppBrowserActivity -> getExternalAppBrowserNavDirections(from, customTabSessionId)
getExternalAppBrowserNavDirections(from, customTabSessionId) is HomeActivity -> getHomeNavDirections(from)
} else -> throw IllegalArgumentException("$this is not supported")
else -> {
getHomeNavDirections(from)
}
} }
private fun Activity.getExternalAppBrowserNavDirections( 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]. * Depending on the [Activity], maybe derive the source of the given [intent].
* *
* @param intent the [SafeIntent] to derive the source from. * @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) { fun Activity.getIntentSource(intent: SafeIntent): String? = when (this) {
is ExternalAppBrowserActivity -> EXTERNAL_APP_BROWSER_INTENT_SOURCE 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? { 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]. * Depending on the [Activity], maybe derive the session ID of the given [intent].
* *
* @param intent the [SafeIntent] to derive the session ID from. * @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) { fun Activity.getIntentSessionId(intent: SafeIntent): String? = when (this) {
is ExternalAppBrowserActivity -> getExternalAppBrowserIntentSessionId(intent) is ExternalAppBrowserActivity -> getExternalAppBrowserIntentSessionId(intent)
else -> null is HomeActivity -> null
else -> throw IllegalArgumentException("$this is not supported")
} }
private fun getExternalAppBrowserIntentSessionId(intent: SafeIntent) = intent.getSessionId() 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]. * Get the breadcrumb message for the [Activity].
* *
* @param destination the [NavDestination] required to provide the destination ID. * @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) { fun Activity.getBreadcrumbMessage(destination: NavDestination): String = when (this) {
is ExternalAppBrowserActivity -> getExternalAppBrowserBreadcrumbMessage(destination.id) 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 { private fun Activity.getExternalAppBrowserBreadcrumbMessage(destinationId: Int): String {

@ -7,10 +7,13 @@ package org.mozilla.fenix.ext
import android.app.Activity import android.app.Activity
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import io.mockk.mockk
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.robolectric.Robolectric import org.robolectric.Robolectric
import org.robolectric.Shadows.shadowOf import org.robolectric.Shadows.shadowOf
@ -41,4 +44,36 @@ class ActivityTest {
for (f in flags) assertEquals(f, window.decorView.systemUiVisibility and f) for (f in flags) assertEquals(f, window.decorView.systemUiVisibility and f)
assertTrue(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)) 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<Activity>()
assertThrows(IllegalArgumentException::class.java) {
activity.getNavDirections(
BrowserDirection.FromGlobal,
)
}
}
@Test
fun `WHEN Activity is not supported THEN getIntentSource throws IllegalArgument exception`() {
val activity = mockk<Activity>()
assertThrows(IllegalArgumentException::class.java) { activity.getIntentSource(mockk()) }
}
@Test
fun `WHEN Activity is not supported THEN getIntentSessionId throws IllegalArgument exception`() {
val activity = mockk<Activity>()
assertThrows(IllegalArgumentException::class.java) { activity.getIntentSessionId(mockk()) }
}
@Test
fun `WHEN Activity is not supported THEN getBreadcrumbMessage throws IllegalArgument exception`() {
val activity = mockk<Activity>()
assertThrows(IllegalArgumentException::class.java) { activity.getBreadcrumbMessage(mockk()) }
}
} }

Loading…
Cancel
Save