|
1 package org.mozilla.gecko.tests; |
|
2 |
|
3 import static org.mozilla.gecko.tests.helpers.AssertionHelper.*; |
|
4 |
|
5 import org.mozilla.gecko.tests.helpers.*; |
|
6 |
|
7 import org.json.JSONException; |
|
8 import org.json.JSONObject; |
|
9 |
|
10 /** |
|
11 * Tests the proper operation of JavascriptBridge and JavaBridge, |
|
12 * which are used by tests for communication between Java and JS. |
|
13 */ |
|
14 public class testJavascriptBridge extends UITest { |
|
15 |
|
16 private static final String TEST_JS = "testJavascriptBridge.js"; |
|
17 |
|
18 private JavascriptBridge js; |
|
19 private boolean syncCallReceived; |
|
20 |
|
21 @Override |
|
22 public void setUp() throws Exception { |
|
23 super.setUp(); |
|
24 js = new JavascriptBridge(this); |
|
25 } |
|
26 |
|
27 @Override |
|
28 public void tearDown() throws Exception { |
|
29 js.disconnect(); |
|
30 super.tearDown(); |
|
31 } |
|
32 |
|
33 public void testJavascriptBridge() { |
|
34 GeckoHelper.blockForReady(); |
|
35 NavigationHelper.enterAndLoadUrl(StringHelper.ROBOCOP_JS_HARNESS_URL + |
|
36 "?path=" + TEST_JS); |
|
37 js.syncCall("check_js_int_arg", (int) 1); |
|
38 } |
|
39 |
|
40 public void checkJavaIntArg(final int int2) { |
|
41 // Async call from JS |
|
42 fAssertEquals("Integer argument matches", 2, int2); |
|
43 js.syncCall("check_js_double_arg", (double) 3.0); |
|
44 } |
|
45 |
|
46 public void checkJavaDoubleArg(final double double4) { |
|
47 // Async call from JS |
|
48 fAssertEquals("Double argument matches", 4.0, double4); |
|
49 js.syncCall("check_js_boolean_arg", (boolean) false); |
|
50 } |
|
51 |
|
52 public void checkJavaBooleanArg(final boolean booltrue) { |
|
53 // Async call from JS |
|
54 fAssertEquals("Boolean argument matches", true, booltrue); |
|
55 js.syncCall("check_js_string_arg", (String) "foo"); |
|
56 } |
|
57 |
|
58 public void checkJavaStringArg(final String stringbar) throws JSONException { |
|
59 // Async call from JS |
|
60 fAssertEquals("String argument matches", "bar", stringbar); |
|
61 final JSONObject obj = new JSONObject(); |
|
62 obj.put("caller", "java"); |
|
63 js.syncCall("check_js_object_arg", (JSONObject) obj); |
|
64 } |
|
65 |
|
66 public void checkJavaObjectArg(final JSONObject obj) throws JSONException { |
|
67 // Async call from JS |
|
68 fAssertEquals("Object argument matches", "js", obj.getString("caller")); |
|
69 js.syncCall("check_js_sync_call"); |
|
70 } |
|
71 |
|
72 public void doJSSyncCall() { |
|
73 // Sync call from JS |
|
74 syncCallReceived = true; |
|
75 js.asyncCall("respond_to_js_sync_call"); |
|
76 } |
|
77 |
|
78 public void checkJSSyncCallReceived() { |
|
79 fAssertTrue("Received sync call before end of test", syncCallReceived); |
|
80 // End of test |
|
81 } |
|
82 } |