-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: CoroutinesTestExtension 구현 * feat: Livedata getOrAwaitValue 구현 * feat: InstantTaskExecutorExtension 구현 * feat: TestFixture 생성 * style: ktlint 적용
- Loading branch information
Showing
5 changed files
with
91 additions
and
16 deletions.
There are no files selected for viewing
16 changes: 0 additions & 16 deletions
16
android/app/src/test/java/com/zzang/chongdae/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
android/app/src/test/java/com/zzang/chongdae/util/CoroutinesTestExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
android/app/src/test/java/com/zzang/chongdae/util/GetOrAwaitValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T> LiveData<T>.getOrAwaitValue( | ||
time: Long = 2, | ||
timeUnit: TimeUnit = TimeUnit.SECONDS, | ||
): T { | ||
var data: T? = null | ||
val latch = CountDownLatch(1) | ||
val observer = | ||
object : Observer<T> { | ||
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 | ||
} |
31 changes: 31 additions & 0 deletions
31
android/app/src/test/java/com/zzang/chongdae/util/InstantTaskExecutorExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
android/app/src/test/java/com/zzang/chongdae/util/TestFixture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.zzang.chongdae.util | ||
|
||
object TestFixture |