@@ -19,6 +19,7 @@ package com.android.samples.webviewdemo
1919import android.content.Context
2020import android.os.Handler
2121import android.os.Looper
22+ import android.util.Log
2223import android.webkit.WebView
2324import androidx.test.core.app.ApplicationProvider
2425import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches
@@ -33,6 +34,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
3334import androidx.test.rule.ActivityTestRule
3435import junit.framework.Assert.assertEquals
3536import junit.framework.Assert.assertTrue
37+ import kotlinx.coroutines.GlobalScope
38+ import kotlinx.coroutines.async
39+ import kotlinx.coroutines.delay
40+ import kotlinx.coroutines.launch
41+ import kotlinx.coroutines.runBlocking
3642import org.hamcrest.CoreMatchers.`is`
3743import org.hamcrest.CoreMatchers.containsString
3844import org.junit.Rule
@@ -90,12 +96,13 @@ class MainActivityTest {
9096 }
9197
9298 @Test
93- fun valueInCallback_compareValueInput_returnsTrue () {
99+ fun valueInCallback_compareValueInput_returnsTrue () = runBlocking() {
94100 mainActivityRule.activity
95101 // Setup
96102 val jsObjName = " jsObject"
97103 val allowedOriginRules = setOf<String >(" https://example.com" )
98104 val expectedMessage = " hello"
105+ val callback = async { return @async " hello" }
99106 // Get a handler that can be used to post to the main thread
100107 val mainHandler = Handler (Looper .getMainLooper());
101108 mainHandler.post {
@@ -106,7 +113,7 @@ class MainActivityTest {
106113 webView,
107114 jsObjName,
108115 allowedOriginRules
109- ) { message -> // save message; call .set()
116+ ) { message -> callback
110117 }
111118 // Inject JsObject into Html
112119 webView.loadDataWithBaseURL(
@@ -118,20 +125,17 @@ class MainActivityTest {
118125 }
119126 }
120127 // evaluate what comes out -> it should be hello
121- // *Note: //.get() is a place holder
122- assertEquals(
123- expectedMessage
124- , " //.get()"
125- )
128+ assertEquals(expectedMessage, callback.await())
126129 }
127130
128131 @Test
129132 // Checks that postMessage runs on the UI thread
130- fun checkingThreadCallbackRunsOn () {
133+ fun checkingThreadCallbackRunsOn () = runBlocking {
131134 mainActivityRule.activity
132135 // Setup
133136 val jsObjName = " jsObject"
134137 val allowedOriginRules = setOf<String >(" https://example.com" )
138+ val callback = async { isUiThread() }
135139 // Get a handler that can be used to post to the main thread
136140 val mainHandler = Handler (Looper .getMainLooper())
137141 // Start Interacting with webView on UI thread
@@ -143,7 +147,7 @@ class MainActivityTest {
143147 webView,
144148 jsObjName,
145149 allowedOriginRules
146- ) { message -> assertTrue(isUiThread()) }
150+ ) { message -> callback }
147151 // Inject JsObject into Html
148152 webView.loadDataWithBaseURL(
149153 " https://example.com" , " <html></html>" ,
@@ -153,6 +157,7 @@ class MainActivityTest {
153157 webView.evaluateJavascript(" ${jsObjName} .postMessage(`hello`)" , null )
154158 }
155159 }
160+ assertTrue(callback.await())
156161 }
157162
158163 /* *
0 commit comments