OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>IndexedDB: Test IDBIndex.getAll.</title> | 2 <title>IndexedDB: Test IDBIndex.getAll.</title> |
3 <script src="../../resources/testharness.js"></script> | 3 <script src="../../resources/testharness.js"></script> |
4 <script src="../../resources/testharnessreport.js"></script> | 4 <script src="../../resources/testharnessreport.js"></script> |
5 <script> | 5 <script> |
6 | 6 |
| 7 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); |
| 8 var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); |
| 9 |
7 function doSetup(dbName, dbVersion, onsuccess) { | 10 function doSetup(dbName, dbVersion, onsuccess) { |
8 var delete_request = indexedDB.deleteDatabase(dbName); | 11 var delete_request = indexedDB.deleteDatabase(dbName); |
9 delete_request.onerror = function() { | 12 delete_request.onerror = function() { |
10 assert_unreached('deleteDatabase should not fail'); | 13 assert_unreached('deleteDatabase should not fail'); |
11 }; | 14 }; |
12 delete_request.onsuccess = function(e) { | 15 delete_request.onsuccess = function(e) { |
13 var req = indexedDB.open(dbName, dbVersion); | 16 var req = indexedDB.open(dbName, dbVersion); |
14 req.onsuccess = onsuccess; | 17 req.onsuccess = onsuccess; |
15 req.onerror = function() { | 18 req.onerror = function() { |
16 assert_unreached('open should not fail'); | 19 assert_unreached('open should not fail'); |
17 }; | 20 }; |
18 req.onupgradeneeded = function(evt) { | 21 req.onupgradeneeded = function(evt) { |
19 var connection = evt.target.result; | 22 var connection = evt.target.result; |
20 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
21 | 23 |
22 var store = connection.createObjectStore('generated', | 24 var store = connection.createObjectStore('generated', |
23 {autoIncrement: true, keyPath: 'id'}); | 25 {autoIncrement: true, keyPath: 'id'}); |
24 var index = store.createIndex('test_idx', 'upper'); | 26 var index = store.createIndex('test_idx', 'upper'); |
25 alphabet.forEach(function(letter) { | 27 alphabet.forEach(function(letter) { |
26 store.put({ch: letter, upper: letter.toUpperCase()}); | 28 store.put({ch: letter, upper: letter.toUpperCase()}); |
27 }); | 29 }); |
28 | 30 |
29 store = connection.createObjectStore('out-of-line', null); | 31 store = connection.createObjectStore('out-of-line', null); |
30 index = store.createIndex('test_idx', 'upper'); | 32 index = store.createIndex('test_idx', 'upper'); |
(...skipping 30 matching lines...) Expand all Loading... |
61 }; | 63 }; |
62 }; | 64 }; |
63 } | 65 } |
64 | 66 |
65 function createGetAllRequest(t, storeName, connection, range, maxCount) { | 67 function createGetAllRequest(t, storeName, connection, range, maxCount) { |
66 var transaction = connection.transaction(storeName, 'readonly'); | 68 var transaction = connection.transaction(storeName, 'readonly'); |
67 var store = transaction.objectStore(storeName); | 69 var store = transaction.objectStore(storeName); |
68 var index = store.index('test_idx'); | 70 var index = store.index('test_idx'); |
69 // TODO(cmumford): Simplify once crbug.com/335871 is fixed. | 71 // TODO(cmumford): Simplify once crbug.com/335871 is fixed. |
70 var req = maxCount !== undefined ? index.getAll(range, maxCount) : | 72 var req = maxCount !== undefined ? index.getAll(range, maxCount) : |
71 range !== undefined ? index.getAll(range) : index.getAll(); | 73 range !== undefined ? index.getAll(range) : index.getAll(); |
72 req.onerror = t.unreached_func('getAll request should succeed'); | 74 req.onerror = t.unreached_func('getAll request should succeed'); |
73 return req; | 75 return req; |
74 } | 76 } |
75 | 77 |
76 doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) { | 78 doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) { |
77 var connection = evt.target.result; | 79 var connection = evt.target.result; |
78 async_test(function(t) { | 80 async_test(function(t) { |
79 var req = createGetAllRequest(t, 'out-of-line', connection, 'C'); | 81 var req = createGetAllRequest(t, 'out-of-line', connection, 'C'); |
80 req.onsuccess = t.step_func(function(evt) { | 82 req.onsuccess = t.step_func(function(evt) { |
81 var data = evt.target.result; | 83 var data = evt.target.result; |
82 assert_class_string(data, 'Array', 'result should be an array'); | 84 assert_class_string(data, 'Array', 'result should be an array'); |
83 assert_equals(data.length, 1); | 85 assert_array_equals(data.map(e => e.ch), ['c']); |
84 assert_object_equals(data[0], {ch: 'c', upper: 'C'}); | 86 assert_array_equals(data.map(e => e.upper), ['C']); |
85 t.done(); | 87 t.done(); |
86 }); | 88 }); |
87 }, 'Single item get'); | 89 }, 'Single item get'); |
88 | 90 |
89 async_test(function(t) { | 91 async_test(function(t) { |
90 var req = createGetAllRequest(t, 'empty', connection); | 92 var req = createGetAllRequest(t, 'empty', connection); |
91 req.onsuccess = t.step_func(function(evt) { | 93 req.onsuccess = t.step_func(function(evt) { |
92 assert_array_equals(evt.target.result, [], | 94 assert_array_equals(evt.target.result, [], |
93 'getAll() on empty object store should return an empty array'); | 95 'getAll() on empty object store should return an empty array'); |
94 t.done(); | 96 t.done(); |
95 }); | 97 }); |
96 }, 'Empty object store'); | 98 }, 'Empty object store'); |
97 | 99 |
98 async_test(function(t) { | 100 async_test(function(t) { |
99 var req = createGetAllRequest(t, 'out-of-line', connection); | 101 var req = createGetAllRequest(t, 'out-of-line', connection); |
100 req.onsuccess = t.step_func(function(evt) { | 102 req.onsuccess = t.step_func(function(evt) { |
101 var data = evt.target.result; | 103 var data = evt.target.result; |
102 assert_class_string(data, 'Array', 'result should be an array'); | 104 assert_class_string(data, 'Array', 'result should be an array'); |
103 assert_equals(data.length, 26); | 105 assert_array_equals(data.map(e => e.ch), alphabet); |
104 assert_object_equals(data[0], {ch: 'a', upper: 'A'}); | 106 assert_array_equals(data.map(e => e.upper), ALPHABET); |
105 assert_object_equals(data[25], {ch: 'z', upper: 'Z'}); | |
106 t.done(); | 107 t.done(); |
107 }); | 108 }); |
108 }, 'Get all keys'); | 109 }, 'Get all keys'); |
109 | 110 |
110 async_test(function(t) { | 111 async_test(function(t) { |
111 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, | 112 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, |
112 10); | 113 10); |
113 req.onsuccess = t.step_func(function(evt) { | 114 req.onsuccess = t.step_func(function(evt) { |
114 var data = evt.target.result; | 115 var data = evt.target.result; |
115 assert_class_string(data, 'Array', 'result should be an array'); | 116 assert_class_string(data, 'Array', 'result should be an array'); |
116 assert_equals(data.length, 10); | 117 assert_array_equals(data.map(e => e.ch), 'abcdefghij'.split('')); |
117 assert_object_equals(data[0], {ch: 'a', upper: 'A'}); | 118 assert_array_equals(data.map(e => e.upper), 'ABCDEFGHIJ'.split('')); |
118 assert_object_equals(data[9], {ch: 'j', upper: 'J'}); | |
119 t.done(); | 119 t.done(); |
120 }); | 120 }); |
121 }, 'maxCount=10'); | 121 }, 'maxCount=10'); |
122 | 122 |
123 async_test(function(t) { | 123 async_test(function(t) { |
124 var req = createGetAllRequest(t, 'out-of-line', connection, | 124 var req = createGetAllRequest(t, 'out-of-line', connection, |
125 IDBKeyRange.bound('G', 'M')); | 125 IDBKeyRange.bound('G', 'M')); |
126 req.onsuccess = t.step_func(function(evt) { | 126 req.onsuccess = t.step_func(function(evt) { |
127 var data = evt.target.result; | 127 var data = evt.target.result; |
128 assert_equals(data.length, 7); | 128 assert_array_equals(data.map(e => e.ch), 'ghijklm'.split('')); |
129 assert_object_equals(data[0], {ch: 'g', upper: 'G'}); | 129 assert_array_equals(data.map(e => e.upper), 'GHIJKLM'.split('')); |
130 assert_object_equals(data[6], {ch: 'm', upper: 'M'}); | |
131 t.done(); | 130 t.done(); |
132 }); | 131 }); |
133 }, 'Get bound range'); | 132 }, 'Get bound range'); |
134 | 133 |
135 async_test(function(t) { | 134 async_test(function(t) { |
136 var req = createGetAllRequest(t, 'out-of-line', connection, | 135 var req = createGetAllRequest(t, 'out-of-line', connection, |
137 IDBKeyRange.bound('G', 'M'), 3); | 136 IDBKeyRange.bound('G', 'M'), 3); |
138 req.onsuccess = t.step_func(function(evt) { | 137 req.onsuccess = t.step_func(function(evt) { |
139 var data = evt.target.result; | 138 var data = evt.target.result; |
140 assert_class_string(data, 'Array', 'result should be an array'); | 139 assert_class_string(data, 'Array', 'result should be an array'); |
141 assert_equals(data.length, 3); | 140 assert_array_equals(data.map(e => e.ch), 'ghi'.split('')); |
142 assert_object_equals(data[0], {ch: 'g', upper: 'G'}); | 141 assert_array_equals(data.map(e => e.upper), 'GHI'.split('')); |
143 assert_object_equals(data[2], {ch: 'i', upper: 'I'}); | |
144 t.done(); | 142 t.done(); |
145 }); | 143 }); |
146 }, 'Get bound range with maxCount'); | 144 }, 'Get bound range with maxCount'); |
147 | 145 |
148 async_test(function(t) { | 146 async_test(function(t) { |
149 var req = createGetAllRequest(t, 'out-of-line', connection, | 147 var req = createGetAllRequest(t, 'out-of-line', connection, |
150 IDBKeyRange.bound('G', 'K', false, true)); | 148 IDBKeyRange.bound('G', 'K', false, true)); |
151 req.onsuccess = t.step_func(function(evt) { | 149 req.onsuccess = t.step_func(function(evt) { |
152 var data = evt.target.result; | 150 var data = evt.target.result; |
153 assert_class_string(data, 'Array', 'result should be an array'); | 151 assert_class_string(data, 'Array', 'result should be an array'); |
154 assert_equals(data.length, 4); | 152 assert_array_equals(data.map(e => e.ch), 'ghij'.split('')); |
155 assert_object_equals(data[0], {ch: 'g', upper: 'G'}); | 153 assert_array_equals(data.map(e => e.upper), 'GHIJ'.split('')); |
156 assert_object_equals(data[1], {ch: 'h', upper: 'H'}); | |
157 assert_object_equals(data[2], {ch: 'i', upper: 'I'}); | |
158 assert_object_equals(data[3], {ch: 'j', upper: 'J'}); | |
159 t.done(); | 154 t.done(); |
160 }); | 155 }); |
161 }, 'Get upper excluded'); | 156 }, 'Get upper excluded'); |
162 | 157 |
163 async_test(function(t) { | 158 async_test(function(t) { |
164 var req = createGetAllRequest(t, 'out-of-line', connection, | 159 var req = createGetAllRequest(t, 'out-of-line', connection, |
165 IDBKeyRange.bound('G', 'K', true, false)); | 160 IDBKeyRange.bound('G', 'K', true, false)); |
166 req.onsuccess = t.step_func(function(evt) { | 161 req.onsuccess = t.step_func(function(evt) { |
167 var data = evt.target.result; | 162 var data = evt.target.result; |
168 assert_class_string(data, 'Array', 'result should be an array'); | 163 assert_class_string(data, 'Array', 'result should be an array'); |
169 assert_equals(data.length, 4); | 164 assert_array_equals(data.map(e => e.ch), 'hijk'.split('')); |
170 assert_object_equals(data[0], {ch: 'h', upper: 'H'}); | 165 assert_array_equals(data.map(e => e.upper), 'HIJK'.split('')); |
171 assert_object_equals(data[1], {ch: 'i', upper: 'I'}); | |
172 assert_object_equals(data[2], {ch: 'j', upper: 'J'}); | |
173 assert_object_equals(data[3], {ch: 'k', upper: 'K'}); | |
174 t.done(); | 166 t.done(); |
175 }); | 167 }); |
176 }, 'Get lower excluded'); | 168 }, 'Get lower excluded'); |
177 | 169 |
178 async_test(function(t) { | 170 async_test(function(t) { |
179 var req = createGetAllRequest(t, 'generated', | 171 var req = createGetAllRequest(t, 'generated', |
180 connection, IDBKeyRange.bound(4, 15), 3); | 172 connection, IDBKeyRange.bound(4, 15), 3); |
181 req.onsuccess = t.step_func(function(evt) { | 173 req.onsuccess = t.step_func(function(evt) { |
182 var data = evt.target.result; | 174 var data = evt.target.result; |
183 assert_true(Array.isArray(data)); | 175 assert_true(Array.isArray(data)); |
184 assert_equals(data.length, 0); | 176 assert_equals(data.length, 0); |
185 t.done(); | 177 t.done(); |
186 }); | 178 }); |
187 }, 'Get bound range (generated) with maxCount'); | 179 }, 'Get bound range (generated) with maxCount'); |
188 | 180 |
189 async_test(function(t) { | 181 async_test(function(t) { |
190 var req = createGetAllRequest(t, 'out-of-line', | 182 var req = createGetAllRequest(t, 'out-of-line', |
191 connection, "Doesn't exist"); | 183 connection, "Doesn't exist"); |
192 req.onsuccess = t.step_func(function(evt) { | 184 req.onsuccess = t.step_func(function(evt) { |
193 assert_array_equals(evt.target.result, [], | 185 assert_array_equals(evt.target.result, [], |
194 'getAll() using a nonexistent key should return an empty array'); | 186 'getAll() using a nonexistent key should return an empty array'); |
195 t.done(); | 187 t.done(); |
196 req.onerror = t.unreached_func('getAll request should succeed'); | 188 req.onerror = t.unreached_func('getAll request should succeed'); |
197 }); | 189 }); |
198 }, 'Non existent key'); | 190 }, 'Non existent key'); |
199 | 191 |
200 async_test(function(t) { | 192 async_test(function(t) { |
201 var transaction = connection.transaction('out-of-line', 'readonly'); | 193 var req = createGetAllRequest(t, 'out-of-line', connection, |
202 var store = transaction.objectStore('out-of-line'); | 194 undefined, 0); |
203 var index = store.index('test_idx'); | 195 req.onsuccess = t.step_func(function(evt) { |
204 assert_throws(new TypeError(), function () { | 196 var data = evt.target.result; |
205 index.getAll(undefined, 0); | 197 assert_class_string(data, 'Array', 'result should be an array'); |
206 }, 'getAll() with maxCount=0 should throw TypeError'); | 198 assert_array_equals(data.map(e => e.ch), alphabet); |
207 t.done(); | 199 assert_array_equals(data.map(e => e.upper), ALPHABET); |
| 200 t.done(); |
| 201 }); |
208 }, 'maxCount=0'); | 202 }, 'maxCount=0'); |
209 | 203 |
210 async_test(function(t) { | 204 async_test(function(t) { |
211 var req = createGetAllRequest(t, 'out-of-line-not-unique', connection, | 205 var req = createGetAllRequest(t, 'out-of-line-not-unique', connection, |
212 'first'); | 206 'first'); |
213 req.onsuccess = t.step_func(function(evt) { | 207 req.onsuccess = t.step_func(function(evt) { |
214 var data = evt.target.result; | 208 var data = evt.target.result; |
215 assert_class_string(data, 'Array', 'result should be an array'); | 209 assert_class_string(data, 'Array', 'result should be an array'); |
216 assert_equals(data.length, 13); | 210 assert_array_equals(data.map(e => e.ch), 'abcdefghijklm'.split('')); |
217 assert_object_equals(data[0], {ch: 'a', half: 'first'}); | 211 assert_true(data.every(e => e.half === 'first')); |
218 assert_object_equals(data[12], {ch: 'm', half: 'first'}); | |
219 t.done(); | 212 t.done(); |
220 }); | 213 }); |
221 }, 'Retrieve multiEntry key'); | 214 }, 'Retrieve multiEntry key'); |
222 | 215 |
223 async_test(function(t) { | 216 async_test(function(t) { |
224 var req = createGetAllRequest(t, 'out-of-line-multi', connection, | 217 var req = createGetAllRequest(t, 'out-of-line-multi', connection, |
225 'vowel'); | 218 'vowel'); |
226 req.onsuccess = t.step_func(function(evt) { | 219 req.onsuccess = t.step_func(function(evt) { |
227 var data = evt.target.result; | 220 var data = evt.target.result; |
228 assert_class_string(data, 'Array', 'result should be an array'); | 221 assert_class_string(data, 'Array', 'result should be an array'); |
229 assert_equals(data.length, 5); | 222 assert_array_equals(data.map(e => e.ch), ['a', 'e', 'i', 'o', 'u']); |
230 assert_object_equals(data[0], {ch: 'a', attribs: ['vowel', 'first']}); | 223 assert_array_equals(data[0].attribs, ['vowel', 'first']); |
231 assert_object_equals(data[1], {ch: 'e', attribs: ['vowel']}); | 224 assert_true(data.every(e => e.attribs[0] === 'vowel')); |
232 assert_object_equals(data[2], {ch: 'i', attribs: ['vowel']}); | |
233 assert_object_equals(data[3], {ch: 'o', attribs: ['vowel']}); | |
234 assert_object_equals(data[4], {ch: 'u', attribs: ['vowel']}); | |
235 t.done(); | 225 t.done(); |
236 }); | 226 }); |
237 }, 'Retrieve one key multiple values'); | 227 }, 'Retrieve one key multiple values'); |
238 }); | 228 }); |
239 | 229 |
240 </script> | 230 </script> |
OLD | NEW |