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