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 + .../libpref/test/unit_ipc/test_existing_prefs.js | 21 ++ .../libpref/test/unit_ipc/test_initial_prefs.js | 18 + modules/libpref/test/unit_ipc/test_large_pref.js | 98 ++++++ .../libpref/test/unit_ipc/test_observed_prefs.js | 16 + modules/libpref/test/unit_ipc/test_update_prefs.js | 38 ++ .../test/unit_ipc/test_user_default_prefs.js | 76 ++++ modules/libpref/test/unit_ipc/xpcshell.ini | 11 + 23 files changed, 1341 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 create mode 100644 modules/libpref/test/unit_ipc/test_existing_prefs.js create mode 100644 modules/libpref/test/unit_ipc/test_initial_prefs.js create mode 100644 modules/libpref/test/unit_ipc/test_large_pref.js create mode 100644 modules/libpref/test/unit_ipc/test_observed_prefs.js create mode 100644 modules/libpref/test/unit_ipc/test_update_prefs.js create mode 100644 modules/libpref/test/unit_ipc/test_user_default_prefs.js create mode 100644 modules/libpref/test/unit_ipc/xpcshell.ini (limited to 'modules/libpref/test') 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 length) { + string = string.substring(length - string.length); + } + return string; +} + +// from prefapi.h +const MAX_ADVISABLE_PREF_LENGTH = 4 * 1024; + +const largeString = makeBuffer(MAX_ADVISABLE_PREF_LENGTH + 1); +const smallString = makeBuffer(4); + +const testValues = [ + {name: "None", value: undefined}, + {name: "Small", value: smallString}, + {name: "Large", value: largeString}, +]; + +function prefName(def, user) { + return "Test.IPC.default" + def.name + "User" + user.name; +} + +function expectedPrefValue(def, user) { + if (user.value) { + return user.value; + } + return def.value; +} + +function run_test() { + let pb = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); + let ps = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService); + let defaultBranch = ps.getDefaultBranch(""); + + let isParent = isParentProcess(); + if (isParent) { + // Set all combinations of none, small and large, for default and user prefs. + for (let def of testValues) { + for (let user of testValues) { + let currPref = prefName(def, user); + if (def.value) { + defaultBranch.setCharPref(currPref, def.value); + } + if (user.value) { + pb.setCharPref(currPref, user.value); + } + } + } + + run_test_in_child("test_large_pref.js"); + } + + // Check that each preference is set or not set, as appropriate. + for (let def of testValues) { + for (let user of testValues) { + if (!def.value && !user.value) { + continue; + } + let pref_name = prefName(def, user); + if (isParent || (def.name != "Large" && user.name != "Large")) { + do_check_eq(pb.getCharPref(pref_name), expectedPrefValue(def, user)); + } else { + // This is the child, and either the default or user value is + // large, so the preference should not be set. + let prefExists; + try { + pb.getCharPref(pref_name); + prefExists = true; + } catch(e) { + prefExists = false; + } + ok(!prefExists, + "Pref " + pref_name + " should not be set in the child"); + } + } + } +} diff --git a/modules/libpref/test/unit_ipc/test_observed_prefs.js b/modules/libpref/test/unit_ipc/test_observed_prefs.js new file mode 100644 index 000000000..c18f7f0fe --- /dev/null +++ b/modules/libpref/test/unit_ipc/test_observed_prefs.js @@ -0,0 +1,16 @@ +var Ci = Components.interfaces; +var Cc = Components.classes; + +function isParentProcess() { + let appInfo = Cc["@mozilla.org/xre/app-info;1"]; + return (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT); +} + +function run_test() { + if (isParentProcess() == false) { + var pb = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); + do_check_eq(pb.getBoolPref("Test.IPC.bool.new"), true); + do_check_eq(pb.getIntPref("Test.IPC.int.new"), 23); + do_check_eq(pb.getCharPref("Test.IPC.char.new"), "hey"); + } +} \ No newline at end of file diff --git a/modules/libpref/test/unit_ipc/test_update_prefs.js b/modules/libpref/test/unit_ipc/test_update_prefs.js new file mode 100644 index 000000000..7e1b0dcd7 --- /dev/null +++ b/modules/libpref/test/unit_ipc/test_update_prefs.js @@ -0,0 +1,38 @@ +var Ci = Components.interfaces; +var Cc = Components.classes; + +function isParentProcess() { + let appInfo = Cc["@mozilla.org/xre/app-info;1"]; + return (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT); +} + +function run_test() { + if (isParentProcess()) { + + do_load_child_test_harness(); + + var pb = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); + + // these prefs are set after the child has been created. + pb.setBoolPref("Test.IPC.bool.new", true); + pb.setIntPref("Test.IPC.int.new", 23); + pb.setCharPref("Test.IPC.char.new", "hey"); + + run_test_in_child("test_observed_prefs.js", testPrefClear); + } +} + +function testPrefClear() { + var pb = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); + pb.clearUserPref("Test.IPC.bool.new"); + + sendCommand( +'var pb = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);\n'+ +'pb.prefHasUserValue("Test.IPC.bool.new");\n', + checkWasCleared); +} + +function checkWasCleared(existsStr) { + do_check_eq(existsStr, "false"); + do_test_finished(); +} \ No newline at end of file diff --git a/modules/libpref/test/unit_ipc/test_user_default_prefs.js b/modules/libpref/test/unit_ipc/test_user_default_prefs.js new file mode 100644 index 000000000..3b8ca28c6 --- /dev/null +++ b/modules/libpref/test/unit_ipc/test_user_default_prefs.js @@ -0,0 +1,76 @@ +var Ci = Components.interfaces; +var Cc = Components.classes; + +var pb = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); + +// This pref is chosen somewhat arbitrarily --- we just need one +// that's guaranteed to have a default value. +const kPrefName = 'intl.accept_languages'; // of type char, which we + // assume below +var initialValue = null; + +function check_child_pref_info_eq(continuation) { + sendCommand( + 'var pb = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);\n'+ + // Returns concatenation "[value],[isUser]" + 'pb.getCharPref("'+ kPrefName +'")+ "," +'+ + 'pb.prefHasUserValue("'+ kPrefName +'");', + function (info) { + let [ value, isUser ] = info.split(','); + do_check_eq(pb.getCharPref(kPrefName), value); + do_check_eq(pb.prefHasUserValue(kPrefName), isUser == "true"); + continuation(); + }); +} + +function run_test() { + // We finish in clean_up() + do_test_pending(); + + try { + if (pb.getCharPref('dom.ipc.processPrelaunch.enabled')) { + dump('WARNING: Content process may already have launched, so this test may not be meaningful.'); + } + } catch(e) { } + + initialValue = pb.getCharPref(kPrefName); + + test_user_setting(); +} + +function test_user_setting() { + // We rely on setting this before the content process starts up. + // When it starts up, it should recognize this as a user pref, not + // a default pref. + pb.setCharPref(kPrefName, 'i-imaginarylanguage'); + // NB: processing of the value-change notification in the child + // process triggered by the above set happens-before the remaining + // code here + check_child_pref_info_eq(function () { + do_check_eq(pb.prefHasUserValue(kPrefName), true); + + test_cleared_is_default(); + }); +} + +function test_cleared_is_default() { + pb.clearUserPref(kPrefName); + // NB: processing of the value-change notification in the child + // process triggered by the above set happens-before the remaining + // code here + check_child_pref_info_eq(function () { + do_check_eq(pb.prefHasUserValue(kPrefName), false); + + clean_up(); + }); +} + +function clean_up() { + pb.setCharPref(kPrefName, initialValue); + // NB: processing of the value-change notification in the child + // process triggered by the above set happens-before the remaining + // code here + check_child_pref_info_eq(function () { + do_test_finished(); + }); +} \ No newline at end of file diff --git a/modules/libpref/test/unit_ipc/xpcshell.ini b/modules/libpref/test/unit_ipc/xpcshell.ini new file mode 100644 index 000000000..319276e68 --- /dev/null +++ b/modules/libpref/test/unit_ipc/xpcshell.ini @@ -0,0 +1,11 @@ +[DEFAULT] +head = +tail = +skip-if = toolkit == 'android' + +[test_existing_prefs.js] +[test_initial_prefs.js] +[test_large_pref.js] +[test_observed_prefs.js] +[test_update_prefs.js] +[test_user_default_prefs.js] -- cgit v1.2.3