From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- modules/libpref/test/unit/data/testPref.js | 6 + modules/libpref/test/unit/data/testPrefSticky.js | 2 + .../libpref/test/unit/data/testPrefStickyUser.js | 5 + modules/libpref/test/unit/extdata/testExt.js | 2 + modules/libpref/test/unit/head_libPrefs.js | 45 +++ modules/libpref/test/unit/test_bug345529.js | 34 ++ modules/libpref/test/unit/test_bug506224.js | 29 ++ modules/libpref/test/unit/test_bug577950.js | 28 ++ modules/libpref/test/unit/test_bug790374.js | 55 +++ modules/libpref/test/unit/test_changeType.js | 63 ++++ modules/libpref/test/unit/test_dirtyPrefs.js | 75 ++++ modules/libpref/test/unit/test_extprefs.js | 70 ++++ modules/libpref/test/unit/test_libPrefs.js | 392 +++++++++++++++++++++ modules/libpref/test/unit/test_stickyprefs.js | 170 +++++++++ modules/libpref/test/unit/test_warnings.js | 69 ++++ modules/libpref/test/unit/xpcshell.ini | 18 + 16 files changed, 1063 insertions(+) create mode 100644 modules/libpref/test/unit/data/testPref.js create mode 100644 modules/libpref/test/unit/data/testPrefSticky.js create mode 100644 modules/libpref/test/unit/data/testPrefStickyUser.js create mode 100644 modules/libpref/test/unit/extdata/testExt.js create mode 100644 modules/libpref/test/unit/head_libPrefs.js create mode 100644 modules/libpref/test/unit/test_bug345529.js create mode 100644 modules/libpref/test/unit/test_bug506224.js create mode 100644 modules/libpref/test/unit/test_bug577950.js create mode 100644 modules/libpref/test/unit/test_bug790374.js create mode 100644 modules/libpref/test/unit/test_changeType.js create mode 100644 modules/libpref/test/unit/test_dirtyPrefs.js create mode 100644 modules/libpref/test/unit/test_extprefs.js create mode 100644 modules/libpref/test/unit/test_libPrefs.js create mode 100644 modules/libpref/test/unit/test_stickyprefs.js create mode 100644 modules/libpref/test/unit/test_warnings.js create mode 100644 modules/libpref/test/unit/xpcshell.ini (limited to 'modules/libpref/test/unit') diff --git a/modules/libpref/test/unit/data/testPref.js b/modules/libpref/test/unit/data/testPref.js new file mode 100644 index 000000000..0864e7ce8 --- /dev/null +++ b/modules/libpref/test/unit/data/testPref.js @@ -0,0 +1,6 @@ +user_pref("testPref.bool1", true); +user_pref("testPref.bool2", false); +user_pref("testPref.int1", 23); +user_pref("testPref.int2", -1236); +user_pref("testPref.char1", "_testPref"); +user_pref("testPref.char2", "älskar"); \ No newline at end of file diff --git a/modules/libpref/test/unit/data/testPrefSticky.js b/modules/libpref/test/unit/data/testPrefSticky.js new file mode 100644 index 000000000..69b3165fb --- /dev/null +++ b/modules/libpref/test/unit/data/testPrefSticky.js @@ -0,0 +1,2 @@ +pref("testPref.unsticky.bool", true); +sticky_pref("testPref.sticky.bool", false); diff --git a/modules/libpref/test/unit/data/testPrefStickyUser.js b/modules/libpref/test/unit/data/testPrefStickyUser.js new file mode 100644 index 000000000..0ea090681 --- /dev/null +++ b/modules/libpref/test/unit/data/testPrefStickyUser.js @@ -0,0 +1,5 @@ +// testPrefSticky.js defined this pref as a sticky_pref(). Once a sticky +// pref has been changed, it's written as a user_pref(). +// So this test file reflects that scenario. +// Note the default in testPrefSticky.js is also false. +user_pref("testPref.sticky.bool", false); diff --git a/modules/libpref/test/unit/extdata/testExt.js b/modules/libpref/test/unit/extdata/testExt.js new file mode 100644 index 000000000..17c684969 --- /dev/null +++ b/modules/libpref/test/unit/extdata/testExt.js @@ -0,0 +1,2 @@ +pref("testPref.bool2", true); +pref("testExtPref.bool", true); diff --git a/modules/libpref/test/unit/head_libPrefs.js b/modules/libpref/test/unit/head_libPrefs.js new file mode 100644 index 000000000..b5e763419 --- /dev/null +++ b/modules/libpref/test/unit/head_libPrefs.js @@ -0,0 +1,45 @@ +/* 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/. */ + +const NS_APP_USER_PROFILE_50_DIR = "ProfD"; + +var Ci = Components.interfaces; +var Cc = Components.classes; +var Cr = Components.results; +var Cu = Components.utils; + +function do_check_throws(f, result, stack) +{ + if (!stack) + stack = Components.stack.caller; + + try { + f(); + } catch (exc) { + if (exc.result == result) + return; + do_throw("expected result " + result + ", caught " + exc, stack); + } + do_throw("expected result " + result + ", none thrown", stack); +} + +var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); + +// Register current test directory as provider for the profile directory. +var provider = { + getFile: function(prop, persistent) { + persistent.value = true; + if (prop == NS_APP_USER_PROFILE_50_DIR) + return dirSvc.get("CurProcD", Ci.nsIFile); + throw Components.Exception("Tried to get test directory '" + prop + "'", Cr.NS_ERROR_FAILURE); + }, + QueryInterface: function(iid) { + if (iid.equals(Ci.nsIDirectoryServiceProvider) || + iid.equals(Ci.nsISupports)) { + return this; + } + throw Cr.NS_ERROR_NO_INTERFACE; + } +}; +dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider); diff --git a/modules/libpref/test/unit/test_bug345529.js b/modules/libpref/test/unit/test_bug345529.js new file mode 100644 index 000000000..2e4616a6b --- /dev/null +++ b/modules/libpref/test/unit/test_bug345529.js @@ -0,0 +1,34 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/licenses/publicdomain/ */ + +// Regression test for bug 345529 - crash removing an observer during an +// nsPref:changed notification. +function run_test() { + const Cc = Components.classes; + const Ci = Components.interfaces; + const PREF_NAME = "testPref"; + + var prefs = Cc["@mozilla.org/preferences-service;1"] + .getService(Ci.nsIPrefBranch); + var observer = { + QueryInterface: function QueryInterface(aIID) { + if (aIID.equals(Ci.nsIObserver) || + aIID.equals(Ci.nsISupports)) + return this; + throw Components.results.NS_NOINTERFACE; + }, + + observe: function observe(aSubject, aTopic, aState) { + prefs.removeObserver(PREF_NAME, observer); + } + } + prefs.addObserver(PREF_NAME, observer, false); + + prefs.setCharPref(PREF_NAME, "test0") + // This second call isn't needed on a clean profile: it makes sure + // the observer gets called even if the pref already had the value + // "test0" before this test. + prefs.setCharPref(PREF_NAME, "test1") + + do_check_true(true); +} diff --git a/modules/libpref/test/unit/test_bug506224.js b/modules/libpref/test/unit/test_bug506224.js new file mode 100644 index 000000000..3fb723974 --- /dev/null +++ b/modules/libpref/test/unit/test_bug506224.js @@ -0,0 +1,29 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/licenses/publicdomain/ */ + +function run_test() { + const Cc = Components.classes; + const Ci = Components.interfaces; + const PREF_NAME = "testPref"; + + var ps = Cc["@mozilla.org/preferences-service;1"] + .getService(Ci.nsIPrefService); + var prefs = ps.getDefaultBranch(null); + var userprefs = ps.getBranch(null); + + prefs.setCharPref(PREF_NAME, "test0"); + prefs.lockPref(PREF_NAME); + do_check_eq("test0", userprefs.getCharPref(PREF_NAME)); + do_check_eq(false, userprefs.prefHasUserValue(PREF_NAME)); + + var file = do_get_profile(); + file.append("prefs.js"); + ps.savePrefFile(file); + + prefs.unlockPref(PREF_NAME); + prefs.setCharPref(PREF_NAME, "test1"); + ps.readUserPrefs(file); + + do_check_eq("test1", userprefs.getCharPref(PREF_NAME)); + do_check_eq(false, userprefs.prefHasUserValue(PREF_NAME)); +} diff --git a/modules/libpref/test/unit/test_bug577950.js b/modules/libpref/test/unit/test_bug577950.js new file mode 100644 index 000000000..b4f28e902 --- /dev/null +++ b/modules/libpref/test/unit/test_bug577950.js @@ -0,0 +1,28 @@ +/* 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/. */ + +function run_test() { + var ps = Cc["@mozilla.org/preferences-service;1"]. + getService(Ci.nsIPrefService); + + var pb = Cc["@mozilla.org/preferences-service;1"]. + getService(Ci.nsIPrefBranch); + + var observer = { + QueryInterface: function QueryInterface(aIID) { + if (aIID.equals(Ci.nsIObserver) || + aIID.equals(Ci.nsISupports)) + return this; + throw Components.results.NS_NOINTERFACE; + }, + + observe: function observe(aSubject, aTopic, aState) { + // Don't do anything. + } + } + + /* Set the same pref twice. This shouldn't leak. */ + pb.addObserver("UserPref.nonexistent.setIntPref", observer, false); + pb.addObserver("UserPref.nonexistent.setIntPref", observer, false); +} diff --git a/modules/libpref/test/unit/test_bug790374.js b/modules/libpref/test/unit/test_bug790374.js new file mode 100644 index 000000000..64ca620ed --- /dev/null +++ b/modules/libpref/test/unit/test_bug790374.js @@ -0,0 +1,55 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/licenses/publicdomain/ */ + +function run_test() { + const Cc = Components.classes; + const Ci = Components.interfaces; + const Cr = Components.results; + const PREF_NAME = "testPref"; + + var ps = Cc["@mozilla.org/preferences-service;1"] + .getService(Ci.nsIPrefService); + var prefs = ps.getDefaultBranch(null); + var userprefs = ps.getBranch(null); + + /* First, test to make sure we can parse a float from a string properly. */ + prefs.setCharPref(PREF_NAME, "9.674"); + prefs.lockPref(PREF_NAME); + var myFloat = 9.674; + var fudge = 0.001; + var floatPref = userprefs.getFloatPref(PREF_NAME); + do_check_true(myFloat+fudge >= floatPref); + do_check_true(myFloat-fudge <= floatPref); + + /* Now test some failure conditions. */ + prefs.unlockPref(PREF_NAME); + prefs.setCharPref(PREF_NAME, ""); + prefs.lockPref(PREF_NAME); + do_check_throws(function() { + userprefs.getFloatPref(PREF_NAME); + }, Cr.NS_ERROR_ILLEGAL_VALUE); + + prefs.unlockPref(PREF_NAME); + prefs.setCharPref(PREF_NAME, "18.0a1"); + prefs.lockPref(PREF_NAME); + + do_check_throws(function() { + userprefs.getFloatPref(PREF_NAME); + }, Cr.NS_ERROR_ILLEGAL_VALUE); + + prefs.unlockPref(PREF_NAME); + prefs.setCharPref(PREF_NAME, "09.25.2012"); + prefs.lockPref(PREF_NAME); + + do_check_throws(function() { + userprefs.getFloatPref(PREF_NAME); + }, Cr.NS_ERROR_ILLEGAL_VALUE); + + prefs.unlockPref(PREF_NAME); + prefs.setCharPref(PREF_NAME, "aString"); + prefs.lockPref(PREF_NAME); + + do_check_throws(function() { + userprefs.getFloatPref(PREF_NAME); + }, Cr.NS_ERROR_ILLEGAL_VALUE); +} diff --git a/modules/libpref/test/unit/test_changeType.js b/modules/libpref/test/unit/test_changeType.js new file mode 100644 index 000000000..573d561a4 --- /dev/null +++ b/modules/libpref/test/unit/test_changeType.js @@ -0,0 +1,63 @@ +/* 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/. */ + +/* Tests for changing the type of a preference (bug 985998) */ + +const PREF_INVALID = 0; +const PREF_BOOL = 128; +const PREF_INT = 64; +const PREF_STRING = 32; + +function run_test() { + + var ps = Cc["@mozilla.org/preferences-service;1"]. + getService(Ci.nsIPrefService); + + let defaultBranch = ps.getDefaultBranch(""); + let userBranch = ps.getBranch(""); + + //**************************************************************************// + // Can't change the type of prefs that have default values + + defaultBranch.setBoolPref("TypeTest.existing.bool", true); + defaultBranch.setIntPref("TypeTest.existing.int", 23); + defaultBranch.setCharPref("TypeTest.existing.char", "hey"); + + // The user branch reads back the expected default + do_check_eq(userBranch.getBoolPref("TypeTest.existing.bool"), true); + do_check_eq(userBranch.getIntPref("TypeTest.existing.int"), 23); + do_check_eq(userBranch.getCharPref("TypeTest.existing.char"), "hey"); + + // All the combinations of attempted type changes + do_check_throws(function() { + userBranch.setCharPref("TypeTest.existing.bool", "boo"); }, Cr.NS_ERROR_UNEXPECTED); + do_check_throws(function() { + userBranch.setIntPref("TypeTest.existing.bool", 5); }, Cr.NS_ERROR_UNEXPECTED); + do_check_throws(function() { + userBranch.setCharPref("TypeTest.existing.int", "boo"); }, Cr.NS_ERROR_UNEXPECTED); + do_check_throws(function() { + userBranch.setBoolPref("TypeTest.existing.int", true); }, Cr.NS_ERROR_UNEXPECTED); + do_check_throws(function() { + userBranch.setBoolPref("TypeTest.existing.char", true); }, Cr.NS_ERROR_UNEXPECTED); + do_check_throws(function() { + userBranch.setIntPref("TypeTest.existing.char", 6); }, Cr.NS_ERROR_UNEXPECTED); + + + //**************************************************************************// + // Prefs that don't have default values can mutate + let pref = "TypeTest.user"; + userBranch.setBoolPref(pref, true); + userBranch.setCharPref(pref, "yay"); + do_check_eq(userBranch.getCharPref(pref), "yay"); + userBranch.setIntPref(pref, 7); + do_check_eq(userBranch.getIntPref(pref), 7); + userBranch.setBoolPref(pref, false); + do_check_eq(userBranch.getBoolPref(pref), false); + userBranch.setIntPref(pref, 8); + do_check_eq(userBranch.getIntPref(pref), 8); + userBranch.setCharPref(pref, "whee"); + do_check_eq(userBranch.getCharPref(pref), "whee"); + userBranch.setBoolPref(pref, true); + do_check_eq(userBranch.getBoolPref(pref), true); +} diff --git a/modules/libpref/test/unit/test_dirtyPrefs.js b/modules/libpref/test/unit/test_dirtyPrefs.js new file mode 100644 index 000000000..361171616 --- /dev/null +++ b/modules/libpref/test/unit/test_dirtyPrefs.js @@ -0,0 +1,75 @@ +/* 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/. */ + +/* Tests for handling of the preferences 'dirty' flag (bug 985998) */ + +const PREF_INVALID = 0; +const PREF_BOOL = 128; +const PREF_INT = 64; +const PREF_STRING = 32; + +function run_test() { + + var ps = Cc["@mozilla.org/preferences-service;1"]. + getService(Ci.nsIPrefService); + + let defaultBranch = ps.getDefaultBranch(""); + let userBranch = ps.getBranch(""); + + let prefFile = do_get_profile(); + prefFile.append("prefs.js"); + + //**************************************************************************// + // prefs are not dirty after a write + ps.savePrefFile(prefFile); + do_check_false(ps.dirty); + + // set a new a user value, we should become dirty + userBranch.setBoolPref("DirtyTest.new.bool", true); + do_check_true(ps.dirty); + ps.savePrefFile(prefFile); + // Overwrite a pref with the same value => not dirty + userBranch.setBoolPref("DirtyTest.new.bool", true); + do_check_false(ps.dirty); + + // Repeat for the other two types + userBranch.setIntPref("DirtyTest.new.int", 1); + do_check_true(ps.dirty); + ps.savePrefFile(prefFile); + // Overwrite a pref with the same value => not dirty + userBranch.setIntPref("DirtyTest.new.int", 1); + do_check_false(ps.dirty); + + userBranch.setCharPref("DirtyTest.new.char", "oop"); + do_check_true(ps.dirty); + ps.savePrefFile(prefFile); + // Overwrite a pref with the same value => not dirty + userBranch.setCharPref("DirtyTest.new.char", "oop"); + do_check_false(ps.dirty); + + // change *type* of a user value -> dirty + userBranch.setBoolPref("DirtyTest.new.char", false); + do_check_true(ps.dirty); + ps.savePrefFile(prefFile); + + // Set a default pref => not dirty (defaults don't go into prefs.js) + defaultBranch.setBoolPref("DirtyTest.existing.bool", true); + do_check_false(ps.dirty); + // Fail to change type of a pref with default value -> not dirty + do_check_throws(function() { + userBranch.setCharPref("DirtyTest.existing.bool", "boo"); }, Cr.NS_ERROR_UNEXPECTED); + do_check_false(ps.dirty); + + // Set user value same as default, not dirty + userBranch.setBoolPref("DirtyTest.existing.bool", true); + do_check_false(ps.dirty); + // User value different from default, dirty + userBranch.setBoolPref("DirtyTest.existing.bool", false); + do_check_true(ps.dirty); + ps.savePrefFile(prefFile); + // Back to default value, dirty again + userBranch.setBoolPref("DirtyTest.existing.bool", true); + do_check_true(ps.dirty); + ps.savePrefFile(prefFile); +} diff --git a/modules/libpref/test/unit/test_extprefs.js b/modules/libpref/test/unit/test_extprefs.js new file mode 100644 index 000000000..37f8de167 --- /dev/null +++ b/modules/libpref/test/unit/test_extprefs.js @@ -0,0 +1,70 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/licenses/publicdomain/ */ + +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); +Components.utils.import("resource://gre/modules/Services.jsm"); + +// The profile directory is already set up in the head_ files. + +function arrayenumerator(a) +{ + return { + i_: 0, + QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]), + hasMoreElements: function ae_hasMoreElements() { + return this.i_ < a.length; + }, + getNext: function ae_getNext() { + return a[this.i_++]; + } + }; +} + +function run_test() { + var ps = Cc["@mozilla.org/preferences-service;1"]. + getService(Ci.nsIPrefService).QueryInterface(Ci.nsIPrefBranch); + + var extprefs = [do_get_file("extdata")]; + + var extProvider = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider, + Ci.nsIDirectoryServiceProvider2]), + getFile: function ep_getFile() { + throw Cr.NS_ERROR_FAILURE; + }, + + getFiles: function ep_getFiles(key) { + if (key != "ExtPrefDL") + throw Cr.NS_ERROR_FAILURE; + + return arrayenumerator(extprefs); + } + }; + + let prefFile = do_get_file("data/testPref.js"); + + do_check_throws(function() { + ps.getBoolPref("testExtPref.bool"); + }, Cr.NS_ERROR_UNEXPECTED); + do_check_throws(function() { + ps.getBoolPref("testPref.bool1"); + }, Cr.NS_ERROR_UNEXPECTED); + + ps.readUserPrefs(prefFile); + + do_check_true(ps.getBoolPref("testPref.bool1")); + ps.setBoolPref("testPref.bool1", false); + do_check_false(ps.getBoolPref("testPref.bool1")); + + dirSvc.registerProvider(extProvider); + Services.obs.notifyObservers(null, "load-extension-defaults", null); + + // The extension default should be available. + do_check_true(ps.getBoolPref("testExtPref.bool")); + + // The extension default should not override existing user prefs + do_check_false(ps.getBoolPref("testPref.bool2")); + + // The extension default should not modify existing set values + do_check_false(ps.getBoolPref("testPref.bool1")); +} diff --git a/modules/libpref/test/unit/test_libPrefs.js b/modules/libpref/test/unit/test_libPrefs.js new file mode 100644 index 000000000..be9c629e0 --- /dev/null +++ b/modules/libpref/test/unit/test_libPrefs.js @@ -0,0 +1,392 @@ +/* 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/. */ + +const PREF_INVALID = 0; +const PREF_BOOL = 128; +const PREF_INT = 64; +const PREF_STRING = 32; + +const MAX_PREF_LENGTH = 1 * 1024 * 1024; + +function makeList(a) +{ + var o = {}; + for(var i=0; i