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
1
test-util-android/.gitignore
vendored
Normal file
1
test-util-android/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/build
|
||||
34
test-util-android/build.gradle
Normal file
34
test-util-android/build.gradle
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
plugins {
|
||||
id 'com.android.library'
|
||||
id 'org.jetbrains.kotlin.android'
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdk 31
|
||||
|
||||
defaultConfig {
|
||||
minSdk 21
|
||||
targetSdk 31
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
|
||||
}
|
||||
0
test-util-android/consumer-rules.pro
Normal file
0
test-util-android/consumer-rules.pro
Normal file
21
test-util-android/proguard-rules.pro
vendored
Normal file
21
test-util-android/proguard-rules.pro
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
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