[X2Go-Commits] [pale-moon] 166/294: Bug 1338306 - nsIPrefBranch.get*Pref should support providing a default value, r=bsmedberg.

git-admin at x2go.org git-admin at x2go.org
Sat Apr 27 08:58:03 CEST 2019


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch upstream/28.5.0
in repository pale-moon.

commit cac3f9678de46298a93537e8913912bba28d89f7
Author: Florian Quèze <florian at queze.net>
Date:   Sat Mar 23 10:02:03 2019 -0400

    Bug 1338306 - nsIPrefBranch.get*Pref should support providing a default value, r=bsmedberg.
---
 modules/libpref/nsIPrefBranch.idl               | 24 +++++++++--
 modules/libpref/nsPrefBranch.cpp                | 57 +++++++++++++++++++++++++
 modules/libpref/test/unit/test_defaultValues.js | 48 +++++++++++++++++++++
 modules/libpref/test/unit/xpcshell.ini          |  1 +
 4 files changed, 126 insertions(+), 4 deletions(-)

diff --git a/modules/libpref/nsIPrefBranch.idl b/modules/libpref/nsIPrefBranch.idl
index ee0c11e..900806b 100644
--- a/modules/libpref/nsIPrefBranch.idl
+++ b/modules/libpref/nsIPrefBranch.idl
@@ -57,12 +57,16 @@ interface nsIPrefBranch : nsISupports
    * Called to get the state of an individual boolean preference.
    *
    * @param aPrefName The boolean preference to get the state of.
+   * @param aDefaultValue The value to return if the preference is not set.
    *
    * @return boolean  The value of the requested boolean preference.
    *
    * @see setBoolPref
    */
-  boolean getBoolPref(in string aPrefName);
+  [optional_argc,binaryname(GetBoolPrefWithDefault)]
+  boolean getBoolPref(in string aPrefName, [optional] in boolean aDefaultValue);
+  [noscript,binaryname(GetBoolPref)]
+  boolean getBoolPrefXPCOM(in string aPrefName);
 
   /**
    * Called to set the state of an individual boolean preference.
@@ -83,23 +87,31 @@ interface nsIPrefBranch : nsISupports
    * are converted to floating point numbers.
    *
    * @param aPrefName The floating point preference to get the state of.
+   * @param aDefaultValue The value to return if the preference is not set.
    *
    * @return float  The value of the requested floating point preference.
    *
    * @see setCharPref
    */
-  float getFloatPref(in string aPrefName);
+  [optional_argc,binaryname(GetFloatPrefWithDefault)]
+  float getFloatPref(in string aPrefName, [optional] in float aDefaultValue);
+  [noscript,binaryname(GetFloatPref)]
+  float getFloatPrefXPCOM(in string aPrefName);
 
   /**
    * Called to get the state of an individual string preference.
    *
    * @param aPrefName The string preference to retrieve.
+   * @param aDefaultValue The string to return if the preference is not set.
    *
    * @return string   The value of the requested string preference.
    *
    * @see setCharPref
    */
-  string getCharPref(in string aPrefName);
+  [optional_argc,binaryname(GetCharPrefWithDefault)]
+  string getCharPref(in string aPrefName, [optional] in string aDefaultValue);
+  [noscript,binaryname(GetCharPref)]
+  string getCharPrefXPCOM(in string aPrefName);
 
   /**
    * Called to set the state of an individual string preference.
@@ -118,12 +130,16 @@ interface nsIPrefBranch : nsISupports
    * Called to get the state of an individual integer preference.
    *
    * @param aPrefName The integer preference to get the value of.
+   * @param aDefaultValue The value to return if the preference is not set.
    *
    * @return long     The value of the requested integer preference.
    *
    * @see setIntPref
    */
