diff --git a/app/src/main/java/org/mozilla/fenix/ext/StrictMode.kt b/app/src/main/java/org/mozilla/fenix/ext/StrictMode.kt deleted file mode 100644 index 54985108f..000000000 --- a/app/src/main/java/org/mozilla/fenix/ext/StrictMode.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* 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.ext - -import android.os.StrictMode -import mozilla.components.support.ktx.android.os.resetAfter -import org.mozilla.fenix.Config - -/** - * Runs the given [functionBlock] and sets the ThreadPolicy after its completion in Debug mode. - * Otherwise simply runs the [functionBlock] - * This function is written in the style of [AutoCloseable.use]. - * @return the value returned by [functionBlock]. - */ -inline fun StrictMode.ThreadPolicy.resetPoliciesAfter(functionBlock: () -> R): R { - // 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 - // - // The expression in this if is duplicated in StrictModeManager.enableStrictMode: see that method - // for details. - return if (Config.channel.isDebug) { - 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 61a14ef1f..f4f3e21a8 100644 --- a/app/src/test/java/org/mozilla/fenix/StrictModeManagerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/StrictModeManagerTest.kt @@ -16,6 +16,8 @@ import io.mockk.slot import io.mockk.unmockkStatic import io.mockk.verify import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.fail import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -73,4 +75,42 @@ class StrictModeManagerTest { callbacks.captured.onFragmentResumed(fragmentManager, mockk()) verify { fragmentManager.unregisterFragmentLifecycleCallbacks(callbacks.captured) } } + + @Test + fun `GIVEN we're in a release build WHEN resetAfter is called THEN we return the value from the function block`() { + val expected = "Hello world" + val actual = releaseManager.resetAfter(StrictMode.allowThreadDiskReads()) { expected } + assertEquals(expected, actual) + } + + @Test + fun `GIVEN we're in a debug build WHEN resetAfter is called THEN we return the value from the function block`() { + val expected = "Hello world" + val actual = debugManager.resetAfter(StrictMode.allowThreadDiskReads()) { expected } + assertEquals(expected, actual) + } + + @Test + fun `GIVEN we're in a release build WHEN resetAfter is called THEN the old policy is not set`() { + releaseManager.resetAfter(StrictMode.allowThreadDiskReads()) { "" } + verify(exactly = 0) { StrictMode.setThreadPolicy(any()) } + } + + @Test + fun `GIVEN we're in a debug build WHEN resetAfter is called THEN the old policy is set`() { + val expectedPolicy = StrictMode.allowThreadDiskReads() + debugManager.resetAfter(expectedPolicy) { "" } + verify { StrictMode.setThreadPolicy(expectedPolicy) } + } + + @Test + fun `GIVEN we're in a debug build WHEN resetAfter is called and an exception is thrown from the function THEN the old policy is set`() { + val expectedPolicy = StrictMode.allowThreadDiskReads() + try { + debugManager.resetAfter(expectedPolicy) { throw IllegalStateException() } + @Suppress("UNREACHABLE_CODE") fail("Expected previous method to throw.") + } catch (e: IllegalStateException) { /* Do nothing */ } + + verify { StrictMode.setThreadPolicy(expectedPolicy) } + } } diff --git a/app/src/test/java/org/mozilla/fenix/ext/StrictModeTest.kt b/app/src/test/java/org/mozilla/fenix/ext/StrictModeTest.kt deleted file mode 100644 index 26854c46d..000000000 --- a/app/src/test/java/org/mozilla/fenix/ext/StrictModeTest.kt +++ /dev/null @@ -1,79 +0,0 @@ -/* 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.ext - -import android.os.StrictMode -import io.mockk.Runs -import io.mockk.every -import io.mockk.just -import io.mockk.mockk -import io.mockk.mockkObject -import io.mockk.mockkStatic -import io.mockk.unmockkObject -import io.mockk.unmockkStatic -import io.mockk.verify -import org.junit.After -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import org.mozilla.fenix.Config -import org.mozilla.fenix.ReleaseChannel -import org.mozilla.fenix.helpers.FenixRobolectricTestRunner - -@RunWith(FenixRobolectricTestRunner::class) -class StrictModeTest { - - private lateinit var threadPolicy: StrictMode.ThreadPolicy - private lateinit var functionBlock: () -> String - - @Before - fun setup() { - threadPolicy = StrictMode.ThreadPolicy.LAX - functionBlock = mockk() - mockkStatic(StrictMode::class) - mockkObject(Config) - - every { StrictMode.setThreadPolicy(threadPolicy) } just Runs - every { functionBlock() } returns "Hello world" - } - - @After - fun teardown() { - unmockkStatic(StrictMode::class) - unmockkObject(Config) - } - - @Test - fun `runs function block in release`() { - every { Config.channel } returns ReleaseChannel.Release - assertEquals("Hello world", threadPolicy.resetPoliciesAfter(functionBlock)) - verify(exactly = 0) { StrictMode.setThreadPolicy(any()) } - } - - @Test - fun `runs function block in debug`() { - every { Config.channel } returns ReleaseChannel.Debug - assertEquals("Hello world", threadPolicy.resetPoliciesAfter(functionBlock)) - verify { StrictMode.setThreadPolicy(threadPolicy) } - } - - @Test - fun `sets thread policy even if function throws`() { - every { Config.channel } returns ReleaseChannel.Debug - every { functionBlock() } throws IllegalStateException() - var exception: IllegalStateException? = null - - try { - threadPolicy.resetPoliciesAfter(functionBlock) - } catch (e: IllegalStateException) { - exception = e - } - - verify { StrictMode.setThreadPolicy(threadPolicy) } - assertNotNull(exception) - } -}