addon-sdk/source/test/test-deprecate.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:3ed65239c9a8
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
5 'use strict';
6
7 const deprecate = require("sdk/util/deprecate");
8 const { LoaderWithHookedConsole } = require("sdk/test/loader");
9 const { get, set } = require("sdk/preferences/service");
10 const PREFERENCE = "devtools.errorconsole.deprecation_warnings";
11
12 exports["test Deprecate Usage"] = function testDeprecateUsage(assert) {
13 set(PREFERENCE, true);
14 let { loader, messages } = LoaderWithHookedConsole(module);
15 let deprecate = loader.require("sdk/util/deprecate");
16
17 function functionIsDeprecated() {
18 deprecate.deprecateUsage("foo");
19 }
20
21 functionIsDeprecated();
22
23 assert.equal(messages.length, 1, "only one error is dispatched");
24 assert.equal(messages[0].type, "error", "the console message is an error");
25
26 let msg = messages[0].msg;
27
28 assert.ok(msg.indexOf("foo") !== -1,
29 "message contains the given message");
30 assert.ok(msg.indexOf("functionIsDeprecated") !== -1,
31 "message contains name of the caller function");
32 assert.ok(msg.indexOf(module.uri) !== -1,
33 "message contains URI of the caller module");
34
35 loader.unload();
36 }
37
38 exports["test Deprecate Function"] = function testDeprecateFunction(assert) {
39 set(PREFERENCE, true);
40 let { loader, messages } = LoaderWithHookedConsole(module);
41 let deprecate = loader.require("sdk/util/deprecate");
42
43 let self = {};
44 let arg1 = "foo";
45 let arg2 = {};
46
47 function originalFunction(a1, a2) {
48 assert.equal(this, self);
49 assert.equal(a1, arg1);
50 assert.equal(a2, arg2);
51 };
52
53 let deprecateFunction = deprecate.deprecateFunction(originalFunction,
54 "bar");
55
56 deprecateFunction.call(self, arg1, arg2);
57
58 assert.equal(messages.length, 1, "only one error is dispatched");
59 assert.equal(messages[0].type, "error", "the console message is an error");
60
61 let msg = messages[0].msg;
62 assert.ok(msg.indexOf("bar") !== -1, "message contains the given message");
63 assert.ok(msg.indexOf("testDeprecateFunction") !== -1,
64 "message contains name of the caller function");
65 assert.ok(msg.indexOf(module.uri) !== -1,
66 "message contains URI of the caller module");
67
68 loader.unload();
69 }
70
71 exports.testDeprecateEvent = function(assert, done) {
72 set(PREFERENCE, true);
73 let { loader, messages } = LoaderWithHookedConsole(module);
74 let deprecate = loader.require("sdk/util/deprecate");
75
76 let { on, emit } = loader.require('sdk/event/core');
77 let testObj = {};
78 testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);
79
80 testObj.on('fire', function() {
81 testObj.on('water', function() {
82 assert.equal(messages.length, 1, "only one error is dispatched");
83 loader.unload();
84 done();
85 })
86 assert.equal(messages.length, 1, "only one error is dispatched");
87 emit(testObj, 'water');
88 });
89 assert.equal(messages.length, 1, "only one error is dispatched");
90 assert.equal(messages[0].type, "error", "the console message is an error");
91 let msg = messages[0].msg;
92 assert.ok(msg.indexOf("BAD") !== -1, "message contains the given message");
93 assert.ok(msg.indexOf("deprecateEvent") !== -1,
94 "message contains name of the caller function");
95 assert.ok(msg.indexOf(module.uri) !== -1,
96 "message contains URI of the caller module");
97
98 emit(testObj, 'fire');
99 }
100
101 exports.testDeprecateSettingToggle = function (assert, done) {
102 let { loader, messages } = LoaderWithHookedConsole(module);
103 let deprecate = loader.require("sdk/util/deprecate");
104
105 function fn () { deprecate.deprecateUsage("foo"); }
106
107 set(PREFERENCE, false);
108 fn();
109 assert.equal(messages.length, 0, 'no deprecation warnings');
110
111 set(PREFERENCE, true);
112 fn();
113 assert.equal(messages.length, 1, 'deprecation warnings when toggled');
114
115 set(PREFERENCE, false);
116 fn();
117 assert.equal(messages.length, 1, 'no new deprecation warnings');
118 done();
119 };
120
121 exports.testDeprecateSetting = function (assert, done) {
122 // Set devtools.errorconsole.deprecation_warnings to false
123 set(PREFERENCE, false);
124
125 let { loader, messages } = LoaderWithHookedConsole(module);
126 let deprecate = loader.require("sdk/util/deprecate");
127
128 // deprecateUsage test
129 function functionIsDeprecated() {
130 deprecate.deprecateUsage("foo");
131 }
132 functionIsDeprecated();
133
134 assert.equal(messages.length, 0,
135 "no errors dispatched on deprecateUsage");
136
137 // deprecateFunction test
138 function originalFunction() {};
139
140 let deprecateFunction = deprecate.deprecateFunction(originalFunction,
141 "bar");
142 deprecateFunction();
143
144 assert.equal(messages.length, 0,
145 "no errors dispatched on deprecateFunction");
146
147 // deprecateEvent
148 let { on, emit } = loader.require('sdk/event/core');
149 let testObj = {};
150 testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);
151
152 testObj.on('fire', () => {
153 assert.equal(messages.length, 0,
154 "no errors dispatched on deprecateEvent");
155 done();
156 });
157
158 emit(testObj, 'fire');
159 }
160 require("test").run(exports);

mercurial