OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
8 | 8 |
| 9 import org.junit.Assert; |
| 10 import org.junit.Before; |
| 11 import org.junit.Rule; |
| 12 import org.junit.Test; |
| 13 import org.junit.runner.RunWith; |
| 14 |
| 15 import org.chromium.base.test.BaseJUnit4ClassRunner; |
9 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
10 import org.chromium.base.test.util.UrlUtils; | 17 import org.chromium.base.test.util.UrlUtils; |
11 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 18 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; | 19 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; |
13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; | 20 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; |
14 import org.chromium.content_shell_apk.ContentShellTestBase; | 21 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
15 | 22 |
16 /** | 23 /** |
17 * Common functionality for testing the Java Bridge. | 24 * Common functionality for testing the Java Bridge. |
18 */ | 25 */ |
19 public class JavaBridgeBareboneTest extends ContentShellTestBase { | 26 @RunWith(BaseJUnit4ClassRunner.class) |
| 27 public class JavaBridgeBareboneTest { |
| 28 @Rule |
| 29 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 30 |
20 private TestCallbackHelperContainer mTestCallbackHelperContainer; | 31 private TestCallbackHelperContainer mTestCallbackHelperContainer; |
21 | 32 |
22 @Override | 33 @Before |
23 protected void setUp() throws Exception { | 34 public void setUp() throws Exception { |
24 super.setUp(); | 35 mActivityTestRule.launchContentShellWithUrl( |
25 launchContentShellWithUrl( | |
26 UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body>
</html>")); | 36 UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body>
</html>")); |
27 waitForActiveShellToBeDoneLoading(); | 37 mActivityTestRule.waitForActiveShellToBeDoneLoading(); |
28 mTestCallbackHelperContainer = new TestCallbackHelperContainer(getConten
tViewCore()); | 38 mTestCallbackHelperContainer = |
| 39 new TestCallbackHelperContainer(mActivityTestRule.getContentView
Core()); |
29 } | 40 } |
30 | 41 |
31 private void injectDummyObject(final String name) throws Throwable { | 42 private void injectDummyObject(final String name) throws Throwable { |
32 runTestOnUiThread(new Runnable() { | 43 mActivityTestRule.runOnUiThread(new Runnable() { |
33 @Override | 44 @Override |
34 public void run() { | 45 public void run() { |
35 getContentViewCore().addPossiblyUnsafeJavascriptInterface( | 46 mActivityTestRule.getContentViewCore().addPossiblyUnsafeJavascri
ptInterface( |
36 new Object(), name, null); | 47 new Object(), name, null); |
37 } | 48 } |
38 }); | 49 }); |
39 } | 50 } |
40 | 51 |
41 private String evaluateJsSync(String jsCode) throws Exception { | 52 private String evaluateJsSync(String jsCode) throws Exception { |
42 OnEvaluateJavaScriptResultHelper javascriptHelper = new OnEvaluateJavaSc
riptResultHelper(); | 53 OnEvaluateJavaScriptResultHelper javascriptHelper = new OnEvaluateJavaSc
riptResultHelper(); |
43 javascriptHelper.evaluateJavaScriptForTests(getWebContents(), jsCode); | 54 javascriptHelper.evaluateJavaScriptForTests(mActivityTestRule.getWebCont
ents(), jsCode); |
44 javascriptHelper.waitUntilHasValue(); | 55 javascriptHelper.waitUntilHasValue(); |
45 return javascriptHelper.getJsonResultAndClear(); | 56 return javascriptHelper.getJsonResultAndClear(); |
46 } | 57 } |
47 | 58 |
48 private void reloadSync() throws Throwable { | 59 private void reloadSync() throws Throwable { |
49 OnPageFinishedHelper pageFinishedHelper = | 60 OnPageFinishedHelper pageFinishedHelper = |
50 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 61 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
51 int currentCallCount = pageFinishedHelper.getCallCount(); | 62 int currentCallCount = pageFinishedHelper.getCallCount(); |
52 runTestOnUiThread(new Runnable() { | 63 mActivityTestRule.runOnUiThread(new Runnable() { |
53 @Override | 64 @Override |
54 public void run() { | 65 public void run() { |
55 getWebContents().getNavigationController().reload(true); | 66 mActivityTestRule.getWebContents().getNavigationController().rel
oad(true); |
56 } | 67 } |
57 }); | 68 }); |
58 pageFinishedHelper.waitForCallback(currentCallCount); | 69 pageFinishedHelper.waitForCallback(currentCallCount); |
59 } | 70 } |
60 | 71 |
61 // If inection happens before evaluating any JS code, then the first evaluat
ion | 72 // If inection happens before evaluating any JS code, then the first evaluat
ion |
62 // triggers the same condition as page reload, which causes Java Bridge to a
dd | 73 // triggers the same condition as page reload, which causes Java Bridge to a
dd |
63 // a JS wrapper. | 74 // a JS wrapper. |
64 // This contradicts the statement of our documentation that injected objects
are | 75 // This contradicts the statement of our documentation that injected objects
are |
65 // only available after the next page (re)load, but it is too late to fix th
at, | 76 // only available after the next page (re)load, but it is too late to fix th
at, |
66 // as existing apps may implicitly rely on this behaviour, something like: | 77 // as existing apps may implicitly rely on this behaviour, something like: |
67 // | 78 // |
68 // webView.loadUrl(...); | 79 // webView.loadUrl(...); |
69 // webView.addJavascriptInterface(new Foo(), "foo"); | 80 // webView.addJavascriptInterface(new Foo(), "foo"); |
70 // webView.loadUrl("javascript:foo.bar();void(0);"); | 81 // webView.loadUrl("javascript:foo.bar();void(0);"); |
71 // | 82 // |
| 83 @Test |
72 @SmallTest | 84 @SmallTest |
73 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 85 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
74 public void testImmediateAddition() throws Throwable { | 86 public void testImmediateAddition() throws Throwable { |
75 injectDummyObject("testObject"); | 87 injectDummyObject("testObject"); |
76 assertEquals("\"object\"", evaluateJsSync("typeof testObject")); | 88 Assert.assertEquals("\"object\"", evaluateJsSync("typeof testObject")); |
77 } | 89 } |
78 | 90 |
79 // Now here, we evaluate some JS before injecting the object, and this behav
es as | 91 // Now here, we evaluate some JS before injecting the object, and this behav
es as |
80 // expected. | 92 // expected. |
| 93 @Test |
81 @SmallTest | 94 @SmallTest |
82 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 95 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
83 public void testNoImmediateAdditionAfterJSEvaluation() throws Throwable { | 96 public void testNoImmediateAdditionAfterJSEvaluation() throws Throwable { |
84 evaluateJsSync("true"); | 97 evaluateJsSync("true"); |
85 injectDummyObject("testObject"); | 98 injectDummyObject("testObject"); |
86 assertEquals("\"undefined\"", evaluateJsSync("typeof testObject")); | 99 Assert.assertEquals("\"undefined\"", evaluateJsSync("typeof testObject")
); |
87 } | 100 } |
88 | 101 |
| 102 @Test |
89 @SmallTest | 103 @SmallTest |
90 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 104 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
91 public void testImmediateAdditionAfterReload() throws Throwable { | 105 public void testImmediateAdditionAfterReload() throws Throwable { |
92 reloadSync(); | 106 reloadSync(); |
93 injectDummyObject("testObject"); | 107 injectDummyObject("testObject"); |
94 assertEquals("\"object\"", evaluateJsSync("typeof testObject")); | 108 Assert.assertEquals("\"object\"", evaluateJsSync("typeof testObject")); |
95 } | 109 } |
96 | 110 |
| 111 @Test |
97 @SmallTest | 112 @SmallTest |
98 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 113 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
99 public void testReloadAfterAddition() throws Throwable { | 114 public void testReloadAfterAddition() throws Throwable { |
100 injectDummyObject("testObject"); | 115 injectDummyObject("testObject"); |
101 reloadSync(); | 116 reloadSync(); |
102 assertEquals("\"object\"", evaluateJsSync("typeof testObject")); | 117 Assert.assertEquals("\"object\"", evaluateJsSync("typeof testObject")); |
103 } | 118 } |
104 } | 119 } |
OLD | NEW |