@ -11,10 +11,8 @@ import io.mockk.confirmVerified
import io.mockk.every
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.mockkStatic
import io.mockk.slot
import io.mockk.slot
import io.mockk.unmockkObject
import io.mockk.unmockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import io.mockk.verify
import org.junit.After
import org.junit.After
@ -26,44 +24,49 @@ import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
@RunWith ( FenixRobolectricTestRunner :: class )
@RunWith ( FenixRobolectricTestRunner :: class )
class StrictModeManagerTest {
class StrictModeManagerTest {
private lateinit var debugManager : StrictModeManager
private lateinit var releaseManager : StrictModeManager
@MockK ( relaxUnitFun = true ) private lateinit var fragmentManager : FragmentManager
@MockK ( relaxUnitFun = true ) private lateinit var fragmentManager : FragmentManager
@Before
@Before
fun setup ( ) {
fun setup ( ) {
MockKAnnotations . init ( this )
MockKAnnotations . init ( this )
mockkStatic ( StrictMode :: class )
mockkStatic ( StrictMode :: class )
mockkObject ( Config )
// 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.
val debugConfig : Config = mockk { every { channel } returns ReleaseChannel . Debug }
debugManager = StrictModeManager ( debugConfig )
val releaseConfig : Config = mockk { every { channel } returns ReleaseChannel . Release }
releaseManager = StrictModeManager ( releaseConfig )
}
}
@After
@After
fun teardown ( ) {
fun teardown ( ) {
unmockkStatic ( StrictMode :: class )
unmockkStatic ( StrictMode :: class )
unmockkObject ( Config )
}
}
@Test
@Test
fun `test enableStrictMode in release` ( ) {
fun `test enableStrictMode in release` ( ) {
every { Config . channel } returns ReleaseChannel . Release
releaseManager . enableStrictMode ( false )
StrictModeManager . enableStrictMode ( false )
verify ( exactly = 0 ) { StrictMode . setThreadPolicy ( any ( ) ) }
verify ( exactly = 0 ) { StrictMode . setThreadPolicy ( any ( ) ) }
verify ( exactly = 0 ) { StrictMode . setVmPolicy ( any ( ) ) }
verify ( exactly = 0 ) { StrictMode . setVmPolicy ( any ( ) ) }
}
}
@Test
@Test
fun `test enableStrictMode in debug` ( ) {
fun `test enableStrictMode in debug` ( ) {
every { Config . channel } returns ReleaseChannel . Debug
debugManager . enableStrictMode ( false )
StrictModeManager . enableStrictMode ( false )
verify { StrictMode . setThreadPolicy ( any ( ) ) }
verify { StrictMode . setThreadPolicy ( any ( ) ) }
verify { StrictMode . setVmPolicy ( any ( ) ) }
verify { StrictMode . setVmPolicy ( any ( ) ) }
}
}
@Test
@Test
fun `test changeStrictModePolicies `( ) {
fun `test changeStrictModePolicies in debug `( ) {
val callbacks = slot < FragmentManager . FragmentLifecycleCallbacks > ( )
val callbacks = slot < FragmentManager . FragmentLifecycleCallbacks > ( )
StrictMode Manager. attachListenerToDisablePenaltyDeath ( fragmentManager )
debug Manager. attachListenerToDisablePenaltyDeath ( fragmentManager )
verify { fragmentManager . registerFragmentLifecycleCallbacks ( capture ( callbacks ) , false ) }
verify { fragmentManager . registerFragmentLifecycleCallbacks ( capture ( callbacks ) , false ) }
confirmVerified ( fragmentManager )
confirmVerified ( fragmentManager )