-  long getIntPref(in string aPrefName);
+  [optional_argc,binaryname(GetIntPrefWithDefault)]
+  long getIntPref(in string aPrefName, [optional] in long aDefaultValue);
+  [noscript,binaryname(GetIntPref)]
+  long getIntPrefXPCOM(in string aPrefName);
 
   /**
    * Called to set the state of an individual integer preference.
diff --git a/modules/libpref/nsPrefBranch.cpp b/modules/libpref/nsPrefBranch.cpp
index 98e06aa..5173db0 100644
--- a/modules/libpref/nsPrefBranch.cpp
+++ b/modules/libpref/nsPrefBranch.cpp
@@ -141,6 +141,20 @@ NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, int32_t *_retval)
   return NS_OK;
 }
 
+NS_IMETHODIMP nsPrefBranch::GetBoolPrefWithDefault(const char *aPrefName,
+                                                   bool aDefaultValue,
+                                                   uint8_t _argc, bool *_retval)
+{
+  nsresult rv = GetBoolPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    *_retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, bool *_retval)
 {
   NS_ENSURE_ARG(aPrefName);
@@ -156,6 +170,20 @@ NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, bool aValue)
   return PREF_SetBoolPref(pref, aValue, mIsDefault);
 }
 
+NS_IMETHODIMP nsPrefBranch::GetFloatPrefWithDefault(const char *aPrefName,
+                                                    float aDefaultValue,
+                                                    uint8_t _argc, float *_retval)
+{
+  nsresult rv = GetFloatPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    *_retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetFloatPref(const char *aPrefName, float *_retval)
 {
   NS_ENSURE_ARG(aPrefName);
@@ -169,6 +197,21 @@ NS_IMETHODIMP nsPrefBranch::GetFloatPref(const char *aPrefName, float *_retval)
   return rv;
 }
 
+NS_IMETHODIMP nsPrefBranch::GetCharPrefWithDefault(const char *aPrefName,
+                                                   const char *aDefaultValue,
+                                                   uint8_t _argc, char **_retval)
+{
+  nsresult rv = GetCharPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    NS_ENSURE_ARG(aDefaultValue);
+    *_retval = NS_strdup(aDefaultValue);
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
 {
   NS_ENSURE_ARG(aPrefName);
@@ -195,6 +238,20 @@ nsresult nsPrefBranch::SetCharPrefInternal(const char *aPrefName, const char *aV
   return PREF_SetCharPref(pref, aValue, mIsDefault);
 }
 
+NS_IMETHODIMP nsPrefBranch::GetIntPrefWithDefault(const char *aPrefName,
+                                                  int32_t aDefaultValue,
+                                                  uint8_t _argc, int32_t *_retval)
+{
+  nsresult rv = GetIntPref(aPrefName, _retval);
+
+  if (NS_FAILED(rv) && _argc == 1) {
+    *_retval = aDefaultValue;
+    return NS_OK;
+  }
+
+  return rv;
+}
+
 NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, int32_t *_retval)
 {
   NS_ENSURE_ARG(aPrefName);
diff --git a/modules/libpref/test/unit/test_defaultValues.js b/modules/libpref/test/unit/test_defaultValues.js
new file mode 100644
index 0000000..d04bcc0
--- /dev/null
+++ b/modules/libpref/test/unit/test_defaultValues.js
@@ -0,0 +1,48 @@
+/* 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 providing a default value to get{Bool,Char,Float,Int}Pref */
+
+function run_test() {
+  var ps = Cc["@mozilla.org/preferences-service;1"]
+             .getService(Ci.nsIPrefService)
+             .QueryInterface(Ci.nsIPrefBranch);
+
+  let prefName = "test.default.values.bool";
+  do_check_throws(function() { ps.getBoolPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getBoolPref(prefName, false), false);
+  strictEqual(ps.getBoolPref(prefName, true), true);
+  ps.setBoolPref(prefName, true);
+  strictEqual(ps.getBoolPref(prefName), true);
+  strictEqual(ps.getBoolPref(prefName, false), true);
+  strictEqual(ps.getBoolPref(prefName, true), true);
+
+  prefName = "test.default.values.char";
+  do_check_throws(function() { ps.getCharPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getCharPref(prefName, ""), "");
+  strictEqual(ps.getCharPref(prefName, "string"), "string");
+  ps.setCharPref(prefName, "foo");
+  strictEqual(ps.getCharPref(prefName), "foo");
+  strictEqual(ps.getCharPref(prefName, "string"), "foo");
+
+  prefName = "test.default.values.float";
+  do_check_throws(function() { ps.getFloatPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getFloatPref(prefName, 3.5), 3.5);
+  strictEqual(ps.getFloatPref(prefName, 0), 0);
+  ps.setCharPref(prefName, 1.75);
+  strictEqual(ps.getFloatPref(prefName), 1.75);
+  strictEqual(ps.getFloatPref(prefName, 3.5), 1.75);
+
+  prefName = "test.default.values.int";
+  do_check_throws(function() { ps.getIntPref(prefName); },
+                  Cr.NS_ERROR_UNEXPECTED);
+  strictEqual(ps.getIntPref(prefName, 3), 3);
+  strictEqual(ps.getIntPref(prefName, 0), 0);
+  ps.setIntPref(prefName, 42);
+  strictEqual(ps.getIntPref(prefName), 42);
+  strictEqual(ps.getIntPref(prefName, 3), 42);
+}
diff --git a/modules/libpref/test/unit/xpcshell.ini b/modules/libpref/test/unit/xpcshell.ini
index 74c5690..6645886 100644
--- a/modules/libpref/test/unit/xpcshell.ini
+++ b/modules/libpref/test/unit/xpcshell.ini
@@ -13,6 +13,7 @@ support-files =
 [test_stickyprefs.js]
 support-files = data/testPrefSticky.js data/testPrefStickyUser.js
 [test_changeType.js]
+[test_defaultValues.js]
 [test_dirtyPrefs.js]
 [test_extprefs.js]
 [test_libPrefs.js]

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/pale-moon.git


More information about the x2go-commits mailing list