test-plain-text-console.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const prefs = require("sdk/preferences/service");
  5. const { id, name } = require("sdk/self");
  6. const { Cc, Cu, Ci } = require("chrome");
  7. const { loadSubScript } = Cc['@mozilla.org/moz/jssubscript-loader;1'].
  8. getService(Ci.mozIJSSubScriptLoader);
  9. const ADDON_LOG_LEVEL_PREF = "extensions." + id + ".sdk.console.logLevel";
  10. const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel";
  11. const HAS_ORIGINAL_ADDON_LOG_LEVEL = prefs.has(ADDON_LOG_LEVEL_PREF);
  12. const ORIGINAL_ADDON_LOG_LEVEL = prefs.get(ADDON_LOG_LEVEL_PREF);
  13. const HAS_ORIGINAL_SDK_LOG_LEVEL = prefs.has(SDK_LOG_LEVEL_PREF);
  14. const ORIGINAL_SDK_LOG_LEVEL = prefs.get(SDK_LOG_LEVEL_PREF);
  15. exports.testPlainTextConsole = function(assert) {
  16. let prints = [];
  17. function print(message) {
  18. prints.push(message);
  19. }
  20. function lastPrint() {
  21. let last = prints.slice(-1)[0];
  22. prints = [];
  23. return last;
  24. }
  25. prefs.set(SDK_LOG_LEVEL_PREF, "all");
  26. prefs.reset(ADDON_LOG_LEVEL_PREF);
  27. let Console = require("sdk/console/plain-text").PlainTextConsole;
  28. let con = new Console(print);
  29. assert.ok("PlainTextConsole instantiates");
  30. con.log('testing', 1, [2, 3, 4]);
  31. assert.equal(lastPrint(), "console.log: " + name + ": testing 1 Array [2,3,4]\n",
  32. "PlainTextConsole.log() must work.");
  33. con.info('testing', 1, [2, 3, 4]);
  34. assert.equal(lastPrint(), "console.info: " + name + ": testing 1 Array [2,3,4]\n",
  35. "PlainTextConsole.info() must work.");
  36. con.warn('testing', 1, [2, 3, 4]);
  37. assert.equal(lastPrint(), "console.warn: " + name + ": testing 1 Array [2,3,4]\n",
  38. "PlainTextConsole.warn() must work.");
  39. con.error('testing', 1, [2, 3, 4]);
  40. assert.equal(prints[0], "console.error: " + name + ": \n",
  41. "PlainTextConsole.error() must work.");
  42. assert.equal(prints[1], " testing\n")
  43. assert.equal(prints[2], " 1\n")
  44. assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
  45. prints = [];
  46. con.debug('testing', 1, [2, 3, 4]);
  47. assert.equal(prints[0], "console.debug: " + name + ": \n",
  48. "PlainTextConsole.debug() must work.");
  49. assert.equal(prints[1], " testing\n")
  50. assert.equal(prints[2], " 1\n")
  51. assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
  52. prints = [];
  53. con.log('testing', undefined);
  54. assert.equal(lastPrint(), "console.log: " + name + ": testing undefined\n",
  55. "PlainTextConsole.log() must stringify undefined.");
  56. con.log('testing', null);
  57. assert.equal(lastPrint(), "console.log: " + name + ": testing null\n",
  58. "PlainTextConsole.log() must stringify null.");
  59. // TODO: Fix console.jsm to detect custom toString.
  60. con.log("testing", { toString: function() "obj.toString()" });
  61. assert.equal(lastPrint(), "console.log: " + name + ": testing {}\n",
  62. "PlainTextConsole.log() doesn't printify custom toString.");
  63. con.log("testing", { toString: function() { throw "fail!"; } });
  64. assert.equal(lastPrint(), "console.log: " + name + ": testing {}\n",
  65. "PlainTextConsole.log() must stringify custom bad toString.");
  66. con.exception(new Error("blah"));
  67. assert.equal(prints[0], "console.error: " + name + ": \n");
  68. let tbLines = prints[1].split("\n");
  69. assert.equal(tbLines[0], " Message: Error: blah");
  70. assert.equal(tbLines[1], " Stack:");
  71. assert.ok(prints[1].indexOf(module.uri + ":84") !== -1);
  72. prints = []
  73. try {
  74. loadSubScript("invalid-url", {});
  75. assert.fail("successed in calling loadSubScript with invalid-url");
  76. }
  77. catch(e) {
  78. con.exception(e);
  79. }
  80. assert.equal(prints[0], "console.error: " + name + ": \n");
  81. assert.equal(prints[1], " Error creating URI (invalid URL scheme?)\n");
  82. prints = [];
  83. con.trace();
  84. let tbLines = prints[0].split("\n");
  85. assert.equal(tbLines[0], "console.trace: " + name + ": ");
  86. assert.ok(tbLines[1].indexOf("_ain-text-console.js 105") == 0);
  87. prints = [];
  88. // Whether or not console methods should print at the various log levels,
  89. // structured as a hash of levels, each of which contains a hash of methods,
  90. // each of whose value is whether or not it should print, i.e.:
  91. // { [level]: { [method]: [prints?], ... }, ... }.
  92. let levels = {
  93. all: { debug: true, log: true, info: true, warn: true, error: true },
  94. debug: { debug: true, log: true, info: true, warn: true, error: true },
  95. info: { debug: false, log: true, info: true, warn: true, error: true },
  96. warn: { debug: false, log: false, info: false, warn: true, error: true },
  97. error: { debug: false, log: false, info: false, warn: false, error: true },
  98. off: { debug: false, log: false, info: false, warn: false, error: false },
  99. };
  100. // The messages we use to test the various methods, as a hash of methods.
  101. let messages = {
  102. debug: "console.debug: " + name + ": \n \n",
  103. log: "console.log: " + name + ": \n",
  104. info: "console.info: " + name + ": \n",
  105. warn: "console.warn: " + name + ": \n",
  106. error: "console.error: " + name + ": \n \n",
  107. };
  108. for (let level in levels) {
  109. let methods = levels[level];
  110. for (let method in methods) {
  111. // We have to reset the log level pref each time we run the test
  112. // because the test runner relies on the console to print test output,
  113. // and test results would not get printed to the console for some
  114. // values of the pref.
  115. prefs.set(SDK_LOG_LEVEL_PREF, level);
  116. con[method]("");
  117. prefs.set(SDK_LOG_LEVEL_PREF, "all");
  118. assert.equal(prints.join(""),
  119. (methods[method] ? messages[method] : ""),
  120. "at log level '" + level + "', " + method + "() " +
  121. (methods[method] ? "prints" : "doesn't print"));
  122. prints = [];
  123. }
  124. }
  125. prefs.set(SDK_LOG_LEVEL_PREF, "off");
  126. prefs.set(ADDON_LOG_LEVEL_PREF, "all");
  127. con.debug("");
  128. assert.equal(prints.join(""), messages["debug"],
  129. "addon log level 'all' overrides SDK log level 'off'");
  130. prints = [];
  131. prefs.set(SDK_LOG_LEVEL_PREF, "all");
  132. prefs.set(ADDON_LOG_LEVEL_PREF, "off");
  133. con.error("");
  134. prefs.reset(ADDON_LOG_LEVEL_PREF);
  135. assert.equal(lastPrint(), null,
  136. "addon log level 'off' overrides SDK log level 'all'");
  137. restorePrefs();
  138. };
  139. exports.testPlainTextConsoleBoundMethods = function(assert) {
  140. let prints = [];
  141. function print(message) {
  142. prints.push(message);
  143. }
  144. function lastPrint() {
  145. let last = prints.slice(-1)[0];
  146. prints = [];
  147. return last;
  148. }
  149. prefs.set(SDK_LOG_LEVEL_PREF, "all");
  150. prefs.reset(ADDON_LOG_LEVEL_PREF);
  151. let Console = require("sdk/console/plain-text").PlainTextConsole;
  152. let { log, info, warn, error, debug, exception, trace } = new Console(print);
  153. assert.ok("PlainTextConsole instantiates");
  154. log('testing', 1, [2, 3, 4]);
  155. assert.equal(lastPrint(), "console.log: " + name + ": testing 1 Array [2,3,4]\n",
  156. "PlainTextConsole.log() must work.");
  157. info('testing', 1, [2, 3, 4]);
  158. assert.equal(lastPrint(), "console.info: " + name + ": testing 1 Array [2,3,4]\n",
  159. "PlainTextConsole.info() must work.");
  160. warn('testing', 1, [2, 3, 4]);
  161. assert.equal(lastPrint(), "console.warn: " + name + ": testing 1 Array [2,3,4]\n",
  162. "PlainTextConsole.warn() must work.");
  163. error('testing', 1, [2, 3, 4]);
  164. assert.equal(prints[0], "console.error: " + name + ": \n",
  165. "PlainTextConsole.error() must work.");
  166. assert.equal(prints[1], " testing\n")
  167. assert.equal(prints[2], " 1\n")
  168. assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
  169. prints = [];
  170. debug('testing', 1, [2, 3, 4]);
  171. assert.equal(prints[0], "console.debug: " + name + ": \n",
  172. "PlainTextConsole.debug() must work.");
  173. assert.equal(prints[1], " testing\n")
  174. assert.equal(prints[2], " 1\n")
  175. assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
  176. prints = [];
  177. exception(new Error("blah"));
  178. assert.equal(prints[0], "console.error: " + name + ": \n");
  179. let tbLines = prints[1].split("\n");
  180. assert.equal(tbLines[0], " Message: Error: blah");
  181. assert.equal(tbLines[1], " Stack:");
  182. assert.ok(prints[1].indexOf(module.uri + ":215") !== -1);
  183. prints = []
  184. trace();
  185. let tbLines = prints[0].split("\n");
  186. assert.equal(tbLines[0], "console.trace: " + name + ": ");
  187. assert.ok(tbLines[1].indexOf("_ain-text-console.js 224") === 0);
  188. prints = [];
  189. restorePrefs();
  190. };
  191. function restorePrefs() {
  192. if (HAS_ORIGINAL_ADDON_LOG_LEVEL)
  193. prefs.set(ADDON_LOG_LEVEL_PREF, ORIGINAL_ADDON_LOG_LEVEL);
  194. else
  195. prefs.reset(ADDON_LOG_LEVEL_PREF);
  196. if (HAS_ORIGINAL_SDK_LOG_LEVEL)
  197. prefs.set(SDK_LOG_LEVEL_PREF, ORIGINAL_SDK_LOG_LEVEL);
  198. else
  199. prefs.reset(SDK_LOG_LEVEL_PREF);
  200. }
  201. require("test").run(exports);