addon-sdk/source/test/test-simple-prefs.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial