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