toolkit/modules/tests/browser/browser_Deprecated.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/tests/browser/browser_Deprecated.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,157 @@
     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 Ci = Components.interfaces;
     1.9 +const Cu = Components.utils;
    1.10 +const PREF_DEPRECATION_WARNINGS = "devtools.errorconsole.deprecation_warnings";
    1.11 +
    1.12 +Cu.import("resource://gre/modules/Services.jsm", this);
    1.13 +Cu.import("resource://gre/modules/Deprecated.jsm", this);
    1.14 +
    1.15 +// Using this named functions to test deprecation and the properly logged
    1.16 +// callstacks.
    1.17 +function basicDeprecatedFunction () {
    1.18 +  Deprecated.warning("this method is deprecated.", "http://example.com");
    1.19 +  return true;
    1.20 +}
    1.21 +
    1.22 +function deprecationFunctionBogusCallstack () {
    1.23 +  Deprecated.warning("this method is deprecated.", "http://example.com", {
    1.24 +    caller: {}
    1.25 +  });
    1.26 +  return true;
    1.27 +}
    1.28 +
    1.29 +function deprecationFunctionCustomCallstack () {
    1.30 +  // Get the nsIStackFrame that will contain the name of this function.
    1.31 +  function getStack () {
    1.32 +    return Components.stack;
    1.33 +  }
    1.34 +  Deprecated.warning("this method is deprecated.", "http://example.com",
    1.35 +    getStack());
    1.36 +  return true;
    1.37 +}
    1.38 +
    1.39 +let tests = [
    1.40 +// Test deprecation warning without passing the callstack.
    1.41 +{
    1.42 +  deprecatedFunction: basicDeprecatedFunction,
    1.43 +  expectedObservation: function (aMessage) {
    1.44 +    testAMessage(aMessage);
    1.45 +    ok(aMessage.errorMessage.indexOf("basicDeprecatedFunction") > 0,
    1.46 +      "Callstack is correctly logged.");
    1.47 +  }
    1.48 +},
    1.49 +// Test a reported error when URL to documentation is not passed.
    1.50 +{
    1.51 +  deprecatedFunction: function () {
    1.52 +    Deprecated.warning("this method is deprecated.");
    1.53 +    return true;
    1.54 +  },
    1.55 +  expectedObservation: function (aMessage) {
    1.56 +    ok(aMessage.errorMessage.indexOf("must provide a URL") > 0,
    1.57 +      "Deprecation warning logged an empty URL argument.");
    1.58 +  }
    1.59 +},
    1.60 +// Test deprecation with a bogus callstack passed as an argument (it will be
    1.61 +// replaced with the current call stack).
    1.62 +{
    1.63 +  deprecatedFunction: deprecationFunctionBogusCallstack,
    1.64 +  expectedObservation: function (aMessage) {
    1.65 +    testAMessage(aMessage);
    1.66 +    ok(aMessage.errorMessage.indexOf("deprecationFunctionBogusCallstack") > 0,
    1.67 +      "Callstack is correctly logged.");
    1.68 +  }
    1.69 +},
    1.70 +// When pref is unset Deprecated.warning should not log anything.
    1.71 +{
    1.72 +  deprecatedFunction: basicDeprecatedFunction,
    1.73 +  expectedObservation: null,
    1.74 +  // Set pref to false.
    1.75 +  logWarnings: false
    1.76 +},
    1.77 +// Test deprecation with a valid custom callstack passed as an argument.
    1.78 +{
    1.79 +  deprecatedFunction: deprecationFunctionCustomCallstack,
    1.80 +  expectedObservation: function (aMessage) {
    1.81 +    testAMessage(aMessage);
    1.82 +    ok(aMessage.errorMessage.indexOf("deprecationFunctionCustomCallstack") > 0,
    1.83 +      "Callstack is correctly logged.");
    1.84 +  },
    1.85 +  // Set pref to true.
    1.86 +  logWarnings: true
    1.87 +}];
    1.88 +
    1.89 +// Which test are we running now?
    1.90 +let idx = -1;
    1.91 +
    1.92 +function test() {
    1.93 +  waitForExplicitFinish();
    1.94 +
    1.95 +  // Check if Deprecated is loaded.
    1.96 +  ok(Deprecated, "Deprecated object exists");
    1.97 +
    1.98 +  nextTest();
    1.99 +}
   1.100 +
   1.101 +// Test Consle Message attributes.
   1.102 +function testAMessage (aMessage) {
   1.103 +  ok(aMessage.errorMessage.indexOf("DEPRECATION WARNING: " +
   1.104 +    "this method is deprecated.") === 0,
   1.105 +    "Deprecation is correctly logged.");
   1.106 +  ok(aMessage.errorMessage.indexOf("http://example.com") > 0,
   1.107 +    "URL is correctly logged.");
   1.108 +}
   1.109 +
   1.110 +function nextTest() {
   1.111 +  idx++;
   1.112 +
   1.113 +  if (idx == tests.length) {
   1.114 +    finish();
   1.115 +    return;
   1.116 +  }
   1.117 +
   1.118 +  info("Running test #" + idx);
   1.119 +  let test = tests[idx];
   1.120 +
   1.121 +  // Deprecation warnings will be logged only when the preference is set.
   1.122 +  if (typeof test.logWarnings !== "undefined") {
   1.123 +    Services.prefs.setBoolPref(PREF_DEPRECATION_WARNINGS, test.logWarnings);
   1.124 +  }
   1.125 +
   1.126 +  // Create a console listener.
   1.127 +  let consoleListener = {
   1.128 +    observe: function (aMessage) {
   1.129 +      // Ignore unexpected messages.
   1.130 +      if (!(aMessage instanceof Ci.nsIScriptError)) {
   1.131 +        return;
   1.132 +      }
   1.133 +      if (aMessage.errorMessage.indexOf("DEPRECATION WARNING: ") < 0 &&
   1.134 +          aMessage.errorMessage.indexOf("must provide a URL") < 0) {
   1.135 +        return;
   1.136 +      }
   1.137 +      ok(aMessage instanceof Ci.nsIScriptError,
   1.138 +        "Deprecation log message is an instance of type nsIScriptError.");
   1.139 +
   1.140 +
   1.141 +      if (test.expectedObservation === null) {
   1.142 +        ok(false, "Deprecated warning not expected");
   1.143 +      }
   1.144 +      else {
   1.145 +        test.expectedObservation(aMessage);
   1.146 +      }
   1.147 +
   1.148 +      Services.console.unregisterListener(consoleListener);
   1.149 +      executeSoon(nextTest);
   1.150 +    }
   1.151 +  };
   1.152 +  Services.console.registerListener(consoleListener);
   1.153 +  test.deprecatedFunction();
   1.154 +  if (test.expectedObservation === null) {
   1.155 +    executeSoon(function() {
   1.156 +      Services.console.unregisterListener(consoleListener);
   1.157 +      executeSoon(nextTest);
   1.158 +    });
   1.159 +  }
   1.160 +}

mercurial