1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/webconsole/test/common.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,183 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; 1.10 + 1.11 +const XHTML_NS = "http://www.w3.org/1999/xhtml"; 1.12 + 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 + 1.15 +let devtools = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools; 1.16 +let WebConsoleUtils = devtools.require("devtools/toolkit/webconsole/utils").Utils; 1.17 + 1.18 +let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] 1.19 + .getService(Ci.nsIConsoleAPIStorage); 1.20 + 1.21 +let {ConsoleServiceListener, ConsoleAPIListener} = 1.22 + devtools.require("devtools/toolkit/webconsole/utils"); 1.23 + 1.24 +function initCommon() 1.25 +{ 1.26 + //Services.prefs.setBoolPref("devtools.debugger.log", true); 1.27 + 1.28 + Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); 1.29 + Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); 1.30 +} 1.31 + 1.32 +function initDebuggerServer() 1.33 +{ 1.34 + if (!DebuggerServer.initialized) { 1.35 + DebuggerServer.init(); 1.36 + DebuggerServer.addBrowserActors(); 1.37 + } 1.38 +} 1.39 + 1.40 +function connectToDebugger(aCallback) 1.41 +{ 1.42 + initCommon(); 1.43 + initDebuggerServer(); 1.44 + 1.45 + let transport = DebuggerServer.connectPipe(); 1.46 + let client = new DebuggerClient(transport); 1.47 + 1.48 + let dbgState = { dbgClient: client }; 1.49 + client.connect(aCallback.bind(null, dbgState)); 1.50 +} 1.51 + 1.52 +function attachConsole(aListeners, aCallback, aAttachToTab) 1.53 +{ 1.54 + function _onAttachConsole(aState, aResponse, aWebConsoleClient) 1.55 + { 1.56 + if (aResponse.error) { 1.57 + Cu.reportError("attachConsole failed: " + aResponse.error + " " + 1.58 + aResponse.message); 1.59 + } 1.60 + 1.61 + aState.client = aWebConsoleClient; 1.62 + 1.63 + aCallback(aState, aResponse); 1.64 + } 1.65 + 1.66 + connectToDebugger(function _onConnect(aState, aResponse) { 1.67 + if (aResponse.error) { 1.68 + Cu.reportError("client.connect() failed: " + aResponse.error + " " + 1.69 + aResponse.message); 1.70 + aCallback(aState, aResponse); 1.71 + return; 1.72 + } 1.73 + 1.74 + aState.dbgClient.listTabs(function _onListTabs(aResponse) { 1.75 + if (aResponse.error) { 1.76 + Cu.reportError("listTabs failed: " + aResponse.error + " " + 1.77 + aResponse.message); 1.78 + aCallback(aState, aResponse); 1.79 + return; 1.80 + } 1.81 + let consoleActor = aAttachToTab ? 1.82 + aResponse.tabs[aResponse.selected].consoleActor : 1.83 + aResponse.consoleActor; 1.84 + aState.actor = consoleActor; 1.85 + aState.dbgClient.attachConsole(consoleActor, aListeners, 1.86 + _onAttachConsole.bind(null, aState)); 1.87 + }); 1.88 + }); 1.89 +} 1.90 + 1.91 +function closeDebugger(aState, aCallback) 1.92 +{ 1.93 + aState.dbgClient.close(aCallback); 1.94 + aState.dbgClient = null; 1.95 + aState.client = null; 1.96 +} 1.97 + 1.98 +function checkConsoleAPICall(aCall, aExpected) 1.99 +{ 1.100 + if (aExpected.level != "trace" && aExpected.arguments) { 1.101 + is(aCall.arguments.length, aExpected.arguments.length, 1.102 + "number of arguments"); 1.103 + } 1.104 + 1.105 + checkObject(aCall, aExpected); 1.106 +} 1.107 + 1.108 +function checkObject(aObject, aExpected) 1.109 +{ 1.110 + for (let name of Object.keys(aExpected)) 1.111 + { 1.112 + let expected = aExpected[name]; 1.113 + let value = aObject[name]; 1.114 + checkValue(name, value, expected); 1.115 + } 1.116 +} 1.117 + 1.118 +function checkValue(aName, aValue, aExpected) 1.119 +{ 1.120 + if (aExpected === null) { 1.121 + ok(!aValue, "'" + aName + "' is null"); 1.122 + } 1.123 + else if (aValue === undefined) { 1.124 + ok(false, "'" + aName + "' is undefined"); 1.125 + } 1.126 + else if (typeof aExpected == "string" || typeof aExpected == "number" || 1.127 + typeof aExpected == "boolean") { 1.128 + is(aValue, aExpected, "property '" + aName + "'"); 1.129 + } 1.130 + else if (aExpected instanceof RegExp) { 1.131 + ok(aExpected.test(aValue), aName + ": " + aExpected + " matched " + aValue); 1.132 + } 1.133 + else if (Array.isArray(aExpected)) { 1.134 + info("checking array for property '" + aName + "'"); 1.135 + checkObject(aValue, aExpected); 1.136 + } 1.137 + else if (typeof aExpected == "object") { 1.138 + info("checking object for property '" + aName + "'"); 1.139 + checkObject(aValue, aExpected); 1.140 + } 1.141 +} 1.142 + 1.143 +function checkHeadersOrCookies(aArray, aExpected) 1.144 +{ 1.145 + let foundHeaders = {}; 1.146 + 1.147 + for (let elem of aArray) { 1.148 + if (!(elem.name in aExpected)) { 1.149 + continue; 1.150 + } 1.151 + foundHeaders[elem.name] = true; 1.152 + info("checking value of header " + elem.name); 1.153 + checkValue(elem.name, elem.value, aExpected[elem.name]); 1.154 + } 1.155 + 1.156 + for (let header in aExpected) { 1.157 + if (!(header in foundHeaders)) { 1.158 + ok(false, header + " was not found"); 1.159 + } 1.160 + } 1.161 +} 1.162 + 1.163 +var gTestState = {}; 1.164 + 1.165 +function runTests(aTests, aEndCallback) 1.166 +{ 1.167 + function driver() 1.168 + { 1.169 + let lastResult, sendToNext; 1.170 + for (let i = 0; i < aTests.length; i++) { 1.171 + gTestState.index = i; 1.172 + let fn = aTests[i]; 1.173 + info("will run test #" + i + ": " + fn.name); 1.174 + lastResult = fn(sendToNext, lastResult); 1.175 + sendToNext = yield lastResult; 1.176 + } 1.177 + yield aEndCallback(sendToNext, lastResult); 1.178 + } 1.179 + gTestState.driver = driver(); 1.180 + return gTestState.driver.next(); 1.181 +} 1.182 + 1.183 +function nextTest(aMessage) 1.184 +{ 1.185 + return gTestState.driver.send(aMessage); 1.186 +}