michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: const deprecate = require("sdk/util/deprecate"); michael@0: const { LoaderWithHookedConsole } = require("sdk/test/loader"); michael@0: const { get, set } = require("sdk/preferences/service"); michael@0: const PREFERENCE = "devtools.errorconsole.deprecation_warnings"; michael@0: michael@0: exports["test Deprecate Usage"] = function testDeprecateUsage(assert) { michael@0: set(PREFERENCE, true); michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: let deprecate = loader.require("sdk/util/deprecate"); michael@0: michael@0: function functionIsDeprecated() { michael@0: deprecate.deprecateUsage("foo"); michael@0: } michael@0: michael@0: functionIsDeprecated(); michael@0: michael@0: assert.equal(messages.length, 1, "only one error is dispatched"); michael@0: assert.equal(messages[0].type, "error", "the console message is an error"); michael@0: michael@0: let msg = messages[0].msg; michael@0: michael@0: assert.ok(msg.indexOf("foo") !== -1, michael@0: "message contains the given message"); michael@0: assert.ok(msg.indexOf("functionIsDeprecated") !== -1, michael@0: "message contains name of the caller function"); michael@0: assert.ok(msg.indexOf(module.uri) !== -1, michael@0: "message contains URI of the caller module"); michael@0: michael@0: loader.unload(); michael@0: } michael@0: michael@0: exports["test Deprecate Function"] = function testDeprecateFunction(assert) { michael@0: set(PREFERENCE, true); michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: let deprecate = loader.require("sdk/util/deprecate"); michael@0: michael@0: let self = {}; michael@0: let arg1 = "foo"; michael@0: let arg2 = {}; michael@0: michael@0: function originalFunction(a1, a2) { michael@0: assert.equal(this, self); michael@0: assert.equal(a1, arg1); michael@0: assert.equal(a2, arg2); michael@0: }; michael@0: michael@0: let deprecateFunction = deprecate.deprecateFunction(originalFunction, michael@0: "bar"); michael@0: michael@0: deprecateFunction.call(self, arg1, arg2); michael@0: michael@0: assert.equal(messages.length, 1, "only one error is dispatched"); michael@0: assert.equal(messages[0].type, "error", "the console message is an error"); michael@0: michael@0: let msg = messages[0].msg; michael@0: assert.ok(msg.indexOf("bar") !== -1, "message contains the given message"); michael@0: assert.ok(msg.indexOf("testDeprecateFunction") !== -1, michael@0: "message contains name of the caller function"); michael@0: assert.ok(msg.indexOf(module.uri) !== -1, michael@0: "message contains URI of the caller module"); michael@0: michael@0: loader.unload(); michael@0: } michael@0: michael@0: exports.testDeprecateEvent = function(assert, done) { michael@0: set(PREFERENCE, true); michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: let deprecate = loader.require("sdk/util/deprecate"); michael@0: michael@0: let { on, emit } = loader.require('sdk/event/core'); michael@0: let testObj = {}; michael@0: testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']); michael@0: michael@0: testObj.on('fire', function() { michael@0: testObj.on('water', function() { michael@0: assert.equal(messages.length, 1, "only one error is dispatched"); michael@0: loader.unload(); michael@0: done(); michael@0: }) michael@0: assert.equal(messages.length, 1, "only one error is dispatched"); michael@0: emit(testObj, 'water'); michael@0: }); michael@0: assert.equal(messages.length, 1, "only one error is dispatched"); michael@0: assert.equal(messages[0].type, "error", "the console message is an error"); michael@0: let msg = messages[0].msg; michael@0: assert.ok(msg.indexOf("BAD") !== -1, "message contains the given message"); michael@0: assert.ok(msg.indexOf("deprecateEvent") !== -1, michael@0: "message contains name of the caller function"); michael@0: assert.ok(msg.indexOf(module.uri) !== -1, michael@0: "message contains URI of the caller module"); michael@0: michael@0: emit(testObj, 'fire'); michael@0: } michael@0: michael@0: exports.testDeprecateSettingToggle = function (assert, done) { michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: let deprecate = loader.require("sdk/util/deprecate"); michael@0: michael@0: function fn () { deprecate.deprecateUsage("foo"); } michael@0: michael@0: set(PREFERENCE, false); michael@0: fn(); michael@0: assert.equal(messages.length, 0, 'no deprecation warnings'); michael@0: michael@0: set(PREFERENCE, true); michael@0: fn(); michael@0: assert.equal(messages.length, 1, 'deprecation warnings when toggled'); michael@0: michael@0: set(PREFERENCE, false); michael@0: fn(); michael@0: assert.equal(messages.length, 1, 'no new deprecation warnings'); michael@0: done(); michael@0: }; michael@0: michael@0: exports.testDeprecateSetting = function (assert, done) { michael@0: // Set devtools.errorconsole.deprecation_warnings to false michael@0: set(PREFERENCE, false); michael@0: michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: let deprecate = loader.require("sdk/util/deprecate"); michael@0: michael@0: // deprecateUsage test michael@0: function functionIsDeprecated() { michael@0: deprecate.deprecateUsage("foo"); michael@0: } michael@0: functionIsDeprecated(); michael@0: michael@0: assert.equal(messages.length, 0, michael@0: "no errors dispatched on deprecateUsage"); michael@0: michael@0: // deprecateFunction test michael@0: function originalFunction() {}; michael@0: michael@0: let deprecateFunction = deprecate.deprecateFunction(originalFunction, michael@0: "bar"); michael@0: deprecateFunction(); michael@0: michael@0: assert.equal(messages.length, 0, michael@0: "no errors dispatched on deprecateFunction"); michael@0: michael@0: // deprecateEvent michael@0: let { on, emit } = loader.require('sdk/event/core'); michael@0: let testObj = {}; michael@0: testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']); michael@0: michael@0: testObj.on('fire', () => { michael@0: assert.equal(messages.length, 0, michael@0: "no errors dispatched on deprecateEvent"); michael@0: done(); michael@0: }); michael@0: michael@0: emit(testObj, 'fire'); michael@0: } michael@0: require("test").run(exports);