Saiedmomen
08/15/2019, 5:47 PMrunBlocking block in test gets stuck at collect call. I think the awaitClose call suspends the runBlocking scope but doesn't affect the android code somehow. Can you please tell me if I'm doing it right and how I can write unit tests for it?louiscad
08/15/2019, 6:42 PMtake(5) or launch 2 coroutines in parallel.Saiedmomen
08/15/2019, 6:47 PMlouiscad
08/15/2019, 6:50 PMFlow is infinite. It can always have new elements caused by new clicks, unless you stop collecting by using another operator or by throwing or cancelling.
lifecycleScope isn't much related. It's an auto-cancelling root scope to launch your lifecycle bound coroutines in.Saiedmomen
08/15/2019, 7:05 PMSaiedmomen
08/15/2019, 7:15 PMSaiedmomen
08/15/2019, 7:18 PMlouiscad
08/15/2019, 7:25 PMcollect extension instead of FlowCollector.louiscad
08/15/2019, 7:28 PMperformClick(). Then, cancelAndJoin() on the collection coroutine, and perform the assertion.Saiedmomen
08/15/2019, 7:35 PMlouiscad
08/15/2019, 7:41 PMfun View.clicksFlow(
disableAfterClick: Boolean = true,
hideAfterClick: Boolean = false
): Flow<Unit> = flow {
while (true) emit(
awaitOneClick(
disableAfterClick = disableAfterClick,
hideAfterClick = hideAfterClick
)
)
}
suspend fun View.awaitOneClick(
disableAfterClick: Boolean = true,
hideAfterClick: Boolean = false
) = try {
if (disableAfterClick) isEnabled = true
if (hideAfterClick) isVisible = true
suspendCancellableCoroutine<Unit> { continuation ->
setOnClickListener {
setOnClickListener(null) // Ensure we can't get two clicks in a row.
continuation.resume(Unit)
}
}
} finally {
setOnClickListener(null)
if (disableAfterClick) isEnabled = false
if (hideAfterClick) isVisible = false
}Saiedmomen
08/15/2019, 8:02 PM