Bug 541943: LightweightTheme doesn't support theme name/description in UTF-8. r=dtownsend

This commit is contained in:
Lingfeng Guan 2010-02-26 09:03:19 -08:00
Родитель 9da0aa2d23
Коммит 5fd942335d
2 изменённых файлов: 66 добавлений и 2 удалений

Просмотреть файл

@ -77,7 +77,8 @@ __defineGetter__("_ioService", function () {
var LightweightThemeManager = {
get usedThemes () {
try {
return JSON.parse(_prefs.getCharPref("usedThemes"));
return JSON.parse(_prefs.getComplexValue("usedThemes",
Ci.nsISupportsString).data);
} catch (e) {
return [];
}
@ -297,7 +298,10 @@ function _updateUsedThemes(aList) {
if (aList.length > MAX_USED_THEMES_COUNT)
aList.length = MAX_USED_THEMES_COUNT;
_prefs.setCharPref("usedThemes", JSON.stringify(aList));
var str = Cc["@mozilla.org/supports-string;1"]
.createInstance(Ci.nsISupportsString);
str.data = JSON.stringify(aList);
_prefs.setComplexValue("usedThemes", Ci.nsISupportsString, str);
_observerService.notifyObservers(null, "lightweight-theme-list-changed", null);
}

Просмотреть файл

@ -122,6 +122,41 @@ function run_test() {
do_check_eq(ltm.usedThemes.length, 0);
do_check_eq(ltm.currentTheme, null);
// Use chinese name to test utf-8, for bug #541943
var chineseTheme = dummy("chinese0");
chineseTheme.name = "中文0";
chineseTheme.description = "中文1";
ltm.currentTheme = chineseTheme;
do_check_eq(ltm.usedThemes.length, 1);
do_check_eq(ltm.currentTheme.name, "中文0");
do_check_eq(ltm.currentTheme.description, "中文1");
do_check_eq(ltm.usedThemes[0].name, "中文0");
do_check_eq(ltm.usedThemes[0].description, "中文1");
do_check_eq(ltm.getUsedTheme("chinese0").name, "中文0");
do_check_eq(ltm.getUsedTheme("chinese0").description, "中文1");
// This name used to break the usedTheme JSON causing all LWTs to be lost
var chineseTheme1 = dummy("chinese1");
chineseTheme1.name = "植物大战僵尸~最爱";
chineseTheme1.description = "植物大战僵尸~最爱";
ltm.currentTheme = chineseTheme1;
do_check_neq(ltm.currentTheme, null);
do_check_eq(ltm.usedThemes.length, 2);
do_check_eq(ltm.currentTheme.name, "植物大战僵尸~最爱");
do_check_eq(ltm.currentTheme.description, "植物大战僵尸~最爱");
do_check_eq(ltm.usedThemes[1].name, "中文0");
do_check_eq(ltm.usedThemes[1].description, "中文1");
do_check_eq(ltm.usedThemes[0].name, "植物大战僵尸~最爱");
do_check_eq(ltm.usedThemes[0].description, "植物大战僵尸~最爱");
ltm.forgetUsedTheme("chinese0");
do_check_eq(ltm.usedThemes.length, 1);
do_check_neq(ltm.currentTheme, null);
ltm.forgetUsedTheme("chinese1");
do_check_eq(ltm.usedThemes.length, 0);
do_check_eq(ltm.currentTheme, null);
do_check_eq(ltm.parseTheme("invalid json"), null);
do_check_eq(ltm.parseTheme('"json string"'), null);
@ -145,6 +180,14 @@ function run_test() {
data.id = [];
do_check_eq(roundtrip(data), null);
// Check whether parseTheme handles international characters right
var chineseTheme2 = dummy();
chineseTheme2.name = "植物大战僵尸~最爱";
chineseTheme2.description = "植物大战僵尸~最爱";
do_check_neq(roundtrip(chineseTheme2), null);
do_check_eq(roundtrip(chineseTheme2).name, "植物大战僵尸~最爱");
do_check_eq(roundtrip(chineseTheme2).description, "植物大战僵尸~最爱");
data = dummy();
data.unknownProperty = "Foo";
do_check_eq(typeof roundtrip(data).unknownProperty, "undefined");
@ -333,4 +376,21 @@ function run_test() {
ltm.forgetUsedTheme("x2");
do_check_eq(ltm.usedThemes.length, 0);
do_check_eq(ltm.currentTheme, null);
// Test whether a JSON set with setCharPref can be retrieved with usedThemes
ltm.currentTheme = dummy("x0");
ltm.currentTheme = dummy("x1");
prefs.setCharPref("lightweightThemes.usedThemes", JSON.stringify(ltm.usedThemes));
do_check_eq(ltm.usedThemes.length, 2);
do_check_eq(ltm.currentTheme.id, "x1");
do_check_eq(ltm.usedThemes[1].id, "x0");
do_check_eq(ltm.usedThemes[0].id, "x1");
ltm.forgetUsedTheme("x0");
do_check_eq(ltm.usedThemes.length, 1);
do_check_neq(ltm.currentTheme, null);
ltm.forgetUsedTheme("x1");
do_check_eq(ltm.usedThemes.length, 0);
do_check_eq(ltm.currentTheme, null);
}