michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function sendMessageToJava(message) { michael@0: SpecialPowers.Services.androidBridge.handleGeckoMessage(message); michael@0: } michael@0: michael@0: function _evalURI(uri, sandbox) { michael@0: // We explicitly allow Cross-Origin requests, since it is useful for michael@0: // testing, but we allow relative URLs by maintaining our baseURI. michael@0: let req = SpecialPowers.Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] michael@0: .createInstance(); michael@0: michael@0: let baseURI = SpecialPowers.Services.io michael@0: .newURI(window.document.baseURI, window.document.characterSet, null); michael@0: let theURI = SpecialPowers.Services.io michael@0: .newURI(uri, window.document.characterSet, baseURI); michael@0: michael@0: req.open('GET', theURI.spec, false); michael@0: req.send(); michael@0: michael@0: return SpecialPowers.Cu.evalInSandbox(req.responseText, sandbox, "1.8", uri, 1); michael@0: } michael@0: michael@0: /** michael@0: * Execute the Javascript file at `uri` in a testing sandbox populated michael@0: * with the Javascript test harness. michael@0: * michael@0: * `uri` should be a String, relative (to window.document.baseURI) or michael@0: * absolute. michael@0: * michael@0: * The Javascript test harness sends all output to Java via michael@0: * Robocop:JS messages. michael@0: */ michael@0: function testOneFile(uri) { michael@0: let HEAD_JS = "robocop_head.js"; michael@0: michael@0: // System principal. This is dangerous, but this is test code that michael@0: // should only run on developer and build farm machines, and the michael@0: // test harness needs access to a lot of the Components API, michael@0: // including Components.stack. Wrapping Components.stack in michael@0: // SpecialPowers magic obfuscates stack traces wonderfully, michael@0: // defeating much of the point of the test harness. michael@0: let principal = SpecialPowers.Cc["@mozilla.org/systemprincipal;1"] michael@0: .createInstance(SpecialPowers.Ci.nsIPrincipal); michael@0: michael@0: let testScope = SpecialPowers.Cu.Sandbox(principal); michael@0: michael@0: // Populate test environment with test harness prerequisites. michael@0: testScope.Components = SpecialPowers.Components; michael@0: testScope._TEST_FILE = uri; michael@0: michael@0: // Output from head.js is fed, line by line, to this function. We michael@0: // send any such output back to the Java Robocop harness. michael@0: testScope.dump = function (str) { michael@0: let message = { type: "Robocop:JS", michael@0: innerType: "progress", michael@0: message: str, michael@0: }; michael@0: sendMessageToJava(message); michael@0: }; michael@0: michael@0: // Populate test environment with test harness. The symbols defined michael@0: // above must be present before executing the test harness. michael@0: _evalURI(HEAD_JS, testScope); michael@0: michael@0: return _evalURI(uri, testScope); michael@0: }