Permalink
Browse files

Merge pull request #531 from erikvold/783453

Fix bug 783453 testing that special characters work as preference names r=@ochameau
  • Loading branch information...
2 parents 1653a66 + 02f8e1f commit dd18fef598347cee4ccd7926ebbd3f9d4b289201 @erikvold erikvold committed Sep 13, 2012
Showing with 48 additions and 71 deletions.
  1. +34 −70 packages/addon-kit/tests/test-simple-prefs.js
  2. +14 −1 packages/api-utils/tests/test-preferences-service.js
@@ -1,20 +1,18 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
"use strict";
const { Loader } = require("test-harness/loader");
const { setTimeout } = require("timers");
const { notify } = require("observer-service");
const { id } = require("self");
+const simplePrefs = require("simple-prefs");
+const { prefs: sp } = simplePrefs;
-exports.testIterations = function(test) {
- test.waitUntilDone();
-
- let loader = Loader(module);
- let sp = loader.require("simple-prefs").prefs;
+const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
+exports.testIterations = function(test) {
sp["test"] = true;
sp["test.test"] = true;
let prefAry = [];
@@ -34,125 +32,92 @@ exports.testIterations = function(test) {
prefAry.push(name);
}
test.assertEqual([].toString(), prefAry.toString(), "for (x in y) part 2/2 works");
-
- loader.unload();
- test.done();
}
exports.testSetGetBool = function(test) {
- test.waitUntilDone();
-
- let loader = Loader(module);
- let sp = loader.require("simple-prefs").prefs;
-
test.assertEqual(sp.test, undefined, "Value should not exist");
sp.test = true;
test.assert(sp.test, "Value read should be the value previously set");
+};
- loader.unload();
- test.done();
+// TEST: setting and getting preferences with special characters work
+exports.testSpecialChars = function(test) {
+ let chars = specialChars.split("");
+ let len = chars.length;
+
+ let count = 0;
+ chars.forEach(function(char) {
+ let rand = Math.random() + "";
+ simplePrefs.on(char, function onPrefChanged() {
+ simplePrefs.removeListener(char, onPrefChanged);
+ test.assertEqual(sp[char], rand, "setting pref with a name that is a special char, " + char + ", worked!");
+
+ // end test
+ if (++count == len)
+ test.done();
+ })
+ sp[char] = rand;
+ });
};
exports.testSetGetInt = function(test) {
- test.waitUntilDone();
-
- // Load the module once, set a value.
- let loader = Loader(module);
- let sp = loader.require("simple-prefs").prefs;
-
test.assertEqual(sp["test-int"], undefined, "Value should not exist");
sp["test-int"] = 1;
test.assertEqual(sp["test-int"], 1, "Value read should be the value previously set");
-
- loader.unload();
- test.done();
};
exports.testSetComplex = function(test) {
- test.waitUntilDone();
-
- let loader = Loader(module);
- let sp = loader.require("simple-prefs").prefs;
-
try {
sp["test-complex"] = {test: true};
test.fail("Complex values are not allowed");
}
catch (e) {
test.pass("Complex values are not allowed");
}
-
- loader.unload();
- test.done();
};
exports.testSetGetString = function(test) {
- test.waitUntilDone();
-
- let loader = Loader(module);
- let sp = loader.require("simple-prefs").prefs;
-
test.assertEqual(sp["test-string"], undefined, "Value should not exist");
sp["test-string"] = "test";
test.assertEqual(sp["test-string"], "test", "Value read should be the value previously set");
-
- loader.unload();
- test.done();
};
exports.testHasAndRemove = function(test) {
- test.waitUntilDone();
-
- let loader = Loader(module);
- let sp = loader.require("simple-prefs").prefs;
-
sp.test = true;
test.assert(("test" in sp), "Value exists");
delete sp.test;
test.assertEqual(sp.test, undefined, "Value should be undefined");
-
- loader.unload();
- test.done();
-
};
exports.testPrefListener = function(test) {
test.waitUntilDone();
- let loader = Loader(module);
- let sp = loader.require("simple-prefs");
-
let listener = function(prefName) {
+ simplePrefs.removeListener('test-listener', listener);
test.assertEqual(prefName, "test-listen", "The prefs listener heard the right event");
test.done();
};
- sp.on("test-listen", listener);
+ simplePrefs.on("test-listen", listener);
- sp.prefs["test-listen"] = true;
- loader.unload();
+ sp["test-listen"] = true;
};
exports.testBtnListener = function(test) {
test.waitUntilDone();
- let loader = Loader(module);
- let sp = loader.require("simple-prefs");
-
- sp.on("test-btn-listen", function() {
+ let name = "test-btn-listen";
+ simplePrefs.on(name, function listener() {
+ simplePrefs.removeListener(name, listener);
test.pass("Button press event was heard");
test.done();
});
- notify((id + "-cmdPressed"), "", "test-btn-listen");
-
- loader.unload();
+ notify((id + "-cmdPressed"), "", name);
};
exports.testPrefRemoveListener = function(test) {
test.waitUntilDone();
- let loader = Loader(module);
- let sp = loader.require("simple-prefs");
let counter = 0;
let listener = function() {
@@ -161,21 +126,20 @@ exports.testPrefRemoveListener = function(test) {
if (++counter > 1)
test.fail("The prefs listener was not removed");
- sp.removeListener("test-listen2", listener);
+ simplePrefs.removeListener("test-listen2", listener);
- sp.prefs["test-listen2"] = false;
+ sp["test-listen2"] = false;
setTimeout(function() {
test.pass("The prefs listener was removed");
- loader.unload();
test.done();
}, 250);
};
- sp.on("test-listen2", listener);
+ simplePrefs.on("test-listen2", listener);
// emit change
- sp.prefs["test-listen2"] = true;
+ sp["test-listen2"] = true;
};
// Bug 710117: Test that simple-pref listeners are removed on unload
@@ -1,14 +1,15 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
"use strict";
const prefs = require("preferences-service");
const Branch = prefs.Branch;
const { Cc, Ci, Cu } = require("chrome");
const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
+const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
+
exports.testReset = function(test) {
prefs.reset("test_reset_pref");
test.assertEqual(prefs.has("test_reset_pref"), false);
@@ -128,3 +129,15 @@ exports.testGetSetLocalized = function(test) {
// Undo our modification
prefs.reset(prefName);
}
+
+// TEST: setting and getting preferences with special characters work
+exports.testSpecialChars = function(test) {
+ let chars = specialChars.split('');
+ const ROOT = "test.";
+
+ chars.forEach(function(char) {
+ let rand = Math.random() + "";
+ prefs.set(ROOT+char, rand);
+ test.assertEqual(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!");
+ });
+};

0 comments on commit dd18fef

Please sign in to comment.