Bug 1316630 - don't emit pref-changed event on gDevTools; r=jdescottes

MozReview-Commit-ID: CCqAf8dBFSY

--HG--
extra : rebase_source : dac78e13ef9e52c5f36c17372d023be62c4396e7
This commit is contained in:
Tom Tromey 2016-11-21 08:47:10 -07:00
parent b6eca933c1
commit f504948b28
13 changed files with 113 additions and 160 deletions

View File

@ -318,14 +318,6 @@ DevTools.prototype = {
theme.id == currTheme) {
Services.prefs.setCharPref("devtools.theme", "light");
let data = {
pref: "devtools.theme",
newValue: "light",
oldValue: currTheme
};
this.emit("pref-changed", data);
this.emit("theme-unregistered", theme);
}

View File

@ -12,6 +12,7 @@
var doc = null, toolbox = null, panelWin = null, modifiedPrefs = [];
const {LocalizationHelper} = require("devtools/shared/l10n");
const L10N = new LocalizationHelper("devtools/client/locales/toolbox.properties");
const {PrefObserver} = require("devtools/client/shared/prefs");
add_task(function* () {
const URL = "data:text/html;charset=utf8,test for dynamically registering " +
@ -151,14 +152,13 @@ function* testSelect(select) {
continue;
}
let observer = new PrefObserver("devtools.");
let deferred = defer();
gDevTools.once("pref-changed", (event, data) => {
if (data.pref == pref) {
ok(true, "Correct pref was changed");
let changeSeen = false;
observer.once(pref, () => {
changeSeen = true;
is(GetPref(pref), option.value, "Preference been switched for " + pref);
} else {
ok(false, "Pref " + pref + " was not changed correctly");
}
deferred.resolve();
});
@ -167,21 +167,22 @@ function* testSelect(select) {
select.dispatchEvent(changeEvent);
yield deferred.promise;
ok(changeSeen, "Correct pref was changed");
observer.destroy();
}
}
function* testMouseClick(node, prefValue) {
let deferred = defer();
let observer = new PrefObserver("devtools.");
let pref = node.getAttribute("data-pref");
gDevTools.once("pref-changed", (event, data) => {
if (data.pref == pref) {
ok(true, "Correct pref was changed");
is(data.oldValue, prefValue, "Previous value is correct for " + pref);
is(data.newValue, !prefValue, "New value is correct for " + pref);
} else {
ok(false, "Pref " + pref + " was not changed correctly");
}
let changeSeen = false;
observer.once(pref, () => {
changeSeen = true;
is(GetPref(pref), !prefValue, "New value is correct for " + pref);
deferred.resolve();
});
@ -195,6 +196,9 @@ function* testMouseClick(node, prefValue) {
});
yield deferred.promise;
ok(changeSeen, "Correct pref was changed");
observer.destroy();
}
function* testToggleTools() {

View File

@ -275,7 +275,7 @@ OptionsPanel.prototype = {
inputRadio.setAttribute("value", theme.id);
inputRadio.setAttribute("name", "devtools-theme-item");
inputRadio.addEventListener("change", function (e) {
setPrefAndEmit(themeBox.getAttribute("data-pref"),
SetPref(themeBox.getAttribute("data-pref"),
e.target.value);
});
@ -305,7 +305,7 @@ OptionsPanel.prototype = {
}
prefCheckbox.addEventListener("change", function (e) {
let checkbox = e.target;
setPrefAndEmit(checkbox.getAttribute("data-pref"), checkbox.checked);
SetPref(checkbox.getAttribute("data-pref"), checkbox.checked);
});
}
// Themes radio inputs are handled in setupThemeList
@ -320,7 +320,7 @@ OptionsPanel.prototype = {
}
radioInput.addEventListener("change", function (e) {
setPrefAndEmit(radioGroup.getAttribute("data-pref"),
SetPref(radioGroup.getAttribute("data-pref"),
e.target.value);
});
}
@ -340,7 +340,7 @@ OptionsPanel.prototype = {
prefSelect.addEventListener("change", function (e) {
let select = e.target;
setPrefAndEmit(select.getAttribute("data-pref"),
SetPref(select.getAttribute("data-pref"),
select.options[select.selectedIndex].value);
});
}
@ -422,17 +422,3 @@ OptionsPanel.prototype = {
return this.destroyPromise;
}
};
/* Set a pref and emit the pref-changed event if needed. */
function setPrefAndEmit(prefName, newValue) {
let data = {
pref: prefName,
newValue: newValue
};
data.oldValue = GetPref(data.pref);
SetPref(data.pref, data.newValue);
if (data.newValue != data.oldValue) {
gDevTools.emit("pref-changed", data);
}
}

View File

@ -113,7 +113,9 @@ function Toolbox(target, selectedTool, hostType, contentWindow, frameId) {
this.highlighterUtils = getHighlighterUtils(this);
this._highlighterReady = this._highlighterReady.bind(this);
this._highlighterHidden = this._highlighterHidden.bind(this);
this._prefChanged = this._prefChanged.bind(this);
this._applyCacheSettings = this._applyCacheSettings.bind(this);
this._applyServiceWorkersTestingSettings =
this._applyServiceWorkersTestingSettings.bind(this);
this._saveSplitConsoleHeight = this._saveSplitConsoleHeight.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onBrowserMessage = this._onBrowserMessage.bind(this);
@ -367,7 +369,10 @@ Toolbox.prototype = {
this.closeButton = this.doc.getElementById("toolbox-close");
this.closeButton.addEventListener("click", this.destroy, true);
gDevTools.on("pref-changed", this._prefChanged);
Services.prefs.addObserver("devtools.cache.disabled", this._applyCacheSettings,
false);
Services.prefs.addObserver("devtools.serviceWorkers.testing.enabled",
this._applyServiceWorkersTestingSettings, false);
let framesMenu = this.doc.getElementById("command-button-frames");
framesMenu.addEventListener("click", this.showFramesMenu, false);
@ -485,29 +490,6 @@ Toolbox.prototype = {
this._telemetry.log(HOST_HISTOGRAM, this._getTelemetryHostId());
},
/**
* Because our panels are lazy loaded this is a good place to watch for
* "pref-changed" events.
* @param {String} event
* The event type, "pref-changed".
* @param {Object} data
* {
* newValue: The new value
* oldValue: The old value
* pref: The name of the preference that has changed
* }
*/
_prefChanged: function (event, data) {
switch (data.pref) {
case "devtools.cache.disabled":
this._applyCacheSettings();
break;
case "devtools.serviceWorkers.testing.enabled":
this._applyServiceWorkersTestingSettings();
break;
}
},
_buildOptions: function () {
let selectOptions = (name, event) => {
// Flip back to the last used panel if we are already
@ -2214,7 +2196,9 @@ Toolbox.prototype = {
gDevTools.off("tool-registered", this._toolRegistered);
gDevTools.off("tool-unregistered", this._toolUnregistered);
gDevTools.off("pref-changed", this._prefChanged);
Services.prefs.removeObserver("devtools.cache.disabled", this._applyCacheSettings);
Services.prefs.removeObserver("devtools.serviceWorkers.testing.enabled",
this._applyServiceWorkersTestingSettings);
this._lastFocusedElement = null;
if (this._sourceMapService) {

View File

@ -198,14 +198,13 @@ function CssComputedView(inspector, document, pageStyle) {
// No results text.
this.noResults = this.styleDocument.getElementById("computedview-no-results");
// Refresh panel when color unit changed.
// Refresh panel when color unit changed or pref for showing
// original sources changes.
this._handlePrefChange = this._handlePrefChange.bind(this);
gDevTools.on("pref-changed", this._handlePrefChange);
// Refresh panel when pref for showing original sources changes
this._onSourcePrefChanged = this._onSourcePrefChanged.bind(this);
this._prefObserver = new PrefObserver("devtools.");
this._prefObserver.on(PREF_ORIG_SOURCES, this._onSourcePrefChanged);
this._prefObserver.on("devtools.defaultColorUnit", this._handlePrefChange);
// The element that we're inspecting, and the document that it comes from.
this._viewedElement = null;
@ -262,8 +261,7 @@ CssComputedView.prototype = {
},
_handlePrefChange: function (event, data) {
if (this._computed && (data.pref === "devtools.defaultColorUnit" ||
data.pref === PREF_ORIG_SOURCES)) {
if (this._computed) {
this.refreshPanel();
}
},
@ -600,6 +598,7 @@ CssComputedView.prototype = {
},
_onSourcePrefChanged: function () {
this._handlePrefChange();
for (let propView of this.propertyViews) {
propView.updateSourceLinks();
}
@ -734,9 +733,8 @@ CssComputedView.prototype = {
this._viewedElement = null;
this._outputParser = null;
gDevTools.off("pref-changed", this._handlePrefChange);
this._prefObserver.off(PREF_ORIG_SOURCES, this._onSourcePrefChanged);
this._prefObserver.off("devtools.defaultColorUnit", this._handlePrefChange);
this._prefObserver.destroy();
// Cancel tree construction

View File

@ -16,7 +16,7 @@ var { loader, require } = BrowserLoaderModule.BrowserLoader({
var { Task } = require("devtools/shared/task");
/* exported Heritage, ViewHelpers, WidgetMethods, setNamedTimeout, clearNamedTimeout */
var { Heritage, ViewHelpers, WidgetMethods, setNamedTimeout, clearNamedTimeout } = require("devtools/client/shared/widgets/view-helpers");
var { gDevTools } = require("devtools/client/framework/devtools");
var { PrefObserver } = require("devtools/client/shared/prefs");
// Events emitted by various objects in the panel.
var EVENTS = require("devtools/client/performance/events");
@ -143,7 +143,8 @@ var PerformanceController = {
RecordingsView.on(EVENTS.UI_RECORDING_SELECTED, this._onRecordingSelectFromView);
DetailsView.on(EVENTS.UI_DETAILS_VIEW_SELECTED, this._pipe);
gDevTools.on("pref-changed", this._onThemeChanged);
this._prefObserver = new PrefObserver("devtools.");
this._prefObserver.on("devtools.theme", this._onThemeChanged);
}),
/**
@ -163,7 +164,8 @@ var PerformanceController = {
RecordingsView.off(EVENTS.UI_RECORDING_SELECTED, this._onRecordingSelectFromView);
DetailsView.off(EVENTS.UI_DETAILS_VIEW_SELECTED, this._pipe);
gDevTools.off("pref-changed", this._onThemeChanged);
this._prefObserver.off("devtools.theme", this._onThemeChanged);
this._prefObserver.destroy();
},
/**
@ -402,14 +404,9 @@ var PerformanceController = {
/*
* Called when the developer tools theme changes.
*/
_onThemeChanged: function (_, data) {
// Right now, gDevTools only emits `pref-changed` for the theme,
// but this could change in the future.
if (data.pref !== "devtools.theme") {
return;
}
this.emit(EVENTS.THEME_CHANGED, data.newValue);
_onThemeChanged: function () {
let newValue = Services.prefs.getCharPref("devtools.theme");
this.emit(EVENTS.THEME_CHANGED, newValue);
},
/**

View File

@ -7,9 +7,9 @@
const HTML_NS = "http://www.w3.org/1999/xhtml";
const Services = require("Services");
const {gDevTools} = require("devtools/client/framework/devtools");
const {HTMLTooltip} = require("devtools/client/shared/widgets/tooltip/HTMLTooltip");
const EventEmitter = require("devtools/shared/event-emitter");
const {PrefObserver} = require("devtools/client/shared/prefs");
let itemIdCounter = 0;
/**
@ -47,7 +47,9 @@ function AutocompletePopup(toolboxDoc, options = {}) {
this.autoThemeEnabled = true;
// Setup theme change listener.
this._handleThemeChange = this._handleThemeChange.bind(this);
gDevTools.on("pref-changed", this._handleThemeChange);
this._prefObserver = new PrefObserver("devtools.");
this._prefObserver.on("devtools.theme", this._handleThemeChange);
this._currentTheme = theme;
}
// Create HTMLTooltip instance
@ -194,7 +196,8 @@ AutocompletePopup.prototype = {
this._list.removeEventListener("click", this.onClick, false);
if (this.autoThemeEnabled) {
gDevTools.off("pref-changed", this._handleThemeChange);
this._prefObserver.off("devtools.theme", this._handleThemeChange);
this._prefObserver.destroy();
}
this._list.remove();
@ -562,25 +565,17 @@ AutocompletePopup.prototype = {
/**
* Manages theme switching for the popup based on the devtools.theme pref.
*
* @private
*
* @param {String} event
* The name of the event. In this case, "pref-changed".
* @param {Object} data
* An object passed by the emitter of the event. In this case, the
* object consists of three properties:
* - pref {String} The name of the preference that was modified.
* - newValue {Object} The new value of the preference.
* - oldValue {Object} The old value of the preference.
*/
_handleThemeChange: function (event, data) {
if (data.pref === "devtools.theme") {
this._tooltip.panel.classList.toggle(data.oldValue + "-theme", false);
this._tooltip.panel.classList.toggle(data.newValue + "-theme", true);
this._list.classList.toggle(data.oldValue + "-theme", false);
this._list.classList.toggle(data.newValue + "-theme", true);
}
_handleThemeChange: function () {
const oldValue = this._currentTheme;
const newValue = Services.prefs.getCharPref("devtools.theme");
this._tooltip.panel.classList.toggle(oldValue + "-theme", false);
this._tooltip.panel.classList.toggle(newValue + "-theme", true);
this._list.classList.toggle(oldValue + "-theme", false);
this._list.classList.toggle(newValue + "-theme", true);
this._currentTheme = newValue;
},
/**

View File

@ -7,6 +7,7 @@
// Tests that theme utilities work
const {getColor, getTheme, setTheme} = require("devtools/client/shared/theme");
const {PrefObserver} = require("devtools/client/shared/prefs");
add_task(function* () {
testGetTheme();
@ -31,13 +32,14 @@ function testGetTheme() {
function testSetTheme() {
let originalTheme = getTheme();
gDevTools.once("pref-changed", (_, { pref, oldValue, newValue }) => {
let prefObserver = new PrefObserver("devtools.");
prefObserver.once("devtools.theme", pref => {
is(pref, "devtools.theme",
"The 'pref-changed' event triggered by setTheme has correct pref.");
is(oldValue, originalTheme,
"The 'pref-changed' event triggered by setTheme has correct oldValue.");
"A preference event triggered by setTheme has correct pref.");
let newValue = Services.prefs.getCharPref("devtools.theme");
is(newValue, "dark",
"The 'pref-changed' event triggered by setTheme has correct newValue.");
"A preference event triggered by setTheme comes after the value is set.");
});
setTheme("dark");
is(Services.prefs.getCharPref("devtools.theme"), "dark",
@ -52,6 +54,8 @@ function testSetTheme() {
is(Services.prefs.getCharPref("devtools.theme"), "unknown",
"setTheme() correctly sets an unknown theme.");
Services.prefs.setCharPref("devtools.theme", originalTheme);
prefObserver.destroy();
}
function testGetColor() {

View File

@ -10,7 +10,6 @@
*/
const Services = require("Services");
const { gDevTools } = require("devtools/client/framework/devtools");
const variableFileContents = require("raw!devtools/client/themes/variables.css");
@ -67,18 +66,9 @@ const getColor = exports.getColor = (type, theme) => {
};
/**
* Mimics selecting the theme selector in the toolbox;
* sets the preference and emits an event on gDevTools to trigger
* the themeing.
* Set the theme preference.
*/
const setTheme = exports.setTheme = (newTheme) => {
let oldTheme = getTheme();
Services.prefs.setCharPref("devtools.theme", newTheme);
gDevTools.emit("pref-changed", {
pref: "devtools.theme",
newValue: newTheme,
oldValue: oldTheme
});
};
/* eslint-enable */

View File

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const {PrefObserver} = require("devtools/client/shared/prefs");
/**
* A collection of `AudioNodeModel`s used throughout the editor
* to keep track of audio nodes within the audio context.
@ -58,7 +60,9 @@ var WebAudioEditorController = {
// Hook into theme change so we can change
// the graph's marker styling, since we can't do this
// with CSS
gDevTools.on("pref-changed", this._onThemeChange);
this._prefObserver = new PrefObserver("");
this._prefObserver.on("devtools.theme", this._onThemeChange);
// Store the AudioNode definitions from the WebAudioFront, if the method exists.
// If not, get the JSON directly. Using the actor method is preferable so the client
@ -90,7 +94,8 @@ var WebAudioEditorController = {
gFront.off("disconnect-node", this._onDisconnectNode);
gFront.off("change-param", this._onChangeParam);
gFront.off("destroy-node", this._onDestroyNode);
gDevTools.off("pref-changed", this._onThemeChange);
this._prefObserver.off("devtools.theme", this._onThemeChange);
this._prefObserver.destroy();
},
/**
@ -129,8 +134,9 @@ var WebAudioEditorController = {
* so that the graph can update marker styling, as that
* cannot currently be done with CSS.
*/
_onThemeChange: function (event, data) {
window.emit(EVENTS.THEME_CHANGE, data.newValue);
_onThemeChange: function () {
let newValue = Services.prefs.getCharPref("devtools.theme");
window.emit(EVENTS.THEME_CHANGE, newValue);
},
/**

View File

@ -8,6 +8,8 @@
"use strict";
const {PrefObserver} = require("devtools/client/shared/prefs");
const TEST_URI = "data:text/html;charset=utf-8,Web Console test for " +
"bug 1307871 - preference for toggling timestamps in messages";
const PREF_MESSAGE_TIMESTAMP = "devtools.webconsole.timestampMessages";
@ -19,9 +21,11 @@ add_task(function* () {
testPrefDefaults(outputEl);
let observer = new PrefObserver("");
let toolbox = gDevTools.getToolbox(hud.target);
let optionsPanel = yield toolbox.selectTool("options");
yield togglePref(optionsPanel);
yield togglePref(optionsPanel, observer);
observer.destroy();
yield testChangedPref(outputEl);
@ -35,13 +39,11 @@ function testPrefDefaults(outputEl) {
"Messages should have no timestamp (class name check)");
}
function* togglePref(panel) {
function* togglePref(panel, observer) {
info("Options panel opened");
info("Changing pref");
let prefChanged = new Promise(resolve => {
gDevTools.once("pref-changed", resolve);
});
let prefChanged = observer.once(PREF_MESSAGE_TIMESTAMP, () => {});
let checkbox = panel.panelDoc.getElementById("webconsole-timestamp-messages");
checkbox.click();

View File

@ -8,6 +8,8 @@
"use strict";
const {PrefObserver} = require("devtools/client/shared/prefs");
const TEST_URI = "data:text/html;charset=utf-8,Web Console test for " +
"bug 722267 - preference for toggling timestamps in messages";
const PREF_MESSAGE_TIMESTAMP = "devtools.webconsole.timestampMessages";
@ -19,8 +21,10 @@ add_task(function* () {
hud = yield openConsole();
let panel = yield consoleOpened();
yield onOptionsPanelSelected(panel);
let observer = new PrefObserver("");
yield onOptionsPanelSelected(panel, observer);
onPrefChanged();
observer.destroy();
Services.prefs.clearUserPref(PREF_MESSAGE_TIMESTAMP);
hud = null;
@ -37,10 +41,10 @@ function consoleOpened() {
return toolbox.selectTool("options");
}
function onOptionsPanelSelected(panel) {
function onOptionsPanelSelected(panel, observer) {
info("options panel opened");
let prefChanged = gDevTools.once("pref-changed", onPrefChanged);
let prefChanged = observer.once(PREF_MESSAGE_TIMESTAMP, () => {});
let checkbox = panel.panelDoc.getElementById("webconsole-timestamp-messages");
checkbox.click();

View File

@ -17,6 +17,7 @@ Cu.import("resource://devtools/client/shared/browser-loader.js", BrowserLoaderMo
const promise = require("promise");
const Services = require("Services");
const Telemetry = require("devtools/client/shared/telemetry");
const {PrefObserver} = require("devtools/client/shared/prefs");
loader.lazyServiceGetter(this, "clipboardHelper",
"@mozilla.org/widget/clipboardhelper;1",
@ -636,11 +637,9 @@ WebConsoleFrame.prototype = {
});
// Toggle the timestamp on preference change
gDevTools.on("pref-changed", this._onToolboxPrefChanged);
this._onToolboxPrefChanged("pref-changed", {
pref: PREF_MESSAGE_TIMESTAMP,
newValue: Services.prefs.getBoolPref(PREF_MESSAGE_TIMESTAMP),
});
this._prefObserver = new PrefObserver("");
this._prefObserver.on(PREF_MESSAGE_TIMESTAMP, this._onToolboxPrefChanged);
this._onToolboxPrefChanged();
this._initShortcuts();
@ -2691,26 +2690,17 @@ WebConsoleFrame.prototype = {
},
/**
* Handler for the pref-changed event coming from the toolbox.
* Currently this function only handles the timestamps preferences.
*
* @private
* @param object event
* This parameter is a string that holds the event name
* pref-changed in this case.
* @param object data
* This is the pref-changed data object.
* Called when the message timestamp pref changes.
*/
_onToolboxPrefChanged: function (event, data) {
if (data.pref == PREF_MESSAGE_TIMESTAMP) {
_onToolboxPrefChanged: function () {
let newValue = Services.prefs.getBoolPref(PREF_MESSAGE_TIMESTAMP);
if (this.NEW_CONSOLE_OUTPUT_ENABLED) {
this.newConsoleOutput.dispatchTimestampsToggle(data.newValue);
} else if (data.newValue) {
this.newConsoleOutput.dispatchTimestampsToggle(newValue);
} else if (newValue) {
this.outputNode.classList.remove("hideTimestamps");
} else {
this.outputNode.classList.add("hideTimestamps");
}
}
},
/**
@ -2818,7 +2808,8 @@ WebConsoleFrame.prototype = {
toolbox.off("webconsole-selected", this._onPanelSelected);
}
gDevTools.off("pref-changed", this._onToolboxPrefChanged);
this._prefObserver.off(PREF_MESSAGE_TIMESTAMP, this._onToolboxPrefChanged);
this._prefObserver.destroy();
this.window.removeEventListener("resize", this.resize, true);
this._repeatNodes = {};