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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-plain-text-console.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,270 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const prefs = require("sdk/preferences/service");
     1.9 +const { id, name } = require("sdk/self");
    1.10 +const { Cc, Cu, Ci } = require("chrome");
    1.11 +const { loadSubScript } = Cc['@mozilla.org/moz/jssubscript-loader;1'].
    1.12 +                     getService(Ci.mozIJSSubScriptLoader);
    1.13 +
    1.14 +const ADDON_LOG_LEVEL_PREF = "extensions." + id + ".sdk.console.logLevel";
    1.15 +const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel";
    1.16 +
    1.17 +const HAS_ORIGINAL_ADDON_LOG_LEVEL = prefs.has(ADDON_LOG_LEVEL_PREF);
    1.18 +const ORIGINAL_ADDON_LOG_LEVEL = prefs.get(ADDON_LOG_LEVEL_PREF);
    1.19 +const HAS_ORIGINAL_SDK_LOG_LEVEL = prefs.has(SDK_LOG_LEVEL_PREF);
    1.20 +const ORIGINAL_SDK_LOG_LEVEL = prefs.get(SDK_LOG_LEVEL_PREF);
    1.21 +
    1.22 +exports.testPlainTextConsole = function(assert) {
    1.23 +  let prints = [];
    1.24 +  function print(message) {
    1.25 +    prints.push(message);
    1.26 +  }
    1.27 +  function lastPrint() {
    1.28 +    let last = prints.slice(-1)[0];
    1.29 +    prints = [];
    1.30 +    return last;
    1.31 +  }
    1.32 +
    1.33 +  prefs.set(SDK_LOG_LEVEL_PREF, "all");
    1.34 +  prefs.reset(ADDON_LOG_LEVEL_PREF);
    1.35 +
    1.36 +  let Console = require("sdk/console/plain-text").PlainTextConsole;
    1.37 +  let con = new Console(print);
    1.38 +
    1.39 +  assert.ok("PlainTextConsole instantiates");
    1.40 +
    1.41 +  con.log('testing', 1, [2, 3, 4]);
    1.42 +  assert.equal(lastPrint(), "console.log: " + name + ": testing 1 Array [2,3,4]\n",
    1.43 +                   "PlainTextConsole.log() must work.");
    1.44 +
    1.45 +  con.info('testing', 1, [2, 3, 4]);
    1.46 +  assert.equal(lastPrint(), "console.info: " + name + ": testing 1 Array [2,3,4]\n",
    1.47 +                   "PlainTextConsole.info() must work.");
    1.48 +
    1.49 +  con.warn('testing', 1, [2, 3, 4]);
    1.50 +  assert.equal(lastPrint(), "console.warn: " + name + ": testing 1 Array [2,3,4]\n",
    1.51 +                   "PlainTextConsole.warn() must work.");
    1.52 +
    1.53 +  con.error('testing', 1, [2, 3, 4]);
    1.54 +  assert.equal(prints[0], "console.error: " + name + ": \n",
    1.55 +                   "PlainTextConsole.error() must work.");
    1.56 +  assert.equal(prints[1], "  testing\n")
    1.57 +  assert.equal(prints[2], "  1\n")
    1.58 +  assert.equal(prints[3], "Array\n    - 0 = 2\n    - 1 = 3\n    - 2 = 4\n    - length = 3\n");
    1.59 +  prints = [];
    1.60 +
    1.61 +  con.debug('testing', 1, [2, 3, 4]);
    1.62 +  assert.equal(prints[0], "console.debug: " + name + ": \n",
    1.63 +                   "PlainTextConsole.debug() must work.");
    1.64 +  assert.equal(prints[1], "  testing\n")
    1.65 +  assert.equal(prints[2], "  1\n")
    1.66 +  assert.equal(prints[3], "Array\n    - 0 = 2\n    - 1 = 3\n    - 2 = 4\n    - length = 3\n");
    1.67 +  prints = [];
    1.68 +
    1.69 +  con.log('testing', undefined);
    1.70 +  assert.equal(lastPrint(), "console.log: " + name + ": testing undefined\n",
    1.71 +                   "PlainTextConsole.log() must stringify undefined.");
    1.72 +
    1.73 +  con.log('testing', null);
    1.74 +  assert.equal(lastPrint(), "console.log: " + name + ": testing null\n",
    1.75 +                   "PlainTextConsole.log() must stringify null.");
    1.76 +
    1.77 +  // TODO: Fix console.jsm to detect custom toString.
    1.78 +  con.log("testing", { toString: function() "obj.toString()" });
    1.79 +  assert.equal(lastPrint(), "console.log: " + name + ": testing {}\n",
    1.80 +                   "PlainTextConsole.log() doesn't printify custom toString.");
    1.81 +
    1.82 +  con.log("testing", { toString: function() { throw "fail!"; } });
    1.83 +  assert.equal(lastPrint(), "console.log: " + name + ": testing {}\n",
    1.84 +                   "PlainTextConsole.log() must stringify custom bad toString.");
    1.85 +
    1.86 +
    1.87 +  con.exception(new Error("blah"));
    1.88 +
    1.89 +
    1.90 +  assert.equal(prints[0], "console.error: " + name + ": \n");
    1.91 +  let tbLines = prints[1].split("\n");
    1.92 +  assert.equal(tbLines[0], "  Message: Error: blah");
    1.93 +  assert.equal(tbLines[1], "  Stack:");
    1.94 +  assert.ok(prints[1].indexOf(module.uri + ":84") !== -1);
    1.95 +  prints = []
    1.96 +
    1.97 +  try {
    1.98 +    loadSubScript("invalid-url", {});
    1.99 +    assert.fail("successed in calling loadSubScript with invalid-url");
   1.100 +  }
   1.101 +  catch(e) {
   1.102 +    con.exception(e);
   1.103 +  }
   1.104 +  assert.equal(prints[0], "console.error: " + name + ": \n");
   1.105 +  assert.equal(prints[1], "  Error creating URI (invalid URL scheme?)\n");
   1.106 +  prints = [];
   1.107 +
   1.108 +  con.trace();
   1.109 +  let tbLines = prints[0].split("\n");
   1.110 +  assert.equal(tbLines[0], "console.trace: " + name + ": ");
   1.111 +  assert.ok(tbLines[1].indexOf("_ain-text-console.js 105") == 0);
   1.112 +  prints = [];
   1.113 +
   1.114 +  // Whether or not console methods should print at the various log levels,
   1.115 +  // structured as a hash of levels, each of which contains a hash of methods,
   1.116 +  // each of whose value is whether or not it should print, i.e.:
   1.117 +  // { [level]: { [method]: [prints?], ... }, ... }.
   1.118 +  let levels = {
   1.119 +    all:   { debug: true,  log: true,  info: true,  warn: true,  error: true  },
   1.120 +    debug: { debug: true,  log: true,  info: true,  warn: true,  error: true  },
   1.121 +    info:  { debug: false, log: true,  info: true,  warn: true,  error: true  },
   1.122 +    warn:  { debug: false, log: false, info: false, warn: true,  error: true  },
   1.123 +    error: { debug: false, log: false, info: false, warn: false, error: true  },
   1.124 +    off:   { debug: false, log: false, info: false, warn: false, error: false },
   1.125 +  };
   1.126 +
   1.127 +  // The messages we use to test the various methods, as a hash of methods.
   1.128 +  let messages = {
   1.129 +    debug: "console.debug: " + name + ": \n  \n",
   1.130 +    log: "console.log: " + name + ": \n",
   1.131 +    info: "console.info: " + name + ": \n",
   1.132 +    warn: "console.warn: " + name + ": \n",
   1.133 +    error: "console.error: " + name + ": \n  \n",
   1.134 +  };
   1.135 +
   1.136 +  for (let level in levels) {
   1.137 +    let methods = levels[level];
   1.138 +    for (let method in methods) {
   1.139 +      // We have to reset the log level pref each time we run the test
   1.140 +      // because the test runner relies on the console to print test output,
   1.141 +      // and test results would not get printed to the console for some
   1.142 +      // values of the pref.
   1.143 +      prefs.set(SDK_LOG_LEVEL_PREF, level);
   1.144 +      con[method]("");
   1.145 +      prefs.set(SDK_LOG_LEVEL_PREF, "all");
   1.146 +      assert.equal(prints.join(""),
   1.147 +                       (methods[method] ? messages[method] : ""),
   1.148 +                       "at log level '" + level + "', " + method + "() " +
   1.149 +                       (methods[method] ? "prints" : "doesn't print"));
   1.150 +      prints = [];
   1.151 +    }
   1.152 +  }
   1.153 +
   1.154 +  prefs.set(SDK_LOG_LEVEL_PREF, "off");
   1.155 +  prefs.set(ADDON_LOG_LEVEL_PREF, "all");
   1.156 +  con.debug("");
   1.157 +  assert.equal(prints.join(""), messages["debug"],
   1.158 +                   "addon log level 'all' overrides SDK log level 'off'");
   1.159 +  prints = [];
   1.160 +
   1.161 +  prefs.set(SDK_LOG_LEVEL_PREF, "all");
   1.162 +  prefs.set(ADDON_LOG_LEVEL_PREF, "off");
   1.163 +  con.error("");
   1.164 +  prefs.reset(ADDON_LOG_LEVEL_PREF);
   1.165 +  assert.equal(lastPrint(), null,
   1.166 +                   "addon log level 'off' overrides SDK log level 'all'");
   1.167 +
   1.168 +  restorePrefs();
   1.169 +};
   1.170 +
   1.171 +exports.testPlainTextConsoleBoundMethods = function(assert) {
   1.172 +  let prints = [];
   1.173 +  function print(message) {
   1.174 +    prints.push(message);
   1.175 +  }
   1.176 +  function lastPrint() {
   1.177 +    let last = prints.slice(-1)[0];
   1.178 +    prints = [];
   1.179 +    return last;
   1.180 +  }
   1.181 +
   1.182 +  prefs.set(SDK_LOG_LEVEL_PREF, "all");
   1.183 +  prefs.reset(ADDON_LOG_LEVEL_PREF);
   1.184 +
   1.185 +  let Console = require("sdk/console/plain-text").PlainTextConsole;
   1.186 +  let { log, info, warn, error, debug, exception, trace } = new Console(print);
   1.187 +
   1.188 +  assert.ok("PlainTextConsole instantiates");
   1.189 +
   1.190 +  log('testing', 1, [2, 3, 4]);
   1.191 +  assert.equal(lastPrint(), "console.log: " + name + ": testing 1 Array [2,3,4]\n",
   1.192 +                   "PlainTextConsole.log() must work.");
   1.193 +
   1.194 +  info('testing', 1, [2, 3, 4]);
   1.195 +  assert.equal(lastPrint(), "console.info: " + name + ": testing 1 Array [2,3,4]\n",
   1.196 +                   "PlainTextConsole.info() must work.");
   1.197 +
   1.198 +  warn('testing', 1, [2, 3, 4]);
   1.199 +  assert.equal(lastPrint(), "console.warn: " + name + ": testing 1 Array [2,3,4]\n",
   1.200 +                   "PlainTextConsole.warn() must work.");
   1.201 +
   1.202 +  error('testing', 1, [2, 3, 4]);
   1.203 +  assert.equal(prints[0], "console.error: " + name + ": \n",
   1.204 +                   "PlainTextConsole.error() must work.");
   1.205 +  assert.equal(prints[1], "  testing\n")
   1.206 +  assert.equal(prints[2], "  1\n")
   1.207 +  assert.equal(prints[3], "Array\n    - 0 = 2\n    - 1 = 3\n    - 2 = 4\n    - length = 3\n");
   1.208 +  prints = [];
   1.209 +
   1.210 +  debug('testing', 1, [2, 3, 4]);
   1.211 +  assert.equal(prints[0], "console.debug: " + name + ": \n",
   1.212 +                   "PlainTextConsole.debug() must work.");
   1.213 +  assert.equal(prints[1], "  testing\n")
   1.214 +  assert.equal(prints[2], "  1\n")
   1.215 +  assert.equal(prints[3], "Array\n    - 0 = 2\n    - 1 = 3\n    - 2 = 4\n    - length = 3\n");
   1.216 +  prints = [];
   1.217 +
   1.218 +  exception(new Error("blah"));
   1.219 +
   1.220 +  assert.equal(prints[0], "console.error: " + name + ": \n");
   1.221 +  let tbLines = prints[1].split("\n");
   1.222 +  assert.equal(tbLines[0], "  Message: Error: blah");
   1.223 +  assert.equal(tbLines[1], "  Stack:");
   1.224 +  assert.ok(prints[1].indexOf(module.uri + ":215") !== -1);
   1.225 +  prints = []
   1.226 +
   1.227 +  trace();
   1.228 +  let tbLines = prints[0].split("\n");
   1.229 +  assert.equal(tbLines[0], "console.trace: " + name + ": ");
   1.230 +  assert.ok(tbLines[1].indexOf("_ain-text-console.js 224") === 0);
   1.231 +  prints = [];
   1.232 +
   1.233 +  restorePrefs();
   1.234 +};
   1.235 +
   1.236 +exports.testConsoleInnerID = function(assert) {
   1.237 +  let Console = require("sdk/console/plain-text").PlainTextConsole;
   1.238 +  let { log, info, warn, error, debug, exception, trace } = new Console(function() {}, "test ID");
   1.239 +
   1.240 +  let messages = [];
   1.241 +  function onMessage({ subject }) {
   1.242 +    let message = subject.wrappedJSObject;
   1.243 +    messages.push({ msg: message.arguments[0], type: message.level, innerID: message.innerID });
   1.244 +  }
   1.245 +
   1.246 +  const system = require("sdk/system/events");
   1.247 +  system.on("console-api-log-event", onMessage);
   1.248 +
   1.249 +  log("Test log");
   1.250 +  warn("Test warning");
   1.251 +  error("Test error");
   1.252 +
   1.253 +  assert.equal(messages.length, 3, "Should see 3 log events");
   1.254 +  assert.deepEqual(messages[0], { msg: "Test log", type: "log", innerID: "test ID" }, "Should see the right event");
   1.255 +  assert.deepEqual(messages[1], { msg: "Test warning", type: "warn", innerID: "test ID" }, "Should see the right event");
   1.256 +  assert.deepEqual(messages[2], { msg: "Test error", type: "error", innerID: "test ID" }, "Should see the right event");
   1.257 +
   1.258 +  system.off("console-api-log-event", onMessage);
   1.259 +};
   1.260 +
   1.261 +function restorePrefs() {
   1.262 +  if (HAS_ORIGINAL_ADDON_LOG_LEVEL)
   1.263 +    prefs.set(ADDON_LOG_LEVEL_PREF, ORIGINAL_ADDON_LOG_LEVEL);
   1.264 +  else
   1.265 +    prefs.reset(ADDON_LOG_LEVEL_PREF);
   1.266 +
   1.267 +  if (HAS_ORIGINAL_SDK_LOG_LEVEL)
   1.268 +    prefs.set(SDK_LOG_LEVEL_PREF, ORIGINAL_SDK_LOG_LEVEL);
   1.269 +  else
   1.270 +    prefs.reset(SDK_LOG_LEVEL_PREF);
   1.271 +}
   1.272 +
   1.273 +require("test").run(exports);

mercurial