|
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/. */ |
|
5 |
|
6 function sendMessageToJava(message) { |
|
7 SpecialPowers.Services.androidBridge.handleGeckoMessage(message); |
|
8 } |
|
9 |
|
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(); |
|
15 |
|
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); |
|
20 |
|
21 req.open('GET', theURI.spec, false); |
|
22 req.send(); |
|
23 |
|
24 return SpecialPowers.Cu.evalInSandbox(req.responseText, sandbox, "1.8", uri, 1); |
|
25 } |
|
26 |
|
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"; |
|
39 |
|
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); |
|
48 |
|
49 let testScope = SpecialPowers.Cu.Sandbox(principal); |
|
50 |
|
51 // Populate test environment with test harness prerequisites. |
|
52 testScope.Components = SpecialPowers.Components; |
|
53 testScope._TEST_FILE = uri; |
|
54 |
|
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 }; |
|
64 |
|
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); |
|
68 |
|
69 return _evalURI(uri, testScope); |
|
70 } |