Skip to content

Commit

Permalink
adds coroutines to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grcoleman committed Jul 27, 2020
1 parent ce63069 commit fa60356
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions WebView/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0"

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.android.samples.webviewdemo
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.webkit.WebView
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches
Expand All @@ -33,6 +34,11 @@ 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.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.containsString
import org.junit.Rule
Expand Down Expand Up @@ -90,12 +96,13 @@ class MainActivityTest {
}

@Test
fun valueInCallback_compareValueInput_returnsTrue() {
fun valueInCallback_compareValueInput_returnsTrue() = runBlocking() {
mainActivityRule.activity
// Setup
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://example.com")
val expectedMessage = "hello"
val callback = async { return@async "hello" }
// Get a handler that can be used to post to the main thread
val mainHandler = Handler(Looper.getMainLooper());
mainHandler.post {
Expand All @@ -106,7 +113,7 @@ class MainActivityTest {
webView,
jsObjName,
allowedOriginRules
) { message -> //save message; call .set()
) { message -> callback
}
//Inject JsObject into Html
webView.loadDataWithBaseURL(
Expand All @@ -118,20 +125,17 @@ class MainActivityTest {
}
}
// evaluate what comes out -> it should be hello
// *Note: //.get() is a place holder
assertEquals(
expectedMessage
, "//.get()"
)
assertEquals(expectedMessage, callback.await())
}

@Test
// Checks that postMessage runs on the UI thread
fun checkingThreadCallbackRunsOn() {
fun checkingThreadCallbackRunsOn() = runBlocking {
mainActivityRule.activity
// Setup
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://example.com")
val callback = async { isUiThread() }
// 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 @@ -143,7 +147,7 @@ class MainActivityTest {
webView,
jsObjName,
allowedOriginRules
) { message -> assertTrue(isUiThread()) }
) { message -> callback }
//Inject JsObject into Html
webView.loadDataWithBaseURL(
"https://example.com", "<html></html>",
Expand All @@ -153,6 +157,7 @@ class MainActivityTest {
webView.evaluateJavascript("${jsObjName}.postMessage(`hello`)", null)
}
}
assertTrue(callback.await())
}

/**
Expand Down

0 comments on commit fa60356

Please sign in to comment.