dom/datastore/tests/file_sync.html

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:2716df214792
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Test for DataStore - sync</title>
6 </head>
7 <body>
8 <div id="container"></div>
9 <script type="application/javascript;version=1.7">
10
11 var gStore;
12 var gRevisions = [];
13 var gCursor;
14 var gExpectedEvents = true;
15
16 function is(a, b, msg) {
17 alert((a === b ? 'OK' : 'KO') + ' ' + msg)
18 }
19
20 function ok(a, msg) {
21 alert((a ? 'OK' : 'KO')+ ' ' + msg)
22 }
23
24 function cbError() {
25 alert('KO error');
26 }
27
28 function finish() {
29 alert('DONE');
30 }
31
32 function testGetDataStores() {
33 navigator.getDataStores('foo').then(function(stores) {
34 is(stores.length, 1, "getDataStores('foo') returns 1 element");
35
36 gStore = stores[0];
37 gRevisions.push(gStore.revisionId);
38
39 gStore.onchange = function(aEvent) {
40 ok(gExpectedEvents, "Events received!");
41 runTest();
42 }
43
44 runTest();
45 }, cbError);
46 }
47
48 function testBasicInterface() {
49 var cursor = gStore.sync();
50 ok(cursor, "Cursor is created");
51 is(cursor.store, gStore, "Cursor.store is the store");
52
53 ok("next" in cursor, "Cursor.next exists");
54 ok("close" in cursor, "Cursor.close exists");
55
56 cursor.close();
57
58 runTest();
59 }
60
61 function testCursor(cursor, steps) {
62 if (!steps.length) {
63 runTest();
64 return;
65 }
66
67 var step = steps.shift();
68 cursor.next().then(function(data) {
69 ok(!!data, "Cursor.next returns data");
70 is(data.operation, step.operation, "Waiting for operation: '" + step.operation + "' received '" + data.operation + "'");
71
72
73 switch (data.operation) {
74 case 'done':
75 is(/[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}/.test(data.revisionId), true, "done has a valid revisionId");
76 is (data.revisionId, gRevisions[gRevisions.length-1], "Last revision matches");
77 break;
78
79 case 'add':
80 case 'update':
81 if ('id' in step) {
82 is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
83 }
84
85 if ('data' in step) {
86 is(data.data, step.data, "next() add: data matches: " + data.data + " " + step.data);
87 }
88
89 break;
90
91 case 'remove':
92 if ('id' in step) {
93 is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
94 }
95
96 break;
97 }
98
99 testCursor(cursor, steps);
100 });
101 }
102
103 var tests = [
104 // Test for GetDataStore
105 testGetDataStores,
106
107 // interface test
108 testBasicInterface,
109
110 // empty DataStore
111 function() {
112 var cursor = gStore.sync();
113 var steps = [ { operation: 'clear' },
114 { operation: 'done' },
115 { operation: 'done' }];
116 testCursor(cursor, steps);
117 },
118
119 function() {
120 gExpectedEvents = false;
121 var cursor = gStore.sync('wrong revision ID');
122 var steps = [ { operation: 'clear' },
123 { operation: 'done' },
124 { operation: 'done' }];
125 testCursor(cursor, steps);
126 },
127
128 function() {
129 var cursor = gStore.sync(gRevisions[0]);
130 var steps = [ { operation: 'done' },
131 { operation: 'done' }];
132 testCursor(cursor, steps);
133 },
134
135 // Test add from scratch
136 function() {
137 gExpectedEvents = true;
138
139 gStore.add(1).then(function(id) {
140 gRevisions.push(gStore.revisionId);
141 ok(true, "Item: " + id + " added");
142 });
143 },
144
145 function() {
146 gStore.add(2,"foobar").then(function(id) {
147 gRevisions.push(gStore.revisionId);
148 ok(true, "Item: " + id + " added");
149 });
150 },
151
152 function() {
153 gStore.add(3,3).then(function(id) {
154 gRevisions.push(gStore.revisionId);
155 ok(true, "Item: " + id + " added");
156 });
157 },
158
159 function() {
160 gExpectedEvents = false;
161 var cursor = gStore.sync();
162 var steps = [ { operation: 'clear', },
163 { operation: 'add', id: 1, data: 1 },
164 { operation: 'add', id: 3, data: 3 },
165 { operation: 'add', id: 'foobar', data: 2 },
166 { operation: 'done' }];
167 testCursor(cursor, steps);
168 },
169
170 function() {
171 var cursor = gStore.sync('wrong revision ID');
172 var steps = [ { operation: 'clear', },
173 { operation: 'add', id: 1, data: 1 },
174 { operation: 'add', id: 3, data: 3 },
175 { operation: 'add', id: 'foobar', data: 2 },
176 { operation: 'done' }];
177 testCursor(cursor, steps);
178 },
179
180 function() {
181 var cursor = gStore.sync(gRevisions[0]);
182 var steps = [ { operation: 'add', id: 1, data: 1 },
183 { operation: 'add', id: 'foobar', data: 2 },
184 { operation: 'add', id: 3, data: 3 },
185 { operation: 'done' }];
186 testCursor(cursor, steps);
187 },
188
189 function() {
190 var cursor = gStore.sync(gRevisions[1]);
191 var steps = [ { operation: 'add', id: 'foobar', data: 2 },
192 { operation: 'add', id: 3, data: 3 },
193 { operation: 'done' }];
194 testCursor(cursor, steps);
195 },
196
197 function() {
198 var cursor = gStore.sync(gRevisions[2]);
199 var steps = [ { operation: 'add', id: 3, data: 3 },
200 { operation: 'done' }];
201 testCursor(cursor, steps);
202 },
203
204 function() {
205 var cursor = gStore.sync(gRevisions[3]);
206 var steps = [ { operation: 'done' }];
207 testCursor(cursor, steps);
208 },
209
210 // Test after an update
211 function() {
212 gExpectedEvents = true;
213 gStore.put(123, 1).then(function() {
214 gRevisions.push(gStore.revisionId);
215 });
216 },
217
218 function() {
219 gExpectedEvents = false;
220 var cursor = gStore.sync();
221 var steps = [ { operation: 'clear', },
222 { operation: 'add', id: 1, data: 123 },
223 { operation: 'add', id: 3, data: 3 },
224 { operation: 'add', id: 'foobar', data: 2 },
225 { operation: 'done' }];
226 testCursor(cursor, steps);
227 },
228
229 function() {
230 var cursor = gStore.sync('wrong revision ID');
231 var steps = [ { operation: 'clear', },
232 { operation: 'add', id: 1, data: 123 },
233 { operation: 'add', id: 3, data: 3 },
234 { operation: 'add', id: 'foobar', data: 2 },
235 { operation: 'done' }];
236 testCursor(cursor, steps);
237 },
238
239 function() {
240 var cursor = gStore.sync(gRevisions[0]);
241 var steps = [ { operation: 'add', id: 1, data: 123 },
242 { operation: 'add', id: 'foobar', data: 2 },
243 { operation: 'add', id: 3, data: 3 },
244 { operation: 'done' }];
245 testCursor(cursor, steps);
246 },
247
248 function() {
249 var cursor = gStore.sync(gRevisions[1]);
250 var steps = [ { operation: 'add', id: 'foobar', data: 2 },
251 { operation: 'add', id: 3, data: 3 },
252 { operation: 'update', id: 1, data: 123 },
253 { operation: 'done' }];
254 testCursor(cursor, steps);
255 },
256
257 function() {
258 var cursor = gStore.sync(gRevisions[2]);
259 var steps = [ { operation: 'add', id: 3, data: 3 },
260 { operation: 'update', id: 1, data: 123 },
261 { operation: 'done' }];
262 testCursor(cursor, steps);
263 },
264
265 function() {
266 var cursor = gStore.sync(gRevisions[3]);
267 var steps = [ { operation: 'update', id: 1, data: 123 },
268 { operation: 'done' }];
269 testCursor(cursor, steps);
270 },
271
272 function() {
273 var cursor = gStore.sync(gRevisions[4]);
274 var steps = [ { operation: 'done' }];
275 testCursor(cursor, steps);
276 },
277
278 // Test after a remove
279 function() {
280 gExpectedEvents = true;
281 gStore.remove(3).then(function() {
282 gRevisions.push(gStore.revisionId);
283 });
284 },
285
286 function() {
287 gExpectedEvents = false;
288 var cursor = gStore.sync();
289 var steps = [ { operation: 'clear', },
290 { operation: 'add', id: 1, data: 123 },
291 { operation: 'add', id: 'foobar', data: 2 },
292 { operation: 'done' }];
293 testCursor(cursor, steps);
294 },
295
296 function() {
297 var cursor = gStore.sync('wrong revision ID');
298 var steps = [ { operation: 'clear', },
299 { operation: 'add', id: 1, data: 123 },
300 { operation: 'add', id: 'foobar', data: 2 },
301 { operation: 'done' }];
302 testCursor(cursor, steps);
303 },
304
305 function() {
306 var cursor = gStore.sync(gRevisions[0]);
307 var steps = [ { operation: 'add', id: 1, data: 123 },
308 { operation: 'add', id: 'foobar', data: 2 },
309 { operation: 'done' }];
310 testCursor(cursor, steps);
311 },
312
313 function() {
314 var cursor = gStore.sync(gRevisions[1]);
315 var steps = [ { operation: 'add', id: 'foobar', data: 2 },
316 { operation: 'update', id: 1, data: 123 },
317 { operation: 'done' }];
318 testCursor(cursor, steps);
319 },
320
321 function() {
322 var cursor = gStore.sync(gRevisions[2]);
323 var steps = [ { operation: 'update', id: 1, data: 123 },
324 { operation: 'done' }];
325 testCursor(cursor, steps);
326 },
327
328 function() {
329 var cursor = gStore.sync(gRevisions[3]);
330 var steps = [ { operation: 'update', id: 1, data: 123 },
331 { operation: 'remove', id: 3 },
332 { operation: 'done' }];
333 testCursor(cursor, steps);
334 },
335
336 function() {
337 var cursor = gStore.sync(gRevisions[4]);
338 var steps = [ { operation: 'remove', id: 3 },
339 { operation: 'done' }];
340 testCursor(cursor, steps);
341 },
342
343 function() {
344 var cursor = gStore.sync(gRevisions[5]);
345 var steps = [ { operation: 'done' }];
346 testCursor(cursor, steps);
347 },
348
349 // New events when the cursor is active
350 function() {
351 gCursor = gStore.sync();
352 var steps = [ { operation: 'clear', },
353 { operation: 'add', id: 1, data: 123 },
354 { operation: 'add', id: 'foobar', data: 2 } ];
355 testCursor(gCursor, steps);
356 },
357
358 function() {
359 gStore.add(42, 2).then(function(id) {
360 ok(true, "Item: " + id + " added");
361 gRevisions.push(gStore.revisionId);
362 runTest();
363 });
364 },
365
366 function() {
367 var steps = [ { operation: 'clear', },
368 { operation: 'add', id: 1, data: 123 },
369 { operation: 'add', id: 2, data: 42 },
370 { operation: 'add', id: 'foobar', data: 2 } ]
371 testCursor(gCursor, steps);
372 },
373
374 function() {
375 gStore.put(43, 2).then(function(id) {
376 gRevisions.push(gStore.revisionId);
377 runTest();
378 });
379 },
380
381 function() {
382 var steps = [ { operation: 'clear', },
383 { operation: 'add', id: 1, data: 123 },
384 { operation: 'add', id: 2, data: 43 },
385 { operation: 'add', id: 'foobar', data: 2 } ]
386 testCursor(gCursor, steps);
387 },
388
389 function() {
390 gStore.remove(2).then(function(id) {
391 gRevisions.push(gStore.revisionId);
392 runTest();
393 });
394 },
395
396 function() {
397 var steps = [ { operation: 'clear', },
398 { operation: 'add', id: 1, data: 123 },
399 { operation: 'add', id: 'foobar', data: 2 } ]
400 testCursor(gCursor, steps);
401 },
402
403 function() {
404 gStore.add(42).then(function(id) {
405 ok(true, "Item: " + id + " added");
406 gRevisions.push(gStore.revisionId);
407 runTest();
408 });
409 },
410
411 function() {
412 var steps = [ { operation: 'clear', },
413 { operation: 'add', id: 1, data: 123 },
414 { operation: 'add', id: 4, data: 42 },
415 { operation: 'add', id: 'foobar', data: 2 } ]
416 testCursor(gCursor, steps);
417 },
418
419 function() {
420 gStore.clear().then(function() {
421 gRevisions.push(gStore.revisionId);
422 runTest();
423 });
424 },
425
426 function() {
427 var steps = [ { operation: 'clear' } ];
428 testCursor(gCursor, steps);
429 },
430
431 function() {
432 gStore.add(42).then(function(id) {
433 ok(true, "Item: " + id + " added");
434 gRevisions.push(gStore.revisionId);
435 runTest();
436 });
437 },
438
439 function() {
440 var steps = [ { operation: 'clear', },
441 { operation: 'add', id: 5, data: 42 } ];
442 testCursor(gCursor, steps);
443 },
444
445 function() {
446 gStore.clear().then(function() {
447 gRevisions.push(gStore.revisionId);
448 runTest();
449 });
450 },
451
452 function() {
453 gStore.add(42).then(function(id) {
454 ok(true, "Item: " + id + " added");
455 gRevisions.push(gStore.revisionId);
456 runTest();
457 });
458 },
459
460 function() {
461 var steps = [ { operation: 'clear' },
462 { operation: 'add', id: 6, data: 42 },
463 { operation: 'done'} ];
464 testCursor(gCursor, steps);
465 },
466
467 function() {
468 gExpectedEvents = true;
469 gStore.add(42).then(function(id) {
470 });
471 }
472 ];
473
474 function runTest() {
475 if (!tests.length) {
476 finish();
477 return;
478 }
479
480 var test = tests.shift();
481 test();
482 }
483
484 runTest();
485 </script>
486 </body>
487 </html>

mercurial