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