|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Test for DataStore - basic operation on a readonly db</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 ok("revisionId" in store, "store.revisionId exists"); |
|
42 ok("getLength" in store, "store.getLength exists"); |
|
43 ok("sync" in store, "store.sync exists"); |
|
44 |
|
45 gStore = stores[0]; |
|
46 |
|
47 runTest(); |
|
48 }, cbError); |
|
49 } |
|
50 |
|
51 function testStoreGet(id, value) { |
|
52 gStore.get(id).then(function(what) { |
|
53 ok(true, "store.get() retrieves data"); |
|
54 is(what, value, "store.get(" + id + ") returns " + value); |
|
55 }, function() { |
|
56 ok(false, "store.get(" + id + ") retrieves data"); |
|
57 }).then(runTest, cbError); |
|
58 } |
|
59 |
|
60 function testStoreAdd(value) { |
|
61 return gStore.add(value).then(function(what) { |
|
62 ok(true, "store.add() is called"); |
|
63 ok(what > 0, "store.add() returns something"); |
|
64 return what; |
|
65 }, cbError); |
|
66 } |
|
67 |
|
68 function testStorePut(value, id) { |
|
69 return gStore.put(value, id).then(function() { |
|
70 ok(true, "store.put() is called"); |
|
71 }, cbError); |
|
72 } |
|
73 |
|
74 function testStoreGetLength(number) { |
|
75 return gStore.getLength().then(function(n) { |
|
76 is(number, n, "store.getLength() returns the right number"); |
|
77 }, cbError); |
|
78 } |
|
79 |
|
80 function testStoreRemove(id) { |
|
81 return gStore.remove(id).then(function() { |
|
82 ok(true, "store.remove() is called"); |
|
83 }, cbError); |
|
84 } |
|
85 |
|
86 function testStoreClear() { |
|
87 return gStore.clear().then(function() { |
|
88 ok(true, "store.clear() is called"); |
|
89 }, cbError); |
|
90 } |
|
91 |
|
92 var tests = [ |
|
93 // Test for GetDataStore |
|
94 testGetDataStores, |
|
95 |
|
96 // Unknown ID |
|
97 function() { testStoreGet(42, undefined); }, |
|
98 function() { testStoreGet(42, undefined); }, // twice |
|
99 |
|
100 // Add + Get - number |
|
101 function() { testStoreAdd(42).then(function(id) { |
|
102 gId = id; runTest(); }, cbError); }, |
|
103 function() { testStoreGet(gId, 42); }, |
|
104 |
|
105 // Add + Get - boolean |
|
106 function() { testStoreAdd(true).then(function(id) { |
|
107 gId = id; runTest(); }, cbError); }, |
|
108 function() { testStoreGet(gId, true); }, |
|
109 |
|
110 // Add + Get - string |
|
111 function() { testStoreAdd("hello world").then(function(id) { |
|
112 gId = id; runTest(); }, cbError); }, |
|
113 function() { testStoreGet(gId, "hello world"); }, |
|
114 |
|
115 // Put + Get - string |
|
116 function() { testStorePut("hello world 2", gId).then(function() { |
|
117 runTest(); }, cbError); }, |
|
118 function() { testStoreGet(gId, "hello world 2"); }, |
|
119 |
|
120 // getLength |
|
121 function() { testStoreGetLength(3).then(function() { runTest(); }, cbError); }, |
|
122 |
|
123 // Remove |
|
124 function() { testStoreRemove(gId).then(function(what) { |
|
125 runTest(); }, cbError); }, |
|
126 function() { testStoreGet(gId, undefined); }, |
|
127 |
|
128 // Remove - wrong ID |
|
129 function() { testStoreRemove(gId).then(function(what) { |
|
130 runTest(); }, cbError); }, |
|
131 |
|
132 // Clear |
|
133 function() { testStoreClear().then(function(what) { |
|
134 runTest(); }, cbError); }, |
|
135 ]; |
|
136 |
|
137 function runTest() { |
|
138 if (!tests.length) { |
|
139 finish(); |
|
140 return; |
|
141 } |
|
142 |
|
143 var test = tests.shift(); |
|
144 test(); |
|
145 } |
|
146 |
|
147 runTest(); |
|
148 </script> |
|
149 </body> |
|
150 </html> |