Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
michael@0 | 7 | |
michael@0 | 8 | const XHTML_NS = "http://www.w3.org/1999/xhtml"; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | let devtools = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools; |
michael@0 | 13 | let WebConsoleUtils = devtools.require("devtools/toolkit/webconsole/utils").Utils; |
michael@0 | 14 | |
michael@0 | 15 | let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] |
michael@0 | 16 | .getService(Ci.nsIConsoleAPIStorage); |
michael@0 | 17 | |
michael@0 | 18 | let {ConsoleServiceListener, ConsoleAPIListener} = |
michael@0 | 19 | devtools.require("devtools/toolkit/webconsole/utils"); |
michael@0 | 20 | |
michael@0 | 21 | function initCommon() |
michael@0 | 22 | { |
michael@0 | 23 | //Services.prefs.setBoolPref("devtools.debugger.log", true); |
michael@0 | 24 | |
michael@0 | 25 | Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); |
michael@0 | 26 | Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | function initDebuggerServer() |
michael@0 | 30 | { |
michael@0 | 31 | if (!DebuggerServer.initialized) { |
michael@0 | 32 | DebuggerServer.init(); |
michael@0 | 33 | DebuggerServer.addBrowserActors(); |
michael@0 | 34 | } |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function connectToDebugger(aCallback) |
michael@0 | 38 | { |
michael@0 | 39 | initCommon(); |
michael@0 | 40 | initDebuggerServer(); |
michael@0 | 41 | |
michael@0 | 42 | let transport = DebuggerServer.connectPipe(); |
michael@0 | 43 | let client = new DebuggerClient(transport); |
michael@0 | 44 | |
michael@0 | 45 | let dbgState = { dbgClient: client }; |
michael@0 | 46 | client.connect(aCallback.bind(null, dbgState)); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | function attachConsole(aListeners, aCallback, aAttachToTab) |
michael@0 | 50 | { |
michael@0 | 51 | function _onAttachConsole(aState, aResponse, aWebConsoleClient) |
michael@0 | 52 | { |
michael@0 | 53 | if (aResponse.error) { |
michael@0 | 54 | Cu.reportError("attachConsole failed: " + aResponse.error + " " + |
michael@0 | 55 | aResponse.message); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | aState.client = aWebConsoleClient; |
michael@0 | 59 | |
michael@0 | 60 | aCallback(aState, aResponse); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | connectToDebugger(function _onConnect(aState, aResponse) { |
michael@0 | 64 | if (aResponse.error) { |
michael@0 | 65 | Cu.reportError("client.connect() failed: " + aResponse.error + " " + |
michael@0 | 66 | aResponse.message); |
michael@0 | 67 | aCallback(aState, aResponse); |
michael@0 | 68 | return; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | aState.dbgClient.listTabs(function _onListTabs(aResponse) { |
michael@0 | 72 | if (aResponse.error) { |
michael@0 | 73 | Cu.reportError("listTabs failed: " + aResponse.error + " " + |
michael@0 | 74 | aResponse.message); |
michael@0 | 75 | aCallback(aState, aResponse); |
michael@0 | 76 | return; |
michael@0 | 77 | } |
michael@0 | 78 | let consoleActor = aAttachToTab ? |
michael@0 | 79 | aResponse.tabs[aResponse.selected].consoleActor : |
michael@0 | 80 | aResponse.consoleActor; |
michael@0 | 81 | aState.actor = consoleActor; |
michael@0 | 82 | aState.dbgClient.attachConsole(consoleActor, aListeners, |
michael@0 | 83 | _onAttachConsole.bind(null, aState)); |
michael@0 | 84 | }); |
michael@0 | 85 | }); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function closeDebugger(aState, aCallback) |
michael@0 | 89 | { |
michael@0 | 90 | aState.dbgClient.close(aCallback); |
michael@0 | 91 | aState.dbgClient = null; |
michael@0 | 92 | aState.client = null; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function checkConsoleAPICall(aCall, aExpected) |
michael@0 | 96 | { |
michael@0 | 97 | if (aExpected.level != "trace" && aExpected.arguments) { |
michael@0 | 98 | is(aCall.arguments.length, aExpected.arguments.length, |
michael@0 | 99 | "number of arguments"); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | checkObject(aCall, aExpected); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | function checkObject(aObject, aExpected) |
michael@0 | 106 | { |
michael@0 | 107 | for (let name of Object.keys(aExpected)) |
michael@0 | 108 | { |
michael@0 | 109 | let expected = aExpected[name]; |
michael@0 | 110 | let value = aObject[name]; |
michael@0 | 111 | checkValue(name, value, expected); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | function checkValue(aName, aValue, aExpected) |
michael@0 | 116 | { |
michael@0 | 117 | if (aExpected === null) { |
michael@0 | 118 | ok(!aValue, "'" + aName + "' is null"); |
michael@0 | 119 | } |
michael@0 | 120 | else if (aValue === undefined) { |
michael@0 | 121 | ok(false, "'" + aName + "' is undefined"); |
michael@0 | 122 | } |
michael@0 | 123 | else if (typeof aExpected == "string" || typeof aExpected == "number" || |
michael@0 | 124 | typeof aExpected == "boolean") { |
michael@0 | 125 | is(aValue, aExpected, "property '" + aName + "'"); |
michael@0 | 126 | } |
michael@0 | 127 | else if (aExpected instanceof RegExp) { |
michael@0 | 128 | ok(aExpected.test(aValue), aName + ": " + aExpected + " matched " + aValue); |
michael@0 | 129 | } |
michael@0 | 130 | else if (Array.isArray(aExpected)) { |
michael@0 | 131 | info("checking array for property '" + aName + "'"); |
michael@0 | 132 | checkObject(aValue, aExpected); |
michael@0 | 133 | } |
michael@0 | 134 | else if (typeof aExpected == "object") { |
michael@0 | 135 | info("checking object for property '" + aName + "'"); |
michael@0 | 136 | checkObject(aValue, aExpected); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | function checkHeadersOrCookies(aArray, aExpected) |
michael@0 | 141 | { |
michael@0 | 142 | let foundHeaders = {}; |
michael@0 | 143 | |
michael@0 | 144 | for (let elem of aArray) { |
michael@0 | 145 | if (!(elem.name in aExpected)) { |
michael@0 | 146 | continue; |
michael@0 | 147 | } |
michael@0 | 148 | foundHeaders[elem.name] = true; |
michael@0 | 149 | info("checking value of header " + elem.name); |
michael@0 | 150 | checkValue(elem.name, elem.value, aExpected[elem.name]); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | for (let header in aExpected) { |
michael@0 | 154 | if (!(header in foundHeaders)) { |
michael@0 | 155 | ok(false, header + " was not found"); |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | var gTestState = {}; |
michael@0 | 161 | |
michael@0 | 162 | function runTests(aTests, aEndCallback) |
michael@0 | 163 | { |
michael@0 | 164 | function driver() |
michael@0 | 165 | { |
michael@0 | 166 | let lastResult, sendToNext; |
michael@0 | 167 | for (let i = 0; i < aTests.length; i++) { |
michael@0 | 168 | gTestState.index = i; |
michael@0 | 169 | let fn = aTests[i]; |
michael@0 | 170 | info("will run test #" + i + ": " + fn.name); |
michael@0 | 171 | lastResult = fn(sendToNext, lastResult); |
michael@0 | 172 | sendToNext = yield lastResult; |
michael@0 | 173 | } |
michael@0 | 174 | yield aEndCallback(sendToNext, lastResult); |
michael@0 | 175 | } |
michael@0 | 176 | gTestState.driver = driver(); |
michael@0 | 177 | return gTestState.driver.next(); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | function nextTest(aMessage) |
michael@0 | 181 | { |
michael@0 | 182 | return gTestState.driver.send(aMessage); |
michael@0 | 183 | } |