1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/robocop_testharness.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +function sendMessageToJava(message) { 1.10 + SpecialPowers.Services.androidBridge.handleGeckoMessage(message); 1.11 +} 1.12 + 1.13 +function _evalURI(uri, sandbox) { 1.14 + // We explicitly allow Cross-Origin requests, since it is useful for 1.15 + // testing, but we allow relative URLs by maintaining our baseURI. 1.16 + let req = SpecialPowers.Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] 1.17 + .createInstance(); 1.18 + 1.19 + let baseURI = SpecialPowers.Services.io 1.20 + .newURI(window.document.baseURI, window.document.characterSet, null); 1.21 + let theURI = SpecialPowers.Services.io 1.22 + .newURI(uri, window.document.characterSet, baseURI); 1.23 + 1.24 + req.open('GET', theURI.spec, false); 1.25 + req.send(); 1.26 + 1.27 + return SpecialPowers.Cu.evalInSandbox(req.responseText, sandbox, "1.8", uri, 1); 1.28 +} 1.29 + 1.30 +/** 1.31 + * Execute the Javascript file at `uri` in a testing sandbox populated 1.32 + * with the Javascript test harness. 1.33 + * 1.34 + * `uri` should be a String, relative (to window.document.baseURI) or 1.35 + * absolute. 1.36 + * 1.37 + * The Javascript test harness sends all output to Java via 1.38 + * Robocop:JS messages. 1.39 + */ 1.40 +function testOneFile(uri) { 1.41 + let HEAD_JS = "robocop_head.js"; 1.42 + 1.43 + // System principal. This is dangerous, but this is test code that 1.44 + // should only run on developer and build farm machines, and the 1.45 + // test harness needs access to a lot of the Components API, 1.46 + // including Components.stack. Wrapping Components.stack in 1.47 + // SpecialPowers magic obfuscates stack traces wonderfully, 1.48 + // defeating much of the point of the test harness. 1.49 + let principal = SpecialPowers.Cc["@mozilla.org/systemprincipal;1"] 1.50 + .createInstance(SpecialPowers.Ci.nsIPrincipal); 1.51 + 1.52 + let testScope = SpecialPowers.Cu.Sandbox(principal); 1.53 + 1.54 + // Populate test environment with test harness prerequisites. 1.55 + testScope.Components = SpecialPowers.Components; 1.56 + testScope._TEST_FILE = uri; 1.57 + 1.58 + // Output from head.js is fed, line by line, to this function. We 1.59 + // send any such output back to the Java Robocop harness. 1.60 + testScope.dump = function (str) { 1.61 + let message = { type: "Robocop:JS", 1.62 + innerType: "progress", 1.63 + message: str, 1.64 + }; 1.65 + sendMessageToJava(message); 1.66 + }; 1.67 + 1.68 + // Populate test environment with test harness. The symbols defined 1.69 + // above must be present before executing the test harness. 1.70 + _evalURI(HEAD_JS, testScope); 1.71 + 1.72 + return _evalURI(uri, testScope); 1.73 +}