Skip to content

Commit

Permalink
uses CompletableDeffered in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grcoleman committed Jul 29, 2020
1 parent c920025 commit e7bb953
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.`is`
Expand Down Expand Up @@ -97,7 +98,8 @@ class MainActivityTest {
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://example.com")
val expectedMessage = "hello"
val callback = async { return@async "hello" }
// val callback = async { return@async "hello" }
val callback = CompletableDeferred<String>()
// Get a handler that can be used to post to the main thread
val mainHandler = Handler(Looper.getMainLooper());
mainHandler.post {
Expand All @@ -108,7 +110,9 @@ class MainActivityTest {
webView,
jsObjName,
allowedOriginRules
) { message -> callback
) { message ->
// check the value of message
callback.complete(message)
}
//Inject JsObject into Html
webView.loadDataWithBaseURL(
Expand All @@ -130,7 +134,7 @@ class MainActivityTest {
// Setup
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://example.com")
val callback = async { isUiThread() }
val callback = CompletableDeferred<Looper>()
// Get a handler that can be used to post to the main thread
val mainHandler = Handler(Looper.getMainLooper())
// Start Interacting with webView on UI thread
Expand All @@ -142,7 +146,8 @@ class MainActivityTest {
webView,
jsObjName,
allowedOriginRules
) { message -> callback }
) { message -> callback.complete(Looper.myLooper()!!) }
// want the callback complete the deferred
//Inject JsObject into Html
webView.loadDataWithBaseURL(
"https://example.com", "<html></html>",
Expand All @@ -152,14 +157,6 @@ class MainActivityTest {
webView.evaluateJavascript("${jsObjName}.postMessage(`hello`)", null)
}
}
assertTrue(callback.await())
}

/**
* Returns true if the current thread is the UI thread based on the
* Looper.
*/
private fun isUiThread(): Boolean {
return Looper.myLooper() == Looper.getMainLooper()
assertTrue(callback.await() == Looper.getMainLooper())
}
}

0 comments on commit e7bb953

Please sign in to comment.