|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Test for DataStore - string or unsigned long keys</title> |
|
6 </head> |
|
7 <body> |
|
8 <div id="container"></div> |
|
9 <script type="application/javascript;version=1.7"> |
|
10 |
|
11 var gStore; |
|
12 var gEvent; |
|
13 var gChangeId; |
|
14 |
|
15 function is(a, b, msg) { |
|
16 alert((a === b ? 'OK' : 'KO') + ' ' + msg) |
|
17 } |
|
18 |
|
19 function ok(a, msg) { |
|
20 alert((a ? 'OK' : 'KO')+ ' ' + msg) |
|
21 } |
|
22 |
|
23 function cbError() { |
|
24 alert('KO error'); |
|
25 } |
|
26 |
|
27 function finish() { |
|
28 alert('DONE'); |
|
29 } |
|
30 |
|
31 function testGetDataStores() { |
|
32 navigator.getDataStores('foo').then(function(stores) { |
|
33 gStore = stores[0]; |
|
34 runTest(); |
|
35 }, cbError); |
|
36 } |
|
37 |
|
38 function testAdd_noKey(key) { |
|
39 gEvent = 'added'; |
|
40 gChangeId = key; |
|
41 |
|
42 gStore.add({ a: 42 }).then(function(id) { |
|
43 is(id, key, "Id must be " + key + " received: " + id); |
|
44 }); |
|
45 } |
|
46 |
|
47 function testAdd_withKey(key) { |
|
48 gEvent = 'added'; |
|
49 gChangeId = key; |
|
50 |
|
51 gStore.add({ a: 42 }, key).then(function(id) { |
|
52 is(id, key, "Id must be " + key + " received: " + id); |
|
53 }); |
|
54 } |
|
55 |
|
56 function testPut(key) { |
|
57 gEvent = 'updated'; |
|
58 gChangeId = key; |
|
59 |
|
60 gStore.put({ a: 42 }, key).then(function(id) { |
|
61 is(id, key, "Id must be " + key + " received: " + id); |
|
62 }); |
|
63 } |
|
64 |
|
65 function testGet(key) { |
|
66 gStore.get(key).then(function(value) { |
|
67 ok(value, "Object received!"); |
|
68 is(value.a, 42, "Object received with right value!"); |
|
69 runTest(); |
|
70 }); |
|
71 } |
|
72 |
|
73 function testArrayGet(key) { |
|
74 gStore.get.apply(gStore, key).then(function(values) { |
|
75 is(values.length, key.length, "Object received!"); |
|
76 for (var i = 0; i < values.length; ++i) { |
|
77 is(values[i].a, 42, "Object received with right value!"); |
|
78 } |
|
79 |
|
80 runTest(); |
|
81 }); |
|
82 } |
|
83 |
|
84 function testRemove(key, success) { |
|
85 gEvent = 'removed'; |
|
86 gChangeId = key; |
|
87 |
|
88 gStore.remove(key).then(function(value) { |
|
89 is(value, success, "Status must be " + success + " received: " + value); |
|
90 if (value == false) { |
|
91 runTest(); |
|
92 return; |
|
93 } |
|
94 }); |
|
95 } |
|
96 |
|
97 function eventListener() { |
|
98 gStore.onchange = function(e) { |
|
99 is(e.operation, gEvent, "Operation matches: " + e.operation + " " + gEvent); |
|
100 ok(e.id === gChangeId, "Operation id matches"); |
|
101 runTest(); |
|
102 }; |
|
103 |
|
104 runTest(); |
|
105 } |
|
106 |
|
107 var tests = [ |
|
108 // Test for GetDataStore |
|
109 testGetDataStores, |
|
110 |
|
111 // Event listener |
|
112 eventListener, |
|
113 |
|
114 // add |
|
115 function() { testAdd_noKey(1); }, |
|
116 function() { testAdd_withKey(123); }, |
|
117 function() { testAdd_noKey(124); }, |
|
118 function() { testAdd_withKey('foobar'); }, |
|
119 function() { testAdd_noKey(125); }, |
|
120 function() { testAdd_withKey('125'); }, |
|
121 function() { testAdd_withKey('126'); }, |
|
122 function() { testAdd_noKey(126); }, |
|
123 |
|
124 // put |
|
125 function() { testPut(42); }, |
|
126 function() { testPut('42'); }, |
|
127 |
|
128 // get |
|
129 function() { testGet('42'); }, |
|
130 function() { testGet(42); }, |
|
131 function() { testGet(1); }, |
|
132 function() { testGet(123); }, |
|
133 function() { testGet(124); }, |
|
134 function() { testGet('foobar'); }, |
|
135 function() { testGet(125); }, |
|
136 function() { testGet('125'); }, |
|
137 function() { testGet('126'); }, |
|
138 function() { testGet(126); }, |
|
139 function() { testArrayGet(['42', 42, 1, 123, 124, 'foobar', 125, '125', '126', 126]); }, |
|
140 |
|
141 // remove |
|
142 function() { testRemove(42, true); }, |
|
143 function() { testRemove('42', true); }, |
|
144 function() { testRemove('43', false); }, |
|
145 function() { testRemove(43, false); }, |
|
146 ]; |
|
147 |
|
148 function runTest() { |
|
149 if (!tests.length) { |
|
150 finish(); |
|
151 return; |
|
152 } |
|
153 |
|
154 var test = tests.shift(); |
|
155 test(); |
|
156 } |
|
157 |
|
158 runTest(); |
|
159 </script> |
|
160 </body> |
|
161 </html> |