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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
8 | 8 |
9 import org.chromium.base.test.util.Feature; | 9 import org.chromium.base.test.util.Feature; |
10 import org.chromium.content_public.browser.JavaScriptCallback; | 10 import org.chromium.content_public.browser.JavaScriptCallback; |
| 11 import org.chromium.content_public.browser.WebContents; |
11 | 12 |
12 /** | 13 /** |
13 * Part of the test suite for the WebView's Java Bridge. | 14 * Part of the test suite for the WebView's Java Bridge. |
14 * | 15 * |
15 * Ensures that injected objects are exposed to child frames as well as the | 16 * Ensures that injected objects are exposed to child frames as well as the |
16 * main frame. | 17 * main frame. |
17 */ | 18 */ |
18 public class JavaBridgeChildFrameTest extends JavaBridgeTestBase { | 19 public class JavaBridgeChildFrameTest extends JavaBridgeTestBase { |
19 private class TestController extends Controller { | 20 private class TestController extends Controller { |
20 private String mStringValue; | 21 private String mStringValue; |
(...skipping 14 matching lines...) Expand all Loading... |
35 @Override | 36 @Override |
36 protected void setUp() throws Exception { | 37 protected void setUp() throws Exception { |
37 super.setUp(); | 38 super.setUp(); |
38 mTestController = new TestController(); | 39 mTestController = new TestController(); |
39 setUpContentView(mTestController, "testController"); | 40 setUpContentView(mTestController, "testController"); |
40 } | 41 } |
41 | 42 |
42 @SmallTest | 43 @SmallTest |
43 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 44 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
44 public void testInjectedObjectPresentInChildFrame() throws Throwable { | 45 public void testInjectedObjectPresentInChildFrame() throws Throwable { |
45 loadDataSync(getContentViewCore(), | 46 loadDataSync(getWebContents().getNavigationController(), |
46 "<html><body><iframe></iframe></body></html>", "text/html", fals
e); | 47 "<html><body><iframe></iframe></body></html>", "text/html", fals
e); |
47 // We are not executing this code as a part of page loading routine to a
void races | 48 // We are not executing this code as a part of page loading routine to a
void races |
48 // with internal Blink events that notify Java Bridge about window objec
t updates. | 49 // with internal Blink events that notify Java Bridge about window objec
t updates. |
49 assertEquals("\"object\"", executeJavaScriptAndGetResult( | 50 assertEquals("\"object\"", executeJavaScriptAndGetResult( |
50 getContentViewCore(), "typeof window.frames[0].testContr
oller")); | 51 getWebContents(), "typeof window.frames[0].testControlle
r")); |
51 executeJavaScriptAndGetResult( | 52 executeJavaScriptAndGetResult( |
52 getContentViewCore(), "window.frames[0].testController.setString
Value('PASS')"); | 53 getWebContents(), "window.frames[0].testController.setStringValu
e('PASS')"); |
53 assertEquals("PASS", mTestController.waitForStringValue()); | 54 assertEquals("PASS", mTestController.waitForStringValue()); |
54 } | 55 } |
55 | 56 |
56 // Verify that loading an iframe doesn't ruin JS wrapper of the main page. | 57 // Verify that loading an iframe doesn't ruin JS wrapper of the main page. |
57 // This is a regression test for the problem described in b/15572824. | 58 // This is a regression test for the problem described in b/15572824. |
58 @SmallTest | 59 @SmallTest |
59 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 60 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
60 public void testMainPageWrapperIsNotBrokenByChildFrame() throws Throwable { | 61 public void testMainPageWrapperIsNotBrokenByChildFrame() throws Throwable { |
61 loadDataSync(getContentViewCore(), | 62 loadDataSync(getWebContents().getNavigationController(), |
62 "<html><body><iframe></iframe></body></html>", "text/html", fals
e); | 63 "<html><body><iframe></iframe></body></html>", "text/html", fals
e); |
63 // In case there is anything wrong with the JS wrapper, an attempt | 64 // In case there is anything wrong with the JS wrapper, an attempt |
64 // to look up its properties will result in an exception being thrown. | 65 // to look up its properties will result in an exception being thrown. |
65 String script = | 66 String script = |
66 "(function(){ try {" + | 67 "(function(){ try {" + |
67 " return typeof testController.setStringValue;" + | 68 " return typeof testController.setStringValue;" + |
68 "} catch (e) {" + | 69 "} catch (e) {" + |
69 " return e.toString();" + | 70 " return e.toString();" + |
70 "} })()"; | 71 "} })()"; |
71 assertEquals("\"function\"", | 72 assertEquals("\"function\"", |
72 executeJavaScriptAndGetResult(getContentViewCore(), script)); | 73 executeJavaScriptAndGetResult(getWebContents(), script)); |
73 // Make sure calling a method also works. | 74 // Make sure calling a method also works. |
74 executeJavaScriptAndGetResult(getContentViewCore(), | 75 executeJavaScriptAndGetResult(getWebContents(), |
75 "testController.setStringValue('PASS');"); | 76 "testController.setStringValue('PASS');"); |
76 assertEquals("PASS", mTestController.waitForStringValue()); | 77 assertEquals("PASS", mTestController.waitForStringValue()); |
77 } | 78 } |
78 | 79 |
79 // Verify that parent page and child frame each has own JS wrapper object. | 80 // Verify that parent page and child frame each has own JS wrapper object. |
80 // Failing to do so exposes parent's context to the child. | 81 // Failing to do so exposes parent's context to the child. |
81 @SmallTest | 82 @SmallTest |
82 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 83 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
83 public void testWrapperIsNotSharedWithChildFrame() throws Throwable { | 84 public void testWrapperIsNotSharedWithChildFrame() throws Throwable { |
84 // Test by setting a custom property on the parent page's injected | 85 // Test by setting a custom property on the parent page's injected |
85 // object and then checking that child frame doesn't see the property. | 86 // object and then checking that child frame doesn't see the property. |
86 loadDataSync(getContentViewCore(), | 87 loadDataSync(getWebContents().getNavigationController(), |
87 "<html><head>" + | 88 "<html><head>" + |
88 "<script>" + | 89 "<script>" + |
89 " window.wProperty = 42;" + | 90 " window.wProperty = 42;" + |
90 " testController.tcProperty = 42;" + | 91 " testController.tcProperty = 42;" + |
91 " function queryProperties(w) {" + | 92 " function queryProperties(w) {" + |
92 " return w.wProperty + ' / ' + w.testController.tcProperty;"
+ | 93 " return w.wProperty + ' / ' + w.testController.tcProperty;"
+ |
93 " }" + | 94 " }" + |
94 "</script>" + | 95 "</script>" + |
95 "</head><body><iframe></iframe></body></html>", "text/html", fal
se); | 96 "</head><body><iframe></iframe></body></html>", "text/html", fal
se); |
96 assertEquals("\"42 / 42\"", | 97 assertEquals("\"42 / 42\"", |
97 executeJavaScriptAndGetResult(getContentViewCore(), "queryProper
ties(window)")); | 98 executeJavaScriptAndGetResult(getWebContents(), "queryProperties
(window)")); |
98 assertEquals("\"undefined / undefined\"", | 99 assertEquals("\"undefined / undefined\"", |
99 executeJavaScriptAndGetResult(getContentViewCore(), | 100 executeJavaScriptAndGetResult(getWebContents(), |
100 "queryProperties(window.frames[0])")); | 101 "queryProperties(window.frames[0])")); |
101 } | 102 } |
102 | 103 |
103 private String executeJavaScriptAndGetResult(final ContentViewCore contentVi
ewCore, | 104 private String executeJavaScriptAndGetResult(final WebContents webContents, |
104 final String script) throws Throwable { | 105 final String script) throws Throwable { |
105 final String[] result = new String[1]; | 106 final String[] result = new String[1]; |
106 class ResultCallback extends JavaBridgeTestBase.Controller | 107 class ResultCallback extends JavaBridgeTestBase.Controller |
107 implements JavaScriptCallback { | 108 implements JavaScriptCallback { |
108 @Override | 109 @Override |
109 public void handleJavaScriptResult(String jsonResult) { | 110 public void handleJavaScriptResult(String jsonResult) { |
110 result[0] = jsonResult; | 111 result[0] = jsonResult; |
111 notifyResultIsReady(); | 112 notifyResultIsReady(); |
112 } | 113 } |
113 } | 114 } |
114 final ResultCallback resultCallback = new ResultCallback(); | 115 final ResultCallback resultCallback = new ResultCallback(); |
115 runTestOnUiThread(new Runnable() { | 116 runTestOnUiThread(new Runnable() { |
116 @Override | 117 @Override |
117 public void run() { | 118 public void run() { |
118 contentViewCore.evaluateJavaScript(script, resultCallback); | 119 webContents.evaluateJavaScript(script, resultCallback); |
119 } | 120 } |
120 }); | 121 }); |
121 resultCallback.waitForResult(); | 122 resultCallback.waitForResult(); |
122 return result[0]; | 123 return result[0]; |
123 } | 124 } |
124 } | 125 } |
OLD | NEW |