1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testJavascriptBridge.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +package org.mozilla.gecko.tests; 1.5 + 1.6 +import static org.mozilla.gecko.tests.helpers.AssertionHelper.*; 1.7 + 1.8 +import org.mozilla.gecko.tests.helpers.*; 1.9 + 1.10 +import org.json.JSONException; 1.11 +import org.json.JSONObject; 1.12 + 1.13 +/** 1.14 + * Tests the proper operation of JavascriptBridge and JavaBridge, 1.15 + * which are used by tests for communication between Java and JS. 1.16 + */ 1.17 +public class testJavascriptBridge extends UITest { 1.18 + 1.19 + private static final String TEST_JS = "testJavascriptBridge.js"; 1.20 + 1.21 + private JavascriptBridge js; 1.22 + private boolean syncCallReceived; 1.23 + 1.24 + @Override 1.25 + public void setUp() throws Exception { 1.26 + super.setUp(); 1.27 + js = new JavascriptBridge(this); 1.28 + } 1.29 + 1.30 + @Override 1.31 + public void tearDown() throws Exception { 1.32 + js.disconnect(); 1.33 + super.tearDown(); 1.34 + } 1.35 + 1.36 + public void testJavascriptBridge() { 1.37 + GeckoHelper.blockForReady(); 1.38 + NavigationHelper.enterAndLoadUrl(StringHelper.ROBOCOP_JS_HARNESS_URL + 1.39 + "?path=" + TEST_JS); 1.40 + js.syncCall("check_js_int_arg", (int) 1); 1.41 + } 1.42 + 1.43 + public void checkJavaIntArg(final int int2) { 1.44 + // Async call from JS 1.45 + fAssertEquals("Integer argument matches", 2, int2); 1.46 + js.syncCall("check_js_double_arg", (double) 3.0); 1.47 + } 1.48 + 1.49 + public void checkJavaDoubleArg(final double double4) { 1.50 + // Async call from JS 1.51 + fAssertEquals("Double argument matches", 4.0, double4); 1.52 + js.syncCall("check_js_boolean_arg", (boolean) false); 1.53 + } 1.54 + 1.55 + public void checkJavaBooleanArg(final boolean booltrue) { 1.56 + // Async call from JS 1.57 + fAssertEquals("Boolean argument matches", true, booltrue); 1.58 + js.syncCall("check_js_string_arg", (String) "foo"); 1.59 + } 1.60 + 1.61 + public void checkJavaStringArg(final String stringbar) throws JSONException { 1.62 + // Async call from JS 1.63 + fAssertEquals("String argument matches", "bar", stringbar); 1.64 + final JSONObject obj = new JSONObject(); 1.65 + obj.put("caller", "java"); 1.66 + js.syncCall("check_js_object_arg", (JSONObject) obj); 1.67 + } 1.68 + 1.69 + public void checkJavaObjectArg(final JSONObject obj) throws JSONException { 1.70 + // Async call from JS 1.71 + fAssertEquals("Object argument matches", "js", obj.getString("caller")); 1.72 + js.syncCall("check_js_sync_call"); 1.73 + } 1.74 + 1.75 + public void doJSSyncCall() { 1.76 + // Sync call from JS 1.77 + syncCallReceived = true; 1.78 + js.asyncCall("respond_to_js_sync_call"); 1.79 + } 1.80 + 1.81 + public void checkJSSyncCallReceived() { 1.82 + fAssertTrue("Received sync call before end of test", syncCallReceived); 1.83 + // End of test 1.84 + } 1.85 +}