mobile/android/base/tests/robocop_testharness.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 function sendMessageToJava(message) {
     7   SpecialPowers.Services.androidBridge.handleGeckoMessage(message);
     8 }
    10 function _evalURI(uri, sandbox) {
    11   // We explicitly allow Cross-Origin requests, since it is useful for
    12   // testing, but we allow relative URLs by maintaining our baseURI.
    13   let req = SpecialPowers.Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
    14                          .createInstance();
    16   let baseURI = SpecialPowers.Services.io
    17                              .newURI(window.document.baseURI, window.document.characterSet, null);
    18   let theURI = SpecialPowers.Services.io
    19                             .newURI(uri, window.document.characterSet, baseURI);
    21   req.open('GET', theURI.spec, false);
    22   req.send();
    24   return SpecialPowers.Cu.evalInSandbox(req.responseText, sandbox, "1.8", uri, 1);
    25 }
    27 /**
    28  * Execute the Javascript file at `uri` in a testing sandbox populated
    29  * with the Javascript test harness.
    30  *
    31  * `uri` should be a String, relative (to window.document.baseURI) or
    32  * absolute.
    33  *
    34  * The Javascript test harness sends all output to Java via
    35  * Robocop:JS messages.
    36  */
    37 function testOneFile(uri) {
    38   let HEAD_JS = "robocop_head.js";
    40   // System principal.  This is dangerous, but this is test code that
    41   // should only run on developer and build farm machines, and the
    42   // test harness needs access to a lot of the Components API,
    43   // including Components.stack.  Wrapping Components.stack in
    44   // SpecialPowers magic obfuscates stack traces wonderfully,
    45   // defeating much of the point of the test harness.
    46   let principal = SpecialPowers.Cc["@mozilla.org/systemprincipal;1"]
    47                                .createInstance(SpecialPowers.Ci.nsIPrincipal);
    49   let testScope = SpecialPowers.Cu.Sandbox(principal);
    51   // Populate test environment with test harness prerequisites.
    52   testScope.Components = SpecialPowers.Components;
    53   testScope._TEST_FILE = uri;
    55   // Output from head.js is fed, line by line, to this function.  We
    56   // send any such output back to the Java Robocop harness.
    57   testScope.dump = function (str) {
    58     let message = { type: "Robocop:JS",
    59                     innerType: "progress",
    60                     message: str,
    61                   };
    62     sendMessageToJava(message);
    63   };
    65   // Populate test environment with test harness.  The symbols defined
    66   // above must be present before executing the test harness.
    67   _evalURI(HEAD_JS, testScope);
    69   return _evalURI(uri, testScope);
    70 }

mercurial