Skip to content

Commit e9e260f

Browse files
author
Steve Block
committed
Add tests for uncaught exceptions from methods called through the Java Bridge
This is a cherry-pick from master. See https://android-git.corp.google.com/g/184260 If a method called on a Java object through the Java Bridge throws an uncaught exception, we throw a JavaScript exception. See WebKit change https://android-git.corp.google.com/g/184252 Bug: 6386557 Change-Id: Ie2a97a26372fb11782b35db09bc2046fb7eb1f86
1 parent 8a1da40 commit e9e260f

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

tests/WebViewTests/src/com/android/webviewtests/JavaBridgeBasicsTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ public void testCallingInvalidMethodRaisesException() throws Throwable {
186186
assertRaisesException("testController.foo()");
187187
}
188188

189+
public void testUncaughtJavaExceptionRaisesJavaException() throws Throwable {
190+
injectObjectAndReload(new Object() {
191+
public void method() { throw new RuntimeException("foo"); }
192+
}, "testObject");
193+
assertRaisesException("testObject.method()");
194+
}
195+
189196
// Note that this requires that we can pass a JavaScript string to Java.
190197
public void testTypeOfStaticMethod() throws Throwable {
191198
injectObjectAndReload(new ObjectWithStaticMethod(), "testObject");
@@ -394,7 +401,6 @@ private void privateMethod() {}
394401
assertEquals("", mTestController.waitForStringValue());
395402
}
396403

397-
// java.lang.reflect only allows access to public methods and fields. See b/6386557.
398404
public void testReflectPublicMethod() throws Throwable {
399405
injectObjectAndReload(new Object() {
400406
public String method() { return "foo"; }
@@ -404,12 +410,33 @@ public void testReflectPublicMethod() throws Throwable {
404410
".toString()"));
405411
}
406412

407-
// java.lang.reflect only allows access to public methods and fields. See b/6386557.
408413
public void testReflectPublicField() throws Throwable {
409414
injectObjectAndReload(new Object() {
410415
public String field = "foo";
411416
}, "testObject");
412417
assertEquals("foo", executeJavaScriptAndGetStringResult(
413418
"testObject.getClass().getField('field').get(testObject).toString()"));
414419
}
420+
421+
public void testReflectPrivateMethodRaisesException() throws Throwable {
422+
injectObjectAndReload(new Object() {
423+
private void method() {};
424+
}, "testObject");
425+
assertRaisesException("testObject.getClass().getMethod('method', null)");
426+
// getDeclaredMethod() is able to access a private method, but invoke()
427+
// throws a Java exception.
428+
assertRaisesException(
429+
"testObject.getClass().getDeclaredMethod('method', null).invoke(testObject, null)");
430+
}
431+
432+
public void testReflectPrivateFieldRaisesException() throws Throwable {
433+
injectObjectAndReload(new Object() {
434+
private int field;
435+
}, "testObject");
436+
assertRaisesException("testObject.getClass().getField('field')");
437+
// getDeclaredField() is able to access a private field, but getInt()
438+
// throws a Java exception.
439+
assertRaisesException(
440+
"testObject.getClass().getDeclaredField('field').getInt(testObject)");
441+
}
415442
}

0 commit comments

Comments
 (0)