test-preferences-service.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 prefs = require("sdk/preferences/service");
  6. const Branch = prefs.Branch;
  7. const { Cc, Ci, Cu } = require("chrome");
  8. const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
  9. const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
  10. exports.testReset = function(assert) {
  11. prefs.reset("test_reset_pref");
  12. assert.equal(prefs.has("test_reset_pref"), false);
  13. assert.equal(prefs.isSet("test_reset_pref"), false);
  14. prefs.set("test_reset_pref", 5);
  15. assert.equal(prefs.has("test_reset_pref"), true);
  16. assert.equal(prefs.isSet("test_reset_pref"), true);
  17. assert.equal(prefs.keys("test_reset_pref").toString(), "test_reset_pref");
  18. };
  19. exports.testGetAndSet = function(assert) {
  20. let svc = Cc["@mozilla.org/preferences-service;1"].
  21. getService(Ci.nsIPrefService).
  22. getBranch(null);
  23. svc.setCharPref("test_set_get_pref", "a normal string");
  24. assert.equal(prefs.get("test_set_get_pref"), "a normal string",
  25. "preferences-service should read from " +
  26. "application-wide preferences service");
  27. prefs.set("test_set_get_pref.integer", 1);
  28. assert.equal(prefs.get("test_set_get_pref.integer"), 1,
  29. "set/get integer preference should work");
  30. assert.equal(
  31. prefs.keys("test_set_get_pref").sort().toString(),
  32. ["test_set_get_pref.integer","test_set_get_pref"].sort().toString());
  33. prefs.set("test_set_get_number_pref", 42);
  34. assert.throws(
  35. function() { prefs.set("test_set_get_number_pref", 3.14159); },
  36. /cannot store non-integer number: 3.14159/,
  37. "setting a float preference should raise an error"
  38. );
  39. assert.equal(prefs.get("test_set_get_number_pref"), 42,
  40. "bad-type write attempt should not overwrite");
  41. // 0x80000000 (no), 0x7fffffff (yes), -0x80000000 (yes), -0x80000001 (no)
  42. assert.throws(
  43. function() { prefs.set("test_set_get_number_pref", Math.pow(2, 31)); },
  44. new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
  45. "2147483648, as number pref values must be in the signed 32\\-bit " +
  46. "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " +
  47. "range, store them as strings."),
  48. "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
  49. );
  50. assert.equal(prefs.get("test_set_get_number_pref"), 42,
  51. "out-of-range write attempt should not overwrite 1");
  52. prefs.set("test_set_get_number_pref", Math.pow(2, 31)-1);
  53. assert.equal(prefs.get("test_set_get_number_pref"), 0x7fffffff,
  54. "in-range write attempt should work 1");
  55. prefs.set("test_set_get_number_pref", -Math.pow(2, 31));
  56. assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
  57. "in-range write attempt should work 2");
  58. assert.throws(
  59. function() { prefs.set("test_set_get_number_pref", -0x80000001); },
  60. new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
  61. "\\-2147483649, as number pref values must be in the signed 32-bit " +
  62. "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " +
  63. "range, store them as strings."),
  64. "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
  65. );
  66. assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
  67. "out-of-range write attempt should not overwrite 2");
  68. prefs.set("test_set_get_pref.string", "foo");
  69. assert.equal(prefs.get("test_set_get_pref.string"), "foo",
  70. "set/get string preference should work");
  71. prefs.set("test_set_get_pref.boolean", true);
  72. assert.equal(prefs.get("test_set_get_pref.boolean"), true,
  73. "set/get boolean preference should work");
  74. prefs.set("test_set_get_unicode_pref", String.fromCharCode(960));
  75. assert.equal(prefs.get("test_set_get_unicode_pref"),
  76. String.fromCharCode(960),
  77. "set/get unicode preference should work");
  78. var unsupportedValues = [null, [], undefined];
  79. unsupportedValues.forEach(
  80. function(value) {
  81. assert.throws(
  82. function() { prefs.set("test_set_pref", value); },
  83. new RegExp("can't set pref test_set_pref to value '" + value + "'; " +
  84. "it isn't a string, integer, or boolean"),
  85. "Setting a pref to " + uneval(value) + " should raise error"
  86. );
  87. });
  88. };
  89. exports.testPrefClass = function(assert) {
  90. var branch = Branch("test_foo");
  91. assert.equal(branch.test, undefined, "test_foo.test is undefined");
  92. branch.test = true;
  93. assert.equal(branch.test, true, "test_foo.test is true");
  94. delete branch.test;
  95. assert.equal(branch.test, undefined, "test_foo.test is undefined");
  96. };
  97. exports.testGetSetLocalized = function(assert) {
  98. let prefName = "general.useragent.locale";
  99. // Ensure that "general.useragent.locale" is a 'localized' pref
  100. let bundleURL = "chrome://global/locale/intl.properties";
  101. prefs.setLocalized(prefName, bundleURL);
  102. // Fetch the expected value directly from the property file
  103. let expectedValue = BundleService.createBundle(bundleURL).
  104. GetStringFromName(prefName).
  105. toLowerCase();
  106. assert.equal(prefs.getLocalized(prefName).toLowerCase(),
  107. expectedValue,
  108. "get localized preference");
  109. // Undo our modification
  110. prefs.reset(prefName);
  111. }
  112. // TEST: setting and getting preferences with special characters work
  113. exports.testSpecialChars = function(assert) {
  114. let chars = specialChars.split('');
  115. const ROOT = "test.";
  116. chars.forEach(function(char) {
  117. let rand = Math.random() + "";
  118. prefs.set(ROOT+char, rand);
  119. assert.equal(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!");
  120. });
  121. };
  122. require('sdk/test').run(exports);