|
1 package org.mozilla.gecko.tests; |
|
2 |
|
3 import org.mozilla.gecko.tests.helpers.JavascriptBridge; |
|
4 import org.mozilla.gecko.tests.helpers.JavascriptMessageParser; |
|
5 |
|
6 import android.util.Log; |
|
7 import java.util.regex.Matcher; |
|
8 import java.util.regex.Pattern; |
|
9 |
|
10 import org.json.JSONObject; |
|
11 import org.mozilla.gecko.Actions; |
|
12 import org.mozilla.gecko.Assert; |
|
13 |
|
14 public class JavascriptTest extends BaseTest { |
|
15 private static final String LOGTAG = "JavascriptTest"; |
|
16 private static final String EVENT_TYPE = JavascriptBridge.EVENT_TYPE; |
|
17 |
|
18 private final String javascriptUrl; |
|
19 |
|
20 public JavascriptTest(String javascriptUrl) { |
|
21 super(); |
|
22 this.javascriptUrl = javascriptUrl; |
|
23 } |
|
24 |
|
25 public void testJavascript() throws Exception { |
|
26 blockForGeckoReady(); |
|
27 |
|
28 // We want to be waiting for Robocop messages before the page is loaded |
|
29 // because the test harness runs each test in the suite (and possibly |
|
30 // completes testing) before the page load event is fired. |
|
31 final Actions.EventExpecter expecter = |
|
32 mActions.expectGeckoEvent(EVENT_TYPE); |
|
33 mAsserter.dumpLog("Registered listener for " + EVENT_TYPE); |
|
34 |
|
35 final String url = getAbsoluteUrl(StringHelper.ROBOCOP_JS_HARNESS_URL + |
|
36 "?path=" + javascriptUrl); |
|
37 mAsserter.dumpLog("Loading JavaScript test from " + url); |
|
38 loadUrl(url); |
|
39 |
|
40 final JavascriptMessageParser testMessageParser = |
|
41 new JavascriptMessageParser(mAsserter, false); |
|
42 try { |
|
43 while (!testMessageParser.isTestFinished()) { |
|
44 if (Log.isLoggable(LOGTAG, Log.VERBOSE)) { |
|
45 Log.v(LOGTAG, "Waiting for " + EVENT_TYPE); |
|
46 } |
|
47 String data = expecter.blockForEventData(); |
|
48 if (Log.isLoggable(LOGTAG, Log.VERBOSE)) { |
|
49 Log.v(LOGTAG, "Got event with data '" + data + "'"); |
|
50 } |
|
51 |
|
52 JSONObject o = new JSONObject(data); |
|
53 String innerType = o.getString("innerType"); |
|
54 if (!"progress".equals(innerType)) { |
|
55 throw new Exception("Unexpected event innerType " + innerType); |
|
56 } |
|
57 |
|
58 String message = o.getString("message"); |
|
59 if (message == null) { |
|
60 throw new Exception("Progress message must not be null"); |
|
61 } |
|
62 testMessageParser.logMessage(message); |
|
63 } |
|
64 |
|
65 if (Log.isLoggable(LOGTAG, Log.DEBUG)) { |
|
66 Log.d(LOGTAG, "Got test finished message"); |
|
67 } |
|
68 } finally { |
|
69 expecter.unregisterListener(); |
|
70 mAsserter.dumpLog("Unregistered listener for " + EVENT_TYPE); |
|
71 } |
|
72 } |
|
73 } |