mobile/android/base/tests/JavascriptTest.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/JavascriptTest.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +package org.mozilla.gecko.tests;
     1.5 +
     1.6 +import org.mozilla.gecko.tests.helpers.JavascriptBridge;
     1.7 +import org.mozilla.gecko.tests.helpers.JavascriptMessageParser;
     1.8 +
     1.9 +import android.util.Log;
    1.10 +import java.util.regex.Matcher;
    1.11 +import java.util.regex.Pattern;
    1.12 +
    1.13 +import org.json.JSONObject;
    1.14 +import org.mozilla.gecko.Actions;
    1.15 +import org.mozilla.gecko.Assert;
    1.16 +
    1.17 +public class JavascriptTest extends BaseTest {
    1.18 +    private static final String LOGTAG = "JavascriptTest";
    1.19 +    private static final String EVENT_TYPE = JavascriptBridge.EVENT_TYPE;
    1.20 +
    1.21 +    private final String javascriptUrl;
    1.22 +
    1.23 +    public JavascriptTest(String javascriptUrl) {
    1.24 +        super();
    1.25 +        this.javascriptUrl = javascriptUrl;
    1.26 +    }
    1.27 +
    1.28 +    public void testJavascript() throws Exception {
    1.29 +        blockForGeckoReady();
    1.30 +
    1.31 +        // We want to be waiting for Robocop messages before the page is loaded
    1.32 +        // because the test harness runs each test in the suite (and possibly
    1.33 +        // completes testing) before the page load event is fired.
    1.34 +        final Actions.EventExpecter expecter =
    1.35 +            mActions.expectGeckoEvent(EVENT_TYPE);
    1.36 +        mAsserter.dumpLog("Registered listener for " + EVENT_TYPE);
    1.37 +
    1.38 +        final String url = getAbsoluteUrl(StringHelper.ROBOCOP_JS_HARNESS_URL +
    1.39 +                                          "?path=" + javascriptUrl);
    1.40 +        mAsserter.dumpLog("Loading JavaScript test from " + url);
    1.41 +        loadUrl(url);
    1.42 +
    1.43 +        final JavascriptMessageParser testMessageParser =
    1.44 +                new JavascriptMessageParser(mAsserter, false);
    1.45 +        try {
    1.46 +            while (!testMessageParser.isTestFinished()) {
    1.47 +                if (Log.isLoggable(LOGTAG, Log.VERBOSE)) {
    1.48 +                    Log.v(LOGTAG, "Waiting for " + EVENT_TYPE);
    1.49 +                }
    1.50 +                String data = expecter.blockForEventData();
    1.51 +                if (Log.isLoggable(LOGTAG, Log.VERBOSE)) {
    1.52 +                    Log.v(LOGTAG, "Got event with data '" + data + "'");
    1.53 +                }
    1.54 +
    1.55 +                JSONObject o = new JSONObject(data);
    1.56 +                String innerType = o.getString("innerType");
    1.57 +                if (!"progress".equals(innerType)) {
    1.58 +                    throw new Exception("Unexpected event innerType " + innerType);
    1.59 +                }
    1.60 +
    1.61 +                String message = o.getString("message");
    1.62 +                if (message == null) {
    1.63 +                    throw new Exception("Progress message must not be null");
    1.64 +                }
    1.65 +                testMessageParser.logMessage(message);
    1.66 +            }
    1.67 +
    1.68 +            if (Log.isLoggable(LOGTAG, Log.DEBUG)) {
    1.69 +                Log.d(LOGTAG, "Got test finished message");
    1.70 +            }
    1.71 +        } finally {
    1.72 +            expecter.unregisterListener();
    1.73 +            mAsserter.dumpLog("Unregistered listener for " + EVENT_TYPE);
    1.74 +        }
    1.75 +    }
    1.76 +}

mercurial