|
| 1 | +package io.sentry.android.core.internal.util |
| 2 | + |
| 3 | +import android.os.StrictMode |
| 4 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 5 | +import kotlin.test.AfterTest |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlin.test.assertNotEquals |
| 9 | +import kotlin.test.assertTrue |
| 10 | +import org.junit.runner.RunWith |
| 11 | + |
| 12 | +@RunWith(AndroidJUnit4::class) |
| 13 | +class AndroidRuntimeManagerTest { |
| 14 | + |
| 15 | + val sut = AndroidRuntimeManager() |
| 16 | + |
| 17 | + @AfterTest |
| 18 | + fun `clean up`() { |
| 19 | + // Revert StrictMode policies to avoid issues with other tests |
| 20 | + StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX) |
| 21 | + StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `runWithRelaxedPolicy changes policy when running and restores it afterwards`() { |
| 26 | + var called = false |
| 27 | + val threadPolicy = StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build() |
| 28 | + val vmPolicy = StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build() |
| 29 | + assertNotEquals(StrictMode.ThreadPolicy.LAX, threadPolicy) |
| 30 | + assertNotEquals(StrictMode.VmPolicy.LAX, vmPolicy) |
| 31 | + |
| 32 | + // Set and assert the StrictMode policies |
| 33 | + StrictMode.setThreadPolicy(threadPolicy) |
| 34 | + StrictMode.setVmPolicy(vmPolicy) |
| 35 | + assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString()) |
| 36 | + assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString()) |
| 37 | + |
| 38 | + // Run the function and assert LAX policies |
| 39 | + called = |
| 40 | + sut.runWithRelaxedPolicy { |
| 41 | + assertEquals( |
| 42 | + StrictMode.ThreadPolicy.LAX.toString(), |
| 43 | + StrictMode.getThreadPolicy().toString(), |
| 44 | + ) |
| 45 | + assertEquals(StrictMode.VmPolicy.LAX.toString(), StrictMode.getVmPolicy().toString()) |
| 46 | + true |
| 47 | + } |
| 48 | + |
| 49 | + // Policies should be reverted back |
| 50 | + assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString()) |
| 51 | + assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString()) |
| 52 | + |
| 53 | + // Ensure the code ran |
| 54 | + assertTrue(called) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `runWithRelaxedPolicy changes policy and restores it afterwards even if the code throws`() { |
| 59 | + var called = false |
| 60 | + val threadPolicy = StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build() |
| 61 | + val vmPolicy = StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build() |
| 62 | + |
| 63 | + // Set and assert the StrictMode policies |
| 64 | + StrictMode.setThreadPolicy(threadPolicy) |
| 65 | + StrictMode.setVmPolicy(vmPolicy) |
| 66 | + |
| 67 | + // Run the function and assert LAX policies |
| 68 | + try { |
| 69 | + sut.runWithRelaxedPolicy { |
| 70 | + assertEquals( |
| 71 | + StrictMode.ThreadPolicy.LAX.toString(), |
| 72 | + StrictMode.getThreadPolicy().toString(), |
| 73 | + ) |
| 74 | + assertEquals(StrictMode.VmPolicy.LAX.toString(), StrictMode.getVmPolicy().toString()) |
| 75 | + called = true |
| 76 | + throw Exception("Test exception") |
| 77 | + } |
| 78 | + } catch (_: Exception) {} |
| 79 | + |
| 80 | + // Policies should be reverted back |
| 81 | + assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString()) |
| 82 | + assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString()) |
| 83 | + |
| 84 | + // Ensure the code ran |
| 85 | + assertTrue(called) |
| 86 | + } |
| 87 | +} |
0 commit comments