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

Commit 8e33fe94 authored by Steve Block's avatar Steve Block
Browse files

Add another test for WebView's Java Bridge

Tests that an exception is raised if the wrong number of arguments are passed
to a method of an injected object.

Bug: 5140673
Change-Id: Ic9f9d09969e0fccbe82584e1a9ca7580f6010c87
parent ba1f05d4
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
        private int mIntValue;
        private long mLongValue;
        private String mStringValue;
        private boolean mBooleanValue;

        public synchronized void setIntValue(int x) {
            mIntValue = x;
@@ -51,6 +52,10 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
            mStringValue = x;
            notifyResultIsReady();
        }
        public synchronized void setBooleanValue(boolean x) {
            mBooleanValue = x;
            notifyResultIsReady();
        }

        public synchronized int waitForIntValue() {
            waitForResult();
@@ -64,6 +69,10 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
            waitForResult();
            return mStringValue;
        }
        public synchronized boolean waitForBooleanValue() {
            waitForResult();
            return mBooleanValue;
        }
    }

    TestController mTestController;
@@ -204,6 +213,23 @@ public class JavaBridgeBasicsTest extends JavaBridgeTestBase {
        assertEquals("2 args", mTestController.waitForStringValue());
    }

    public void testCallMethodWithWrongNumberOfArgumentsRaisesException() throws Throwable {
        class Test {
            public void run(String script) throws Throwable {
                executeJavaScript("try {" +
                                  script + ";" +
                                  "  testController.setBooleanValue(false);" +
                                  "} catch (exception) {" +
                                  "  testController.setBooleanValue(true);" +
                                  "}");
                assertTrue(mTestController.waitForBooleanValue());
            }
        }
        Test test = new Test();
        test.run("testController.setIntValue()");
        test.run("testController.setIntValue(42, 42)");
    }

    public void testObjectPersistsAcrossPageLoads() throws Throwable {
        assertEquals("object", executeJavaScriptAndGetStringResult("typeof testController"));
        runTestOnUiThread(new Runnable() {