michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.mozilla.gecko.tests.helpers.JavascriptBridge; michael@0: import org.mozilla.gecko.tests.helpers.JavascriptMessageParser; michael@0: michael@0: import android.util.Log; michael@0: import java.util.regex.Matcher; michael@0: import java.util.regex.Pattern; michael@0: michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.Actions; michael@0: import org.mozilla.gecko.Assert; michael@0: michael@0: public class JavascriptTest extends BaseTest { michael@0: private static final String LOGTAG = "JavascriptTest"; michael@0: private static final String EVENT_TYPE = JavascriptBridge.EVENT_TYPE; michael@0: michael@0: private final String javascriptUrl; michael@0: michael@0: public JavascriptTest(String javascriptUrl) { michael@0: super(); michael@0: this.javascriptUrl = javascriptUrl; michael@0: } michael@0: michael@0: public void testJavascript() throws Exception { michael@0: blockForGeckoReady(); michael@0: michael@0: // We want to be waiting for Robocop messages before the page is loaded michael@0: // because the test harness runs each test in the suite (and possibly michael@0: // completes testing) before the page load event is fired. michael@0: final Actions.EventExpecter expecter = michael@0: mActions.expectGeckoEvent(EVENT_TYPE); michael@0: mAsserter.dumpLog("Registered listener for " + EVENT_TYPE); michael@0: michael@0: final String url = getAbsoluteUrl(StringHelper.ROBOCOP_JS_HARNESS_URL + michael@0: "?path=" + javascriptUrl); michael@0: mAsserter.dumpLog("Loading JavaScript test from " + url); michael@0: loadUrl(url); michael@0: michael@0: final JavascriptMessageParser testMessageParser = michael@0: new JavascriptMessageParser(mAsserter, false); michael@0: try { michael@0: while (!testMessageParser.isTestFinished()) { michael@0: if (Log.isLoggable(LOGTAG, Log.VERBOSE)) { michael@0: Log.v(LOGTAG, "Waiting for " + EVENT_TYPE); michael@0: } michael@0: String data = expecter.blockForEventData(); michael@0: if (Log.isLoggable(LOGTAG, Log.VERBOSE)) { michael@0: Log.v(LOGTAG, "Got event with data '" + data + "'"); michael@0: } michael@0: michael@0: JSONObject o = new JSONObject(data); michael@0: String innerType = o.getString("innerType"); michael@0: if (!"progress".equals(innerType)) { michael@0: throw new Exception("Unexpected event innerType " + innerType); michael@0: } michael@0: michael@0: String message = o.getString("message"); michael@0: if (message == null) { michael@0: throw new Exception("Progress message must not be null"); michael@0: } michael@0: testMessageParser.logMessage(message); michael@0: } michael@0: michael@0: if (Log.isLoggable(LOGTAG, Log.DEBUG)) { michael@0: Log.d(LOGTAG, "Got test finished message"); michael@0: } michael@0: } finally { michael@0: expecter.unregisterListener(); michael@0: mAsserter.dumpLog("Unregistered listener for " + EVENT_TYPE); michael@0: } michael@0: } michael@0: }