test-deprecate.js 5.1 KB

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