Bug 1341304 - Emit "selection-changed" event on the DevTools toolbox object. r=jdescottes,ochameau

MozReview-Commit-ID: G2jRxqDH9wx

--HG--
extra : rebase_source : 732de8680b935e6a0d4bee6e4b614ac462badc12
This commit is contained in:
Luca Greco 2017-07-06 19:47:21 +02:00
Родитель f1266850f7
Коммит 6f0dbffd3d
3 изменённых файлов: 52 добавлений и 0 удалений

Просмотреть файл

@ -91,6 +91,7 @@ run-if = e10s
[browser_toolbox_select_event.js]
skip-if = e10s # Bug 1069044 - destroyInspector may hang during shutdown
[browser_toolbox_selected_tool_unavailable.js]
[browser_toolbox_selectionchanged_event.js]
[browser_toolbox_sidebar.js]
[browser_toolbox_sidebar_events.js]
[browser_toolbox_sidebar_existing_tabs.js]

Просмотреть файл

@ -0,0 +1,41 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const PAGE_URL = "data:text/html;charset=utf-8,<body><div></div></body>";
add_task(function* () {
let tab = yield addTab(PAGE_URL);
let toolbox = yield openToolboxForTab(tab, "inspector", "bottom");
let inspector = toolbox.getCurrentPanel();
let root = yield inspector.walker.getRootNode();
let body = yield inspector.walker.querySelector(root, "body");
let node = yield inspector.walker.querySelector(root, "div");
is(inspector.selection.nodeFront, body, "Body is selected by default");
// Listen to selection changed
let onSelectionChanged = toolbox.once("selection-changed");
info("Select the div and wait for the selection-changed event to be fired.");
inspector.selection.setNodeFront(node, "browser-context-menu");
yield onSelectionChanged;
is(inspector.selection.nodeFront, node, "Div is now selected");
// Listen to cleared selection changed
let onClearSelectionChanged = toolbox.once("selection-changed");
info("Clear the selection and wait for the selection-changed event to be fired.");
inspector.selection.setNodeFront(undefined, "browser-context-menu");
yield onClearSelectionChanged;
is(inspector.selection.nodeFront, undefined, "The selection is undefined as expected");
});

Просмотреть файл

@ -137,6 +137,7 @@ function Toolbox(target, selectedTool, hostType, contentWindow, frameId) {
this._onPickerStarted = this._onPickerStarted.bind(this);
this._onPickerStopped = this._onPickerStopped.bind(this);
this._onInspectObject = this._onInspectObject.bind(this);
this._onNewSelectedNodeFront = this._onNewSelectedNodeFront.bind(this);
this.selectTool = this.selectTool.bind(this);
this._target.on("close", this.destroy);
@ -2219,6 +2220,7 @@ Toolbox.prototype = {
let showAllAnonymousContent = Services.prefs.getBoolPref(pref);
this._walker = yield this._inspector.getWalker({ showAllAnonymousContent });
this._selection = new Selection(this._walker);
this._selection.on("new-node-front", this._onNewSelectedNodeFront);
if (this.highlighterUtils.isRemoteHighlightable()) {
this.walker.on("highlighter-ready", this._highlighterReady);
@ -2232,6 +2234,13 @@ Toolbox.prototype = {
return this._initInspector;
},
_onNewSelectedNodeFront: function (evt) {
// Emit a "selection-changed" event when the toolbox.selection has been set
// to a new node (or cleared). Currently used in the WebExtensions APIs (to
// provide the `devtools.panels.elements.onSelectionChanged` event).
this.emit("selection-changed");
},
_onInspectObject: function (evt, packet) {
this.inspectObjectActor(packet.objectActor, packet.inspectFromAnnotation);
},
@ -2303,6 +2312,7 @@ Toolbox.prototype = {
yield this._highlighter.destroy();
}
if (this._selection) {
this._selection.off("new-node-front", this._onNewSelectedNodeFront);
this._selection.destroy();
}