browser_console_consolejsm_output.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. // Test that Console.jsm outputs messages to the Browser Console, bug 851231.
  5. "use strict";
  6. function onNewMessage(aEvent, aNewMessages) {
  7. for (let msg of aNewMessages) {
  8. // Messages that shouldn't be output contain the substring FAIL_TEST
  9. if (msg.node.textContent.includes("FAIL_TEST")) {
  10. ok(false, "Message shouldn't have been output: " + msg.node.textContent);
  11. }
  12. }
  13. }
  14. add_task(function* () {
  15. let consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"];
  16. let storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage);
  17. storage.clearEvents();
  18. let {console} = Cu.import("resource://gre/modules/Console.jsm", {});
  19. console.log("bug861338-log-cached");
  20. let hud = yield HUDService.toggleBrowserConsole();
  21. yield waitForMessages({
  22. webconsole: hud,
  23. messages: [{
  24. name: "cached console.log message",
  25. text: "bug861338-log-cached",
  26. category: CATEGORY_WEBDEV,
  27. severity: SEVERITY_LOG,
  28. }],
  29. });
  30. hud.jsterm.clearOutput(true);
  31. function testTrace() {
  32. console.trace();
  33. }
  34. console.time("foobarTimer");
  35. let foobar = { bug851231prop: "bug851231value" };
  36. console.log("bug851231-log");
  37. console.info("bug851231-info");
  38. console.warn("bug851231-warn");
  39. console.error("bug851231-error", foobar);
  40. console.debug("bug851231-debug");
  41. console.dir(document);
  42. testTrace();
  43. console.timeEnd("foobarTimer");
  44. info("wait for the Console.jsm messages");
  45. let results = yield waitForMessages({
  46. webconsole: hud,
  47. messages: [
  48. {
  49. name: "console.log output",
  50. text: "bug851231-log",
  51. category: CATEGORY_WEBDEV,
  52. severity: SEVERITY_LOG,
  53. },
  54. {
  55. name: "console.info output",
  56. text: "bug851231-info",
  57. category: CATEGORY_WEBDEV,
  58. severity: SEVERITY_INFO,
  59. },
  60. {
  61. name: "console.warn output",
  62. text: "bug851231-warn",
  63. category: CATEGORY_WEBDEV,
  64. severity: SEVERITY_WARNING,
  65. },
  66. {
  67. name: "console.error output",
  68. text: /\bbug851231-error\b.+\{\s*bug851231prop:\s"bug851231value"\s*\}/,
  69. category: CATEGORY_WEBDEV,
  70. severity: SEVERITY_ERROR,
  71. objects: true,
  72. },
  73. {
  74. name: "console.debug output",
  75. text: "bug851231-debug",
  76. category: CATEGORY_WEBDEV,
  77. severity: SEVERITY_LOG,
  78. },
  79. {
  80. name: "console.trace output",
  81. consoleTrace: {
  82. file: "browser_console_consolejsm_output.js",
  83. fn: "testTrace",
  84. },
  85. },
  86. {
  87. name: "console.dir output",
  88. consoleDir: /XULDocument\s+.+\s+chrome:\/\/.+\/browser\.xul/,
  89. },
  90. {
  91. name: "console.time output",
  92. consoleTime: "foobarTimer",
  93. },
  94. {
  95. name: "console.timeEnd output",
  96. consoleTimeEnd: "foobarTimer",
  97. },
  98. ],
  99. });
  100. let consoleErrorMsg = results[3];
  101. ok(consoleErrorMsg, "console.error message element found");
  102. let clickable = consoleErrorMsg.clickableElements[0];
  103. ok(clickable, "clickable object found for console.error");
  104. let deferred = promise.defer();
  105. let onFetch = (aEvent, aVar) => {
  106. // Skip the notification from console.dir variablesview-fetched.
  107. if (aVar._variablesView != hud.jsterm._variablesView) {
  108. return;
  109. }
  110. hud.jsterm.off("variablesview-fetched", onFetch);
  111. deferred.resolve(aVar);
  112. };
  113. hud.jsterm.on("variablesview-fetched", onFetch);
  114. clickable.scrollIntoView(false);
  115. info("wait for variablesview-fetched");
  116. executeSoon(() =>
  117. EventUtils.synthesizeMouse(clickable, 2, 2, {}, hud.iframeWindow));
  118. let varView = yield deferred.promise;
  119. ok(varView, "object inspector opened on click");
  120. yield findVariableViewProperties(varView, [{
  121. name: "bug851231prop",
  122. value: "bug851231value",
  123. }], { webconsole: hud });
  124. yield HUDService.toggleBrowserConsole();
  125. });
  126. add_task(function* testPrefix() {
  127. let consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"];
  128. let storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage);
  129. storage.clearEvents();
  130. let {ConsoleAPI} = Cu.import("resource://gre/modules/Console.jsm", {});
  131. let consoleOptions = {
  132. maxLogLevel: "error",
  133. prefix: "Log Prefix",
  134. };
  135. let console2 = new ConsoleAPI(consoleOptions);
  136. console2.error("Testing a prefix");
  137. console2.log("FAIL_TEST: Below the maxLogLevel");
  138. let hud = yield HUDService.toggleBrowserConsole();
  139. hud.ui.on("new-messages", onNewMessage);
  140. yield waitForMessages({
  141. webconsole: hud,
  142. messages: [{
  143. name: "cached console.error message",
  144. prefix: "Log Prefix:",
  145. severity: SEVERITY_ERROR,
  146. text: "Testing a prefix",
  147. }],
  148. });
  149. hud.jsterm.clearOutput(true);
  150. hud.ui.off("new-messages", onNewMessage);
  151. yield HUDService.toggleBrowserConsole();
  152. });
  153. add_task(function* testMaxLogLevelPrefMissing() {
  154. let consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"];
  155. let storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage);
  156. storage.clearEvents();
  157. let {ConsoleAPI} = Cu.import("resource://gre/modules/Console.jsm", {});
  158. let consoleOptions = {
  159. maxLogLevel: "error",
  160. maxLogLevelPref: "testing.maxLogLevel",
  161. };
  162. let console = new ConsoleAPI(consoleOptions);
  163. is(Services.prefs.getPrefType(consoleOptions.maxLogLevelPref),
  164. Services.prefs.PREF_INVALID,
  165. "Check log level pref is missing");
  166. // Since the maxLogLevelPref doesn't exist, we should fallback to the passed
  167. // maxLogLevel of "error".
  168. console.warn("FAIL_TEST: Below the maxLogLevel");
  169. console.error("Error should be shown");
  170. let hud = yield HUDService.toggleBrowserConsole();
  171. hud.ui.on("new-messages", onNewMessage);
  172. yield waitForMessages({
  173. webconsole: hud,
  174. messages: [{
  175. name: "defaulting to error level",
  176. severity: SEVERITY_ERROR,
  177. text: "Error should be shown",
  178. }],
  179. });
  180. hud.jsterm.clearOutput(true);
  181. hud.ui.off("new-messages", onNewMessage);
  182. yield HUDService.toggleBrowserConsole();
  183. });
  184. add_task(function* testMaxLogLevelPref() {
  185. let consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"];
  186. let storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage);
  187. storage.clearEvents();
  188. let {ConsoleAPI} = Cu.import("resource://gre/modules/Console.jsm", {});
  189. let consoleOptions = {
  190. maxLogLevel: "error",
  191. maxLogLevelPref: "testing.maxLogLevel",
  192. };
  193. info("Setting the pref to warn");
  194. Services.prefs.setCharPref(consoleOptions.maxLogLevelPref, "Warn");
  195. let console = new ConsoleAPI(consoleOptions);
  196. is(console.maxLogLevel, "warn", "Check pref was read at initialization");
  197. console.info("FAIL_TEST: info is below the maxLogLevel");
  198. console.error("Error should be shown");
  199. console.warn("Warn should be shown due to the initial pref value");
  200. info("Setting the pref to info");
  201. Services.prefs.setCharPref(consoleOptions.maxLogLevelPref, "INFO");
  202. is(console.maxLogLevel, "info", "Check pref was lowercased");
  203. console.info("info should be shown due to the pref change being observed");
  204. info("Clearing the pref");
  205. Services.prefs.clearUserPref(consoleOptions.maxLogLevelPref);
  206. console.warn("FAIL_TEST: Shouldn't be shown due to defaulting to error");
  207. console.error("Should be shown due to defaulting to error");
  208. let hud = yield HUDService.toggleBrowserConsole();
  209. hud.ui.on("new-messages", onNewMessage);
  210. yield waitForMessages({
  211. webconsole: hud,
  212. messages: [{
  213. name: "error > warn",
  214. severity: SEVERITY_ERROR,
  215. text: "Error should be shown",
  216. },
  217. {
  218. name: "warn is the inital pref value",
  219. severity: SEVERITY_WARNING,
  220. text: "Warn should be shown due to the initial pref value",
  221. },
  222. {
  223. name: "pref changed to info",
  224. severity: SEVERITY_INFO,
  225. text: "info should be shown due to the pref change being observed",
  226. },
  227. {
  228. name: "default to intial maxLogLevel if pref is removed",
  229. severity: SEVERITY_ERROR,
  230. text: "Should be shown due to defaulting to error",
  231. }],
  232. });
  233. hud.jsterm.clearOutput(true);
  234. hud.ui.off("new-messages", onNewMessage);
  235. yield HUDService.toggleBrowserConsole();
  236. });