Skip to content

Commit

Permalink
Enable Robolectric paused mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnasSmicius committed Sep 13, 2023
1 parent 9d76e73 commit e8e0864
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions library/src/test/java/com/vinted/coper/CoperImplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue

@ExperimentalCoroutinesApi
@RunWith(RobolectricTestRunner::class)
@LooperMode(LooperMode.Mode.PAUSED)
@Config(manifest = Config.NONE, sdk = [23, 27])
class CoperImplTest {

Expand All @@ -39,7 +41,7 @@ class CoperImplTest {

@Before
fun setup() = runTest {
fixture.mockGetFragmentWithStub()
runIdlingMainThread { fixture.mockGetFragmentWithStub() }
mockCheckPermissions("test", PackageManager.PERMISSION_GRANTED)
}

Expand Down Expand Up @@ -415,7 +417,7 @@ class CoperImplTest {
)

val fragment = withTimeout(1000) {
fixture.getFragmentSafely()
runIdlingMainThread { fixture.getFragmentSafely() }
}

assertEquals(fragment, activity.supportFragmentManager.findFragmentByTag(FRAGMENT_TAG))
Expand Down Expand Up @@ -467,7 +469,7 @@ class CoperImplTest {
lifecycle = activity.lifecycle,
)

val fragment = fixture.getFragmentSafely()
val fragment = runIdlingMainThread { fixture.getFragmentSafely() }

val job = async {
fixture.request(permission)
Expand All @@ -486,7 +488,7 @@ class CoperImplTest {
lifecycle = activity.lifecycle,
fragmentManager = fragmentManager
)
val fragment = fixture.getFragmentSafely()
val fragment = runIdlingMainThread { fixture.getFragmentSafely() }
val permission = "onConfigurationChange"
activity.setPermissionResult(permission, PackageManager.PERMISSION_DENIED)

Expand Down Expand Up @@ -854,4 +856,12 @@ class CoperImplTest {
.add(spyCoperFragment, FRAGMENT_TAG)
.commitNow()
}

private suspend fun <T> runIdlingMainThread(block: suspend () -> T): T {
return coroutineScope {
val asyncBlock = async { block() }
val asyncMainThreadIdle = async { shadowOf(getMainLooper()).idle() }
awaitAll(asyncBlock, asyncMainThreadIdle).first() as T
}
}
}

0 comments on commit e8e0864

Please sign in to comment.