Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03069c82 authored by Steve Block's avatar Steve Block Committed by Android Git Automerger
Browse files

am e9e260fb: Add tests for uncaught exceptions from methods called through the Java Bridge

* commit 'e9e260fb':
  Add tests for uncaught exceptions from methods called through the Java Bridge
parents b3d8ea35 e9e260fb
Loading
Loading
Loading
Loading
+29 −2
Original line number Diff line number Diff line
@@ -186,6 +186,13 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
        assertRaisesException("testController.foo()");
    }

    public void testUncaughtJavaExceptionRaisesJavaException() throws Throwable {
        injectObjectAndReload(new Object() {
            public void method() { throw new RuntimeException("foo"); }
        }, "testObject");
        assertRaisesException("testObject.method()");
    }

    // Note that this requires that we can pass a JavaScript string to Java.
    public void testTypeOfStaticMethod() throws Throwable {
        injectObjectAndReload(new ObjectWithStaticMethod(), "testObject");
@@ -394,7 +401,6 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
        assertEquals("", mTestController.waitForStringValue());
    }

    // java.lang.reflect only allows access to public methods and fields. See b/6386557.
    public void testReflectPublicMethod() throws Throwable {
        injectObjectAndReload(new Object() {
            public String method() { return "foo"; }
@@ -404,7 +410,6 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
                ".toString()"));
    }

    // java.lang.reflect only allows access to public methods and fields. See b/6386557.
    public void testReflectPublicField() throws Throwable {
        injectObjectAndReload(new Object() {
            public String field = "foo";
@@ -412,4 +417,26 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
        assertEquals("foo", executeJavaScriptAndGetStringResult(
                "testObject.getClass().getField('field').get(testObject).toString()"));
    }

    public void testReflectPrivateMethodRaisesException() throws Throwable {
        injectObjectAndReload(new Object() {
            private void method() {};
        }, "testObject");
        assertRaisesException("testObject.getClass().getMethod('method', null)");
        // getDeclaredMethod() is able to access a private method, but invoke()
        // throws a Java exception.
        assertRaisesException(
                "testObject.getClass().getDeclaredMethod('method', null).invoke(testObject, null)");
    }

    public void testReflectPrivateFieldRaisesException() throws Throwable {
        injectObjectAndReload(new Object() {
            private int field;
        }, "testObject");
        assertRaisesException("testObject.getClass().getField('field')");
        // getDeclaredField() is able to access a private field, but getInt()
        // throws a Java exception.
        assertRaisesException(
                "testObject.getClass().getDeclaredField('field').getInt(testObject)");
    }
}