|
7 | 7 | package com.onesignal.extensions
|
8 | 8 |
|
9 | 9 | import android.app.Application
|
| 10 | +import io.kotest.common.runBlocking |
10 | 11 | import io.kotest.core.extensions.ConstructorExtension
|
11 | 12 | import io.kotest.core.extensions.TestCaseExtension
|
12 | 13 | import io.kotest.core.spec.AutoScan
|
13 | 14 | import io.kotest.core.spec.Spec
|
14 | 15 | import io.kotest.core.test.TestCase
|
15 | 16 | import io.kotest.core.test.TestResult
|
16 | 17 | import org.robolectric.annotation.Config
|
| 18 | +import java.util.concurrent.Callable |
17 | 19 | import kotlin.reflect.KClass
|
18 | 20 | import kotlin.reflect.full.findAnnotation
|
19 | 21 | import kotlin.time.Duration
|
@@ -94,11 +96,18 @@ internal class RobolectricExtension : ConstructorExtension, TestCaseExtension {
|
94 | 96 | testCase: TestCase,
|
95 | 97 | execute: suspend (TestCase) -> TestResult,
|
96 | 98 | ): TestResult {
|
97 |
| - val containedRobolectricRunner = ContainedRobolectricRunner(testCase.spec::class.getConfig()) |
98 |
| - containedRobolectricRunner.containedBefore() |
99 |
| - val result = execute(testCase) |
100 |
| - containedRobolectricRunner.containedAfter() |
101 |
| - return result |
| 99 | + val containedRobolectricRunner = |
| 100 | + ContainedRobolectricRunner(testCase.spec::class.getConfig()) |
| 101 | + // sdkEnvironment.runOnMainThread is important to ensure Robolectric's |
| 102 | + // looper state doesn't carry over to the next test class. |
| 103 | + return containedRobolectricRunner.sdkEnvironment.runOnMainThread( |
| 104 | + Callable { |
| 105 | + containedRobolectricRunner.containedBefore() |
| 106 | + val result = runBlocking { execute(testCase) } |
| 107 | + containedRobolectricRunner.containedAfter() |
| 108 | + result |
| 109 | + }, |
| 110 | + ) |
102 | 111 | }
|
103 | 112 | }
|
104 | 113 |
|
|
0 commit comments