michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import static org.mozilla.gecko.tests.helpers.AssertionHelper.*; michael@0: michael@0: import org.mozilla.gecko.tests.helpers.*; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: /** michael@0: * Tests the proper operation of JavascriptBridge and JavaBridge, michael@0: * which are used by tests for communication between Java and JS. michael@0: */ michael@0: public class testJavascriptBridge extends UITest { michael@0: michael@0: private static final String TEST_JS = "testJavascriptBridge.js"; michael@0: michael@0: private JavascriptBridge js; michael@0: private boolean syncCallReceived; michael@0: michael@0: @Override michael@0: public void setUp() throws Exception { michael@0: super.setUp(); michael@0: js = new JavascriptBridge(this); michael@0: } michael@0: michael@0: @Override michael@0: public void tearDown() throws Exception { michael@0: js.disconnect(); michael@0: super.tearDown(); michael@0: } michael@0: michael@0: public void testJavascriptBridge() { michael@0: GeckoHelper.blockForReady(); michael@0: NavigationHelper.enterAndLoadUrl(StringHelper.ROBOCOP_JS_HARNESS_URL + michael@0: "?path=" + TEST_JS); michael@0: js.syncCall("check_js_int_arg", (int) 1); michael@0: } michael@0: michael@0: public void checkJavaIntArg(final int int2) { michael@0: // Async call from JS michael@0: fAssertEquals("Integer argument matches", 2, int2); michael@0: js.syncCall("check_js_double_arg", (double) 3.0); michael@0: } michael@0: michael@0: public void checkJavaDoubleArg(final double double4) { michael@0: // Async call from JS michael@0: fAssertEquals("Double argument matches", 4.0, double4); michael@0: js.syncCall("check_js_boolean_arg", (boolean) false); michael@0: } michael@0: michael@0: public void checkJavaBooleanArg(final boolean booltrue) { michael@0: // Async call from JS michael@0: fAssertEquals("Boolean argument matches", true, booltrue); michael@0: js.syncCall("check_js_string_arg", (String) "foo"); michael@0: } michael@0: michael@0: public void checkJavaStringArg(final String stringbar) throws JSONException { michael@0: // Async call from JS michael@0: fAssertEquals("String argument matches", "bar", stringbar); michael@0: final JSONObject obj = new JSONObject(); michael@0: obj.put("caller", "java"); michael@0: js.syncCall("check_js_object_arg", (JSONObject) obj); michael@0: } michael@0: michael@0: public void checkJavaObjectArg(final JSONObject obj) throws JSONException { michael@0: // Async call from JS michael@0: fAssertEquals("Object argument matches", "js", obj.getString("caller")); michael@0: js.syncCall("check_js_sync_call"); michael@0: } michael@0: michael@0: public void doJSSyncCall() { michael@0: // Sync call from JS michael@0: syncCallReceived = true; michael@0: js.asyncCall("respond_to_js_sync_call"); michael@0: } michael@0: michael@0: public void checkJSSyncCallReceived() { michael@0: fAssertTrue("Received sync call before end of test", syncCallReceived); michael@0: // End of test michael@0: } michael@0: }