OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview.test.util; | 5 package org.chromium.android_webview.test.util; |
6 | 6 |
7 import android.test.InstrumentationTestCase; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
8 | 8 |
9 import junit.framework.Assert; | 9 import android.app.Instrumentation; |
10 | 10 |
11 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 11 import org.junit.Assert; |
12 | 12 |
13 import org.chromium.android_webview.AwContents; | 13 import org.chromium.android_webview.AwContents; |
14 import org.chromium.content.browser.test.util.Criteria; | 14 import org.chromium.content.browser.test.util.Criteria; |
15 import org.chromium.content.browser.test.util.CriteriaHelper; | 15 import org.chromium.content.browser.test.util.CriteriaHelper; |
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; | 16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; |
17 | 17 |
18 /** | 18 /** |
19 * Collection of functions for JavaScript-based interactions with a page. | 19 * Collection of functions for JavaScript-based interactions with a page. |
20 */ | 20 */ |
21 public class JSUtils { | 21 public class JSUtils { |
22 private static final long WAIT_TIMEOUT_MS = scaleTimeout(2000); | 22 private static final long WAIT_TIMEOUT_MS = scaleTimeout(2000); |
23 private static final int CHECK_INTERVAL = 100; | 23 private static final int CHECK_INTERVAL = 100; |
24 | 24 |
25 public static void clickOnLinkUsingJs( | 25 public static void clickOnLinkUsingJs(final Instrumentation instrumentation, |
26 final InstrumentationTestCase testCase, | |
27 final AwContents awContents, | 26 final AwContents awContents, |
28 final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHel
per, | 27 final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHel
per, |
29 final String linkId) throws Exception { | 28 final String linkId) throws Exception { |
30 | |
31 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 29 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
32 @Override | 30 @Override |
33 public boolean isSatisfied() { | 31 public boolean isSatisfied() { |
34 try { | 32 try { |
35 String linkIsNotNull = executeJavaScriptAndWaitForResult(tes
tCase, awContents, | 33 String linkIsNotNull = executeJavaScriptAndWaitForResult(ins
trumentation, |
36 onEvaluateJavaScriptResultHelper, | 34 awContents, onEvaluateJavaScriptResultHelper, |
37 "document.getElementById('" + linkId + "') != null")
; | 35 "document.getElementById('" + linkId + "') != null")
; |
38 return linkIsNotNull.equals("true"); | 36 return linkIsNotNull.equals("true"); |
39 } catch (Throwable t) { | 37 } catch (Throwable t) { |
40 t.printStackTrace(); | 38 t.printStackTrace(); |
41 Assert.fail("Failed to check if DOM is loaded: " + t.toStrin
g()); | 39 Assert.fail("Failed to check if DOM is loaded: " + t.toStrin
g()); |
42 return false; | 40 return false; |
43 } | 41 } |
44 } | 42 } |
45 }, WAIT_TIMEOUT_MS, CHECK_INTERVAL); | 43 }, WAIT_TIMEOUT_MS, CHECK_INTERVAL); |
46 | 44 |
47 testCase.getInstrumentation().runOnMainSync(new Runnable() { | 45 instrumentation.runOnMainSync(new Runnable() { |
48 @Override | 46 @Override |
49 public void run() { | 47 public void run() { |
50 awContents.getWebContents().evaluateJavaScriptForTests( | 48 awContents.getWebContents().evaluateJavaScriptForTests( |
51 "var evObj = new MouseEvent('click', {bubbles: true});" | 49 "var evObj = new MouseEvent('click', {bubbles: true});" |
52 + "document.getElementById('" + linkId + "').dis
patchEvent(evObj);" | 50 + "document.getElementById('" + linkId + "').dis
patchEvent(evObj);" |
53 + "console.log('element with id [" + linkId + "]
clicked');", | 51 + "console.log('element with id [" + linkId + "]
clicked');", |
54 null); | 52 null); |
55 } | 53 } |
56 }); | 54 }); |
57 } | 55 } |
58 | 56 |
59 public static String executeJavaScriptAndWaitForResult( | 57 public static String executeJavaScriptAndWaitForResult(Instrumentation instr
umentation, |
60 InstrumentationTestCase testCase, | |
61 final AwContents awContents, | 58 final AwContents awContents, |
62 final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHel
per, | 59 final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHel
per, |
63 final String code) throws Exception { | 60 final String code) throws Exception { |
64 testCase.getInstrumentation().runOnMainSync(new Runnable() { | 61 instrumentation.runOnMainSync(new Runnable() { |
65 @Override | 62 @Override |
66 public void run() { | 63 public void run() { |
67 onEvaluateJavaScriptResultHelper.evaluateJavaScriptForTests( | 64 onEvaluateJavaScriptResultHelper.evaluateJavaScriptForTests( |
68 awContents.getWebContents(), code); | 65 awContents.getWebContents(), code); |
69 } | 66 } |
70 }); | 67 }); |
71 onEvaluateJavaScriptResultHelper.waitUntilHasValue(); | 68 onEvaluateJavaScriptResultHelper.waitUntilHasValue(); |
72 Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", | 69 Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", |
73 onEvaluateJavaScriptResultHelper.hasValue()); | 70 onEvaluateJavaScriptResultHelper.hasValue()); |
74 return onEvaluateJavaScriptResultHelper.getJsonResultAndClear(); | 71 return onEvaluateJavaScriptResultHelper.getJsonResultAndClear(); |
75 } | 72 } |
76 } | 73 } |
OLD | NEW |