No issue: fix tests related to inline change.

The difficulty in mocking StrictMode.resetAfter is concerning.
I'm starting to second-guess whether or not making strict mode manager a class
was a good idea.
pull/216/head
Michael Comella 4 years ago committed by Michael Comella
parent 7b5cd8c165
commit 7c0d00c800

@ -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,

@ -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 <reified T : IntentProcessor> mockIntentProcessor(): T {

@ -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<T>())
}
}
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)
}
}

@ -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<Settings>(relaxed = true) }
override val strictMode by lazy { TestStrictModeManager() }
}

@ -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 <R> resetAfter(policy: StrictMode.ThreadPolicy, functionBlock: () -> R): R {
return functionBlock()
}
}
Loading…
Cancel
Save