browser/devtools/scratchpad/test/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
michael@0 8 const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {});
michael@0 9 const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
michael@0 10
michael@0 11 let gScratchpadWindow; // Reference to the Scratchpad chrome window object
michael@0 12
michael@0 13 gDevTools.testing = true;
michael@0 14 SimpleTest.registerCleanupFunction(() => {
michael@0 15 gDevTools.testing = false;
michael@0 16 });
michael@0 17
michael@0 18 /**
michael@0 19 * Open a Scratchpad window.
michael@0 20 *
michael@0 21 * @param function aReadyCallback
michael@0 22 * Optional. The function you want invoked when the Scratchpad instance
michael@0 23 * is ready.
michael@0 24 * @param object aOptions
michael@0 25 * Optional. Options for opening the scratchpad:
michael@0 26 * - window
michael@0 27 * Provide this if there's already a Scratchpad window you want to wait
michael@0 28 * loading for.
michael@0 29 * - state
michael@0 30 * Scratchpad state object. This is used when Scratchpad is open.
michael@0 31 * - noFocus
michael@0 32 * Boolean that tells you do not want the opened window to receive
michael@0 33 * focus.
michael@0 34 * @return nsIDOMWindow
michael@0 35 * The new window object that holds Scratchpad. Note that the
michael@0 36 * gScratchpadWindow global is also updated to reference the new window
michael@0 37 * object.
michael@0 38 */
michael@0 39 function openScratchpad(aReadyCallback, aOptions)
michael@0 40 {
michael@0 41 aOptions = aOptions || {};
michael@0 42
michael@0 43 let win = aOptions.window ||
michael@0 44 Scratchpad.ScratchpadManager.openScratchpad(aOptions.state);
michael@0 45 if (!win) {
michael@0 46 return;
michael@0 47 }
michael@0 48
michael@0 49 let onLoad = function() {
michael@0 50 win.removeEventListener("load", onLoad, false);
michael@0 51
michael@0 52 win.Scratchpad.addObserver({
michael@0 53 onReady: function(aScratchpad) {
michael@0 54 aScratchpad.removeObserver(this);
michael@0 55
michael@0 56 if (aOptions.noFocus) {
michael@0 57 aReadyCallback(win, aScratchpad);
michael@0 58 } else {
michael@0 59 waitForFocus(aReadyCallback.bind(null, win, aScratchpad), win);
michael@0 60 }
michael@0 61 }
michael@0 62 });
michael@0 63 };
michael@0 64
michael@0 65 if (aReadyCallback) {
michael@0 66 win.addEventListener("load", onLoad, false);
michael@0 67 }
michael@0 68
michael@0 69 gScratchpadWindow = win;
michael@0 70 return gScratchpadWindow;
michael@0 71 }
michael@0 72
michael@0 73 /**
michael@0 74 * Create a temporary file, write to it and call a callback
michael@0 75 * when done.
michael@0 76 *
michael@0 77 * @param string aName
michael@0 78 * Name of your temporary file.
michael@0 79 * @param string aContent
michael@0 80 * Temporary file's contents.
michael@0 81 * @param function aCallback
michael@0 82 * Optional callback to be called when we're done writing
michael@0 83 * to the file. It will receive two parameters: status code
michael@0 84 * and a file object.
michael@0 85 */
michael@0 86 function createTempFile(aName, aContent, aCallback=function(){})
michael@0 87 {
michael@0 88 // Create a temporary file.
michael@0 89 let file = FileUtils.getFile("TmpD", [aName]);
michael@0 90 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));
michael@0 91
michael@0 92 // Write the temporary file.
michael@0 93 let fout = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 94 createInstance(Ci.nsIFileOutputStream);
michael@0 95 fout.init(file.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
michael@0 96 parseInt("644", 8), fout.DEFER_OPEN);
michael@0 97
michael@0 98 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
michael@0 99 createInstance(Ci.nsIScriptableUnicodeConverter);
michael@0 100 converter.charset = "UTF-8";
michael@0 101 let fileContentStream = converter.convertToInputStream(aContent);
michael@0 102
michael@0 103 NetUtil.asyncCopy(fileContentStream, fout, function (aStatus) {
michael@0 104 aCallback(aStatus, file);
michael@0 105 });
michael@0 106 }
michael@0 107
michael@0 108 /**
michael@0 109 * Run a set of asychronous tests sequentially defined by input and output.
michael@0 110 *
michael@0 111 * @param Scratchpad aScratchpad
michael@0 112 * The scratchpad to use in running the tests.
michael@0 113 * @param array aTests
michael@0 114 * An array of test objects, each with the following properties:
michael@0 115 * - method
michael@0 116 * Scratchpad method to use, one of "run", "display", or "inspect".
michael@0 117 * - code
michael@0 118 * Code to run in the scratchpad.
michael@0 119 * - result
michael@0 120 * Expected code that will be in the scratchpad upon completion.
michael@0 121 * - label
michael@0 122 * The tests label which will be logged in the test runner output.
michael@0 123 * @return Promise
michael@0 124 * The promise that will be resolved when all tests are finished.
michael@0 125 */
michael@0 126 function runAsyncTests(aScratchpad, aTests)
michael@0 127 {
michael@0 128 let deferred = promise.defer();
michael@0 129
michael@0 130 (function runTest() {
michael@0 131 if (aTests.length) {
michael@0 132 let test = aTests.shift();
michael@0 133 aScratchpad.setText(test.code);
michael@0 134 aScratchpad[test.method]().then(function success() {
michael@0 135 is(aScratchpad.getText(), test.result, test.label);
michael@0 136 runTest();
michael@0 137 }, function failure(error) {
michael@0 138 ok(false, error.stack + " " + test.label);
michael@0 139 runTest();
michael@0 140 });
michael@0 141 } else {
michael@0 142 deferred.resolve();
michael@0 143 }
michael@0 144 })();
michael@0 145
michael@0 146 return deferred.promise;
michael@0 147 }
michael@0 148
michael@0 149 /**
michael@0 150 * Run a set of asychronous tests sequentially with callbacks to prepare each
michael@0 151 * test and to be called when the test result is ready.
michael@0 152 *
michael@0 153 * @param Scratchpad aScratchpad
michael@0 154 * The scratchpad to use in running the tests.
michael@0 155 * @param array aTests
michael@0 156 * An array of test objects, each with the following properties:
michael@0 157 * - method
michael@0 158 * Scratchpad method to use, one of "run", "display", or "inspect".
michael@0 159 * - prepare
michael@0 160 * The callback to run just prior to executing the scratchpad method.
michael@0 161 * - then
michael@0 162 * The callback to run when the scratchpad execution promise resolves.
michael@0 163 * @return Promise
michael@0 164 * The promise that will be resolved when all tests are finished.
michael@0 165 */
michael@0 166 function runAsyncCallbackTests(aScratchpad, aTests)
michael@0 167 {
michael@0 168 let deferred = promise.defer();
michael@0 169
michael@0 170 (function runTest() {
michael@0 171 if (aTests.length) {
michael@0 172 let test = aTests.shift();
michael@0 173 test.prepare();
michael@0 174 aScratchpad[test.method]().then(test.then.bind(test)).then(runTest);
michael@0 175 } else {
michael@0 176 deferred.resolve();
michael@0 177 }
michael@0 178 })();
michael@0 179
michael@0 180 return deferred.promise;
michael@0 181 }
michael@0 182
michael@0 183
michael@0 184 function cleanup()
michael@0 185 {
michael@0 186 if (gScratchpadWindow) {
michael@0 187 gScratchpadWindow.close();
michael@0 188 gScratchpadWindow = null;
michael@0 189 }
michael@0 190 while (gBrowser.tabs.length > 1) {
michael@0 191 gBrowser.removeCurrentTab();
michael@0 192 }
michael@0 193 }
michael@0 194
michael@0 195 registerCleanupFunction(cleanup);

mercurial