Browse Source

store non-ASCII stings correctly

Myk Melez 15 years ago
parent
commit
f1724d0300
2 changed files with 29 additions and 3 deletions
  1. 7 3
      Preferences.js
  2. 22 0
      test/unit/test_Preferences.js

+ 7 - 3
Preferences.js

@@ -75,7 +75,7 @@ Preferences.prototype = {
 
     switch (this._prefSvc.getPrefType(prefName)) {
       case Ci.nsIPrefBranch.PREF_STRING:
-        return this._prefSvc.getCharPref(prefName);
+        return this._prefSvc.getComplexValue(prefName, Ci.nsISupportsString).data;
 
       case Ci.nsIPrefBranch.PREF_INT:
         return this._prefSvc.getIntPref(prefName);
@@ -106,9 +106,13 @@ Preferences.prototype = {
         break;
 
       case "string":
-      default:
-        this._prefSvc.setCharPref(prefName, prefValue);
+      default: {
+        let string = Cc["@mozilla.org/supports-string;1"].
+                     createInstance(Ci.nsISupportsString);
+        string.data = prefValue;
+        this._prefSvc.setComplexValue(prefName, Ci.nsISupportsString, string);
         break;
+      }
     }
   },
 

+ 22 - 0
test/unit/test_Preferences.js

@@ -31,6 +31,28 @@ function test_set_get_multiple_prefs() {
   Preferences.resetBranch("test_set_get_multiple_prefs.");
 }
 
+function test_set_get_unicode_pref() {
+  Preferences.set("test_set_get_unicode_pref", String.fromCharCode(960));
+  do_check_eq(Preferences.get("test_set_get_unicode_pref"), String.fromCharCode(960));
+
+  // Clean up.
+  Preferences.reset("test_set_get_unicode_pref");
+}
+
+// Make sure that we can get a string pref that we didn't set ourselves
+// (i.e. that the way we get a string pref using getComplexValue doesn't
+// hork us getting a string pref that wasn't set using setComplexValue).
+function test_get_string_pref() {
+  let svc = Cc["@mozilla.org/preferences-service;1"].
+            getService(Ci.nsIPrefService).
+            getBranch("");
+  svc.setCharPref("test_get_string_pref", "a normal string");
+  do_check_eq(Preferences.get("test_get_string_pref"), "a normal string");
+
+  // Clean up.
+  Preferences.reset("test_get_string_pref");
+}
+
 function test_reset_pref() {
   Preferences.set("test_reset_pref", 1);
   Preferences.reset("test_reset_pref");