|
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 <p id="display"></p> |
|
9 <div id="content" style="display: none"> |
|
10 |
|
11 </div> |
|
12 <pre id="test"> |
|
13 <script type="application/javascript;version=1.7"> |
|
14 |
|
15 var gStore; |
|
16 var gChangeId = null; |
|
17 var gChangeOperation = null; |
|
18 |
|
19 function is(a, b, msg) { |
|
20 alert((a === b ? 'OK' : 'KO') + ' ' + msg) |
|
21 } |
|
22 |
|
23 function ok(a, msg) { |
|
24 alert((a ? 'OK' : 'KO')+ ' ' + msg) |
|
25 } |
|
26 |
|
27 function cbError() { |
|
28 alert('KO error'); |
|
29 } |
|
30 |
|
31 function finish() { |
|
32 alert('DONE'); |
|
33 } |
|
34 |
|
35 function testGetDataStores() { |
|
36 navigator.getDataStores('foo').then(function(stores) { |
|
37 is(stores.length, 1, "getDataStores('foo') returns 1 element"); |
|
38 is(stores[0].name, 'foo', 'The dataStore.name is foo'); |
|
39 is(stores[0].readOnly, false, 'The dataStore foo is not in readonly'); |
|
40 |
|
41 gStore = stores[0]; |
|
42 runTest(); |
|
43 }, cbError); |
|
44 } |
|
45 |
|
46 function testStoreAdd(value, expectedId) { |
|
47 gStore.add(value).then(function(id) { |
|
48 is(id, expectedId, "store.add() is called"); |
|
49 }, cbError); |
|
50 } |
|
51 |
|
52 function testStorePut(value, id) { |
|
53 gStore.put(value, id).then(function(retId) { |
|
54 is(id, retId, "store.put() is called with the right id"); |
|
55 }, cbError); |
|
56 } |
|
57 |
|
58 function testStoreRemove(id, expectedSuccess) { |
|
59 gStore.remove(id).then(function(success) { |
|
60 is(success, expectedSuccess, "store.remove() returns the right value"); |
|
61 }, cbError); |
|
62 } |
|
63 |
|
64 function testStoreClear() { |
|
65 gStore.clear().catch(cbError); |
|
66 } |
|
67 |
|
68 function eventListener(evt) { |
|
69 ok(evt, "OnChangeListener is called with data"); |
|
70 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(evt.revisionId), true, "event.revisionId returns something"); |
|
71 if (gChangeId) { |
|
72 is(evt.id, gChangeId, "OnChangeListener is called with the right ID: " + evt.id); |
|
73 } |
|
74 is(evt.operation, gChangeOperation, "OnChangeListener is called with the right operation:" + evt.operation + " " + gChangeOperation); |
|
75 runTest(); |
|
76 } |
|
77 |
|
78 var tests = [ |
|
79 // Test for GetDataStore |
|
80 testGetDataStores, |
|
81 |
|
82 // Add onchange = function |
|
83 function() { |
|
84 gStore.onchange = eventListener; |
|
85 is(gStore.onchange, eventListener, "onChange is set"); |
|
86 runTest(); |
|
87 }, |
|
88 |
|
89 // Add |
|
90 function() { gChangeId = 1; gChangeOperation = 'added'; |
|
91 testStoreAdd({ number: 42 }, 1); }, |
|
92 |
|
93 // Put |
|
94 function() { gChangeId = 1; gChangeOperation = 'updated'; |
|
95 testStorePut({ number: 43 }, 1); }, |
|
96 |
|
97 // Remove |
|
98 function() { gChangeId = 1; gChangeOperation = 'removed'; |
|
99 testStoreRemove(1, true); }, |
|
100 |
|
101 // Clear |
|
102 function() { gChangeId = 0; gChangeOperation = 'cleared'; |
|
103 testStoreClear(); }, |
|
104 |
|
105 // Remove onchange function and replace it with addEventListener |
|
106 function() { |
|
107 gStore.onchange = null; |
|
108 gStore.addEventListener('change', eventListener); |
|
109 runTest(); |
|
110 }, |
|
111 |
|
112 // Add |
|
113 function() { gChangeId = 2; gChangeOperation = 'added'; |
|
114 testStoreAdd({ number: 42 }, 2); }, |
|
115 |
|
116 // Put |
|
117 function() { gChangeId = 2; gChangeOperation = 'updated'; |
|
118 testStorePut({ number: 43 }, 2); }, |
|
119 |
|
120 // Remove |
|
121 function() { gChangeId = 2; gChangeOperation = 'removed'; |
|
122 testStoreRemove(2, true); }, |
|
123 |
|
124 // Clear |
|
125 function() { gChangeId = 0; gChangeOperation = 'cleared'; |
|
126 testStoreClear(); }, |
|
127 |
|
128 // Remove event listener |
|
129 function() { |
|
130 gStore.removeEventListener('change', eventListener); |
|
131 runTest(); |
|
132 }, |
|
133 ]; |
|
134 |
|
135 function runTest() { |
|
136 if (!tests.length) { |
|
137 finish(); |
|
138 return; |
|
139 } |
|
140 |
|
141 var test = tests.shift(); |
|
142 test(); |
|
143 } |
|
144 |
|
145 runTest(); |
|
146 </script> |
|
147 </pre> |
|
148 </body> |
|
149 </html> |