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