diff --git a/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt b/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt index b52faf9b38..13448b305f 100644 --- a/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt +++ b/app/src/main/java/org/mozilla/fenix/StrictModeManager.kt @@ -6,9 +6,12 @@ package org.mozilla.fenix import android.os.Build import android.os.StrictMode +import androidx.annotation.VisibleForTesting +import androidx.annotation.VisibleForTesting.PRIVATE import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import mozilla.components.support.ktx.android.os.resetAfter +import org.mozilla.fenix.perf.Performance private const val MANUFACTURE_HUAWEI: String = "HUAWEI" private const val MANUFACTURE_ONE_PLUS: String = "OnePlus" @@ -18,9 +21,14 @@ private const val MANUFACTURE_ONE_PLUS: String = "OnePlus" */ class StrictModeManager(config: Config) { + val logger = Performance.logger // public to be accessible by inline functions. + // This is public so it can be used by inline functions. val isEnabledByBuildConfig = config.channel.isDebug + @VisibleForTesting(otherwise = PRIVATE) + var suppressionCount: Long = 0 + /*** * Enables strict mode for debug purposes. meant to be run only in the main process. * @param setPenaltyDeath boolean value to decide setting the penaltyDeath as a penalty. @@ -83,6 +91,12 @@ class StrictModeManager(config: Config) { // Calling resetAfter takes 1-2ms (unknown device) so we only execute it if StrictMode can // actually be enabled. https://github.com/mozilla-mobile/fenix/issues/11617 return if (isEnabledByBuildConfig) { + // This can overflow and crash. However, it's unlikely we'll suppress StrictMode 9 + // quintillion times in a build config where StrictMode is enabled so we don't handle it + // because it'd increase complexity. + suppressionCount += 1 + logger.warn("StrictMode violation suppressed: #$suppressionCount") + policy.resetAfter(functionBlock) } else { functionBlock() diff --git a/app/src/test/java/org/mozilla/fenix/StrictModeManagerTest.kt b/app/src/test/java/org/mozilla/fenix/StrictModeManagerTest.kt index f4f3e21a81..a6a79e5dcd 100644 --- a/app/src/test/java/org/mozilla/fenix/StrictModeManagerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/StrictModeManagerTest.kt @@ -113,4 +113,11 @@ class StrictModeManagerTest { verify { StrictMode.setThreadPolicy(expectedPolicy) } } + + @Test + fun `GIVEN we're in debug mode WHEN we suppress StrictMode THEN the suppressed count increases`() { + assertEquals(0, debugManager.suppressionCount) + debugManager.resetAfter(StrictMode.allowThreadDiskReads()) { "" } + assertEquals(1, debugManager.suppressionCount) + } }