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 | "use strict"; |
michael@0 | 4 | |
michael@0 | 5 | const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; |
michael@0 | 6 | |
michael@0 | 7 | let { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); |
michael@0 | 8 | |
michael@0 | 9 | // Disable logging for all the tests. Both the debugger server and frontend will |
michael@0 | 10 | // be affected by this pref. |
michael@0 | 11 | let gEnableLogging = Services.prefs.getBoolPref("devtools.debugger.log"); |
michael@0 | 12 | Services.prefs.setBoolPref("devtools.debugger.log", false); |
michael@0 | 13 | |
michael@0 | 14 | let { Task } = Cu.import("resource://gre/modules/Task.jsm", {}); |
michael@0 | 15 | let { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {}); |
michael@0 | 16 | let { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm", {}); |
michael@0 | 17 | let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); |
michael@0 | 18 | let { DebuggerServer } = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {}); |
michael@0 | 19 | let { DebuggerClient } = Cu.import("resource://gre/modules/devtools/dbg-client.jsm", {}); |
michael@0 | 20 | |
michael@0 | 21 | let { CallWatcherFront } = devtools.require("devtools/server/actors/call-watcher"); |
michael@0 | 22 | let { CanvasFront } = devtools.require("devtools/server/actors/canvas"); |
michael@0 | 23 | let TiltGL = devtools.require("devtools/tilt/tilt-gl"); |
michael@0 | 24 | let TargetFactory = devtools.TargetFactory; |
michael@0 | 25 | let Toolbox = devtools.Toolbox; |
michael@0 | 26 | |
michael@0 | 27 | const EXAMPLE_URL = "http://example.com/browser/browser/devtools/canvasdebugger/test/"; |
michael@0 | 28 | const SIMPLE_CANVAS_URL = EXAMPLE_URL + "doc_simple-canvas.html"; |
michael@0 | 29 | const SIMPLE_CANVAS_TRANSPARENT_URL = EXAMPLE_URL + "doc_simple-canvas-transparent.html"; |
michael@0 | 30 | const SIMPLE_CANVAS_DEEP_STACK_URL = EXAMPLE_URL + "doc_simple-canvas-deep-stack.html"; |
michael@0 | 31 | |
michael@0 | 32 | // All tests are asynchronous. |
michael@0 | 33 | waitForExplicitFinish(); |
michael@0 | 34 | |
michael@0 | 35 | let gToolEnabled = Services.prefs.getBoolPref("devtools.canvasdebugger.enabled"); |
michael@0 | 36 | |
michael@0 | 37 | registerCleanupFunction(() => { |
michael@0 | 38 | info("finish() was called, cleaning up..."); |
michael@0 | 39 | Services.prefs.setBoolPref("devtools.debugger.log", gEnableLogging); |
michael@0 | 40 | Services.prefs.setBoolPref("devtools.canvasdebugger.enabled", gToolEnabled); |
michael@0 | 41 | |
michael@0 | 42 | // Some of yhese tests use a lot of memory due to GL contexts, so force a GC |
michael@0 | 43 | // to help fragmentation. |
michael@0 | 44 | info("Forcing GC after canvas debugger test."); |
michael@0 | 45 | Cu.forceGC(); |
michael@0 | 46 | }); |
michael@0 | 47 | |
michael@0 | 48 | function addTab(aUrl, aWindow) { |
michael@0 | 49 | info("Adding tab: " + aUrl); |
michael@0 | 50 | |
michael@0 | 51 | let deferred = promise.defer(); |
michael@0 | 52 | let targetWindow = aWindow || window; |
michael@0 | 53 | let targetBrowser = targetWindow.gBrowser; |
michael@0 | 54 | |
michael@0 | 55 | targetWindow.focus(); |
michael@0 | 56 | let tab = targetBrowser.selectedTab = targetBrowser.addTab(aUrl); |
michael@0 | 57 | let linkedBrowser = tab.linkedBrowser; |
michael@0 | 58 | |
michael@0 | 59 | linkedBrowser.addEventListener("load", function onLoad() { |
michael@0 | 60 | linkedBrowser.removeEventListener("load", onLoad, true); |
michael@0 | 61 | info("Tab added and finished loading: " + aUrl); |
michael@0 | 62 | deferred.resolve(tab); |
michael@0 | 63 | }, true); |
michael@0 | 64 | |
michael@0 | 65 | return deferred.promise; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | function removeTab(aTab, aWindow) { |
michael@0 | 69 | info("Removing tab."); |
michael@0 | 70 | |
michael@0 | 71 | let deferred = promise.defer(); |
michael@0 | 72 | let targetWindow = aWindow || window; |
michael@0 | 73 | let targetBrowser = targetWindow.gBrowser; |
michael@0 | 74 | let tabContainer = targetBrowser.tabContainer; |
michael@0 | 75 | |
michael@0 | 76 | tabContainer.addEventListener("TabClose", function onClose(aEvent) { |
michael@0 | 77 | tabContainer.removeEventListener("TabClose", onClose, false); |
michael@0 | 78 | info("Tab removed and finished closing."); |
michael@0 | 79 | deferred.resolve(); |
michael@0 | 80 | }, false); |
michael@0 | 81 | |
michael@0 | 82 | targetBrowser.removeTab(aTab); |
michael@0 | 83 | return deferred.promise; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | function handleError(aError) { |
michael@0 | 87 | ok(false, "Got an error: " + aError.message + "\n" + aError.stack); |
michael@0 | 88 | finish(); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | let gRequiresWebGL = false; |
michael@0 | 92 | |
michael@0 | 93 | function ifTestingSupported() { |
michael@0 | 94 | ok(false, "You need to define a 'ifTestingSupported' function."); |
michael@0 | 95 | finish(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function ifTestingUnsupported() { |
michael@0 | 99 | todo(false, "Skipping test because some required functionality isn't supported."); |
michael@0 | 100 | finish(); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function test() { |
michael@0 | 104 | let generator = isTestingSupported() ? ifTestingSupported : ifTestingUnsupported; |
michael@0 | 105 | Task.spawn(generator).then(null, handleError); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | function createCanvas() { |
michael@0 | 109 | return document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function isTestingSupported() { |
michael@0 | 113 | if (!gRequiresWebGL) { |
michael@0 | 114 | info("This test does not require WebGL support."); |
michael@0 | 115 | return true; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | let supported = |
michael@0 | 119 | !TiltGL.isWebGLForceEnabled() && |
michael@0 | 120 | TiltGL.isWebGLSupported() && |
michael@0 | 121 | TiltGL.create3DContext(createCanvas()); |
michael@0 | 122 | |
michael@0 | 123 | info("This test requires WebGL support."); |
michael@0 | 124 | info("Apparently, WebGL is" + (supported ? "" : " not") + " supported."); |
michael@0 | 125 | return supported; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | function once(aTarget, aEventName, aUseCapture = false) { |
michael@0 | 129 | info("Waiting for event: '" + aEventName + "' on " + aTarget + "."); |
michael@0 | 130 | |
michael@0 | 131 | let deferred = promise.defer(); |
michael@0 | 132 | |
michael@0 | 133 | for (let [add, remove] of [ |
michael@0 | 134 | ["on", "off"], // Use event emitter before DOM events for consistency |
michael@0 | 135 | ["addEventListener", "removeEventListener"], |
michael@0 | 136 | ["addListener", "removeListener"] |
michael@0 | 137 | ]) { |
michael@0 | 138 | if ((add in aTarget) && (remove in aTarget)) { |
michael@0 | 139 | aTarget[add](aEventName, function onEvent(...aArgs) { |
michael@0 | 140 | aTarget[remove](aEventName, onEvent, aUseCapture); |
michael@0 | 141 | deferred.resolve(...aArgs); |
michael@0 | 142 | }, aUseCapture); |
michael@0 | 143 | break; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | return deferred.promise; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | function waitForTick() { |
michael@0 | 151 | let deferred = promise.defer(); |
michael@0 | 152 | executeSoon(deferred.resolve); |
michael@0 | 153 | return deferred.promise; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | function navigateInHistory(aTarget, aDirection, aWaitForTargetEvent = "navigate") { |
michael@0 | 157 | executeSoon(() => content.history[aDirection]()); |
michael@0 | 158 | return once(aTarget, aWaitForTargetEvent); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | function navigate(aTarget, aUrl, aWaitForTargetEvent = "navigate") { |
michael@0 | 162 | executeSoon(() => aTarget.activeTab.navigateTo(aUrl)); |
michael@0 | 163 | return once(aTarget, aWaitForTargetEvent); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | function reload(aTarget, aWaitForTargetEvent = "navigate") { |
michael@0 | 167 | executeSoon(() => aTarget.activeTab.reload()); |
michael@0 | 168 | return once(aTarget, aWaitForTargetEvent); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | function initServer() { |
michael@0 | 172 | if (!DebuggerServer.initialized) { |
michael@0 | 173 | DebuggerServer.init(() => true); |
michael@0 | 174 | DebuggerServer.addBrowserActors(); |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | function initCallWatcherBackend(aUrl) { |
michael@0 | 179 | info("Initializing a call watcher front."); |
michael@0 | 180 | initServer(); |
michael@0 | 181 | |
michael@0 | 182 | return Task.spawn(function*() { |
michael@0 | 183 | let tab = yield addTab(aUrl); |
michael@0 | 184 | let target = TargetFactory.forTab(tab); |
michael@0 | 185 | let debuggee = target.window.wrappedJSObject; |
michael@0 | 186 | |
michael@0 | 187 | yield target.makeRemote(); |
michael@0 | 188 | |
michael@0 | 189 | let front = new CallWatcherFront(target.client, target.form); |
michael@0 | 190 | return [target, debuggee, front]; |
michael@0 | 191 | }); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | function initCanavsDebuggerBackend(aUrl) { |
michael@0 | 195 | info("Initializing a canvas debugger front."); |
michael@0 | 196 | initServer(); |
michael@0 | 197 | |
michael@0 | 198 | return Task.spawn(function*() { |
michael@0 | 199 | let tab = yield addTab(aUrl); |
michael@0 | 200 | let target = TargetFactory.forTab(tab); |
michael@0 | 201 | let debuggee = target.window.wrappedJSObject; |
michael@0 | 202 | |
michael@0 | 203 | yield target.makeRemote(); |
michael@0 | 204 | |
michael@0 | 205 | let front = new CanvasFront(target.client, target.form); |
michael@0 | 206 | return [target, debuggee, front]; |
michael@0 | 207 | }); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | function initCanavsDebuggerFrontend(aUrl) { |
michael@0 | 211 | info("Initializing a canvas debugger pane."); |
michael@0 | 212 | |
michael@0 | 213 | return Task.spawn(function*() { |
michael@0 | 214 | let tab = yield addTab(aUrl); |
michael@0 | 215 | let target = TargetFactory.forTab(tab); |
michael@0 | 216 | let debuggee = target.window.wrappedJSObject; |
michael@0 | 217 | |
michael@0 | 218 | yield target.makeRemote(); |
michael@0 | 219 | |
michael@0 | 220 | Services.prefs.setBoolPref("devtools.canvasdebugger.enabled", true); |
michael@0 | 221 | let toolbox = yield gDevTools.showToolbox(target, "canvasdebugger"); |
michael@0 | 222 | let panel = toolbox.getCurrentPanel(); |
michael@0 | 223 | return [target, debuggee, panel]; |
michael@0 | 224 | }); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | function teardown(aPanel) { |
michael@0 | 228 | info("Destroying the specified canvas debugger."); |
michael@0 | 229 | |
michael@0 | 230 | return promise.all([ |
michael@0 | 231 | once(aPanel, "destroyed"), |
michael@0 | 232 | removeTab(aPanel.target.tab) |
michael@0 | 233 | ]); |
michael@0 | 234 | } |