Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 Cu.import("resource://services-sync/engines.js");
5 Cu.import("resource://services-sync/service.js");
6 Cu.import("resource://services-sync/util.js");
7 Cu.import("resource://testing-common/services/sync/utils.js");
9 function makeSteamEngine() {
10 return new SyncEngine('Steam', Service);
11 }
13 let server;
15 function test_url_attributes() {
16 _("SyncEngine url attributes");
17 let syncTesting = new SyncTestingInfrastructure(server);
18 Service.clusterURL = "https://cluster/";
19 let engine = makeSteamEngine();
20 try {
21 do_check_eq(engine.storageURL, "https://cluster/1.1/foo/storage/");
22 do_check_eq(engine.engineURL, "https://cluster/1.1/foo/storage/steam");
23 do_check_eq(engine.metaURL, "https://cluster/1.1/foo/storage/meta/global");
24 } finally {
25 Svc.Prefs.resetBranch("");
26 }
27 }
29 function test_syncID() {
30 _("SyncEngine.syncID corresponds to preference");
31 let syncTesting = new SyncTestingInfrastructure(server);
32 let engine = makeSteamEngine();
33 try {
34 // Ensure pristine environment
35 do_check_eq(Svc.Prefs.get("steam.syncID"), undefined);
37 // Performing the first get on the attribute will generate a new GUID.
38 do_check_eq(engine.syncID, "fake-guid-0");
39 do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-0");
41 Svc.Prefs.set("steam.syncID", Utils.makeGUID());
42 do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-1");
43 do_check_eq(engine.syncID, "fake-guid-1");
44 } finally {
45 Svc.Prefs.resetBranch("");
46 }
47 }
49 function test_lastSync() {
50 _("SyncEngine.lastSync and SyncEngine.lastSyncLocal correspond to preferences");
51 let syncTesting = new SyncTestingInfrastructure(server);
52 let engine = makeSteamEngine();
53 try {
54 // Ensure pristine environment
55 do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined);
56 do_check_eq(engine.lastSync, 0);
57 do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined);
58 do_check_eq(engine.lastSyncLocal, 0);
60 // Floats are properly stored as floats and synced with the preference
61 engine.lastSync = 123.45;
62 do_check_eq(engine.lastSync, 123.45);
63 do_check_eq(Svc.Prefs.get("steam.lastSync"), "123.45");
65 // Integer is properly stored
66 engine.lastSyncLocal = 67890;
67 do_check_eq(engine.lastSyncLocal, 67890);
68 do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), "67890");
70 // resetLastSync() resets the value (and preference) to 0
71 engine.resetLastSync();
72 do_check_eq(engine.lastSync, 0);
73 do_check_eq(Svc.Prefs.get("steam.lastSync"), "0");
74 } finally {
75 Svc.Prefs.resetBranch("");
76 }
77 }
79 function test_toFetch() {
80 _("SyncEngine.toFetch corresponds to file on disk");
81 let syncTesting = new SyncTestingInfrastructure(server);
82 const filename = "weave/toFetch/steam.json";
83 let engine = makeSteamEngine();
84 try {
85 // Ensure pristine environment
86 do_check_eq(engine.toFetch.length, 0);
88 // Write file to disk
89 let toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
90 engine.toFetch = toFetch;
91 do_check_eq(engine.toFetch, toFetch);
92 // toFetch is written asynchronously
93 engine._store._sleep(0);
94 let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
95 do_check_eq(fakefile, JSON.stringify(toFetch));
97 // Read file from disk
98 toFetch = [Utils.makeGUID(), Utils.makeGUID()];
99 syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(toFetch);
100 engine.loadToFetch();
101 do_check_eq(engine.toFetch.length, 2);
102 do_check_eq(engine.toFetch[0], toFetch[0]);
103 do_check_eq(engine.toFetch[1], toFetch[1]);
104 } finally {
105 Svc.Prefs.resetBranch("");
106 }
107 }
109 function test_previousFailed() {
110 _("SyncEngine.previousFailed corresponds to file on disk");
111 let syncTesting = new SyncTestingInfrastructure(server);
112 const filename = "weave/failed/steam.json";
113 let engine = makeSteamEngine();
114 try {
115 // Ensure pristine environment
116 do_check_eq(engine.previousFailed.length, 0);
118 // Write file to disk
119 let previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
120 engine.previousFailed = previousFailed;
121 do_check_eq(engine.previousFailed, previousFailed);
122 // previousFailed is written asynchronously
123 engine._store._sleep(0);
124 let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
125 do_check_eq(fakefile, JSON.stringify(previousFailed));
127 // Read file from disk
128 previousFailed = [Utils.makeGUID(), Utils.makeGUID()];
129 syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(previousFailed);
130 engine.loadPreviousFailed();
131 do_check_eq(engine.previousFailed.length, 2);
132 do_check_eq(engine.previousFailed[0], previousFailed[0]);
133 do_check_eq(engine.previousFailed[1], previousFailed[1]);
134 } finally {
135 Svc.Prefs.resetBranch("");
136 }
137 }
139 function test_resetClient() {
140 _("SyncEngine.resetClient resets lastSync and toFetch");
141 let syncTesting = new SyncTestingInfrastructure(server);
142 let engine = makeSteamEngine();
143 try {
144 // Ensure pristine environment
145 do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined);
146 do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined);
147 do_check_eq(engine.toFetch.length, 0);
149 engine.lastSync = 123.45;
150 engine.lastSyncLocal = 67890;
151 engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
152 engine.previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
154 engine.resetClient();
155 do_check_eq(engine.lastSync, 0);
156 do_check_eq(engine.lastSyncLocal, 0);
157 do_check_eq(engine.toFetch.length, 0);
158 do_check_eq(engine.previousFailed.length, 0);
159 } finally {
160 Svc.Prefs.resetBranch("");
161 }
162 }
164 function test_wipeServer() {
165 _("SyncEngine.wipeServer deletes server data and resets the client.");
166 let engine = makeSteamEngine();
168 const PAYLOAD = 42;
169 let steamCollection = new ServerWBO("steam", PAYLOAD);
170 let server = httpd_setup({
171 "/1.1/foo/storage/steam": steamCollection.handler()
172 });
173 let syncTesting = new SyncTestingInfrastructure(server);
174 do_test_pending();
176 try {
177 // Some data to reset.
178 engine.lastSync = 123.45;
179 engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
181 _("Wipe server data and reset client.");
182 engine.wipeServer();
183 do_check_eq(steamCollection.payload, undefined);
184 do_check_eq(engine.lastSync, 0);
185 do_check_eq(engine.toFetch.length, 0);
187 } finally {
188 server.stop(do_test_finished);
189 Svc.Prefs.resetBranch("");
190 }
191 }
193 function run_test() {
194 server = httpd_setup({});
195 test_url_attributes();
196 test_syncID();
197 test_lastSync();
198 test_toFetch();
199 test_previousFailed();
200 test_resetClient();
201 test_wipeServer();
203 server.stop(run_next_test);
204 }