diff options
Diffstat (limited to 'dom/console/tests/xpcshell')
-rw-r--r-- | dom/console/tests/xpcshell/head.js | 22 | ||||
-rw-r--r-- | dom/console/tests/xpcshell/test_basic.js | 29 | ||||
-rw-r--r-- | dom/console/tests/xpcshell/test_console_shouldLog.js | 35 | ||||
-rw-r--r-- | dom/console/tests/xpcshell/test_failing_console_listener.js | 35 | ||||
-rw-r--r-- | dom/console/tests/xpcshell/test_formatting.js | 77 | ||||
-rw-r--r-- | dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js | 42 | ||||
-rw-r--r-- | dom/console/tests/xpcshell/xpcshell.toml | 13 |
7 files changed, 253 insertions, 0 deletions
diff --git a/dom/console/tests/xpcshell/head.js b/dom/console/tests/xpcshell/head.js new file mode 100644 index 0000000000..446eb9ba9e --- /dev/null +++ b/dom/console/tests/xpcshell/head.js @@ -0,0 +1,22 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService( + Ci.nsIConsoleAPIStorage +); + +// This is intended to just be a drop-in replacement for an old observer +// notification. +function addConsoleStorageListener(listener) { + listener.__handler = (message, id) => { + listener.observe(message, id); + }; + ConsoleAPIStorage.addLogEventListener( + listener.__handler, + Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal) + ); +} + +function removeConsoleStorageListener(listener) { + ConsoleAPIStorage.removeLogEventListener(listener.__handler); +} diff --git a/dom/console/tests/xpcshell/test_basic.js b/dom/console/tests/xpcshell/test_basic.js new file mode 100644 index 0000000000..5736912979 --- /dev/null +++ b/dom/console/tests/xpcshell/test_basic.js @@ -0,0 +1,29 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +add_task(async function () { + Assert.ok("console" in this); + + let p = new Promise(resolve => { + function consoleListener() { + addConsoleStorageListener(this); + } + + consoleListener.prototype = { + observe(aSubject) { + let obj = aSubject.wrappedJSObject; + Assert.ok(obj.arguments[0] === 42, "Message received!"); + Assert.ok(obj.ID === "jsm", "The ID is JSM"); + Assert.ok(obj.innerID.endsWith("test_basic.js"), "The innerID matches"); + + removeConsoleStorageListener(this); + resolve(); + }, + }; + + new consoleListener(); + }); + + console.log(42); + await p; +}); diff --git a/dom/console/tests/xpcshell/test_console_shouldLog.js b/dom/console/tests/xpcshell/test_console_shouldLog.js new file mode 100644 index 0000000000..9e78c474db --- /dev/null +++ b/dom/console/tests/xpcshell/test_console_shouldLog.js @@ -0,0 +1,35 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +add_task(async function test_shouldLog_maxLogLevel() { + let ci = console.createInstance({ maxLogLevel: "Warn" }); + + Assert.ok( + ci.shouldLog("Error"), + "Should return true for logging a higher level" + ); + Assert.ok( + ci.shouldLog("Warn"), + "Should return true for logging the same level" + ); + Assert.ok( + !ci.shouldLog("Debug"), + "Should return false for logging a lower level;" + ); +}); + +add_task(async function test_shouldLog_maxLogLevelPref() { + Services.prefs.setStringPref("test.log", "Warn"); + let ci = console.createInstance({ maxLogLevelPref: "test.log" }); + + Assert.ok( + !ci.shouldLog("Debug"), + "Should return false for logging a lower level;" + ); + + Services.prefs.setStringPref("test.log", "Debug"); + Assert.ok( + ci.shouldLog("Debug"), + "Should return true for logging a lower level after pref update" + ); +}); diff --git a/dom/console/tests/xpcshell/test_failing_console_listener.js b/dom/console/tests/xpcshell/test_failing_console_listener.js new file mode 100644 index 0000000000..bb1b7cd46c --- /dev/null +++ b/dom/console/tests/xpcshell/test_failing_console_listener.js @@ -0,0 +1,35 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +add_task(async function () { + Assert.ok("console" in this); + + // Add a first listener that synchronously throws. + const removeListener1 = addConsoleListener(() => { + throw new Error("Fail"); + }); + + // Add a second listener updating a flag we can observe from the test. + let secondListenerCalled = false; + const removeListener2 = addConsoleListener( + () => (secondListenerCalled = true) + ); + + console.log(42); + Assert.ok(secondListenerCalled, "Second listener was called"); + + // Cleanup listeners. + removeListener1(); + removeListener2(); +}); + +function addConsoleListener(callback) { + const principal = Cc["@mozilla.org/systemprincipal;1"].createInstance( + Ci.nsIPrincipal + ); + ConsoleAPIStorage.addLogEventListener(callback, principal); + + return () => { + ConsoleAPIStorage.removeLogEventListener(callback, principal); + }; +} diff --git a/dom/console/tests/xpcshell/test_formatting.js b/dom/console/tests/xpcshell/test_formatting.js new file mode 100644 index 0000000000..89d5a1947e --- /dev/null +++ b/dom/console/tests/xpcshell/test_formatting.js @@ -0,0 +1,77 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +add_task(async function () { + Assert.ok("console" in this); + + const tests = [ + // Plain value. + [[42], ["42"]], + + // Integers. + [["%d", 42], ["42"]], + [["%i", 42], ["42"]], + [["c%iao", 42], ["c42ao"]], + + // Floats. + [["%2.4f", 42], ["42.0000"]], + [["%2.2f", 42], ["42.00"]], + [["%1.2f", 42], ["42.00"]], + [["a%3.2fb", 42], ["a42.00b"]], + [["%f", NaN], ["NaN"]], + + // Strings + [["%s", 42], ["42"]], + + // Empty values. + [ + ["", 42], + ["", "42"], + ], + [ + ["", 42], + ["", "42"], + ], + ]; + + let p = new Promise(resolve => { + let t = 0; + + function consoleListener() { + addConsoleStorageListener(this); + } + + consoleListener.prototype = { + observe(aSubject) { + let test = tests[t++]; + + let obj = aSubject.wrappedJSObject; + Assert.equal( + obj.arguments.length, + test[1].length, + "Same number of arguments" + ); + for (let i = 0; i < test[1].length; ++i) { + Assert.equal( + "" + obj.arguments[i], + test[1][i], + "Message received: " + test[1][i] + ); + } + + if (t === tests.length) { + removeConsoleStorageListener(this); + resolve(); + } + }, + }; + + new consoleListener(); + }); + + tests.forEach(test => { + console.log(...test[0]); + }); + + await p; +}); diff --git a/dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js b/dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js new file mode 100644 index 0000000000..f96519183c --- /dev/null +++ b/dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js @@ -0,0 +1,42 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +add_task(async function () { + let p = new Promise(resolve => { + function consoleListener() { + addConsoleStorageListener(this); + } + + consoleListener.prototype = { + observe(aSubject) { + let obj = aSubject.wrappedJSObject; + Assert.ok(obj.arguments[0] === "Hello world!", "Message received!"); + Assert.ok(obj.ID === "scope", "The ID is the scope"); + Assert.ok( + obj.innerID === "ServiceWorker", + "The innerID is ServiceWorker" + ); + Assert.ok(obj.filename === "filename", "The filename matches"); + Assert.ok(obj.lineNumber === 42, "The lineNumber matches"); + Assert.ok(obj.columnNumber === 24, "The columnNumber matches"); + Assert.ok(obj.level === "error", "The level is correct"); + + removeConsoleStorageListener(this); + resolve(); + }, + }; + + new consoleListener(); + }); + + let ci = console.createInstance(); + ci.reportForServiceWorkerScope( + "scope", + "Hello world!", + "filename", + 42, + 24, + "error" + ); + await p; +}); diff --git a/dom/console/tests/xpcshell/xpcshell.toml b/dom/console/tests/xpcshell/xpcshell.toml new file mode 100644 index 0000000000..c5cc54d665 --- /dev/null +++ b/dom/console/tests/xpcshell/xpcshell.toml @@ -0,0 +1,13 @@ +[DEFAULT] +head = "head.js" +support-files = "" + +["test_basic.js"] + +["test_console_shouldLog.js"] + +["test_failing_console_listener.js"] + +["test_formatting.js"] + +["test_reportForServiceWorkerScope.js"] |