summaryrefslogtreecommitdiff
path: root/addon-sdk/source/test/test-deprecate.js
blob: c1bd443c614f779e615be3b0ad35d1e99b52c728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const deprecate = require("sdk/util/deprecate");
const { LoaderWithHookedConsole } = require("sdk/test/loader");
const { get, set } = require("sdk/preferences/service");
const PREFERENCE = "devtools.errorconsole.deprecation_warnings";

exports["test Deprecate Usage"] = function testDeprecateUsage(assert) {
  set(PREFERENCE, true);
  let { loader, messages } = LoaderWithHookedConsole(module);
  let deprecate = loader.require("sdk/util/deprecate");

  function functionIsDeprecated() {
    deprecate.deprecateUsage("foo");
  }

  functionIsDeprecated();

  assert.equal(messages.length, 1, "only one error is dispatched");
  assert.equal(messages[0].type, "error", "the console message is an error");

  let msg = messages[0].msg;

  assert.ok(msg.indexOf("foo") !== -1,
            "message contains the given message");
  assert.ok(msg.indexOf("functionIsDeprecated") !== -1,
            "message contains name of the caller function");
  assert.ok(msg.indexOf(module.uri) !== -1,
             "message contains URI of the caller module");

  loader.unload();
}

exports["test Deprecate Function"] = function testDeprecateFunction(assert) {
  set(PREFERENCE, true);
  let { loader, messages } = LoaderWithHookedConsole(module);
  let deprecate = loader.require("sdk/util/deprecate");

  let self = {};
  let arg1 = "foo";
  let arg2 = {};

  function originalFunction(a1, a2) {
    assert.equal(this, self);
    assert.equal(a1, arg1);
    assert.equal(a2, arg2);
  };

  let deprecateFunction = deprecate.deprecateFunction(originalFunction,
                                                       "bar");

  deprecateFunction.call(self, arg1, arg2);

  assert.equal(messages.length, 1, "only one error is dispatched");
  assert.equal(messages[0].type, "error", "the console message is an error");

  let msg = messages[0].msg;
  assert.ok(msg.indexOf("bar") !== -1, "message contains the given message");
  assert.ok(msg.indexOf("testDeprecateFunction") !== -1,
            "message contains name of the caller function");
  assert.ok(msg.indexOf(module.uri) !== -1,
            "message contains URI of the caller module");

  loader.unload();
}

exports.testDeprecateEvent = function(assert, done) {
  set(PREFERENCE, true);
  let { loader, messages } = LoaderWithHookedConsole(module);
  let deprecate = loader.require("sdk/util/deprecate");

  let { on, emit } = loader.require('sdk/event/core');
  let testObj = {};
  testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);

  testObj.on('fire', function() {
    testObj.on('water', function() {
      assert.equal(messages.length, 1, "only one error is dispatched");
      loader.unload();
      done();
    })
    assert.equal(messages.length, 1, "only one error is dispatched");
    emit(testObj, 'water');
  });

  assert.equal(messages.length, 1, "only one error is dispatched");
  assert.equal(messages[0].type, "error", "the console message is an error");
  let msg = messages[0].msg;
  assert.ok(msg.indexOf("BAD") !== -1, "message contains the given message");
  assert.ok(msg.indexOf("deprecateEvent") !== -1,
            "message contains name of the caller function");
  assert.ok(msg.indexOf(module.uri) !== -1,
            "message contains URI of the caller module");

  emit(testObj, 'fire');
}

exports.testDeprecateSettingToggle = function (assert) {
  let { loader, messages } = LoaderWithHookedConsole(module);
  let deprecate = loader.require("sdk/util/deprecate");

  function fn () { deprecate.deprecateUsage("foo"); }

  set(PREFERENCE, false);
  fn();
  assert.equal(messages.length, 0, 'no deprecation warnings');

  set(PREFERENCE, true);
  fn();
  assert.equal(messages.length, 1, 'deprecation warnings when toggled');

  set(PREFERENCE, false);
  fn();
  assert.equal(messages.length, 1, 'no new deprecation warnings');
};

exports.testDeprecateSetting = function (assert, done) {
  // Set devtools.errorconsole.deprecation_warnings to false
  set(PREFERENCE, false);

  let { loader, messages } = LoaderWithHookedConsole(module);
  let deprecate = loader.require("sdk/util/deprecate");

  // deprecateUsage test
  function functionIsDeprecated() {
    deprecate.deprecateUsage("foo");
  }
  functionIsDeprecated();

  assert.equal(messages.length, 0,
    "no errors dispatched on deprecateUsage");

  // deprecateFunction test
  function originalFunction() {};

  let deprecateFunction = deprecate.deprecateFunction(originalFunction,
                                                       "bar");
  deprecateFunction();

  assert.equal(messages.length, 0,
    "no errors dispatched on deprecateFunction");

  // deprecateEvent
  let { on, emit } = loader.require('sdk/event/core');
  let testObj = {};
  testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);

  testObj.on('fire', () => {
    assert.equal(messages.length, 0,
      "no errors dispatched on deprecateEvent");
    done();
  });

  emit(testObj, 'fire');
}

require("sdk/test").run(exports);