Bug 1805728 - Stop using Cu.reportError in Deprecated.sys.mjs. r=mossop

Differential Revision: https://phabricator.services.mozilla.com/D164715
This commit is contained in:
Mark Banner 2022-12-18 10:10:48 +00:00
Родитель 168b779eae
Коммит d0d17578a8
4 изменённых файлов: 73 добавлений и 87 удалений

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

@ -2102,7 +2102,6 @@ module.exports = {
"toolkit/content/tests/chrome/window_browser_drop.xhtml",
"toolkit/modules/tests/browser/browser_AsyncPrefs.js",
"toolkit/modules/tests/browser/browser_BrowserUtils.js",
"toolkit/modules/tests/browser/browser_Deprecated.js",
"toolkit/modules/tests/browser/browser_web_channel.js",
"toolkit/modules/tests/browser/file_web_channel_iframe.html",
"toolkit/modules/tests/chrome/test_bug544442_checkCert.xhtml",

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

@ -95,6 +95,33 @@ export var TestUtils = {
});
},
/**
* Listens for any console messages (logged via console.*) and returns them
* when the returned function is called.
*
* @returns {function}
* Returns an async function that when called will wait for a tick, then stop
* listening to any more console messages and finally will return the
* messages that have been received.
*/
listenForConsoleMessages() {
let messages = [];
function observe(message) {
messages.push(message);
}
ConsoleAPIStorage.addLogEventListener(
observe,
Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal)
);
return async () => {
await TestUtils.waitForTick();
ConsoleAPIStorage.removeLogEventListener(observe);
return messages;
};
},
/**
* Waits for the specified topic to be observed.
*

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

@ -60,7 +60,7 @@ export var Deprecated = {
// If URL is not provided, report an error.
if (!aUrl) {
Cu.reportError(
console.error(
"Error in Deprecated.warning: warnings must " +
"provide a URL documenting this deprecation."
);
@ -77,6 +77,6 @@ export var Deprecated = {
stringifyCallstack(aStack);
// Report deprecation warning.
Cu.reportError(textMessage);
console.error(textMessage);
},
};

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

@ -11,12 +11,12 @@ const { Deprecated } = ChromeUtils.importESModule(
// Using this named functions to test deprecation and the properly logged
// callstacks.
function basicDeprecatedFunction() {
Deprecated.warning("this method is deprecated.", "http://example.com");
Deprecated.warning("this method is deprecated.", "https://example.com");
return true;
}
function deprecationFunctionBogusCallstack() {
Deprecated.warning("this method is deprecated.", "http://example.com", {
Deprecated.warning("this method is deprecated.", "https://example.com", {
caller: {},
});
return true;
@ -29,7 +29,7 @@ function deprecationFunctionCustomCallstack() {
}
Deprecated.warning(
"this method is deprecated.",
"http://example.com",
"https://example.com",
getStack()
);
return true;
@ -42,7 +42,7 @@ var tests = [
expectedObservation(aMessage) {
testAMessage(aMessage);
ok(
aMessage.errorMessage.indexOf("basicDeprecatedFunction") > 0,
aMessage.indexOf("basicDeprecatedFunction") > 0,
"Callstack is correctly logged."
);
},
@ -55,7 +55,7 @@ var tests = [
},
expectedObservation(aMessage) {
ok(
aMessage.errorMessage.indexOf("must provide a URL") > 0,
aMessage.indexOf("must provide a URL") > 0,
"Deprecation warning logged an empty URL argument."
);
},
@ -67,25 +67,18 @@ var tests = [
expectedObservation(aMessage) {
testAMessage(aMessage);
ok(
aMessage.errorMessage.indexOf("deprecationFunctionBogusCallstack") > 0,
aMessage.indexOf("deprecationFunctionBogusCallstack") > 0,
"Callstack is correctly logged."
);
},
},
// When pref is unset Deprecated.warning should not log anything.
{
deprecatedFunction: basicDeprecatedFunction,
expectedObservation: null,
// Set pref to false.
logWarnings: false,
},
// Test deprecation with a valid custom callstack passed as an argument.
{
deprecatedFunction: deprecationFunctionCustomCallstack,
expectedObservation(aMessage) {
testAMessage(aMessage);
ok(
aMessage.errorMessage.indexOf("deprecationFunctionCustomCallstack") > 0,
aMessage.indexOf("deprecationFunctionCustomCallstack") > 0,
"Callstack is correctly logged."
);
},
@ -94,82 +87,49 @@ var tests = [
},
];
// Which test are we running now?
var idx = -1;
// Test Console Message attributes.
function testAMessage(aMessage) {
ok(
aMessage.indexOf("DEPRECATION WARNING: this method is deprecated.") === 0,
"Deprecation is correctly logged."
);
ok(aMessage.indexOf("https://example.com") > 0, "URL is correctly logged.");
}
function test() {
waitForExplicitFinish();
add_task(async function test_setup() {
Services.prefs.setBoolPref(PREF_DEPRECATION_WARNINGS, true);
// Check if Deprecated is loaded.
ok(Deprecated, "Deprecated object exists");
});
nextTest();
}
add_task(async function test_pref_enabled() {
for (let [idx, test] of tests.entries()) {
info("Running test #" + idx);
// Test Consle Message attributes.
function testAMessage(aMessage) {
ok(
aMessage.errorMessage.indexOf(
"DEPRECATION WARNING: this method is deprecated."
) === 0,
"Deprecation is correctly logged."
);
ok(
aMessage.errorMessage.indexOf("http://example.com") > 0,
"URL is correctly logged."
);
}
function nextTest() {
idx++;
if (idx == tests.length) {
finish();
return;
}
info("Running test #" + idx);
let test = tests[idx];
// Deprecation warnings will be logged only when the preference is set.
if (typeof test.logWarnings !== "undefined") {
Services.prefs.setBoolPref(PREF_DEPRECATION_WARNINGS, test.logWarnings);
}
// Create a console listener.
let consoleListener = {
observe(aMessage) {
// Ignore unexpected messages.
if (!(aMessage instanceof Ci.nsIScriptError)) {
return;
}
if (
!aMessage.errorMessage.includes("DEPRECATION WARNING: ") &&
!aMessage.errorMessage.includes("must provide a URL")
) {
return;
}
ok(
aMessage instanceof Ci.nsIScriptError,
"Deprecation log message is an instance of type nsIScriptError."
let promiseObserved = TestUtils.consoleMessageObserved(subject => {
let msg = subject.wrappedJSObject.arguments?.[0];
return (
msg.includes("DEPRECATION WARNING: ") ||
msg.includes("must provide a URL")
);
if (test.expectedObservation === null) {
ok(false, "Deprecated warning not expected");
} else {
test.expectedObservation(aMessage);
}
Services.console.unregisterListener(consoleListener);
executeSoon(nextTest);
},
};
Services.console.registerListener(consoleListener);
test.deprecatedFunction();
if (test.expectedObservation === null) {
executeSoon(function() {
Services.console.unregisterListener(consoleListener);
executeSoon(nextTest);
});
test.deprecatedFunction();
let msg = await promiseObserved;
test.expectedObservation(msg.wrappedJSObject.arguments?.[0]);
}
}
});
add_task(async function test_pref_disabled() {
// Deprecation warnings will be logged only when the preference is set.
Services.prefs.setBoolPref(PREF_DEPRECATION_WARNINGS, false);
let endFn = TestUtils.listenForConsoleMessages();
basicDeprecatedFunction();
let messages = await endFn();
Assert.equal(messages.length, 0, "Should not have received any messages");
});