browser_console_consolejsm_output.js (4501B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that Console.sys.mjs outputs messages to the Browser Console. 5 6 "use strict"; 7 8 add_task(async function testCategoryLogs() { 9 const consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"]; 10 const storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage); 11 storage.clearEvents(); 12 13 console.log("bug861338-log-cached"); 14 15 const hud = await BrowserConsoleManager.toggleBrowserConsole(); 16 17 await checkMessageExists(hud, "bug861338-log-cached"); 18 19 await clearOutput(hud); 20 21 function testTrace() { 22 console.trace(); 23 } 24 25 console.time("foobarTimer"); 26 const foobar = { bug851231prop: "bug851231value" }; 27 28 console.log("bug851231-log"); 29 console.info("bug851231-info"); 30 console.warn("bug851231-warn"); 31 console.error("bug851231-error", foobar); 32 console.debug("bug851231-debug"); 33 console.dir({ "bug851231-dir": 1 }); 34 testTrace(); 35 console.timeEnd("foobarTimer"); 36 37 info("wait for the Console.sys.mjs messages"); 38 39 await checkMessageExists(hud, "bug851231-log"); 40 await checkMessageExists(hud, "bug851231-info"); 41 await checkMessageExists(hud, "bug851231-warn"); 42 await checkMessageExists(hud, "bug851231-error"); 43 await checkMessageExists(hud, "bug851231-debug"); 44 await checkMessageExists(hud, "bug851231-dir"); 45 await checkMessageExists(hud, "console.trace()"); 46 await checkMessageExists(hud, "foobarTimer"); 47 48 await clearOutput(hud); 49 await BrowserConsoleManager.toggleBrowserConsole(); 50 }); 51 52 add_task(async function testFilter() { 53 const consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"]; 54 const storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage); 55 storage.clearEvents(); 56 57 const { ConsoleAPI } = ChromeUtils.importESModule( 58 "resource://gre/modules/Console.sys.mjs" 59 ); 60 const console2 = new ConsoleAPI(); 61 const hud = await BrowserConsoleManager.toggleBrowserConsole(); 62 63 // Enable the error category and disable the log category. 64 await setFilterState(hud, { 65 error: true, 66 log: false, 67 }); 68 69 const shouldBeVisible = "Should be visible"; 70 const shouldBeHidden = "Should be hidden"; 71 72 console2.log(shouldBeHidden); 73 console2.error(shouldBeVisible); 74 75 await checkMessageExists(hud, shouldBeVisible); 76 // Here we can safely assert that the log message is not visible, since the 77 // error message was logged after and is visible. 78 await checkMessageHidden(hud, shouldBeHidden); 79 80 await resetFilters(hud); 81 await clearOutput(hud); 82 }); 83 84 // Test that console.profile / profileEnd trigger the right events 85 add_task(async function testProfile() { 86 const consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"]; 87 const storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage); 88 const { ConsoleAPI } = ChromeUtils.importESModule( 89 "resource://gre/modules/Console.sys.mjs" 90 ); 91 const console = new ConsoleAPI(); 92 93 storage.clearEvents(); 94 95 const profilerEvents = []; 96 97 function observer(subject, topic) { 98 is(topic, "console-api-profiler", "The topic is 'console-api-profiler'"); 99 const subjectObj = subject.wrappedJSObject; 100 const event = { action: subjectObj.action, name: subjectObj.arguments[0] }; 101 info(`Profiler event: action=${event.action}, name=${event.name}`); 102 profilerEvents.push(event); 103 } 104 105 Services.obs.addObserver(observer, "console-api-profiler"); 106 107 console.profile("test"); 108 console.profileEnd("test"); 109 110 Services.obs.removeObserver(observer, "console-api-profiler"); 111 112 // Test that no messages were logged to the storage 113 const consoleEvents = storage.getEvents(); 114 is(consoleEvents.length, 0, "There are zero logged messages"); 115 116 // Test that two profiler events were fired 117 is(profilerEvents.length, 2, "Got two profiler events"); 118 is(profilerEvents[0].action, "profile", "First event has the right action"); 119 is(profilerEvents[0].name, "test", "First event has the right name"); 120 is( 121 profilerEvents[1].action, 122 "profileEnd", 123 "Second event has the right action" 124 ); 125 is(profilerEvents[1].name, "test", "Second event has the right name"); 126 }); 127 128 async function checkMessageExists(hud, msg) { 129 info(`Checking "${msg}" was logged`); 130 const message = await waitFor(() => findConsoleAPIMessage(hud, msg)); 131 ok(message, `"${msg}" was logged`); 132 } 133 134 async function checkMessageHidden(hud, msg) { 135 info(`Checking "${msg}" was not logged`); 136 await waitFor(() => findConsoleAPIMessage(hud, msg) == null); 137 ok(true, `"${msg}" was not logged`); 138 }