Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 'use strict';
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";
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");
17 function functionIsDeprecated() {
18 deprecate.deprecateUsage("foo");
19 }
21 functionIsDeprecated();
23 assert.equal(messages.length, 1, "only one error is dispatched");
24 assert.equal(messages[0].type, "error", "the console message is an error");
26 let msg = messages[0].msg;
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");
35 loader.unload();
36 }
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");
43 let self = {};
44 let arg1 = "foo";
45 let arg2 = {};
47 function originalFunction(a1, a2) {
48 assert.equal(this, self);
49 assert.equal(a1, arg1);
50 assert.equal(a2, arg2);
51 };
53 let deprecateFunction = deprecate.deprecateFunction(originalFunction,
54 "bar");
56 deprecateFunction.call(self, arg1, arg2);
58 assert.equal(messages.length, 1, "only one error is dispatched");
59 assert.equal(messages[0].type, "error", "the console message is an error");
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");
68 loader.unload();
69 }
71 exports.testDeprecateEvent = function(assert, done) {
72 set(PREFERENCE, true);
73 let { loader, messages } = LoaderWithHookedConsole(module);
74 let deprecate = loader.require("sdk/util/deprecate");
76 let { on, emit } = loader.require('sdk/event/core');
77 let testObj = {};
78 testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);
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");
98 emit(testObj, 'fire');
99 }
101 exports.testDeprecateSettingToggle = function (assert, done) {
102 let { loader, messages } = LoaderWithHookedConsole(module);
103 let deprecate = loader.require("sdk/util/deprecate");
105 function fn () { deprecate.deprecateUsage("foo"); }
107 set(PREFERENCE, false);
108 fn();
109 assert.equal(messages.length, 0, 'no deprecation warnings');
111 set(PREFERENCE, true);
112 fn();
113 assert.equal(messages.length, 1, 'deprecation warnings when toggled');
115 set(PREFERENCE, false);
116 fn();
117 assert.equal(messages.length, 1, 'no new deprecation warnings');
118 done();
119 };
121 exports.testDeprecateSetting = function (assert, done) {
122 // Set devtools.errorconsole.deprecation_warnings to false
123 set(PREFERENCE, false);
125 let { loader, messages } = LoaderWithHookedConsole(module);
126 let deprecate = loader.require("sdk/util/deprecate");
128 // deprecateUsage test
129 function functionIsDeprecated() {
130 deprecate.deprecateUsage("foo");
131 }
132 functionIsDeprecated();
134 assert.equal(messages.length, 0,
135 "no errors dispatched on deprecateUsage");
137 // deprecateFunction test
138 function originalFunction() {};
140 let deprecateFunction = deprecate.deprecateFunction(originalFunction,
141 "bar");
142 deprecateFunction();
144 assert.equal(messages.length, 0,
145 "no errors dispatched on deprecateFunction");
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']);
152 testObj.on('fire', () => {
153 assert.equal(messages.length, 0,
154 "no errors dispatched on deprecateEvent");
155 done();
156 });
158 emit(testObj, 'fire');
159 }
160 require("test").run(exports);