1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/scratchpad/test/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +/* vim: set ts=2 et sw=2 tw=80: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {}); 1.11 +const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {}); 1.12 +const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); 1.13 + 1.14 +let gScratchpadWindow; // Reference to the Scratchpad chrome window object 1.15 + 1.16 +gDevTools.testing = true; 1.17 +SimpleTest.registerCleanupFunction(() => { 1.18 + gDevTools.testing = false; 1.19 +}); 1.20 + 1.21 +/** 1.22 + * Open a Scratchpad window. 1.23 + * 1.24 + * @param function aReadyCallback 1.25 + * Optional. The function you want invoked when the Scratchpad instance 1.26 + * is ready. 1.27 + * @param object aOptions 1.28 + * Optional. Options for opening the scratchpad: 1.29 + * - window 1.30 + * Provide this if there's already a Scratchpad window you want to wait 1.31 + * loading for. 1.32 + * - state 1.33 + * Scratchpad state object. This is used when Scratchpad is open. 1.34 + * - noFocus 1.35 + * Boolean that tells you do not want the opened window to receive 1.36 + * focus. 1.37 + * @return nsIDOMWindow 1.38 + * The new window object that holds Scratchpad. Note that the 1.39 + * gScratchpadWindow global is also updated to reference the new window 1.40 + * object. 1.41 + */ 1.42 +function openScratchpad(aReadyCallback, aOptions) 1.43 +{ 1.44 + aOptions = aOptions || {}; 1.45 + 1.46 + let win = aOptions.window || 1.47 + Scratchpad.ScratchpadManager.openScratchpad(aOptions.state); 1.48 + if (!win) { 1.49 + return; 1.50 + } 1.51 + 1.52 + let onLoad = function() { 1.53 + win.removeEventListener("load", onLoad, false); 1.54 + 1.55 + win.Scratchpad.addObserver({ 1.56 + onReady: function(aScratchpad) { 1.57 + aScratchpad.removeObserver(this); 1.58 + 1.59 + if (aOptions.noFocus) { 1.60 + aReadyCallback(win, aScratchpad); 1.61 + } else { 1.62 + waitForFocus(aReadyCallback.bind(null, win, aScratchpad), win); 1.63 + } 1.64 + } 1.65 + }); 1.66 + }; 1.67 + 1.68 + if (aReadyCallback) { 1.69 + win.addEventListener("load", onLoad, false); 1.70 + } 1.71 + 1.72 + gScratchpadWindow = win; 1.73 + return gScratchpadWindow; 1.74 +} 1.75 + 1.76 +/** 1.77 + * Create a temporary file, write to it and call a callback 1.78 + * when done. 1.79 + * 1.80 + * @param string aName 1.81 + * Name of your temporary file. 1.82 + * @param string aContent 1.83 + * Temporary file's contents. 1.84 + * @param function aCallback 1.85 + * Optional callback to be called when we're done writing 1.86 + * to the file. It will receive two parameters: status code 1.87 + * and a file object. 1.88 + */ 1.89 +function createTempFile(aName, aContent, aCallback=function(){}) 1.90 +{ 1.91 + // Create a temporary file. 1.92 + let file = FileUtils.getFile("TmpD", [aName]); 1.93 + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8)); 1.94 + 1.95 + // Write the temporary file. 1.96 + let fout = Cc["@mozilla.org/network/file-output-stream;1"]. 1.97 + createInstance(Ci.nsIFileOutputStream); 1.98 + fout.init(file.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, 1.99 + parseInt("644", 8), fout.DEFER_OPEN); 1.100 + 1.101 + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. 1.102 + createInstance(Ci.nsIScriptableUnicodeConverter); 1.103 + converter.charset = "UTF-8"; 1.104 + let fileContentStream = converter.convertToInputStream(aContent); 1.105 + 1.106 + NetUtil.asyncCopy(fileContentStream, fout, function (aStatus) { 1.107 + aCallback(aStatus, file); 1.108 + }); 1.109 +} 1.110 + 1.111 +/** 1.112 + * Run a set of asychronous tests sequentially defined by input and output. 1.113 + * 1.114 + * @param Scratchpad aScratchpad 1.115 + * The scratchpad to use in running the tests. 1.116 + * @param array aTests 1.117 + * An array of test objects, each with the following properties: 1.118 + * - method 1.119 + * Scratchpad method to use, one of "run", "display", or "inspect". 1.120 + * - code 1.121 + * Code to run in the scratchpad. 1.122 + * - result 1.123 + * Expected code that will be in the scratchpad upon completion. 1.124 + * - label 1.125 + * The tests label which will be logged in the test runner output. 1.126 + * @return Promise 1.127 + * The promise that will be resolved when all tests are finished. 1.128 + */ 1.129 +function runAsyncTests(aScratchpad, aTests) 1.130 +{ 1.131 + let deferred = promise.defer(); 1.132 + 1.133 + (function runTest() { 1.134 + if (aTests.length) { 1.135 + let test = aTests.shift(); 1.136 + aScratchpad.setText(test.code); 1.137 + aScratchpad[test.method]().then(function success() { 1.138 + is(aScratchpad.getText(), test.result, test.label); 1.139 + runTest(); 1.140 + }, function failure(error) { 1.141 + ok(false, error.stack + " " + test.label); 1.142 + runTest(); 1.143 + }); 1.144 + } else { 1.145 + deferred.resolve(); 1.146 + } 1.147 + })(); 1.148 + 1.149 + return deferred.promise; 1.150 +} 1.151 + 1.152 +/** 1.153 + * Run a set of asychronous tests sequentially with callbacks to prepare each 1.154 + * test and to be called when the test result is ready. 1.155 + * 1.156 + * @param Scratchpad aScratchpad 1.157 + * The scratchpad to use in running the tests. 1.158 + * @param array aTests 1.159 + * An array of test objects, each with the following properties: 1.160 + * - method 1.161 + * Scratchpad method to use, one of "run", "display", or "inspect". 1.162 + * - prepare 1.163 + * The callback to run just prior to executing the scratchpad method. 1.164 + * - then 1.165 + * The callback to run when the scratchpad execution promise resolves. 1.166 + * @return Promise 1.167 + * The promise that will be resolved when all tests are finished. 1.168 + */ 1.169 +function runAsyncCallbackTests(aScratchpad, aTests) 1.170 +{ 1.171 + let deferred = promise.defer(); 1.172 + 1.173 + (function runTest() { 1.174 + if (aTests.length) { 1.175 + let test = aTests.shift(); 1.176 + test.prepare(); 1.177 + aScratchpad[test.method]().then(test.then.bind(test)).then(runTest); 1.178 + } else { 1.179 + deferred.resolve(); 1.180 + } 1.181 + })(); 1.182 + 1.183 + return deferred.promise; 1.184 +} 1.185 + 1.186 + 1.187 +function cleanup() 1.188 +{ 1.189 + if (gScratchpadWindow) { 1.190 + gScratchpadWindow.close(); 1.191 + gScratchpadWindow = null; 1.192 + } 1.193 + while (gBrowser.tabs.length > 1) { 1.194 + gBrowser.removeCurrentTab(); 1.195 + } 1.196 +} 1.197 + 1.198 +registerCleanupFunction(cleanup);