diff --git a/android/app/src/test/java/com/zzang/chongdae/ExampleUnitTest.kt b/android/app/src/test/java/com/zzang/chongdae/ExampleUnitTest.kt deleted file mode 100644 index 04067def..00000000 --- a/android/app/src/test/java/com/zzang/chongdae/ExampleUnitTest.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.zzang.chongdae - -import org.junit.Test -import org.junit.jupiter.api.Assertions.assertEquals - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} diff --git a/android/app/src/test/java/com/zzang/chongdae/util/CoroutinesTestExtension.kt b/android/app/src/test/java/com/zzang/chongdae/util/CoroutinesTestExtension.kt new file mode 100644 index 00000000..930a37eb --- /dev/null +++ b/android/app/src/test/java/com/zzang/chongdae/util/CoroutinesTestExtension.kt @@ -0,0 +1,24 @@ +package com.zzang.chongdae.util + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestDispatcher +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.jupiter.api.extension.AfterEachCallback +import org.junit.jupiter.api.extension.BeforeEachCallback +import org.junit.jupiter.api.extension.ExtensionContext + +@ExperimentalCoroutinesApi +class CoroutinesTestExtension( + private val dispatcher: TestDispatcher = UnconfinedTestDispatcher(), +) : BeforeEachCallback, AfterEachCallback { + override fun beforeEach(context: ExtensionContext?) { + Dispatchers.setMain(dispatcher) + } + + override fun afterEach(context: ExtensionContext?) { + Dispatchers.resetMain() + } +} diff --git a/android/app/src/test/java/com/zzang/chongdae/util/GetOrAwaitValue.kt b/android/app/src/test/java/com/zzang/chongdae/util/GetOrAwaitValue.kt new file mode 100644 index 00000000..e5e530b2 --- /dev/null +++ b/android/app/src/test/java/com/zzang/chongdae/util/GetOrAwaitValue.kt @@ -0,0 +1,33 @@ +package com.zzang.chongdae.util + +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeoutException + +fun LiveData.getOrAwaitValue( + time: Long = 2, + timeUnit: TimeUnit = TimeUnit.SECONDS, +): T { + var data: T? = null + val latch = CountDownLatch(1) + val observer = + object : Observer { + override fun onChanged(value: T) { + data = value + latch.countDown() + this@getOrAwaitValue.removeObserver(this) + } + } + + this.observeForever(observer) + + // Don't wait indefinitely if the LiveData is not set. + if (!latch.await(time, timeUnit)) { + throw TimeoutException("LiveData value was never set.") + } + + @Suppress("UNCHECKED_CAST") + return data as T +} diff --git a/android/app/src/test/java/com/zzang/chongdae/util/InstantTaskExecutorExtension.kt b/android/app/src/test/java/com/zzang/chongdae/util/InstantTaskExecutorExtension.kt new file mode 100644 index 00000000..6413d929 --- /dev/null +++ b/android/app/src/test/java/com/zzang/chongdae/util/InstantTaskExecutorExtension.kt @@ -0,0 +1,31 @@ +package com.zzang.chongdae.util + +import androidx.arch.core.executor.ArchTaskExecutor +import androidx.arch.core.executor.TaskExecutor +import org.junit.jupiter.api.extension.AfterEachCallback +import org.junit.jupiter.api.extension.BeforeEachCallback +import org.junit.jupiter.api.extension.ExtensionContext + +class InstantTaskExecutorExtension : BeforeEachCallback, AfterEachCallback { + override fun beforeEach(context: ExtensionContext?) { + ArchTaskExecutor.getInstance().setDelegate( + object : TaskExecutor() { + override fun executeOnDiskIO(runnable: Runnable) { + runnable.run() + } + + override fun postToMainThread(runnable: Runnable) { + runnable.run() + } + + override fun isMainThread(): Boolean { + return true + } + }, + ) + } + + override fun afterEach(context: ExtensionContext?) { + ArchTaskExecutor.getInstance().setDelegate(null) + } +} diff --git a/android/app/src/test/java/com/zzang/chongdae/util/TestFixture.kt b/android/app/src/test/java/com/zzang/chongdae/util/TestFixture.kt new file mode 100644 index 00000000..8d387111 --- /dev/null +++ b/android/app/src/test/java/com/zzang/chongdae/util/TestFixture.kt @@ -0,0 +1,3 @@ +package com.zzang.chongdae.util + +object TestFixture