test-simple-prefs.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 { Loader } = require("sdk/test/loader");
  6. const { setTimeout } = require("sdk/timers");
  7. const { emit } = require("sdk/system/events");
  8. const { id } = require("sdk/self");
  9. const simplePrefs = require("sdk/simple-prefs");
  10. const { prefs: sp } = simplePrefs;
  11. const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
  12. exports.testIterations = function(assert) {
  13. sp["test"] = true;
  14. sp["test.test"] = true;
  15. let prefAry = [];
  16. for (var name in sp ) {
  17. prefAry.push(name);
  18. }
  19. assert.ok("test" in sp);
  20. assert.ok(!sp.getPropertyDescriptor);
  21. assert.ok(Object.prototype.hasOwnProperty.call(sp, "test"));
  22. assert.equal(["test", "test.test"].toString(), prefAry.sort().toString(), "for (x in y) part 1/2 works");
  23. assert.equal(["test", "test.test"].toString(), Object.keys(sp).sort().toString(), "Object.keys works");
  24. delete sp["test"];
  25. delete sp["test.test"];
  26. let prefAry = [];
  27. for (var name in sp ) {
  28. prefAry.push(name);
  29. }
  30. assert.equal([].toString(), prefAry.toString(), "for (x in y) part 2/2 works");
  31. }
  32. exports.testSetGetBool = function(assert) {
  33. assert.equal(sp.test, undefined, "Value should not exist");
  34. sp.test = true;
  35. assert.ok(sp.test, "Value read should be the value previously set");
  36. };
  37. // TEST: setting and getting preferences with special characters work
  38. exports.testSpecialChars = function(assert, done) {
  39. let chars = specialChars.split("");
  40. let len = chars.length;
  41. let count = 0;
  42. chars.forEach(function(char) {
  43. let rand = Math.random() + "";
  44. simplePrefs.on(char, function onPrefChanged() {
  45. simplePrefs.removeListener(char, onPrefChanged);
  46. assert.equal(sp[char], rand, "setting pref with a name that is a special char, " + char + ", worked!");
  47. // end test
  48. if (++count == len)
  49. done();
  50. })
  51. sp[char] = rand;
  52. });
  53. };
  54. exports.testSetGetInt = function(assert) {
  55. assert.equal(sp["test-int"], undefined, "Value should not exist");
  56. sp["test-int"] = 1;
  57. assert.equal(sp["test-int"], 1, "Value read should be the value previously set");
  58. };
  59. exports.testSetComplex = function(assert) {
  60. try {
  61. sp["test-complex"] = {test: true};
  62. assert.fail("Complex values are not allowed");
  63. }
  64. catch (e) {
  65. assert.pass("Complex values are not allowed");
  66. }
  67. };
  68. exports.testSetGetString = function(assert) {
  69. assert.equal(sp["test-string"], undefined, "Value should not exist");
  70. sp["test-string"] = "test";
  71. assert.equal(sp["test-string"], "test", "Value read should be the value previously set");
  72. };
  73. exports.testHasAndRemove = function(assert) {
  74. sp.test = true;
  75. assert.ok(("test" in sp), "Value exists");
  76. delete sp.test;
  77. assert.equal(sp.test, undefined, "Value should be undefined");
  78. };
  79. exports.testPrefListener = function(assert, done) {
  80. let listener = function(prefName) {
  81. simplePrefs.removeListener('test-listener', listener);
  82. assert.equal(prefName, "test-listen", "The prefs listener heard the right event");
  83. done();
  84. };
  85. simplePrefs.on("test-listen", listener);
  86. sp["test-listen"] = true;
  87. // Wildcard listen
  88. let toSet = ['wildcard1','wildcard.pref2','wildcard.even.longer.test'];
  89. let observed = [];
  90. let wildlistener = function(prefName) {
  91. if (toSet.indexOf(prefName) > -1) observed.push(prefName);
  92. };
  93. simplePrefs.on('',wildlistener);
  94. toSet.forEach(function(pref) {
  95. sp[pref] = true;
  96. });
  97. assert.ok((observed.length == 3 && toSet.length == 3),
  98. "Wildcard lengths inconsistent" + JSON.stringify([observed.length, toSet.length]));
  99. toSet.forEach(function(pref,ii) {
  100. assert.equal(observed[ii], pref, "Wildcard observed " + pref);
  101. });
  102. simplePrefs.removeListener('',wildlistener);
  103. };
  104. exports.testBtnListener = function(assert, done) {
  105. let name = "test-btn-listen";
  106. simplePrefs.on(name, function listener() {
  107. simplePrefs.removeListener(name, listener);
  108. assert.pass("Button press event was heard");
  109. done();
  110. });
  111. emit((id + "-cmdPressed"), { subject: "", data: name });
  112. };
  113. exports.testPrefRemoveListener = function(assert, done) {
  114. let counter = 0;
  115. let listener = function() {
  116. assert.pass("The prefs listener was not removed yet");
  117. if (++counter > 1)
  118. assert.fail("The prefs listener was not removed");
  119. simplePrefs.removeListener("test-listen2", listener);
  120. sp["test-listen2"] = false;
  121. setTimeout(function() {
  122. assert.pass("The prefs listener was removed");
  123. done();
  124. }, 250);
  125. };
  126. simplePrefs.on("test-listen2", listener);
  127. // emit change
  128. sp["test-listen2"] = true;
  129. };
  130. // Bug 710117: Test that simple-pref listeners are removed on unload
  131. exports.testPrefUnloadListener = function(assert, done) {
  132. let loader = Loader(module);
  133. let sp = loader.require("sdk/simple-prefs");
  134. let counter = 0;
  135. let listener = function() {
  136. assert.equal(++counter, 1, "This listener should only be called once");
  137. loader.unload();
  138. // this may not execute after unload, but definitely shouldn't fire listener
  139. sp.prefs["test-listen3"] = false;
  140. // this should execute, but also definitely shouldn't fire listener
  141. require("sdk/simple-prefs").prefs["test-listen3"] = false;
  142. done();
  143. };
  144. sp.on("test-listen3", listener);
  145. // emit change
  146. sp.prefs["test-listen3"] = true;
  147. };
  148. // Bug 710117: Test that simple-pref listeners are removed on unload
  149. exports.testPrefUnloadWildcardListener = function(assert, done) {
  150. let testpref = "test-wildcard-unload-listener";
  151. let loader = Loader(module);
  152. let sp = loader.require("sdk/simple-prefs");
  153. let counter = 0;
  154. let listener = function() {
  155. assert.equal(++counter, 1, "This listener should only be called once");
  156. loader.unload();
  157. // this may not execute after unload, but definitely shouldn't fire listener
  158. sp.prefs[testpref] = false;
  159. // this should execute, but also definitely shouldn't fire listener
  160. require("sdk/simple-prefs").prefs[testpref] = false;
  161. done();
  162. };
  163. sp.on("", listener);
  164. // emit change
  165. sp.prefs[testpref] = true;
  166. };
  167. // Bug 732919 - JSON.stringify() fails on simple-prefs.prefs
  168. exports.testPrefJSONStringification = function(assert) {
  169. var sp = require("sdk/simple-prefs").prefs;
  170. assert.equal(
  171. Object.keys(sp).join(),
  172. Object.keys(JSON.parse(JSON.stringify(sp))).join(),
  173. "JSON stringification should work.");
  174. };
  175. require('sdk/test').run(exports);