Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit fa60356

Browse files
committed
adds coroutines to tests
1 parent ce63069 commit fa60356

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

WebView/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@ dependencies {
7373
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
7474
androidTestImplementation 'androidx.test:rules:1.1.1'
7575
androidTestImplementation 'androidx.test:runner:1.1.0'
76+
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0"
77+
7678
}

WebView/app/src/androidTest/java/com/android/samples/webviewdemo/MainActivityTest.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.android.samples.webviewdemo
1919
import android.content.Context
2020
import android.os.Handler
2121
import android.os.Looper
22+
import android.util.Log
2223
import android.webkit.WebView
2324
import androidx.test.core.app.ApplicationProvider
2425
import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches
@@ -33,6 +34,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
3334
import androidx.test.rule.ActivityTestRule
3435
import junit.framework.Assert.assertEquals
3536
import 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
3642
import org.hamcrest.CoreMatchers.`is`
3743
import org.hamcrest.CoreMatchers.containsString
3844
import 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

Comments
 (0)