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

Side by Side Diff: content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeBasicsTest.java

Issue 481803004: Removing ContentViewCore dependencies from few functions which acts as direct wrapper to WebContents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed nits. Created 6 years, 3 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 unified diff | Download patch
OLDNEW
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.browser.test.util.TestCallbackHelperContainer; 10 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 protected void injectObjectAndReload(final Object object, final String name, 110 protected void injectObjectAndReload(final Object object, final String name,
111 final Class<? extends Annotation> requiredAnnotation) throws Throwab le { 111 final Class<? extends Annotation> requiredAnnotation) throws Throwab le {
112 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 112 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
113 mTestCallbackHelperContainer.getOnPageFinishedHelper(); 113 mTestCallbackHelperContainer.getOnPageFinishedHelper();
114 int currentCallCount = onPageFinishedHelper.getCallCount(); 114 int currentCallCount = onPageFinishedHelper.getCallCount();
115 runTestOnUiThread(new Runnable() { 115 runTestOnUiThread(new Runnable() {
116 @Override 116 @Override
117 public void run() { 117 public void run() {
118 getContentViewCore().addPossiblyUnsafeJavascriptInterface(object , 118 getContentViewCore().addPossiblyUnsafeJavascriptInterface(object ,
119 name, requiredAnnotation); 119 name, requiredAnnotation);
120 getContentViewCore().reload(true); 120 getContentViewCore().getWebContents().getNavigationController(). reload(true);
121 } 121 }
122 }); 122 });
123 onPageFinishedHelper.waitForCallback(currentCallCount); 123 onPageFinishedHelper.waitForCallback(currentCallCount);
124 } 124 }
125 125
126 protected void synchronousPageReload() throws Throwable { 126 protected void synchronousPageReload() throws Throwable {
127 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 127 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
128 mTestCallbackHelperContainer.getOnPageFinishedHelper(); 128 mTestCallbackHelperContainer.getOnPageFinishedHelper();
129 int currentCallCount = onPageFinishedHelper.getCallCount(); 129 int currentCallCount = onPageFinishedHelper.getCallCount();
130 runTestOnUiThread(new Runnable() { 130 runTestOnUiThread(new Runnable() {
131 @Override 131 @Override
132 public void run() { 132 public void run() {
133 getContentViewCore().reload(true); 133 getContentViewCore().getWebContents().getNavigationController(). reload(true);
134 } 134 }
135 }); 135 });
136 onPageFinishedHelper.waitForCallback(currentCallCount); 136 onPageFinishedHelper.waitForCallback(currentCallCount);
137 } 137 }
138 138
139 // Note that this requires that we can pass a JavaScript boolean to Java. 139 // Note that this requires that we can pass a JavaScript boolean to Java.
140 private void assertRaisesException(String script) throws Throwable { 140 private void assertRaisesException(String script) throws Throwable {
141 executeJavaScript("try {" + 141 executeJavaScript("try {" +
142 script + ";" + 142 script + ";" +
143 " testController.setBooleanValue(false);" + 143 " testController.setBooleanValue(false);" +
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 @SmallTest 188 @SmallTest
189 @Feature({"AndroidWebView", "Android-JavaBridge"}) 189 @Feature({"AndroidWebView", "Android-JavaBridge"})
190 public void testRemoveObjectNotAdded() throws Throwable { 190 public void testRemoveObjectNotAdded() throws Throwable {
191 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 191 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
192 mTestCallbackHelperContainer.getOnPageFinishedHelper(); 192 mTestCallbackHelperContainer.getOnPageFinishedHelper();
193 int currentCallCount = onPageFinishedHelper.getCallCount(); 193 int currentCallCount = onPageFinishedHelper.getCallCount();
194 runTestOnUiThread(new Runnable() { 194 runTestOnUiThread(new Runnable() {
195 @Override 195 @Override
196 public void run() { 196 public void run() {
197 getContentViewCore().removeJavascriptInterface("foo"); 197 getContentViewCore().removeJavascriptInterface("foo");
198 getContentViewCore().reload(true); 198 getContentViewCore().getWebContents().getNavigationController(). reload(true);
199 } 199 }
200 }); 200 });
201 onPageFinishedHelper.waitForCallback(currentCallCount); 201 onPageFinishedHelper.waitForCallback(currentCallCount);
202 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof fo o")); 202 assertEquals("undefined", executeJavaScriptAndGetStringResult("typeof fo o"));
203 } 203 }
204 204
205 @SmallTest 205 @SmallTest
206 @Feature({"AndroidWebView", "Android-JavaBridge"}) 206 @Feature({"AndroidWebView", "Android-JavaBridge"})
207 public void testTypeOfMethod() throws Throwable { 207 public void testTypeOfMethod() throws Throwable {
208 assertEquals("function", 208 assertEquals("function",
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 350 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
351 mTestCallbackHelperContainer.getOnPageFinishedHelper(); 351 mTestCallbackHelperContainer.getOnPageFinishedHelper();
352 int currentCallCount = onPageFinishedHelper.getCallCount(); 352 int currentCallCount = onPageFinishedHelper.getCallCount();
353 runTestOnUiThread(new Runnable() { 353 runTestOnUiThread(new Runnable() {
354 @Override 354 @Override
355 public void run() { 355 public void run() {
356 getContentViewCore().addPossiblyUnsafeJavascriptInterface( 356 getContentViewCore().addPossiblyUnsafeJavascriptInterface(
357 testObject, "testObject1", null); 357 testObject, "testObject1", null);
358 getContentViewCore().addPossiblyUnsafeJavascriptInterface( 358 getContentViewCore().addPossiblyUnsafeJavascriptInterface(
359 testObject, "testObject2", null); 359 testObject, "testObject2", null);
360 getContentViewCore().reload(true); 360 getContentViewCore().getWebContents().getNavigationController(). reload(true);
361 } 361 }
362 }); 362 });
363 onPageFinishedHelper.waitForCallback(currentCallCount); 363 onPageFinishedHelper.waitForCallback(currentCallCount);
364 executeJavaScript("testObject1.method()"); 364 executeJavaScript("testObject1.method()");
365 assertEquals(1, mTestController.waitForIntValue()); 365 assertEquals(1, mTestController.waitForIntValue());
366 executeJavaScript("testObject2.method()"); 366 executeJavaScript("testObject2.method()");
367 assertEquals(2, mTestController.waitForIntValue()); 367 assertEquals(2, mTestController.waitForIntValue());
368 } 368 }
369 369
370 @SmallTest 370 @SmallTest
(...skipping 26 matching lines...) Expand all
397 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 397 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
398 mTestCallbackHelperContainer.getOnPageFinishedHelper(); 398 mTestCallbackHelperContainer.getOnPageFinishedHelper();
399 int currentCallCount = onPageFinishedHelper.getCallCount(); 399 int currentCallCount = onPageFinishedHelper.getCallCount();
400 runTestOnUiThread(new Runnable() { 400 runTestOnUiThread(new Runnable() {
401 @Override 401 @Override
402 public void run() { 402 public void run() {
403 getContentViewCore().addPossiblyUnsafeJavascriptInterface( 403 getContentViewCore().addPossiblyUnsafeJavascriptInterface(
404 object, "testObject", null); 404 object, "testObject", null);
405 getContentViewCore().addPossiblyUnsafeJavascriptInterface( 405 getContentViewCore().addPossiblyUnsafeJavascriptInterface(
406 innerObject, "innerObject", null); 406 innerObject, "innerObject", null);
407 getContentViewCore().reload(true); 407 getContentViewCore().getWebContents().getNavigationController(). reload(true);
408 } 408 }
409 }); 409 });
410 onPageFinishedHelper.waitForCallback(currentCallCount); 410 onPageFinishedHelper.waitForCallback(currentCallCount);
411 executeJavaScript("testObject.getInnerObject().method()"); 411 executeJavaScript("testObject.getInnerObject().method()");
412 assertEquals(1, mTestController.waitForIntValue()); 412 assertEquals(1, mTestController.waitForIntValue());
413 executeJavaScript("innerObject.method()"); 413 executeJavaScript("innerObject.method()");
414 assertEquals(2, mTestController.waitForIntValue()); 414 assertEquals(2, mTestController.waitForIntValue());
415 } 415 }
416 416
417 // Verify that Java objects returned from bridge object methods are derefere nced 417 // Verify that Java objects returned from bridge object methods are derefere nced
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 assertEquals("derived", mTestController.waitForStringValue()); 538 assertEquals("derived", mTestController.waitForStringValue());
539 } 539 }
540 540
541 @SmallTest 541 @SmallTest
542 @Feature({"AndroidWebView", "Android-JavaBridge"}) 542 @Feature({"AndroidWebView", "Android-JavaBridge"})
543 public void testEnumerateMembers() throws Throwable { 543 public void testEnumerateMembers() throws Throwable {
544 injectObjectAndReload(new Object() { 544 injectObjectAndReload(new Object() {
545 public void method() {} 545 public void method() {}
546 private void privateMethod() {} 546 private void privateMethod() {}
547 public int field; 547 public int field;
548 private int privateField; 548 private int mPrivateField;
549 }, "testObject"); 549 }, "testObject");
550 executeJavaScript( 550 executeJavaScript(
551 "var result = \"\"; " + 551 "var result = \"\"; " +
552 "for (x in testObject) { result += \" \" + x } " + 552 "for (x in testObject) { result += \" \" + x } " +
553 "testController.setStringValue(result);"); 553 "testController.setStringValue(result);");
554 assertEquals(" equals getClass hashCode method notify notifyAll toString wait", 554 assertEquals(" equals getClass hashCode method notify notifyAll toString wait",
555 mTestController.waitForStringValue()); 555 mTestController.waitForStringValue());
556 } 556 }
557 557
558 @SmallTest 558 @SmallTest
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 assertRaisesException( 591 assertRaisesException(
592 "testObject.myGetClass().getDeclaredMethod('method', null)." + 592 "testObject.myGetClass().getDeclaredMethod('method', null)." +
593 "invoke(testObject, null)"); 593 "invoke(testObject, null)");
594 } 594 }
595 595
596 @SmallTest 596 @SmallTest
597 @Feature({"AndroidWebView", "Android-JavaBridge"}) 597 @Feature({"AndroidWebView", "Android-JavaBridge"})
598 public void testReflectPrivateFieldRaisesException() throws Throwable { 598 public void testReflectPrivateFieldRaisesException() throws Throwable {
599 injectObjectAndReload(new Object() { 599 injectObjectAndReload(new Object() {
600 public Class<?> myGetClass() { return getClass(); } 600 public Class<?> myGetClass() { return getClass(); }
601 private int field; 601 private int mField;
602 }, "testObject"); 602 }, "testObject");
603 assertRaisesException("testObject.myGetClass().getField('field')"); 603 assertRaisesException("testObject.myGetClass().getField('field')");
604 // getDeclaredField() is able to access a private field, but getInt() 604 // getDeclaredField() is able to access a private field, but getInt()
605 // throws a Java exception. 605 // throws a Java exception.
606 assertRaisesException( 606 assertRaisesException(
607 "testObject.myGetClass().getDeclaredField('field').getInt(testOb ject)"); 607 "testObject.myGetClass().getDeclaredField('field').getInt(testOb ject)");
608 } 608 }
609 609
610 @SmallTest 610 @SmallTest
611 @Feature({"AndroidWebView", "Android-JavaBridge"}) 611 @Feature({"AndroidWebView", "Android-JavaBridge"})
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 // Manually inject the Test object, making sure to use the 767 // Manually inject the Test object, making sure to use the
768 // ContentViewCore#addJavascriptInterface, not the possibly unsafe versi on. 768 // ContentViewCore#addJavascriptInterface, not the possibly unsafe versi on.
769 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 769 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
770 mTestCallbackHelperContainer.getOnPageFinishedHelper(); 770 mTestCallbackHelperContainer.getOnPageFinishedHelper();
771 int currentCallCount = onPageFinishedHelper.getCallCount(); 771 int currentCallCount = onPageFinishedHelper.getCallCount();
772 runTestOnUiThread(new Runnable() { 772 runTestOnUiThread(new Runnable() {
773 @Override 773 @Override
774 public void run() { 774 public void run() {
775 getContentViewCore().addJavascriptInterface(new Test(), 775 getContentViewCore().addJavascriptInterface(new Test(),
776 "testObject"); 776 "testObject");
777 getContentViewCore().reload(true); 777 getContentViewCore().getWebContents().getNavigationController(). reload(true);
778 } 778 }
779 }); 779 });
780 onPageFinishedHelper.waitForCallback(currentCallCount); 780 onPageFinishedHelper.waitForCallback(currentCallCount);
781 781
782 // Test#allowed() should pass, as it is annotated with JavascriptInterfa ce. 782 // Test#allowed() should pass, as it is annotated with JavascriptInterfa ce.
783 assertEquals("bar", executeJavaScriptAndGetStringResult( 783 assertEquals("bar", executeJavaScriptAndGetStringResult(
784 "testObject.allowed()")); 784 "testObject.allowed()"));
785 785
786 // Test#blocked() should fail, as it isn't annotated with JavascriptInte rface. 786 // Test#blocked() should fail, as it isn't annotated with JavascriptInte rface.
787 assertRaisesException("testObject.blocked()"); 787 assertRaisesException("testObject.blocked()");
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 } 835 }
836 836
837 @SmallTest 837 @SmallTest
838 @Feature({"AndroidWebView", "Android-JavaBridge"}) 838 @Feature({"AndroidWebView", "Android-JavaBridge"})
839 public void testAccessToObjectGetClassIsBlocked() throws Throwable { 839 public void testAccessToObjectGetClassIsBlocked() throws Throwable {
840 injectObjectAndReload(new Object(), "testObject"); 840 injectObjectAndReload(new Object(), "testObject");
841 assertEquals("function", executeJavaScriptAndGetStringResult("typeof tes tObject.getClass")); 841 assertEquals("function", executeJavaScriptAndGetStringResult("typeof tes tObject.getClass"));
842 assertRaisesException("testObject.getClass()"); 842 assertRaisesException("testObject.getClass()");
843 } 843 }
844 } 844 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698