forked from android/views-widgets-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request android#177 from android/webview-testing
Webview testing
- Loading branch information
Showing
6 changed files
with
234 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
WebView/app/src/androidTest/java/com/android/samples/webviewdemo/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
145 changes: 145 additions & 0 deletions
145
WebView/app/src/androidTest/java/com/android/samples/webviewdemo/MainActivityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.samples.webviewdemo | ||
|
||
import android.content.Context | ||
import android.os.Looper | ||
import android.webkit.WebView | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches | ||
import androidx.test.espresso.web.model.Atoms.castOrDie | ||
import androidx.test.espresso.web.model.Atoms.script | ||
import androidx.test.espresso.web.sugar.Web.onWebView | ||
import androidx.test.espresso.web.webdriver.DriverAtoms.* | ||
import androidx.test.espresso.web.webdriver.Locator | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.filters.MediumTest | ||
import androidx.test.rule.ActivityTestRule | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import org.hamcrest.CoreMatchers.`is` | ||
import org.hamcrest.CoreMatchers.containsString | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
/** | ||
* Launch, interact, and verify conditions in an activity that has a WebView instance. | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
@MediumTest | ||
class MainActivityTest { | ||
|
||
private val context = ApplicationProvider.getApplicationContext<Context>() | ||
|
||
@Rule | ||
@JvmField | ||
val mainActivityRule = ActivityTestRule(MainActivity::class.java) | ||
|
||
// Test to check that the drop down menu behaves as expected | ||
@Test | ||
fun dropDownMenu_SanFran() { | ||
onWebView() | ||
.forceJavascriptEnabled() | ||
.withElement(findElement(Locator.ID, "location")) | ||
.perform(webClick()) // Similar to perform(click()) | ||
.withElement(findElement(Locator.ID, "SF")) | ||
.perform(webClick()) // Similar to perform(click()) | ||
.withElement(findElement(Locator.ID, "title")) | ||
.check(webMatches(getText(), containsString("San Francisco"))) | ||
} | ||
|
||
// Test for checking createJsObject | ||
@Test | ||
fun jsObjectIsInjectedAndContainsPostMessage() { | ||
onWebView() | ||
.check( | ||
webMatches( | ||
script( | ||
"return jsObject && typeof jsObject.postMessage == \"function\"", | ||
castOrDie(Boolean::class.javaObjectType) | ||
), | ||
`is`(true) | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun valueInCallback_compareValueInput() = runBlocking { | ||
// Setup | ||
val jsObjName = "jsObject" | ||
val allowedOriginRules = setOf("https://example.com") | ||
val expectedMessage = "hello" | ||
val onMessageReceived = CompletableDeferred<String?>() | ||
launch(Dispatchers.Main) { | ||
val webView = WebView(context).apply { | ||
settings.javaScriptEnabled = true | ||
} | ||
// Create & inject JsObject | ||
createJsObject( | ||
webView, | ||
jsObjName, | ||
allowedOriginRules | ||
) { message -> | ||
onMessageReceived.complete(message) | ||
} | ||
webView.loadDataWithBaseURL( | ||
"https://example.com", | ||
"<html><script>${jsObjName}.postMessage('${expectedMessage}')</script></html>", | ||
"text/html", | ||
null, | ||
null | ||
) | ||
} | ||
// evaluate argument being passed into onMessageReceived | ||
assertEquals(expectedMessage, onMessageReceived.await()) | ||
} | ||
|
||
@Test | ||
fun checkingThreadCallbackRunsOn() = runBlocking { | ||
// Setup | ||
val jsObjName = "jsObject" | ||
val allowedOriginRules = setOf("https://example.com") | ||
val expectedMessage = "hello" | ||
val onMessageReceived = CompletableDeferred<Looper?>() | ||
launch(Dispatchers.Main) { | ||
val webView = WebView(context).apply { | ||
settings.javaScriptEnabled = true | ||
} | ||
// Create & inject JsObject | ||
createJsObject( | ||
webView, | ||
jsObjName, | ||
allowedOriginRules | ||
) { | ||
onMessageReceived.complete(Looper.myLooper()) | ||
} | ||
webView.loadDataWithBaseURL( | ||
"https://example.com", | ||
"<html><script>${jsObjName}.postMessage('${expectedMessage}')</script></html>", | ||
"text/html", | ||
null, | ||
null | ||
) | ||
} | ||
assertEquals(onMessageReceived.await(), Looper.getMainLooper()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
WebView/app/src/main/java/com/android/samples/webviewdemo/JsObject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.samples.webviewdemo | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import android.webkit.JavascriptInterface | ||
import android.webkit.WebView | ||
import androidx.webkit.WebViewCompat | ||
import androidx.webkit.WebViewFeature | ||
|
||
// Create a handler that runs on the UI thread | ||
private val handler: Handler = Handler(Looper.getMainLooper()) | ||
|
||
/** | ||
* Injects a JavaScript object which supports a {@code postMessage()} method. | ||
* A feature check is used to determine if the preferred API, WebMessageListener, is supported. | ||
* If it is, then WebMessageListener will be used to create a JavaScript object. The object will be | ||
* injected into all of the frames that have an origin matching those in {@code allowedOriginRules}. | ||
* <p> | ||
* If WebMessageListener is not supported then the method will defer to using JavascriptInterface | ||
* to create the JavaScript object. | ||
* <p> | ||
* The {@code postMessage()} methods in the Javascript objects created by WebMessageListener and | ||
* JavascriptInterface both make calls to the same callback, {@code onMessageReceived()}. | ||
* In this case, the callback invokes native Android sharing. | ||
* <p> | ||
* The WebMessageListener invokes callbacks on the UI thread by default. However, | ||
* JavascriptInterface invokes callbacks on a background thread by default. In order to | ||
* guarantee thread safety and that the caller always gets consistent behavior the the callback | ||
* should always be called on the UI thread. To change the default behavior of JavascriptInterface, | ||
* the callback is wrapped in a handler which will tell it to run on the UI thread instead of the default | ||
* background thread it would otherwise be invoked on. | ||
* <p> | ||
* @param webview the component that WebMessageListener or JavascriptInterface will be added to | ||
* @param jsObjName the name that will be given to the Javascript objects created by either | ||
* WebMessageListener or JavascriptInterface | ||
* @param allowedOriginRules a set of origins used only by WebMessageListener, if a frame matches an | ||
* origin in this set then it will have the JS object injected into it | ||
* @param onMessageReceived invoked on UI thread with message passed in from JavaScript postMessage() call | ||
*/ | ||
fun createJsObject( | ||
webview: WebView, | ||
jsObjName: String, | ||
allowedOriginRules: Set<String>, | ||
onMessageReceived: (message: String) -> Unit | ||
) { | ||
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) { | ||
WebViewCompat.addWebMessageListener( | ||
webview, jsObjName, allowedOriginRules | ||
) { _, message, _, _, _ -> onMessageReceived(message.data!!) } | ||
} else { | ||
webview.addJavascriptInterface(object { | ||
@JavascriptInterface | ||
fun postMessage(message: String) { | ||
// Use the handler to invoke method on UI thread | ||
handler.post { onMessageReceived(message) } | ||
} | ||
}, jsObjName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters