Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Model-View-Intent with Unit Tests - FeedViewTestExtension.kt
@ExperimentalCoroutinesApi
class FeedViewTestExtension : BeforeEachCallback, AfterEachCallback, ParameterResolver {
override fun beforeEach(context: ExtensionContext?) {
// Set TestCoroutineDispatcher.
Dispatchers.setMain(context?.root
?.getStore(TEST_COROUTINE_DISPATCHER_NAMESPACE)
?.get(TEST_COROUTINE_DISPATCHER_KEY, TestCoroutineDispatcher::class.java)!!)
// Set LiveData Executor.
ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
override fun executeOnDiskIO(runnable: Runnable) = runnable.run()
override fun postToMainThread(runnable: Runnable) = runnable.run()
override fun isMainThread(): Boolean = true
})
}
override fun afterEach(context: ExtensionContext?) {
// Reset TestCoroutineDispatcher.
Dispatchers.resetMain()
context?.root
?.getStore(TEST_COROUTINE_DISPATCHER_NAMESPACE)
?.get(TEST_COROUTINE_DISPATCHER_KEY, TestCoroutineDispatcher::class.java)!!
.cleanupTestCoroutines()
context.root
?.getStore(TEST_COROUTINE_SCOPE_NAMESPACE)
?.get(TEST_COROUTINE_SCOPE_KEY, TestCoroutineScope::class.java)!!
.cleanupTestCoroutines()
// Clear LiveData Executor
ArchTaskExecutor.getInstance().setDelegate(null)
unmockkAll()
}
override fun supportsParameter(
parameterContext: ParameterContext?,
extensionContext: ExtensionContext?
) =
parameterContext?.parameter?.type == TestCoroutineDispatcher::class.java
|| parameterContext?.parameter?.type == TestCoroutineScope::class.java
override fun resolveParameter(
parameterContext: ParameterContext?,
extensionContext: ExtensionContext?
) =
if (parameterContext?.parameter?.type == TestCoroutineDispatcher::class.java)
getTestCoroutineDispatcher(extensionContext).let { dipatcher ->
if (dipatcher == null) saveAndReturnTestCoroutineDispatcher(extensionContext)
else dipatcher
}
else if (parameterContext?.parameter?.type == TestCoroutineScope::class.java)
getTestCoroutineScope(extensionContext).let { scope ->
if (scope == null) saveAndReturnTestCoroutineScope(extensionContext)
else scope
}
else null
private fun getTestCoroutineDispatcher(context: ExtensionContext?) = context?.root
?.getStore(TEST_COROUTINE_DISPATCHER_NAMESPACE)
?.get(TEST_COROUTINE_DISPATCHER_KEY, TestCoroutineDispatcher::class.java)
private fun saveAndReturnTestCoroutineDispatcher(extensionContext: ExtensionContext?) =
TestCoroutineDispatcher().apply {
extensionContext?.root
?.getStore(TEST_COROUTINE_DISPATCHER_NAMESPACE)
?.put(TEST_COROUTINE_DISPATCHER_KEY, this)
}
private fun getTestCoroutineScope(context: ExtensionContext?) = context?.root
?.getStore(TEST_COROUTINE_SCOPE_NAMESPACE)
?.get(TEST_COROUTINE_SCOPE_KEY, TestCoroutineScope::class.java)
private fun saveAndReturnTestCoroutineScope(context: ExtensionContext?) =
TestCoroutineScope(getTestCoroutineDispatcher(context)!!).apply {
context?.root
?.getStore(TEST_COROUTINE_SCOPE_NAMESPACE)
?.put(TEST_COROUTINE_SCOPE_KEY, this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment