summaryrefslogtreecommitdiff
path: root/coroutine-utils-testing
diff options
context:
space:
mode:
Diffstat (limited to 'coroutine-utils-testing')
-rw-r--r--coroutine-utils-testing/api/coroutine-utils-testing.api8
-rw-r--r--coroutine-utils-testing/build.gradle.kts14
-rw-r--r--coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt45
3 files changed, 67 insertions, 0 deletions
diff --git a/coroutine-utils-testing/api/coroutine-utils-testing.api b/coroutine-utils-testing/api/coroutine-utils-testing.api
new file mode 100644
index 00000000..a90e209f
--- /dev/null
+++ b/coroutine-utils-testing/api/coroutine-utils-testing.api
@@ -0,0 +1,8 @@
+public final class dev/msfjarvis/aps/test/CoroutineTestRule : org/junit/rules/TestWatcher {
+ public fun <init> ()V
+ public fun <init> (Lkotlinx/coroutines/test/TestDispatcher;)V
+ public synthetic fun <init> (Lkotlinx/coroutines/test/TestDispatcher;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
+ public final fun getTestDispatcher ()Lkotlinx/coroutines/test/TestDispatcher;
+ public final fun getTestDispatcherProvider ()Ldev/msfjarvis/aps/util/coroutines/DispatcherProvider;
+}
+
diff --git a/coroutine-utils-testing/build.gradle.kts b/coroutine-utils-testing/build.gradle.kts
new file mode 100644
index 00000000..96d87dc1
--- /dev/null
+++ b/coroutine-utils-testing/build.gradle.kts
@@ -0,0 +1,14 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+plugins {
+ kotlin("jvm")
+ id("com.github.android-password-store.kotlin-library")
+}
+
+dependencies {
+ implementation(projects.coroutineUtils)
+ implementation(libs.testing.junit)
+ implementation(libs.kotlin.coroutines.test)
+}
diff --git a/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt
new file mode 100644
index 00000000..fa4a2d41
--- /dev/null
+++ b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt
@@ -0,0 +1,45 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.test
+
+import dev.msfjarvis.aps.util.coroutines.DispatcherProvider
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.TestCoroutineScheduler
+import kotlinx.coroutines.test.TestDispatcher
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.setMain
+import org.junit.rules.TestWatcher
+import org.junit.runner.Description
+
+/**
+ * JUnit [TestWatcher] to correctly handle setting and resetting a given [testDispatcher] for tests.
+ */
+@ExperimentalCoroutinesApi
+public class CoroutineTestRule(
+ public val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(TestCoroutineScheduler()),
+) : TestWatcher() {
+
+ public val testDispatcherProvider: DispatcherProvider =
+ object : DispatcherProvider {
+ override fun default(): CoroutineDispatcher = testDispatcher
+ override fun io(): CoroutineDispatcher = testDispatcher
+ override fun main(): CoroutineDispatcher = testDispatcher
+ override fun unconfined(): CoroutineDispatcher = testDispatcher
+ }
+
+ override fun starting(description: Description?) {
+ super.starting(description)
+ Dispatchers.setMain(testDispatcher)
+ }
+
+ override fun finished(description: Description?) {
+ super.finished(description)
+ Dispatchers.resetMain()
+ }
+}