Issue#67 Extract runOnUIAwaitOnCurrent into separate module
This commit is contained in:
parent
4932b4b2e0
commit
689aee9702
12 changed files with 74 additions and 6 deletions
5
test-util-android/src/main/AndroidManifest.xml
Normal file
5
test-util-android/src/main/AndroidManifest.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.fnives.test.showcase.android.testutil">
|
||||
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package org.fnives.test.showcase.android.testutil.synchronization
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
/**
|
||||
* Runs the given action on the MainThread and blocks currentThread, until it is completed.
|
||||
*
|
||||
* It is safe to call this from the MainThread.
|
||||
*/
|
||||
fun runOnUIAwaitOnCurrent(action: () -> Unit) {
|
||||
if (Looper.myLooper() === Looper.getMainLooper()) {
|
||||
action()
|
||||
} else {
|
||||
val deferred = CompletableDeferred<Unit>()
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
action()
|
||||
deferred.complete(Unit)
|
||||
}
|
||||
runBlocking { deferred.await() }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue