michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/LightweightThemeManager.jsm"); michael@0: Cu.import("resource://gre/modules/Preferences.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://services-common/utils.js"); michael@0: Cu.import("resource://services-sync/engines/prefs.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: const PREFS_GUID = CommonUtils.encodeBase64URL(Services.appinfo.ID); michael@0: michael@0: loadAddonTestFunctions(); michael@0: startupManager(); michael@0: michael@0: function makePersona(id) { michael@0: return { michael@0: id: id || Math.random().toString(), michael@0: name: Math.random().toString(), michael@0: headerURL: "http://localhost:1234/a" michael@0: }; michael@0: } michael@0: michael@0: function run_test() { michael@0: let store = Service.engineManager.get("prefs")._store; michael@0: let prefs = new Preferences(); michael@0: try { michael@0: michael@0: _("Test fixtures."); michael@0: Svc.Prefs.set("prefs.sync.testing.int", true); michael@0: Svc.Prefs.set("prefs.sync.testing.string", true); michael@0: Svc.Prefs.set("prefs.sync.testing.bool", true); michael@0: Svc.Prefs.set("prefs.sync.testing.dont.change", true); michael@0: Svc.Prefs.set("prefs.sync.testing.turned.off", false); michael@0: Svc.Prefs.set("prefs.sync.testing.nonexistent", true); michael@0: michael@0: prefs.set("testing.int", 123); michael@0: prefs.set("testing.string", "ohai"); michael@0: prefs.set("testing.bool", true); michael@0: prefs.set("testing.dont.change", "Please don't change me."); michael@0: prefs.set("testing.turned.off", "I won't get synced."); michael@0: prefs.set("testing.not.turned.on", "I won't get synced either!"); michael@0: michael@0: _("The GUID corresponds to XUL App ID."); michael@0: let allIDs = store.getAllIDs(); michael@0: let ids = Object.keys(allIDs); michael@0: do_check_eq(ids.length, 1); michael@0: do_check_eq(ids[0], PREFS_GUID); michael@0: do_check_true(allIDs[PREFS_GUID], true); michael@0: michael@0: do_check_true(store.itemExists(PREFS_GUID)); michael@0: do_check_false(store.itemExists("random-gibberish")); michael@0: michael@0: _("Unknown prefs record is created as deleted."); michael@0: let record = store.createRecord("random-gibberish", "prefs"); michael@0: do_check_true(record.deleted); michael@0: michael@0: _("Prefs record contains only prefs that should be synced."); michael@0: record = store.createRecord(PREFS_GUID, "prefs"); michael@0: do_check_eq(record.value["testing.int"], 123); michael@0: do_check_eq(record.value["testing.string"], "ohai"); michael@0: do_check_eq(record.value["testing.bool"], true); michael@0: do_check_eq(record.value["testing.nonexistent"], null); michael@0: do_check_false("testing.turned.off" in record.value); michael@0: do_check_false("testing.not.turned.on" in record.value); michael@0: michael@0: _("Prefs record contains pref sync prefs too."); michael@0: do_check_eq(record.value["services.sync.prefs.sync.testing.int"], true); michael@0: do_check_eq(record.value["services.sync.prefs.sync.testing.string"], true); michael@0: do_check_eq(record.value["services.sync.prefs.sync.testing.bool"], true); michael@0: do_check_eq(record.value["services.sync.prefs.sync.testing.dont.change"], true); michael@0: do_check_eq(record.value["services.sync.prefs.sync.testing.turned.off"], false); michael@0: do_check_eq(record.value["services.sync.prefs.sync.testing.nonexistent"], true); michael@0: michael@0: _("Update some prefs, including one that's to be reset/deleted."); michael@0: Svc.Prefs.set("testing.deleteme", "I'm going to be deleted!"); michael@0: record = new PrefRec("prefs", PREFS_GUID); michael@0: record.value = { michael@0: "testing.int": 42, michael@0: "testing.string": "im in ur prefs", michael@0: "testing.bool": false, michael@0: "testing.deleteme": null, michael@0: "services.sync.prefs.sync.testing.somepref": true michael@0: }; michael@0: store.update(record); michael@0: do_check_eq(prefs.get("testing.int"), 42); michael@0: do_check_eq(prefs.get("testing.string"), "im in ur prefs"); michael@0: do_check_eq(prefs.get("testing.bool"), false); michael@0: do_check_eq(prefs.get("testing.deleteme"), undefined); michael@0: do_check_eq(prefs.get("testing.dont.change"), "Please don't change me."); michael@0: do_check_eq(Svc.Prefs.get("prefs.sync.testing.somepref"), true); michael@0: michael@0: _("Enable persona"); michael@0: // Ensure we don't go to the network to fetch personas and end up leaking michael@0: // stuff. michael@0: Services.io.offline = true; michael@0: do_check_false(!!prefs.get("lightweightThemes.isThemeSelected")); michael@0: do_check_eq(LightweightThemeManager.currentTheme, null); michael@0: michael@0: let persona1 = makePersona(); michael@0: let persona2 = makePersona(); michael@0: let usedThemes = JSON.stringify([persona1, persona2]); michael@0: record.value = { michael@0: "lightweightThemes.isThemeSelected": true, michael@0: "lightweightThemes.usedThemes": usedThemes michael@0: }; michael@0: store.update(record); michael@0: do_check_true(prefs.get("lightweightThemes.isThemeSelected")); michael@0: do_check_true(Utils.deepEquals(LightweightThemeManager.currentTheme, michael@0: persona1)); michael@0: michael@0: _("Disable persona"); michael@0: record.value = { michael@0: "lightweightThemes.isThemeSelected": false, michael@0: "lightweightThemes.usedThemes": usedThemes michael@0: }; michael@0: store.update(record); michael@0: do_check_false(prefs.get("lightweightThemes.isThemeSelected")); michael@0: do_check_eq(LightweightThemeManager.currentTheme, null); michael@0: michael@0: _("Only the current app's preferences are applied."); michael@0: record = new PrefRec("prefs", "some-fake-app"); michael@0: record.value = { michael@0: "testing.int": 98 michael@0: }; michael@0: store.update(record); michael@0: do_check_eq(prefs.get("testing.int"), 42); michael@0: michael@0: } finally { michael@0: prefs.resetBranch(""); michael@0: } michael@0: }