Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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">
11 </div>
12 <pre id="test">
13 <script type="application/javascript;version=1.7">
15 var gStore;
16 var gChangeId = null;
17 var gChangeOperation = null;
19 function is(a, b, msg) {
20 alert((a === b ? 'OK' : 'KO') + ' ' + msg)
21 }
23 function ok(a, msg) {
24 alert((a ? 'OK' : 'KO')+ ' ' + msg)
25 }
27 function cbError() {
28 alert('KO error');
29 }
31 function finish() {
32 alert('DONE');
33 }
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');
41 gStore = stores[0];
42 runTest();
43 }, cbError);
44 }
46 function testStoreAdd(value, expectedId) {
47 gStore.add(value).then(function(id) {
48 is(id, expectedId, "store.add() is called");
49 }, cbError);
50 }
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 }
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 }
64 function testStoreClear() {
65 gStore.clear().catch(cbError);
66 }
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 }
78 var tests = [
79 // Test for GetDataStore
80 testGetDataStores,
82 // Add onchange = function
83 function() {
84 gStore.onchange = eventListener;
85 is(gStore.onchange, eventListener, "onChange is set");
86 runTest();
87 },
89 // Add
90 function() { gChangeId = 1; gChangeOperation = 'added';
91 testStoreAdd({ number: 42 }, 1); },
93 // Put
94 function() { gChangeId = 1; gChangeOperation = 'updated';
95 testStorePut({ number: 43 }, 1); },
97 // Remove
98 function() { gChangeId = 1; gChangeOperation = 'removed';
99 testStoreRemove(1, true); },
101 // Clear
102 function() { gChangeId = 0; gChangeOperation = 'cleared';
103 testStoreClear(); },
105 // Remove onchange function and replace it with addEventListener
106 function() {
107 gStore.onchange = null;
108 gStore.addEventListener('change', eventListener);
109 runTest();
110 },
112 // Add
113 function() { gChangeId = 2; gChangeOperation = 'added';
114 testStoreAdd({ number: 42 }, 2); },
116 // Put
117 function() { gChangeId = 2; gChangeOperation = 'updated';
118 testStorePut({ number: 43 }, 2); },
120 // Remove
121 function() { gChangeId = 2; gChangeOperation = 'removed';
122 testStoreRemove(2, true); },
124 // Clear
125 function() { gChangeId = 0; gChangeOperation = 'cleared';
126 testStoreClear(); },
128 // Remove event listener
129 function() {
130 gStore.removeEventListener('change', eventListener);
131 runTest();
132 },
133 ];
135 function runTest() {
136 if (!tests.length) {
137 finish();
138 return;
139 }
141 var test = tests.shift();
142 test();
143 }
145 runTest();
146 </script>
147 </pre>
148 </body>
149 </html>