services/sync/tests/unit/test_prefs_store.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://gre/modules/LightweightThemeManager.jsm");
michael@0 5 Cu.import("resource://gre/modules/Preferences.jsm");
michael@0 6 Cu.import("resource://gre/modules/Services.jsm");
michael@0 7 Cu.import("resource://services-common/utils.js");
michael@0 8 Cu.import("resource://services-sync/engines/prefs.js");
michael@0 9 Cu.import("resource://services-sync/service.js");
michael@0 10 Cu.import("resource://services-sync/util.js");
michael@0 11
michael@0 12 const PREFS_GUID = CommonUtils.encodeBase64URL(Services.appinfo.ID);
michael@0 13
michael@0 14 loadAddonTestFunctions();
michael@0 15 startupManager();
michael@0 16
michael@0 17 function makePersona(id) {
michael@0 18 return {
michael@0 19 id: id || Math.random().toString(),
michael@0 20 name: Math.random().toString(),
michael@0 21 headerURL: "http://localhost:1234/a"
michael@0 22 };
michael@0 23 }
michael@0 24
michael@0 25 function run_test() {
michael@0 26 let store = Service.engineManager.get("prefs")._store;
michael@0 27 let prefs = new Preferences();
michael@0 28 try {
michael@0 29
michael@0 30 _("Test fixtures.");
michael@0 31 Svc.Prefs.set("prefs.sync.testing.int", true);
michael@0 32 Svc.Prefs.set("prefs.sync.testing.string", true);
michael@0 33 Svc.Prefs.set("prefs.sync.testing.bool", true);
michael@0 34 Svc.Prefs.set("prefs.sync.testing.dont.change", true);
michael@0 35 Svc.Prefs.set("prefs.sync.testing.turned.off", false);
michael@0 36 Svc.Prefs.set("prefs.sync.testing.nonexistent", true);
michael@0 37
michael@0 38 prefs.set("testing.int", 123);
michael@0 39 prefs.set("testing.string", "ohai");
michael@0 40 prefs.set("testing.bool", true);
michael@0 41 prefs.set("testing.dont.change", "Please don't change me.");
michael@0 42 prefs.set("testing.turned.off", "I won't get synced.");
michael@0 43 prefs.set("testing.not.turned.on", "I won't get synced either!");
michael@0 44
michael@0 45 _("The GUID corresponds to XUL App ID.");
michael@0 46 let allIDs = store.getAllIDs();
michael@0 47 let ids = Object.keys(allIDs);
michael@0 48 do_check_eq(ids.length, 1);
michael@0 49 do_check_eq(ids[0], PREFS_GUID);
michael@0 50 do_check_true(allIDs[PREFS_GUID], true);
michael@0 51
michael@0 52 do_check_true(store.itemExists(PREFS_GUID));
michael@0 53 do_check_false(store.itemExists("random-gibberish"));
michael@0 54
michael@0 55 _("Unknown prefs record is created as deleted.");
michael@0 56 let record = store.createRecord("random-gibberish", "prefs");
michael@0 57 do_check_true(record.deleted);
michael@0 58
michael@0 59 _("Prefs record contains only prefs that should be synced.");
michael@0 60 record = store.createRecord(PREFS_GUID, "prefs");
michael@0 61 do_check_eq(record.value["testing.int"], 123);
michael@0 62 do_check_eq(record.value["testing.string"], "ohai");
michael@0 63 do_check_eq(record.value["testing.bool"], true);
michael@0 64 do_check_eq(record.value["testing.nonexistent"], null);
michael@0 65 do_check_false("testing.turned.off" in record.value);
michael@0 66 do_check_false("testing.not.turned.on" in record.value);
michael@0 67
michael@0 68 _("Prefs record contains pref sync prefs too.");
michael@0 69 do_check_eq(record.value["services.sync.prefs.sync.testing.int"], true);
michael@0 70 do_check_eq(record.value["services.sync.prefs.sync.testing.string"], true);
michael@0 71 do_check_eq(record.value["services.sync.prefs.sync.testing.bool"], true);
michael@0 72 do_check_eq(record.value["services.sync.prefs.sync.testing.dont.change"], true);
michael@0 73 do_check_eq(record.value["services.sync.prefs.sync.testing.turned.off"], false);
michael@0 74 do_check_eq(record.value["services.sync.prefs.sync.testing.nonexistent"], true);
michael@0 75
michael@0 76 _("Update some prefs, including one that's to be reset/deleted.");
michael@0 77 Svc.Prefs.set("testing.deleteme", "I'm going to be deleted!");
michael@0 78 record = new PrefRec("prefs", PREFS_GUID);
michael@0 79 record.value = {
michael@0 80 "testing.int": 42,
michael@0 81 "testing.string": "im in ur prefs",
michael@0 82 "testing.bool": false,
michael@0 83 "testing.deleteme": null,
michael@0 84 "services.sync.prefs.sync.testing.somepref": true
michael@0 85 };
michael@0 86 store.update(record);
michael@0 87 do_check_eq(prefs.get("testing.int"), 42);
michael@0 88 do_check_eq(prefs.get("testing.string"), "im in ur prefs");
michael@0 89 do_check_eq(prefs.get("testing.bool"), false);
michael@0 90 do_check_eq(prefs.get("testing.deleteme"), undefined);
michael@0 91 do_check_eq(prefs.get("testing.dont.change"), "Please don't change me.");
michael@0 92 do_check_eq(Svc.Prefs.get("prefs.sync.testing.somepref"), true);
michael@0 93
michael@0 94 _("Enable persona");
michael@0 95 // Ensure we don't go to the network to fetch personas and end up leaking
michael@0 96 // stuff.
michael@0 97 Services.io.offline = true;
michael@0 98 do_check_false(!!prefs.get("lightweightThemes.isThemeSelected"));
michael@0 99 do_check_eq(LightweightThemeManager.currentTheme, null);
michael@0 100
michael@0 101 let persona1 = makePersona();
michael@0 102 let persona2 = makePersona();
michael@0 103 let usedThemes = JSON.stringify([persona1, persona2]);
michael@0 104 record.value = {
michael@0 105 "lightweightThemes.isThemeSelected": true,
michael@0 106 "lightweightThemes.usedThemes": usedThemes
michael@0 107 };
michael@0 108 store.update(record);
michael@0 109 do_check_true(prefs.get("lightweightThemes.isThemeSelected"));
michael@0 110 do_check_true(Utils.deepEquals(LightweightThemeManager.currentTheme,
michael@0 111 persona1));
michael@0 112
michael@0 113 _("Disable persona");
michael@0 114 record.value = {
michael@0 115 "lightweightThemes.isThemeSelected": false,
michael@0 116 "lightweightThemes.usedThemes": usedThemes
michael@0 117 };
michael@0 118 store.update(record);
michael@0 119 do_check_false(prefs.get("lightweightThemes.isThemeSelected"));
michael@0 120 do_check_eq(LightweightThemeManager.currentTheme, null);
michael@0 121
michael@0 122 _("Only the current app's preferences are applied.");
michael@0 123 record = new PrefRec("prefs", "some-fake-app");
michael@0 124 record.value = {
michael@0 125 "testing.int": 98
michael@0 126 };
michael@0 127 store.update(record);
michael@0 128 do_check_eq(prefs.get("testing.int"), 42);
michael@0 129
michael@0 130 } finally {
michael@0 131 prefs.resetBranch("");
michael@0 132 }
michael@0 133 }

mercurial