dom/datastore/tests/file_arrays.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:01f247f48017
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Test for DataStore - add([array]) remove([array])</title>
6 </head>
7 <body>
8 <div id="container"></div>
9 <script type="application/javascript;version=1.7">
10
11 var gStore;
12
13 function is(a, b, msg) {
14 alert((a === b ? 'OK' : 'KO') + ' ' + msg)
15 }
16
17 function ok(a, msg) {
18 alert((a ? 'OK' : 'KO')+ ' ' + msg)
19 }
20
21 function cbError() {
22 alert('KO error');
23 }
24
25 function finish() {
26 alert('DONE');
27 }
28
29 function testGetDataStores() {
30 navigator.getDataStores('foo').then(function(stores) {
31 is(stores.length, 1, "getDataStores('foo') returns 1 element");
32 is(stores[0].name, 'foo', 'The dataStore.name is foo');
33 is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
34
35 var store = stores[0];
36 ok("get" in store, "store.get exists");
37 ok("put" in store, "store.put exists");
38 ok("add" in store, "store.add exists");
39 ok("remove" in store, "store.remove exists");
40 ok("clear" in store, "store.clear exists");
41
42 gStore = stores[0];
43
44 runTest();
45 }, cbError);
46 }
47
48 var itemNumber = 60;
49
50 function testStoreAdd() {
51 var objects = [];
52 for (var i = 0; i < itemNumber; ++i) {
53 objects.push(i);
54 }
55
56 function testStoreAddInternal() {
57 if (!objects.length) {
58 ok(true, "We inserted " + itemNumber + " items");
59 runTest();
60 return;
61 }
62
63 var obj = objects.shift();
64 gStore.add(obj).then(function() {
65 ok(true, "We inserted a new item!");
66 testStoreAddInternal();
67 }, cbError);
68 }
69
70 testStoreAddInternal();
71 }
72
73 function testStoreGet() {
74 var objects = [];
75 for (var i = 1; i <= itemNumber; ++i) {
76 objects.push(i);
77 }
78
79 gStore.get.apply(gStore, objects).then(function(data) {
80 is(data.length, objects.length, "Get - Data matches");
81 for (var i = 0; i < data.length; ++i) {
82 is(data[i], objects[i] - 1, "Get - Data matches: " + i + " " + data[i] + " == " + objects[i]);
83 }
84 runTest();
85 }, cbError);
86 }
87
88 var tests = [
89 // Test for GetDataStore
90 testGetDataStores,
91
92 // Add many items
93 function() { testStoreAdd() },
94 function() { testStoreGet() },
95 ];
96
97 function runTest() {
98 if (!tests.length) {
99 finish();
100 return;
101 }
102
103 var test = tests.shift();
104 test();
105 }
106
107 runTest();
108 </script>
109 </body>
110 </html>

mercurial