For #13959: add marker when StrictMode is suppressed.

pull/216/head
Michael Comella 4 years ago committed by Michael Comella
parent 3b5d6d58d9
commit 50959d997e

@ -10,12 +10,14 @@
package org.mozilla.fenix package org.mozilla.fenix
import android.os.Build import android.os.Build
import android.os.Looper
import android.os.StrictMode import android.os.StrictMode
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE import androidx.annotation.VisibleForTesting.PRIVATE
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import mozilla.components.support.ktx.android.os.resetAfter import mozilla.components.support.ktx.android.os.resetAfter
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.perf.Performance import org.mozilla.fenix.perf.Performance
private const val MANUFACTURE_HUAWEI: String = "HUAWEI" private const val MANUFACTURE_HUAWEI: String = "HUAWEI"
@ -24,9 +26,19 @@ private const val MANUFACTURE_ONE_PLUS: String = "OnePlus"
/** /**
* Manages strict mode settings for the application. * Manages strict mode settings for the application.
*/ */
class StrictModeManager(config: Config) { class StrictModeManager(
config: Config,
// Ideally, we'd pass in a more specific value but there is a circular dependency: StrictMode
// is passed into Core but we'd need to pass in Core here. Instead, we take components and later
// fetch the value we need from it.
//
// Public to be accessible by inline functions.
val components: Components
) {
val logger = Performance.logger // public to be accessible by inline functions. val logger = Performance.logger // public to be accessible by inline functions.
val mainLooper = Looper.getMainLooper() // public to be accessible by inline functions.
// This is public so it can be used by inline functions. // This is public so it can be used by inline functions.
val isEnabledByBuildConfig = config.channel.isDebug val isEnabledByBuildConfig = config.channel.isDebug
@ -111,6 +123,9 @@ class StrictModeManager(config: Config) {
// because it'd increase complexity. // because it'd increase complexity.
suppressionCount += 1 suppressionCount += 1
logger.warn("StrictMode violation suppressed: #$suppressionCount") logger.warn("StrictMode violation suppressed: #$suppressionCount")
if (Thread.currentThread() == mainLooper.thread) { // markers only supported on main thread.
components.core.engine.profiler?.addMarker("StrictMode.suppression", "Count: $suppressionCount")
}
policy.resetAfter(functionBlock) policy.resetAfter(functionBlock)
} else { } else {

@ -132,7 +132,7 @@ class Components(private val context: Context) {
val performance by lazy { PerformanceComponent() } val performance by lazy { PerformanceComponent() }
val push by lazy { Push(context, analytics.crashReporter) } val push by lazy { Push(context, analytics.crashReporter) }
val wifiConnectionMonitor by lazy { WifiConnectionMonitor(context as Application) } val wifiConnectionMonitor by lazy { WifiConnectionMonitor(context as Application) }
val strictMode by lazy { StrictModeManager(Config) } val strictMode by lazy { StrictModeManager(Config, this) }
val settings by lazy { Settings(context) } val settings by lazy { Settings(context) }

@ -21,6 +21,7 @@ import org.junit.Assert.fail
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
@RunWith(FenixRobolectricTestRunner::class) @RunWith(FenixRobolectricTestRunner::class)
@ -36,13 +37,15 @@ class StrictModeManagerTest {
MockKAnnotations.init(this) MockKAnnotations.init(this)
mockkStatic(StrictMode::class) mockkStatic(StrictMode::class)
val components: Components = mockk(relaxed = true)
// These tests log a warning that mockk couldn't set the backing field of Config.channel // These tests log a warning that mockk couldn't set the backing field of Config.channel
// but it doesn't seem to impact their correctness so I'm ignoring it. // but it doesn't seem to impact their correctness so I'm ignoring it.
val debugConfig: Config = mockk { every { channel } returns ReleaseChannel.Debug } val debugConfig: Config = mockk { every { channel } returns ReleaseChannel.Debug }
debugManager = StrictModeManager(debugConfig) debugManager = StrictModeManager(debugConfig, components)
val releaseConfig: Config = mockk { every { channel } returns ReleaseChannel.Release } val releaseConfig: Config = mockk { every { channel } returns ReleaseChannel.Release }
releaseManager = StrictModeManager(releaseConfig) releaseManager = StrictModeManager(releaseConfig, components)
} }
@After @After

Loading…
Cancel
Save