Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2447)

Unified Diff: android_webview/test/shell/src/org/chromium/android_webview/test/util/JSUtils.java

Issue 2201783003: Add test to ensure shouldOverrideUrlLoading throws Java exception (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reupload PS1 with similarity 40% to show JSUtils.java as moved file. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/test/shell/src/org/chromium/android_webview/test/util/JSUtils.java
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/util/JSUtils.java b/android_webview/test/shell/src/org/chromium/android_webview/test/util/JSUtils.java
similarity index 49%
rename from android_webview/javatests/src/org/chromium/android_webview/test/util/JSUtils.java
rename to android_webview/test/shell/src/org/chromium/android_webview/test/util/JSUtils.java
index 306be04687e7d03ee9ff5eaba1d10aae9d851bd6..84b5a72befab325ae63ceeb928aeadd54c194eb9 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/util/JSUtils.java
+++ b/android_webview/test/shell/src/org/chromium/android_webview/test/util/JSUtils.java
@@ -11,6 +11,7 @@ import junit.framework.Assert;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import org.chromium.android_webview.AwContents;
+import org.chromium.base.ThreadUtils;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
@@ -19,15 +20,14 @@ import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
* Collection of functions for JavaScript-based interactions with a page.
*/
public class JSUtils {
+ private static final String TAG = JSUtils.class.getSimpleName();
private static final long WAIT_TIMEOUT_MS = scaleTimeout(2000);
private static final int CHECK_INTERVAL = 100;
- public static void clickOnLinkUsingJs(
- final InstrumentationTestCase testCase,
+ public static void clickOnLinkUsingJs(final InstrumentationTestCase testCase,
final AwContents awContents,
final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper,
final String linkId) throws Exception {
-
CriteriaHelper.pollInstrumentationThread(new Criteria() {
@Override
public boolean isSatisfied() {
@@ -56,8 +56,7 @@ public class JSUtils {
});
}
- public static String executeJavaScriptAndWaitForResult(
- InstrumentationTestCase testCase,
+ public static String executeJavaScriptAndWaitForResult(InstrumentationTestCase testCase,
final AwContents awContents,
final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper,
final String code) throws Exception {
@@ -73,4 +72,62 @@ public class JSUtils {
onEvaluateJavaScriptResultHelper.hasValue());
return onEvaluateJavaScriptResultHelper.getJsonResultAndClear();
}
+
+ /**
+ * This method executes javascript by posting to the UI thread and then having the current
+ * thread wait for a result.
+ * To avoid deadlocks, this method should not be run from the UI thread!
+ */
+ public static void clickOnLinkUsingJsFromNonUiThread(final AwContents awContents,
boliu 2016/08/02 20:16:00 seems complicated, can you just navigate from js d
gsennton 2016/08/02 20:33:57 You mean rather than waiting for the link to show
boliu 2016/08/02 20:39:17 Yeah, then you don't even need to load a real page
gsennton 2016/09/06 15:26:08 Huh, I just assumed I needed to click a link to tr
+ final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper,
+ final String linkId) throws Exception {
+ // pollInstrumentationThread uses the current thread to poll for the given criteria to be
+ // fulfilled.
+ CriteriaHelper.pollInstrumentationThread(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ try {
+ String linkIsNotNull = executeJavaScriptOnUiThreadAndWaitForResult(awContents,
+ onEvaluateJavaScriptResultHelper,
+ "document.getElementById('" + linkId + "') != null");
+ return linkIsNotNull.equals("true");
+ } catch (Throwable t) {
+ t.printStackTrace();
+ Assert.fail("Failed to check if DOM is loaded: " + t.toString());
+ return false;
+ }
+ }
+ }, WAIT_TIMEOUT_MS, CHECK_INTERVAL);
+
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() {
+ @Override
+ public void run() {
+ awContents.getWebContents().evaluateJavaScriptForTests(
+ "var evObj = new MouseEvent('click', {bubbles: true});"
+ + "document.getElementById('" + linkId + "').dispatchEvent(evObj);"
+ + "console.log('element with id [" + linkId + "] clicked');",
+ null);
+ }
+ });
+ }
+
+ /**
+ * Execute some javascript on the Ui-thread, this method should not be called from the UI thread
+ * as it will block the current thread until the javascript code has been executed.
+ */
+ public static String executeJavaScriptOnUiThreadAndWaitForResult(final AwContents awContents,
boliu 2016/08/02 20:16:00 this seems copied? reuse JavaScriptUtils. executeJ
gsennton 2016/08/02 20:33:57 Yeah, both of these methods are just copied from t
+ final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper,
+ final String code) throws Exception {
+ ThreadUtils.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ onEvaluateJavaScriptResultHelper.evaluateJavaScriptForTests(
+ awContents.getWebContents(), code);
+ }
+ });
+ onEvaluateJavaScriptResultHelper.waitUntilHasValue();
+ Assert.assertTrue("Failed to retrieve JavaScript evaluation results.",
+ onEvaluateJavaScriptResultHelper.hasValue());
+ return onEvaluateJavaScriptResultHelper.getJsonResultAndClear();
+ }
}

Powered by Google App Engine
This is Rietveld 408576698