1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_prefs_store.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://gre/modules/LightweightThemeManager.jsm"); 1.8 +Cu.import("resource://gre/modules/Preferences.jsm"); 1.9 +Cu.import("resource://gre/modules/Services.jsm"); 1.10 +Cu.import("resource://services-common/utils.js"); 1.11 +Cu.import("resource://services-sync/engines/prefs.js"); 1.12 +Cu.import("resource://services-sync/service.js"); 1.13 +Cu.import("resource://services-sync/util.js"); 1.14 + 1.15 +const PREFS_GUID = CommonUtils.encodeBase64URL(Services.appinfo.ID); 1.16 + 1.17 +loadAddonTestFunctions(); 1.18 +startupManager(); 1.19 + 1.20 +function makePersona(id) { 1.21 + return { 1.22 + id: id || Math.random().toString(), 1.23 + name: Math.random().toString(), 1.24 + headerURL: "http://localhost:1234/a" 1.25 + }; 1.26 +} 1.27 + 1.28 +function run_test() { 1.29 + let store = Service.engineManager.get("prefs")._store; 1.30 + let prefs = new Preferences(); 1.31 + try { 1.32 + 1.33 + _("Test fixtures."); 1.34 + Svc.Prefs.set("prefs.sync.testing.int", true); 1.35 + Svc.Prefs.set("prefs.sync.testing.string", true); 1.36 + Svc.Prefs.set("prefs.sync.testing.bool", true); 1.37 + Svc.Prefs.set("prefs.sync.testing.dont.change", true); 1.38 + Svc.Prefs.set("prefs.sync.testing.turned.off", false); 1.39 + Svc.Prefs.set("prefs.sync.testing.nonexistent", true); 1.40 + 1.41 + prefs.set("testing.int", 123); 1.42 + prefs.set("testing.string", "ohai"); 1.43 + prefs.set("testing.bool", true); 1.44 + prefs.set("testing.dont.change", "Please don't change me."); 1.45 + prefs.set("testing.turned.off", "I won't get synced."); 1.46 + prefs.set("testing.not.turned.on", "I won't get synced either!"); 1.47 + 1.48 + _("The GUID corresponds to XUL App ID."); 1.49 + let allIDs = store.getAllIDs(); 1.50 + let ids = Object.keys(allIDs); 1.51 + do_check_eq(ids.length, 1); 1.52 + do_check_eq(ids[0], PREFS_GUID); 1.53 + do_check_true(allIDs[PREFS_GUID], true); 1.54 + 1.55 + do_check_true(store.itemExists(PREFS_GUID)); 1.56 + do_check_false(store.itemExists("random-gibberish")); 1.57 + 1.58 + _("Unknown prefs record is created as deleted."); 1.59 + let record = store.createRecord("random-gibberish", "prefs"); 1.60 + do_check_true(record.deleted); 1.61 + 1.62 + _("Prefs record contains only prefs that should be synced."); 1.63 + record = store.createRecord(PREFS_GUID, "prefs"); 1.64 + do_check_eq(record.value["testing.int"], 123); 1.65 + do_check_eq(record.value["testing.string"], "ohai"); 1.66 + do_check_eq(record.value["testing.bool"], true); 1.67 + do_check_eq(record.value["testing.nonexistent"], null); 1.68 + do_check_false("testing.turned.off" in record.value); 1.69 + do_check_false("testing.not.turned.on" in record.value); 1.70 + 1.71 + _("Prefs record contains pref sync prefs too."); 1.72 + do_check_eq(record.value["services.sync.prefs.sync.testing.int"], true); 1.73 + do_check_eq(record.value["services.sync.prefs.sync.testing.string"], true); 1.74 + do_check_eq(record.value["services.sync.prefs.sync.testing.bool"], true); 1.75 + do_check_eq(record.value["services.sync.prefs.sync.testing.dont.change"], true); 1.76 + do_check_eq(record.value["services.sync.prefs.sync.testing.turned.off"], false); 1.77 + do_check_eq(record.value["services.sync.prefs.sync.testing.nonexistent"], true); 1.78 + 1.79 + _("Update some prefs, including one that's to be reset/deleted."); 1.80 + Svc.Prefs.set("testing.deleteme", "I'm going to be deleted!"); 1.81 + record = new PrefRec("prefs", PREFS_GUID); 1.82 + record.value = { 1.83 + "testing.int": 42, 1.84 + "testing.string": "im in ur prefs", 1.85 + "testing.bool": false, 1.86 + "testing.deleteme": null, 1.87 + "services.sync.prefs.sync.testing.somepref": true 1.88 + }; 1.89 + store.update(record); 1.90 + do_check_eq(prefs.get("testing.int"), 42); 1.91 + do_check_eq(prefs.get("testing.string"), "im in ur prefs"); 1.92 + do_check_eq(prefs.get("testing.bool"), false); 1.93 + do_check_eq(prefs.get("testing.deleteme"), undefined); 1.94 + do_check_eq(prefs.get("testing.dont.change"), "Please don't change me."); 1.95 + do_check_eq(Svc.Prefs.get("prefs.sync.testing.somepref"), true); 1.96 + 1.97 + _("Enable persona"); 1.98 + // Ensure we don't go to the network to fetch personas and end up leaking 1.99 + // stuff. 1.100 + Services.io.offline = true; 1.101 + do_check_false(!!prefs.get("lightweightThemes.isThemeSelected")); 1.102 + do_check_eq(LightweightThemeManager.currentTheme, null); 1.103 + 1.104 + let persona1 = makePersona(); 1.105 + let persona2 = makePersona(); 1.106 + let usedThemes = JSON.stringify([persona1, persona2]); 1.107 + record.value = { 1.108 + "lightweightThemes.isThemeSelected": true, 1.109 + "lightweightThemes.usedThemes": usedThemes 1.110 + }; 1.111 + store.update(record); 1.112 + do_check_true(prefs.get("lightweightThemes.isThemeSelected")); 1.113 + do_check_true(Utils.deepEquals(LightweightThemeManager.currentTheme, 1.114 + persona1)); 1.115 + 1.116 + _("Disable persona"); 1.117 + record.value = { 1.118 + "lightweightThemes.isThemeSelected": false, 1.119 + "lightweightThemes.usedThemes": usedThemes 1.120 + }; 1.121 + store.update(record); 1.122 + do_check_false(prefs.get("lightweightThemes.isThemeSelected")); 1.123 + do_check_eq(LightweightThemeManager.currentTheme, null); 1.124 + 1.125 + _("Only the current app's preferences are applied."); 1.126 + record = new PrefRec("prefs", "some-fake-app"); 1.127 + record.value = { 1.128 + "testing.int": 98 1.129 + }; 1.130 + store.update(record); 1.131 + do_check_eq(prefs.get("testing.int"), 42); 1.132 + 1.133 + } finally { 1.134 + prefs.resetBranch(""); 1.135 + } 1.136 +}