OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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.browser.JavascriptInterface; | |
11 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 10 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
12 | 11 |
13 import java.lang.annotation.Annotation; | 12 import java.lang.annotation.Annotation; |
14 import java.lang.annotation.ElementType; | 13 import java.lang.annotation.ElementType; |
15 import java.lang.annotation.Retention; | 14 import java.lang.annotation.Retention; |
16 import java.lang.annotation.RetentionPolicy; | 15 import java.lang.annotation.RetentionPolicy; |
17 import java.lang.annotation.Target; | 16 import java.lang.annotation.Target; |
18 | 17 |
19 /** | 18 /** |
20 * Part of the test suite for the Java Bridge. Tests a number of features includ
ing ... | 19 * Part of the test suite for the Java Bridge. Tests a number of features includ
ing ... |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 protected void injectObjectAndReload(final Object object, final String name, | 97 protected void injectObjectAndReload(final Object object, final String name, |
99 final Class<? extends Annotation> requiredAnnotation) throws Throwab
le { | 98 final Class<? extends Annotation> requiredAnnotation) throws Throwab
le { |
100 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 99 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
101 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 100 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
102 int currentCallCount = onPageFinishedHelper.getCallCount(); | 101 int currentCallCount = onPageFinishedHelper.getCallCount(); |
103 runTestOnUiThread(new Runnable() { | 102 runTestOnUiThread(new Runnable() { |
104 @Override | 103 @Override |
105 public void run() { | 104 public void run() { |
106 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface(object, | 105 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface(object, |
107 name, requiredAnnotation); | 106 name, requiredAnnotation); |
108 getContentView().reload(); | 107 getContentView().getContentViewCore().reload(true); |
109 } | 108 } |
110 }); | 109 }); |
111 onPageFinishedHelper.waitForCallback(currentCallCount); | 110 onPageFinishedHelper.waitForCallback(currentCallCount); |
112 } | 111 } |
113 | 112 |
114 // Note that this requires that we can pass a JavaScript boolean to Java. | 113 // Note that this requires that we can pass a JavaScript boolean to Java. |
115 private void assertRaisesException(String script) throws Throwable { | 114 private void assertRaisesException(String script) throws Throwable { |
116 executeJavaScript("try {" + | 115 executeJavaScript("try {" + |
117 script + ";" + | 116 script + ";" + |
118 " testController.setBooleanValue(false);" + | 117 " testController.setBooleanValue(false);" + |
(...skipping 20 matching lines...) Expand all Loading... |
139 new Object(), "testObject", null); | 138 new Object(), "testObject", null); |
140 } | 139 } |
141 }); | 140 }); |
142 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof te
stObject")); | 141 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof te
stObject")); |
143 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 142 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
144 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 143 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
145 int currentCallCount = onPageFinishedHelper.getCallCount(); | 144 int currentCallCount = onPageFinishedHelper.getCallCount(); |
146 runTestOnUiThread(new Runnable() { | 145 runTestOnUiThread(new Runnable() { |
147 @Override | 146 @Override |
148 public void run() { | 147 public void run() { |
149 getContentView().reload(); | 148 getContentView().getContentViewCore().reload(true); |
150 } | 149 } |
151 }); | 150 }); |
152 onPageFinishedHelper.waitForCallback(currentCallCount); | 151 onPageFinishedHelper.waitForCallback(currentCallCount); |
153 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testO
bject")); | 152 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testO
bject")); |
154 } | 153 } |
155 | 154 |
156 @SmallTest | 155 @SmallTest |
157 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 156 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
158 public void testRemovalNotReflectedUntilReload() throws Throwable { | 157 public void testRemovalNotReflectedUntilReload() throws Throwable { |
159 injectObjectAndReload(new Object(), "testObject"); | 158 injectObjectAndReload(new Object(), "testObject"); |
160 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testO
bject")); | 159 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testO
bject")); |
161 runTestOnUiThread(new Runnable() { | 160 runTestOnUiThread(new Runnable() { |
162 @Override | 161 @Override |
163 public void run() { | 162 public void run() { |
164 getContentView().getContentViewCore().removeJavascriptInterface(
"testObject"); | 163 getContentView().getContentViewCore().removeJavascriptInterface(
"testObject"); |
165 } | 164 } |
166 }); | 165 }); |
167 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testO
bject")); | 166 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testO
bject")); |
168 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 167 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
169 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 168 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
170 int currentCallCount = onPageFinishedHelper.getCallCount(); | 169 int currentCallCount = onPageFinishedHelper.getCallCount(); |
171 runTestOnUiThread(new Runnable() { | 170 runTestOnUiThread(new Runnable() { |
172 @Override | 171 @Override |
173 public void run() { | 172 public void run() { |
174 getContentView().reload(); | 173 getContentView().getContentViewCore().reload(true); |
175 } | 174 } |
176 }); | 175 }); |
177 onPageFinishedHelper.waitForCallback(currentCallCount); | 176 onPageFinishedHelper.waitForCallback(currentCallCount); |
178 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof te
stObject")); | 177 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof te
stObject")); |
179 } | 178 } |
180 | 179 |
181 @SmallTest | 180 @SmallTest |
182 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 181 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
183 public void testRemoveObjectNotAdded() throws Throwable { | 182 public void testRemoveObjectNotAdded() throws Throwable { |
184 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 183 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
185 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 184 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
186 int currentCallCount = onPageFinishedHelper.getCallCount(); | 185 int currentCallCount = onPageFinishedHelper.getCallCount(); |
187 runTestOnUiThread(new Runnable() { | 186 runTestOnUiThread(new Runnable() { |
188 @Override | 187 @Override |
189 public void run() { | 188 public void run() { |
190 getContentView().getContentViewCore().removeJavascriptInterface(
"foo"); | 189 getContentView().getContentViewCore().removeJavascriptInterface(
"foo"); |
191 getContentView().reload(); | 190 getContentView().getContentViewCore().reload(true); |
192 } | 191 } |
193 }); | 192 }); |
194 onPageFinishedHelper.waitForCallback(currentCallCount); | 193 onPageFinishedHelper.waitForCallback(currentCallCount); |
195 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof fo
o")); | 194 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof fo
o")); |
196 } | 195 } |
197 | 196 |
198 @SmallTest | 197 @SmallTest |
199 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 198 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
200 public void testTypeOfMethod() throws Throwable { | 199 public void testTypeOfMethod() throws Throwable { |
201 assertEquals("function", | 200 assertEquals("function", |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 @SmallTest | 315 @SmallTest |
317 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 316 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
318 public void testObjectPersistsAcrossPageLoads() throws Throwable { | 317 public void testObjectPersistsAcrossPageLoads() throws Throwable { |
319 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testC
ontroller")); | 318 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testC
ontroller")); |
320 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 319 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
321 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 320 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
322 int currentCallCount = onPageFinishedHelper.getCallCount(); | 321 int currentCallCount = onPageFinishedHelper.getCallCount(); |
323 runTestOnUiThread(new Runnable() { | 322 runTestOnUiThread(new Runnable() { |
324 @Override | 323 @Override |
325 public void run() { | 324 public void run() { |
326 getContentView().reload(); | 325 getContentView().getContentViewCore().reload(true); |
327 } | 326 } |
328 }); | 327 }); |
329 onPageFinishedHelper.waitForCallback(currentCallCount); | 328 onPageFinishedHelper.waitForCallback(currentCallCount); |
330 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testC
ontroller")); | 329 assertEquals("object", executeJavaScriptAndGetStringResult("typeof testC
ontroller")); |
331 } | 330 } |
332 | 331 |
333 @SmallTest | 332 @SmallTest |
334 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 333 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
335 public void testSameObjectInjectedMultipleTimes() throws Throwable { | 334 public void testSameObjectInjectedMultipleTimes() throws Throwable { |
336 class TestObject { | 335 class TestObject { |
337 private int mNumMethodInvocations; | 336 private int mNumMethodInvocations; |
338 public void method() { mTestController.setIntValue(++mNumMethodInvoc
ations); } | 337 public void method() { mTestController.setIntValue(++mNumMethodInvoc
ations); } |
339 } | 338 } |
340 final TestObject testObject = new TestObject(); | 339 final TestObject testObject = new TestObject(); |
341 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 340 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
342 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 341 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
343 int currentCallCount = onPageFinishedHelper.getCallCount(); | 342 int currentCallCount = onPageFinishedHelper.getCallCount(); |
344 runTestOnUiThread(new Runnable() { | 343 runTestOnUiThread(new Runnable() { |
345 @Override | 344 @Override |
346 public void run() { | 345 public void run() { |
347 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( | 346 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( |
348 testObject, "testObject1", null); | 347 testObject, "testObject1", null); |
349 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( | 348 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( |
350 testObject, "testObject2", null); | 349 testObject, "testObject2", null); |
351 getContentView().reload(); | 350 getContentView().getContentViewCore().reload(true); |
352 } | 351 } |
353 }); | 352 }); |
354 onPageFinishedHelper.waitForCallback(currentCallCount); | 353 onPageFinishedHelper.waitForCallback(currentCallCount); |
355 executeJavaScript("testObject1.method()"); | 354 executeJavaScript("testObject1.method()"); |
356 assertEquals(1, mTestController.waitForIntValue()); | 355 assertEquals(1, mTestController.waitForIntValue()); |
357 executeJavaScript("testObject2.method()"); | 356 executeJavaScript("testObject2.method()"); |
358 assertEquals(2, mTestController.waitForIntValue()); | 357 assertEquals(2, mTestController.waitForIntValue()); |
359 } | 358 } |
360 | 359 |
361 @SmallTest | 360 @SmallTest |
(...skipping 26 matching lines...) Expand all Loading... |
388 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 387 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
389 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 388 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
390 int currentCallCount = onPageFinishedHelper.getCallCount(); | 389 int currentCallCount = onPageFinishedHelper.getCallCount(); |
391 runTestOnUiThread(new Runnable() { | 390 runTestOnUiThread(new Runnable() { |
392 @Override | 391 @Override |
393 public void run() { | 392 public void run() { |
394 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( | 393 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( |
395 object, "testObject", null); | 394 object, "testObject", null); |
396 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( | 395 getContentView().getContentViewCore().addPossiblyUnsafeJavascrip
tInterface( |
397 innerObject, "innerObject", null); | 396 innerObject, "innerObject", null); |
398 getContentView().reload(); | 397 getContentView().getContentViewCore().reload(true); |
399 } | 398 } |
400 }); | 399 }); |
401 onPageFinishedHelper.waitForCallback(currentCallCount); | 400 onPageFinishedHelper.waitForCallback(currentCallCount); |
402 executeJavaScript("testObject.getInnerObject().method()"); | 401 executeJavaScript("testObject.getInnerObject().method()"); |
403 assertEquals(1, mTestController.waitForIntValue()); | 402 assertEquals(1, mTestController.waitForIntValue()); |
404 executeJavaScript("innerObject.method()"); | 403 executeJavaScript("innerObject.method()"); |
405 assertEquals(2, mTestController.waitForIntValue()); | 404 assertEquals(2, mTestController.waitForIntValue()); |
406 } | 405 } |
407 | 406 |
408 @SmallTest | 407 @SmallTest |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof te
stObject.method")); | 449 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof te
stObject.method")); |
451 } | 450 } |
452 | 451 |
453 @SmallTest | 452 @SmallTest |
454 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 453 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
455 public void testOverriddenMethod() throws Throwable { | 454 public void testOverriddenMethod() throws Throwable { |
456 class Base { | 455 class Base { |
457 public void method() { mTestController.setStringValue("base"); } | 456 public void method() { mTestController.setStringValue("base"); } |
458 } | 457 } |
459 class Derived extends Base { | 458 class Derived extends Base { |
| 459 @Override |
460 public void method() { mTestController.setStringValue("derived"); } | 460 public void method() { mTestController.setStringValue("derived"); } |
461 } | 461 } |
462 injectObjectAndReload(new Derived(), "testObject"); | 462 injectObjectAndReload(new Derived(), "testObject"); |
463 executeJavaScript("testObject.method()"); | 463 executeJavaScript("testObject.method()"); |
464 assertEquals("derived", mTestController.waitForStringValue()); | 464 assertEquals("derived", mTestController.waitForStringValue()); |
465 } | 465 } |
466 | 466 |
467 @SmallTest | 467 @SmallTest |
468 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 468 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
469 public void testEnumerateMembers() throws Throwable { | 469 public void testEnumerateMembers() throws Throwable { |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 // Manually inject the Test object, making sure to use the | 688 // Manually inject the Test object, making sure to use the |
689 // ContentViewCore#addJavascriptInterface, not the possibly unsafe versi
on. | 689 // ContentViewCore#addJavascriptInterface, not the possibly unsafe versi
on. |
690 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 690 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
691 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | 691 mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
692 int currentCallCount = onPageFinishedHelper.getCallCount(); | 692 int currentCallCount = onPageFinishedHelper.getCallCount(); |
693 runTestOnUiThread(new Runnable() { | 693 runTestOnUiThread(new Runnable() { |
694 @Override | 694 @Override |
695 public void run() { | 695 public void run() { |
696 getContentView().getContentViewCore().addJavascriptInterface(new
Test(), | 696 getContentView().getContentViewCore().addJavascriptInterface(new
Test(), |
697 "testObject"); | 697 "testObject"); |
698 getContentView().reload(); | 698 getContentView().getContentViewCore().reload(true); |
699 } | 699 } |
700 }); | 700 }); |
701 onPageFinishedHelper.waitForCallback(currentCallCount); | 701 onPageFinishedHelper.waitForCallback(currentCallCount); |
702 | 702 |
703 // Test#allowed() should pass, as it is annotated with JavascriptInterfa
ce. | 703 // Test#allowed() should pass, as it is annotated with JavascriptInterfa
ce. |
704 assertEquals("bar", executeJavaScriptAndGetStringResult( | 704 assertEquals("bar", executeJavaScriptAndGetStringResult( |
705 "testObject.allowed()")); | 705 "testObject.allowed()")); |
706 | 706 |
707 // Test#blocked() should fail, as it isn't annotated with JavascriptInte
rface. | 707 // Test#blocked() should fail, as it isn't annotated with JavascriptInte
rface. |
708 assertRaisesException("testObject.blocked()"); | 708 assertRaisesException("testObject.blocked()"); |
709 assertEquals("undefined", executeJavaScriptAndGetStringResult( | 709 assertEquals("undefined", executeJavaScriptAndGetStringResult( |
710 "typeof testObject.blocked")); | 710 "typeof testObject.blocked")); |
711 } | 711 } |
712 } | 712 } |
OLD | NEW |