Issue#67 Extract MainThread synchronization into a separate module
This commit is contained in:
parent
1c0153db75
commit
bbe077dde8
8 changed files with 36 additions and 42 deletions
|
|
@ -32,4 +32,5 @@ android {
|
|||
dependencies {
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
|
||||
implementation "androidx.test:core:$androidx_test_version"
|
||||
implementation"androidx.test.espresso:espresso-core:$espresso_version"
|
||||
}
|
||||
|
|
@ -2,8 +2,11 @@ package org.fnives.test.showcase.android.testutil.synchronization
|
|||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.test.espresso.Espresso
|
||||
import androidx.test.espresso.matcher.ViewMatchers
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.fnives.test.showcase.android.testutil.viewaction.LoopMainThreadFor
|
||||
|
||||
/**
|
||||
* Runs the given action on the MainThread and blocks currentThread, until it is completed.
|
||||
|
|
@ -22,3 +25,11 @@ fun runOnUIAwaitOnCurrent(action: () -> Unit) {
|
|||
runBlocking { deferred.await() }
|
||||
}
|
||||
}
|
||||
|
||||
fun loopMainThreadFor(delay: Long) {
|
||||
if (Looper.getMainLooper().thread == Thread.currentThread()) {
|
||||
Thread.sleep(200L)
|
||||
} else {
|
||||
Espresso.onView(ViewMatchers.isRoot()).perform(LoopMainThreadFor(delay))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.fnives.test.showcase.android.testutil.viewaction
|
||||
|
||||
import android.view.View
|
||||
import androidx.test.espresso.UiController
|
||||
import androidx.test.espresso.ViewAction
|
||||
import org.hamcrest.Matcher
|
||||
import org.hamcrest.Matchers
|
||||
|
||||
class LoopMainThreadFor(private val delayInMillis: Long) : ViewAction {
|
||||
override fun getConstraints(): Matcher<View> = Matchers.isA(View::class.java)
|
||||
|
||||
override fun getDescription(): String = "loop MainThread for $delayInMillis milliseconds"
|
||||
|
||||
override fun perform(uiController: UiController, view: View?) {
|
||||
uiController.loopMainThreadForAtLeast(delayInMillis)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.fnives.test.showcase.android.testutil.viewaction
|
||||
|
||||
import android.view.View
|
||||
import androidx.test.espresso.UiController
|
||||
import androidx.test.espresso.ViewAction
|
||||
import org.hamcrest.Matcher
|
||||
import org.hamcrest.Matchers
|
||||
|
||||
class LoopMainThreadUntilIdle : ViewAction {
|
||||
override fun getConstraints(): Matcher<View> = Matchers.isA(View::class.java)
|
||||
|
||||
override fun getDescription(): String = "loop MainThread for until Idle"
|
||||
|
||||
override fun perform(uiController: UiController, view: View?) {
|
||||
uiController.loopMainThreadUntilIdle()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue