browser/devtools/inspector/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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cu = Components.utils;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 const Cc = Components.classes;
michael@0 8
michael@0 9 // Services.prefs.setBoolPref("devtools.debugger.log", true);
michael@0 10 // SimpleTest.registerCleanupFunction(() => {
michael@0 11 // Services.prefs.clearUserPref("devtools.debugger.log");
michael@0 12 // });
michael@0 13
michael@0 14 //Services.prefs.setBoolPref("devtools.dump.emit", true);
michael@0 15
michael@0 16 let tempScope = {};
michael@0 17 Cu.import("resource://gre/modules/devtools/LayoutHelpers.jsm", tempScope);
michael@0 18 let LayoutHelpers = tempScope.LayoutHelpers;
michael@0 19
michael@0 20 let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", tempScope);
michael@0 21 let TargetFactory = devtools.TargetFactory;
michael@0 22
michael@0 23 Components.utils.import("resource://gre/modules/devtools/Console.jsm", tempScope);
michael@0 24 let console = tempScope.console;
michael@0 25
michael@0 26 // Import the GCLI test helper
michael@0 27 let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
michael@0 28 Services.scriptloader.loadSubScript(testDir + "../../../commandline/test/helpers.js", this);
michael@0 29
michael@0 30 gDevTools.testing = true;
michael@0 31 SimpleTest.registerCleanupFunction(() => {
michael@0 32 gDevTools.testing = false;
michael@0 33 });
michael@0 34
michael@0 35 SimpleTest.registerCleanupFunction(() => {
michael@0 36 console.error("Here we are\n");
michael@0 37 let {DebuggerServer} = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {});
michael@0 38 console.error("DebuggerServer open connections: " + Object.getOwnPropertyNames(DebuggerServer._connections).length);
michael@0 39
michael@0 40 Services.prefs.clearUserPref("devtools.dump.emit");
michael@0 41 Services.prefs.clearUserPref("devtools.inspector.activeSidebar");
michael@0 42 });
michael@0 43
michael@0 44 /**
michael@0 45 * Simple DOM node accesor function that takes either a node or a string css
michael@0 46 * selector as argument and returns the corresponding node
michael@0 47 * @param {String|DOMNode} nodeOrSelector
michael@0 48 * @return {DOMNode}
michael@0 49 */
michael@0 50 function getNode(nodeOrSelector) {
michael@0 51 return typeof nodeOrSelector === "string" ?
michael@0 52 content.document.querySelector(nodeOrSelector) :
michael@0 53 nodeOrSelector;
michael@0 54 }
michael@0 55
michael@0 56 /**
michael@0 57 * Set the inspector's current selection to a node or to the first match of the
michael@0 58 * given css selector
michael@0 59 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
michael@0 60 * loaded in the toolbox
michael@0 61 * @param {String} reason Defaults to "test" which instructs the inspector not
michael@0 62 * to highlight the node upon selection
michael@0 63 * @param {String} reason Defaults to "test" which instructs the inspector not to highlight the node upon selection
michael@0 64 * @return a promise that resolves when the inspector is updated with the new
michael@0 65 * node
michael@0 66 */
michael@0 67 function selectNode(nodeOrSelector, inspector, reason="test") {
michael@0 68 info("Selecting the node " + nodeOrSelector);
michael@0 69 let node = getNode(nodeOrSelector);
michael@0 70 let updated = inspector.once("inspector-updated");
michael@0 71 inspector.selection.setNode(node, reason);
michael@0 72 return updated;
michael@0 73 }
michael@0 74
michael@0 75 /**
michael@0 76 * Open the toolbox, with the inspector tool visible.
michael@0 77 * @param {Function} cb Optional callback, if you don't want to use the returned
michael@0 78 * promise
michael@0 79 * @return a promise that resolves when the inspector is ready
michael@0 80 */
michael@0 81 let openInspector = Task.async(function*(cb) {
michael@0 82 info("Opening the inspector");
michael@0 83 let target = TargetFactory.forTab(gBrowser.selectedTab);
michael@0 84
michael@0 85 let inspector, toolbox;
michael@0 86
michael@0 87 // Checking if the toolbox and the inspector are already loaded
michael@0 88 // The inspector-updated event should only be waited for if the inspector
michael@0 89 // isn't loaded yet
michael@0 90 toolbox = gDevTools.getToolbox(target);
michael@0 91 if (toolbox) {
michael@0 92 inspector = toolbox.getPanel("inspector");
michael@0 93 if (inspector) {
michael@0 94 info("Toolbox and inspector already open");
michael@0 95 if (cb) {
michael@0 96 return cb(inspector, toolbox);
michael@0 97 } else {
michael@0 98 return {
michael@0 99 toolbox: toolbox,
michael@0 100 inspector: inspector
michael@0 101 };
michael@0 102 }
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 info("Opening the toolbox");
michael@0 107 toolbox = yield gDevTools.showToolbox(target, "inspector");
michael@0 108 yield waitForToolboxFrameFocus(toolbox);
michael@0 109 inspector = toolbox.getPanel("inspector");
michael@0 110
michael@0 111 info("Waiting for the inspector to update");
michael@0 112 yield inspector.once("inspector-updated");
michael@0 113
michael@0 114 if (cb) {
michael@0 115 return cb(inspector, toolbox);
michael@0 116 } else {
michael@0 117 return {
michael@0 118 toolbox: toolbox,
michael@0 119 inspector: inspector
michael@0 120 };
michael@0 121 }
michael@0 122 });
michael@0 123
michael@0 124 /**
michael@0 125 * Wait for the toolbox frame to receive focus after it loads
michael@0 126 * @param {Toolbox} toolbox
michael@0 127 * @return a promise that resolves when focus has been received
michael@0 128 */
michael@0 129 function waitForToolboxFrameFocus(toolbox) {
michael@0 130 info("Making sure that the toolbox's frame is focused");
michael@0 131 let def = promise.defer();
michael@0 132 let win = toolbox.frame.contentWindow;
michael@0 133 waitForFocus(def.resolve, win);
michael@0 134 return def.promise;
michael@0 135 }
michael@0 136
michael@0 137 /**
michael@0 138 * Open the toolbox, with the inspector tool visible, and the sidebar that
michael@0 139 * corresponds to the given id selected
michael@0 140 * @return a promise that resolves when the inspector is ready and the sidebar
michael@0 141 * view is visible and ready
michael@0 142 */
michael@0 143 let openInspectorSideBar = Task.async(function*(id) {
michael@0 144 let {toolbox, inspector} = yield openInspector();
michael@0 145
michael@0 146 if (!hasSideBarTab(inspector, id)) {
michael@0 147 info("Waiting for the " + id + " sidebar to be ready");
michael@0 148 yield inspector.sidebar.once(id + "-ready");
michael@0 149 }
michael@0 150
michael@0 151 info("Selecting the " + id + " sidebar");
michael@0 152 inspector.sidebar.select(id);
michael@0 153
michael@0 154 return {
michael@0 155 toolbox: toolbox,
michael@0 156 inspector: inspector,
michael@0 157 view: inspector.sidebar.getWindowForTab(id)[id].view
michael@0 158 };
michael@0 159 });
michael@0 160
michael@0 161 /**
michael@0 162 * Open the toolbox, with the inspector tool visible, and the computed-view
michael@0 163 * sidebar tab selected.
michael@0 164 * @return a promise that resolves when the inspector is ready and the computed
michael@0 165 * view is visible and ready
michael@0 166 */
michael@0 167 function openComputedView() {
michael@0 168 return openInspectorSideBar("computedview");
michael@0 169 }
michael@0 170
michael@0 171 /**
michael@0 172 * Open the toolbox, with the inspector tool visible, and the rule-view
michael@0 173 * sidebar tab selected.
michael@0 174 * @return a promise that resolves when the inspector is ready and the rule
michael@0 175 * view is visible and ready
michael@0 176 */
michael@0 177 function openRuleView() {
michael@0 178 return openInspectorSideBar("ruleview");
michael@0 179 }
michael@0 180
michael@0 181 /**
michael@0 182 * Checks whether the inspector's sidebar corresponding to the given id already
michael@0 183 * exists
michael@0 184 * @param {InspectorPanel}
michael@0 185 * @param {String}
michael@0 186 * @return {Boolean}
michael@0 187 */
michael@0 188 function hasSideBarTab(inspector, id) {
michael@0 189 return !!inspector.sidebar.getWindowForTab(id);
michael@0 190 }
michael@0 191
michael@0 192 function getActiveInspector()
michael@0 193 {
michael@0 194 let target = TargetFactory.forTab(gBrowser.selectedTab);
michael@0 195 return gDevTools.getToolbox(target).getPanel("inspector");
michael@0 196 }
michael@0 197
michael@0 198 function getNodeFront(node)
michael@0 199 {
michael@0 200 let inspector = getActiveInspector();
michael@0 201 return inspector.walker.frontForRawNode(node);
michael@0 202 }
michael@0 203
michael@0 204 function getHighlighter()
michael@0 205 {
michael@0 206 return gBrowser.selectedBrowser.parentNode.querySelector(".highlighter-container");
michael@0 207 }
michael@0 208
michael@0 209 function getSimpleBorderRect() {
michael@0 210 let {p1, p2, p3, p4} = getBoxModelStatus().border.points;
michael@0 211
michael@0 212 return {
michael@0 213 top: p1.y,
michael@0 214 left: p1.x,
michael@0 215 width: p2.x - p1.x,
michael@0 216 height: p4.y - p1.y
michael@0 217 };
michael@0 218 }
michael@0 219
michael@0 220 function getBoxModelRoot() {
michael@0 221 let highlighter = getHighlighter();
michael@0 222 return highlighter.querySelector(".box-model-root");
michael@0 223 }
michael@0 224
michael@0 225 function getBoxModelStatus() {
michael@0 226 let root = getBoxModelRoot();
michael@0 227 let inspector = getActiveInspector();
michael@0 228
michael@0 229 return {
michael@0 230 visible: !root.hasAttribute("hidden"),
michael@0 231 currentNode: inspector.walker.currentNode,
michael@0 232 margin: {
michael@0 233 points: getPointsForRegion("margin"),
michael@0 234 visible: isRegionHidden("margin")
michael@0 235 },
michael@0 236 border: {
michael@0 237 points: getPointsForRegion("border"),
michael@0 238 visible: isRegionHidden("border")
michael@0 239 },
michael@0 240 padding: {
michael@0 241 points: getPointsForRegion("padding"),
michael@0 242 visible: isRegionHidden("padding")
michael@0 243 },
michael@0 244 content: {
michael@0 245 points: getPointsForRegion("content"),
michael@0 246 visible: isRegionHidden("content")
michael@0 247 },
michael@0 248 guides: {
michael@0 249 top: getGuideStatus("top"),
michael@0 250 right: getGuideStatus("right"),
michael@0 251 bottom: getGuideStatus("bottom"),
michael@0 252 left: getGuideStatus("left")
michael@0 253 }
michael@0 254 };
michael@0 255 }
michael@0 256
michael@0 257 function getGuideStatus(location) {
michael@0 258 let root = getBoxModelRoot();
michael@0 259 let guide = root.querySelector(".box-model-guide-" + location);
michael@0 260
michael@0 261 return {
michael@0 262 visible: !guide.hasAttribute("hidden"),
michael@0 263 x1: guide.getAttribute("x1"),
michael@0 264 y1: guide.getAttribute("y1"),
michael@0 265 x2: guide.getAttribute("x2"),
michael@0 266 y2: guide.getAttribute("y2")
michael@0 267 };
michael@0 268 }
michael@0 269
michael@0 270 function getPointsForRegion(region) {
michael@0 271 let root = getBoxModelRoot();
michael@0 272 let box = root.querySelector(".box-model-" + region);
michael@0 273 let points = box.getAttribute("points").split(/[, ]/);
michael@0 274
michael@0 275 // We multiply each value by 1 to cast it into a number
michael@0 276 return {
michael@0 277 p1: {
michael@0 278 x: parseFloat(points[0]),
michael@0 279 y: parseFloat(points[1])
michael@0 280 },
michael@0 281 p2: {
michael@0 282 x: parseFloat(points[2]),
michael@0 283 y: parseFloat(points[3])
michael@0 284 },
michael@0 285 p3: {
michael@0 286 x: parseFloat(points[4]),
michael@0 287 y: parseFloat(points[5])
michael@0 288 },
michael@0 289 p4: {
michael@0 290 x: parseFloat(points[6]),
michael@0 291 y: parseFloat(points[7])
michael@0 292 }
michael@0 293 };
michael@0 294 }
michael@0 295
michael@0 296 function isRegionHidden(region) {
michael@0 297 let root = getBoxModelRoot();
michael@0 298 let box = root.querySelector(".box-model-" + region);
michael@0 299
michael@0 300 return !box.hasAttribute("hidden");
michael@0 301 }
michael@0 302
michael@0 303 function isHighlighting()
michael@0 304 {
michael@0 305 let root = getBoxModelRoot();
michael@0 306 return !root.hasAttribute("hidden");
michael@0 307 }
michael@0 308
michael@0 309 function getHighlitNode()
michael@0 310 {
michael@0 311 if (isHighlighting()) {
michael@0 312 let helper = new LayoutHelpers(window.content);
michael@0 313 let points = getBoxModelStatus().content.points;
michael@0 314 let x = (points.p1.x + points.p2.x + points.p3.x + points.p4.x) / 4;
michael@0 315 let y = (points.p1.y + points.p2.y + points.p3.y + points.p4.y) / 4;
michael@0 316
michael@0 317 return helper.getElementFromPoint(window.content.document, x, y);
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321 function computedView()
michael@0 322 {
michael@0 323 let sidebar = getActiveInspector().sidebar;
michael@0 324 let iframe = sidebar.tabbox.querySelector(".iframe-computedview");
michael@0 325 return iframe.contentWindow.computedView;
michael@0 326 }
michael@0 327
michael@0 328 function computedViewTree()
michael@0 329 {
michael@0 330 return computedView().view;
michael@0 331 }
michael@0 332
michael@0 333 function ruleView()
michael@0 334 {
michael@0 335 let sidebar = getActiveInspector().sidebar;
michael@0 336 let iframe = sidebar.tabbox.querySelector(".iframe-ruleview");
michael@0 337 return iframe.contentWindow.ruleView;
michael@0 338 }
michael@0 339
michael@0 340 function getComputedView() {
michael@0 341 let inspector = getActiveInspector();
michael@0 342 return inspector.sidebar.getWindowForTab("computedview").computedview.view;
michael@0 343 }
michael@0 344
michael@0 345 function waitForView(aName, aCallback) {
michael@0 346 let inspector = getActiveInspector();
michael@0 347 if (inspector.sidebar.getTab(aName)) {
michael@0 348 aCallback();
michael@0 349 } else {
michael@0 350 inspector.sidebar.once(aName + "-ready", aCallback);
michael@0 351 }
michael@0 352 }
michael@0 353
michael@0 354 function synthesizeKeyFromKeyTag(aKeyId) {
michael@0 355 let key = document.getElementById(aKeyId);
michael@0 356 isnot(key, null, "Successfully retrieved the <key> node");
michael@0 357
michael@0 358 let modifiersAttr = key.getAttribute("modifiers");
michael@0 359
michael@0 360 let name = null;
michael@0 361
michael@0 362 if (key.getAttribute("keycode"))
michael@0 363 name = key.getAttribute("keycode");
michael@0 364 else if (key.getAttribute("key"))
michael@0 365 name = key.getAttribute("key");
michael@0 366
michael@0 367 isnot(name, null, "Successfully retrieved keycode/key");
michael@0 368
michael@0 369 let modifiers = {
michael@0 370 shiftKey: modifiersAttr.match("shift"),
michael@0 371 ctrlKey: modifiersAttr.match("ctrl"),
michael@0 372 altKey: modifiersAttr.match("alt"),
michael@0 373 metaKey: modifiersAttr.match("meta"),
michael@0 374 accelKey: modifiersAttr.match("accel")
michael@0 375 }
michael@0 376
michael@0 377 EventUtils.synthesizeKey(name, modifiers);
michael@0 378 }
michael@0 379
michael@0 380 function focusSearchBoxUsingShortcut(panelWin, callback) {
michael@0 381 panelWin.focus();
michael@0 382 let key = panelWin.document.getElementById("nodeSearchKey");
michael@0 383 isnot(key, null, "Successfully retrieved the <key> node");
michael@0 384
michael@0 385 let modifiersAttr = key.getAttribute("modifiers");
michael@0 386
michael@0 387 let name = null;
michael@0 388
michael@0 389 if (key.getAttribute("keycode")) {
michael@0 390 name = key.getAttribute("keycode");
michael@0 391 } else if (key.getAttribute("key")) {
michael@0 392 name = key.getAttribute("key");
michael@0 393 }
michael@0 394
michael@0 395 isnot(name, null, "Successfully retrieved keycode/key");
michael@0 396
michael@0 397 let modifiers = {
michael@0 398 shiftKey: modifiersAttr.match("shift"),
michael@0 399 ctrlKey: modifiersAttr.match("ctrl"),
michael@0 400 altKey: modifiersAttr.match("alt"),
michael@0 401 metaKey: modifiersAttr.match("meta"),
michael@0 402 accelKey: modifiersAttr.match("accel")
michael@0 403 }
michael@0 404
michael@0 405 let searchBox = panelWin.document.getElementById("inspector-searchbox");
michael@0 406 searchBox.addEventListener("focus", function onFocus() {
michael@0 407 searchBox.removeEventListener("focus", onFocus, false);
michael@0 408 callback && callback();
michael@0 409 }, false);
michael@0 410 EventUtils.synthesizeKey(name, modifiers);
michael@0 411 }
michael@0 412
michael@0 413 function getComputedPropertyValue(aName)
michael@0 414 {
michael@0 415 let computedview = getComputedView();
michael@0 416 let props = computedview.styleDocument.querySelectorAll(".property-view");
michael@0 417
michael@0 418 for (let prop of props) {
michael@0 419 let name = prop.querySelector(".property-name");
michael@0 420
michael@0 421 if (name.textContent === aName) {
michael@0 422 let value = prop.querySelector(".property-value");
michael@0 423 return value.textContent;
michael@0 424 }
michael@0 425 }
michael@0 426 }
michael@0 427
michael@0 428 function getContainerForRawNode(markupView, rawNode)
michael@0 429 {
michael@0 430 let front = markupView.walker.frontForRawNode(rawNode);
michael@0 431 let container = markupView.getContainer(front);
michael@0 432 return container;
michael@0 433 }
michael@0 434
michael@0 435 SimpleTest.registerCleanupFunction(function () {
michael@0 436 let target = TargetFactory.forTab(gBrowser.selectedTab);
michael@0 437 gDevTools.closeToolbox(target);
michael@0 438 });

mercurial