|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Test for DataStore - transactional semantics</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 gStore = stores[0]; |
|
36 |
|
37 runTest(); |
|
38 }, cbError); |
|
39 } |
|
40 |
|
41 function checkError(e) { |
|
42 ok(e instanceof DOMError, "Expecting a DOMError"); |
|
43 is(e.name, "ConstraintError", "DOMError.name must be ConstraintError"); |
|
44 } |
|
45 |
|
46 var tests = [ |
|
47 // Test for GetDataStore |
|
48 testGetDataStores, |
|
49 |
|
50 // Add |
|
51 function() { gStore.add("data", null, 'foobar').catch(function(e) { |
|
52 ok(true, "Add rejects when the revision is wrong"); |
|
53 checkError(e); runTest(); }); }, |
|
54 |
|
55 function() { gStore.add("data").then(function() { |
|
56 ok(true, "Add succeeded without revisionId"); |
|
57 runTest(); }, cbError); }, |
|
58 |
|
59 function() { gStore.add("data", null, gStore.revisionId).then(function() { |
|
60 ok(true, "Add succeeded with the correct revisionId"); |
|
61 runTest(); }, cbError); }, |
|
62 |
|
63 function() { gStore.add("data", 42, 'foobar').catch(function(e) { |
|
64 ok(true, "Add rejects when the revision is wrong"); |
|
65 checkError(e); runTest(); }); }, |
|
66 |
|
67 function() { gStore.add("data", 42).then(function() { |
|
68 ok(true, "Add succeeded without revisionId"); |
|
69 runTest(); }, cbError); }, |
|
70 |
|
71 function() { gStore.add("data", 43, gStore.revisionId).then(function() { |
|
72 ok(true, "Add succeeded with the correct revisionId"); |
|
73 runTest(); }, cbError); }, |
|
74 |
|
75 // Put |
|
76 function() { gStore.put("data", 42, 'foobar').catch(function(e) { |
|
77 ok(true, "Put rejects when the revision is wrong"); |
|
78 checkError(e); runTest(); }); }, |
|
79 |
|
80 function() { gStore.put("data", 42).then(function() { |
|
81 ok(true, "Put succeeded without revisionId"); |
|
82 runTest(); }, cbError); }, |
|
83 |
|
84 function() { gStore.put("data", 42, gStore.revisionId).then(function() { |
|
85 ok(true, "Put succeeded with the correct revisionId"); |
|
86 runTest(); }, cbError); }, |
|
87 |
|
88 // Remove |
|
89 function() { gStore.remove(42, 'foobar').catch(function(e) { |
|
90 ok(true, "Remove rejects when the revision is wrong"); |
|
91 checkError(e); runTest(); }); }, |
|
92 |
|
93 function() { gStore.remove(42).then(function() { |
|
94 ok(true, "Remove succeeded without revisionId"); |
|
95 runTest(); }, cbError); }, |
|
96 |
|
97 function() { gStore.remove(42, gStore.revisionId).then(function() { |
|
98 ok(true, "Remove succeeded with the correct revisionId"); |
|
99 runTest(); }, cbError); }, |
|
100 |
|
101 // Clear |
|
102 function() { gStore.clear('foobar').catch(function(e) { |
|
103 ok(true, "Clear rejects when the revision is wrong"); |
|
104 checkError(e); runTest(); }); }, |
|
105 |
|
106 function() { gStore.clear().then(function() { |
|
107 ok(true, "Clear succeeded without revisionId"); |
|
108 runTest(); }, cbError); }, |
|
109 |
|
110 function() { gStore.clear(gStore.revisionId).then(function() { |
|
111 ok(true, "Clear succeeded with the correct revisionId"); |
|
112 runTest(); }, cbError); }, |
|
113 |
|
114 ]; |
|
115 |
|
116 function runTest() { |
|
117 if (!tests.length) { |
|
118 finish(); |
|
119 return; |
|
120 } |
|
121 |
|
122 var test = tests.shift(); |
|
123 test(); |
|
124 } |
|
125 |
|
126 runTest(); |
|
127 </script> |
|
128 </body> |
|
129 </html> |