diff --git a/.eslintrc.js b/.eslintrc.js index 1faae1042a4f..8c1d510a3796 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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", diff --git a/testing/modules/TestUtils.sys.mjs b/testing/modules/TestUtils.sys.mjs index 3537e0816039..c3daacbceb56 100644 --- a/testing/modules/TestUtils.sys.mjs +++ b/testing/modules/TestUtils.sys.mjs @@ -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. * diff --git a/toolkit/modules/Deprecated.sys.mjs b/toolkit/modules/Deprecated.sys.mjs index cd430b9676e6..5c3a0dae18cc 100644 --- a/toolkit/modules/Deprecated.sys.mjs +++ b/toolkit/modules/Deprecated.sys.mjs @@ -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); }, }; diff --git a/toolkit/modules/tests/browser/browser_Deprecated.js b/toolkit/modules/tests/browser/browser_Deprecated.js index 1b915a797203..0f0645527fe0 100644 --- a/toolkit/modules/tests/browser/browser_Deprecated.js +++ b/toolkit/modules/tests/browser/browser_Deprecated.js @@ -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"); +});