Browse Source

implement resetBranch ourselves, as the current implementation of nsIPrefBranch in Mozilla doesn't implement it for us

Myk Melez 15 years ago
parent
commit
ba233cdb05
2 changed files with 36 additions and 2 deletions
  1. 19 2
      Preferences.js
  2. 17 0
      test/unit/test_Preferences.js

+ 19 - 2
Preferences.js

@@ -138,6 +138,13 @@ Preferences.prototype = {
   },
   },
 
 
   reset: function(prefName) {
   reset: function(prefName) {
+    // We can't check for |prefName.constructor == Array| here, since we have
+    // a different global object, so we check the constructor name instead.
+    if (typeof prefName == "object" && prefName.constructor.name == Array.name) {
+      prefName.map(function(v) this.reset(v), this);
+      return;
+    }
+
     try {
     try {
       this._prefSvc.clearUserPref(prefName);
       this._prefSvc.clearUserPref(prefName);
     }
     }
@@ -149,13 +156,23 @@ Preferences.prototype = {
       // resetting first or trap exceptions after the fact.  It passes through
       // resetting first or trap exceptions after the fact.  It passes through
       // other exceptions, however, so callers know about them, since we don't
       // other exceptions, however, so callers know about them, since we don't
       // know what other exceptions might be thrown and what they might mean.
       // know what other exceptions might be thrown and what they might mean.
-      if (ex.result != Components.results.NS_ERROR_UNEXPECTED)
+      if (ex.result != Cr.NS_ERROR_UNEXPECTED)
         throw ex;
         throw ex;
     }
     }
   },
   },
 
 
   resetBranch: function(prefBranch) {
   resetBranch: function(prefBranch) {
-    this._prefSvc.resetBranch(prefBranch);
+    try {
+      this._prefSvc.resetBranch(prefBranch);
+    }
+    catch(ex) {
+      // The current implementation of nsIPrefBranch in Mozilla
+      // doesn't implement resetBranch, so we do it ourselves.
+      if (ex.result == Cr.NS_ERROR_NOT_IMPLEMENTED)
+        this.reset(this._prefSvc.getChildList(prefBranch, []));
+      else
+        throw ex;
+    }
   }
   }
 
 
 };
 };

+ 17 - 0
test/unit/test_Preferences.js

@@ -5,3 +5,20 @@ Components.utils.import("resource://jsmodules/Preferences.js");
 function test_reset_nonexistent_pref() {
 function test_reset_nonexistent_pref() {
   Preferences.reset("nonexistent.pref");
   Preferences.reset("nonexistent.pref");
 }
 }
+
+// Make sure the module doesn't throw an exception when asked to reset
+// a nonexistent pref branch.
+function test_reset_nonexistent_pref_branch() {
+  Preferences.resetBranch("nonexistent.pref.branch.");
+}
+
+function test_reset_pref_branch() {
+  Preferences.set("pref.branch.foo", 1);
+  Preferences.set("pref.branch.bar", 2);
+  do_check_eq(Preferences.get("pref.branch.foo"), 1);
+  do_check_eq(Preferences.get("pref.branch.bar"), 2);
+
+  Preferences.resetBranch("pref.branch.");
+  do_check_eq(Preferences.get("pref.branch.foo"), undefined);
+  do_check_eq(Preferences.get("pref.branch.bar"), undefined);
+}