addon-sdk/source/test/test-plain-text-console.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial