diff --git a/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt b/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt index 3902b795e..4d48b3835 100644 --- a/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt +++ b/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt @@ -19,6 +19,7 @@ import androidx.fragment.app.FragmentManager import mozilla.components.support.ktx.android.os.resetAfter import org.mozilla.fenix.components.Components import org.mozilla.fenix.perf.Performance +import org.mozilla.fenix.utils.Mockable private const val MANUFACTURE_HUAWEI: String = "HUAWEI" private const val MANUFACTURE_ONE_PLUS: String = "OnePlus" @@ -29,6 +30,7 @@ private val mainLooper = Looper.getMainLooper() /** * Manages strict mode settings for the application. */ +@Mockable class StrictModeManager( config: Config, diff --git a/app/src/test/java/org/mozilla/fenix/IntentReceiverActivityTest.kt b/app/src/test/java/org/mozilla/fenix/IntentReceiverActivityTest.kt index a94d79414..271736386 100644 --- a/app/src/test/java/org/mozilla/fenix/IntentReceiverActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/IntentReceiverActivityTest.kt @@ -17,6 +17,7 @@ import io.mockk.verify import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runBlockingTest import mozilla.components.feature.intent.processing.IntentProcessor +import mozilla.components.support.test.robolectric.testContext import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -202,7 +203,10 @@ class IntentReceiverActivityTest { every { activity.settings() } returns settings every { activity.components.analytics } returns mockk(relaxed = true) every { activity.components.intentProcessors } returns intentProcessors - every { activity.components.strictMode } returns mockk(relaxed = true) + + // For some reason, activity.components doesn't return application.components, which is the + // globally defined TestComponents, so we redirect it. + every { activity.components.strictMode } returns testContext.components.strictMode } private inline fun mockIntentProcessor(): T { diff --git a/app/src/test/java/org/mozilla/fenix/components/AccountAbnormalitiesTest.kt b/app/src/test/java/org/mozilla/fenix/components/AccountAbnormalitiesTest.kt index c69007c10..224496fd0 100644 --- a/app/src/test/java/org/mozilla/fenix/components/AccountAbnormalitiesTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/AccountAbnormalitiesTest.kt @@ -17,6 +17,9 @@ import org.junit.Assert.assertEquals import org.junit.Assert.fail import org.junit.Test import org.junit.runner.RunWith +import org.mozilla.fenix.StrictModeManager +import org.mozilla.fenix.helpers.TestStrictModeManager +import kotlin.coroutines.CoroutineContext @RunWith(FenixRobolectricTestRunner::class) class AccountAbnormalitiesTest { @@ -25,7 +28,7 @@ class AccountAbnormalitiesTest { val crashReporter: CrashReporter = mockk() // no account present - val accountAbnormalities = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true)) + val accountAbnormalities = newAccountAbnormalities(crashReporter) try { accountAbnormalities.userRequestedLogout() @@ -52,7 +55,7 @@ class AccountAbnormalitiesTest { val crashReporter: CrashReporter = mockk(relaxed = true) val accountManager: FxaAccountManager = mockk(relaxed = true) - val accountAbnormalities = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true), this.coroutineContext) + val accountAbnormalities = newAccountAbnormalities(crashReporter, this.coroutineContext) accountAbnormalities.accountManagerStarted(accountManager) // Logout action must be preceded by auth. @@ -65,7 +68,7 @@ class AccountAbnormalitiesTest { val crashReporter: CrashReporter = mockk(relaxed = true) val accountManager: FxaAccountManager = mockk(relaxed = true) - val accountAbnormalities = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true), this.coroutineContext) + val accountAbnormalities = newAccountAbnormalities(crashReporter, this.coroutineContext) accountAbnormalities.accountManagerStarted(accountManager) accountAbnormalities.onAuthenticated(mockk(), mockk()) @@ -83,7 +86,7 @@ class AccountAbnormalitiesTest { val crashReporter: CrashReporter = mockk(relaxed = true) val accountManager: FxaAccountManager = mockk(relaxed = true) - val accountAbnormalities = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true), this.coroutineContext) + val accountAbnormalities = newAccountAbnormalities(crashReporter, this.coroutineContext) accountAbnormalities.accountManagerStarted(accountManager) // User didn't request this logout. @@ -96,7 +99,7 @@ class AccountAbnormalitiesTest { val crashReporter: CrashReporter = mockk(relaxed = true) val accountManager: FxaAccountManager = mockk(relaxed = true) - val accountAbnormalities = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true), this.coroutineContext) + val accountAbnormalities = newAccountAbnormalities(crashReporter, this.coroutineContext) accountAbnormalities.accountManagerStarted(accountManager) accountAbnormalities.onAuthenticated(mockk(), mockk()) @@ -104,7 +107,7 @@ class AccountAbnormalitiesTest { every { accountManager.authenticatedAccount() } returns null // Pretend we restart, and instantiate a new middleware instance. - val accountAbnormalities2 = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true), this.coroutineContext) + val accountAbnormalities2 = newAccountAbnormalities(crashReporter, this.coroutineContext) // mock accountManager doesn't have an account, but we expect it to have one since we // were authenticated before our "restart". accountAbnormalities2.accountManagerStarted(accountManager) @@ -117,7 +120,7 @@ class AccountAbnormalitiesTest { val crashReporter: CrashReporter = mockk() val accountManager: FxaAccountManager = mockk(relaxed = true) - val accountAbnormalities = AccountAbnormalities(testContext, crashReporter, mockk(relaxed = true), this.coroutineContext) + val accountAbnormalities = newAccountAbnormalities(crashReporter, coroutineContext) accountAbnormalities.accountManagerStarted(accountManager) // We saw an auth event, then user requested a logout. @@ -131,4 +134,13 @@ class AccountAbnormalitiesTest { crashReporter.submitCaughtException(any()) } } + + private fun newAccountAbnormalities( + crashReporter: CrashReporter, + coroutineContext: CoroutineContext? = null + ): AccountAbnormalities = if (coroutineContext != null) { + AccountAbnormalities(testContext, crashReporter, TestStrictModeManager() as StrictModeManager, coroutineContext) + } else { + AccountAbnormalities(testContext, crashReporter, TestStrictModeManager() as StrictModeManager) + } } diff --git a/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt b/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt index 1d31eaf56..a37c30071 100644 --- a/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt +++ b/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt @@ -6,6 +6,7 @@ package org.mozilla.fenix.components import android.content.Context import io.mockk.mockk +import org.mozilla.fenix.helpers.TestStrictModeManager import org.mozilla.fenix.utils.ClipboardHandler import org.mozilla.fenix.utils.Settings @@ -33,4 +34,6 @@ class TestComponents(private val context: Context) : Components(context) { override val clipboardHandler by lazy { ClipboardHandler(context) } override val settings by lazy { mockk(relaxed = true) } + + override val strictMode by lazy { TestStrictModeManager() } } diff --git a/app/src/test/java/org/mozilla/fenix/helpers/TestStrictModeManager.kt b/app/src/test/java/org/mozilla/fenix/helpers/TestStrictModeManager.kt new file mode 100644 index 000000000..3f8d5f233 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/helpers/TestStrictModeManager.kt @@ -0,0 +1,22 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.helpers + +import android.os.StrictMode +import io.mockk.mockk +import org.mozilla.fenix.StrictModeManager + +/** + * A test version of [StrictModeManager]. This class is difficult to mock because of [resetAfter] + * so we provide a test implementation. + */ +class TestStrictModeManager : StrictModeManager(mockk(relaxed = true), mockk(relaxed = true)) { + + // This method is hard to mock because this method needs to return the return value of the + // function passed in. + override fun resetAfter(policy: StrictMode.ThreadPolicy, functionBlock: () -> R): R { + return functionBlock() + } +}