Chromium Code Reviews| 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 | |
| 7 function doSetup(dbName, dbVersion, onsuccess) { | 9 function doSetup(dbName, dbVersion, onsuccess) { |
| 8 var delete_request = indexedDB.deleteDatabase(dbName); | 10 var delete_request = indexedDB.deleteDatabase(dbName); |
| 9 delete_request.onerror = function() { | 11 delete_request.onerror = function() { |
| 10 assert_unreached('deleteDatabase should not fail'); | 12 assert_unreached('deleteDatabase should not fail'); |
| 11 }; | 13 }; |
| 12 delete_request.onsuccess = function(e) { | 14 delete_request.onsuccess = function(e) { |
| 13 var req = indexedDB.open(dbName, dbVersion); | 15 var req = indexedDB.open(dbName, dbVersion); |
| 14 req.onsuccess = onsuccess; | 16 req.onsuccess = onsuccess; |
| 15 req.onerror = function() { | 17 req.onerror = function() { |
| 16 assert_unreached('open should not fail'); | 18 assert_unreached('open should not fail'); |
| 17 }; | 19 }; |
| 18 req.onupgradeneeded = function(evt) { | 20 req.onupgradeneeded = function(evt) { |
| 19 var connection = evt.target.result; | 21 var connection = evt.target.result; |
| 20 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
| 21 | 22 |
| 22 var store = connection.createObjectStore('generated', | 23 var store = connection.createObjectStore('generated', |
| 23 {autoIncrement: true, keyPath: 'id'}); | 24 {autoIncrement: true, keyPath: 'id'}); |
| 24 var index = store.createIndex('test_idx', 'upper'); | 25 var index = store.createIndex('test_idx', 'upper'); |
| 25 alphabet.forEach(function(letter) { | 26 alphabet.forEach(function(letter) { |
| 26 store.put({ch: letter, upper: letter.toUpperCase()}); | 27 store.put({ch: letter, upper: letter.toUpperCase()}); |
| 27 }); | 28 }); |
| 28 | 29 |
| 29 store = connection.createObjectStore('out-of-line', null); | 30 store = connection.createObjectStore('out-of-line', null); |
| 30 index = store.createIndex('test_idx', 'upper'); | 31 index = store.createIndex('test_idx', 'upper'); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 61 }; | 62 }; |
| 62 }; | 63 }; |
| 63 } | 64 } |
| 64 | 65 |
| 65 function createGetAllRequest(t, storeName, connection, range, maxCount) { | 66 function createGetAllRequest(t, storeName, connection, range, maxCount) { |
| 66 var transaction = connection.transaction(storeName, 'readonly'); | 67 var transaction = connection.transaction(storeName, 'readonly'); |
| 67 var store = transaction.objectStore(storeName); | 68 var store = transaction.objectStore(storeName); |
| 68 var index = store.index('test_idx'); | 69 var index = store.index('test_idx'); |
| 69 // TODO(cmumford): Simplify once crbug.com/335871 is fixed. | 70 // TODO(cmumford): Simplify once crbug.com/335871 is fixed. |
| 70 var req = maxCount !== undefined ? index.getAll(range, maxCount) : | 71 var req = maxCount !== undefined ? index.getAll(range, maxCount) : |
| 71 range !== undefined ? index.getAll(range) : index.getAll(); | 72 range !== undefined ? index.getAll(range) : index.getAll(); |
| 72 req.onerror = t.unreached_func('getAll request should succeed'); | 73 req.onerror = t.unreached_func('getAll request should succeed'); |
| 73 return req; | 74 return req; |
| 74 } | 75 } |
| 75 | 76 |
| 76 doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) { | 77 doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) { |
| 77 var connection = evt.target.result; | 78 var connection = evt.target.result; |
| 78 async_test(function(t) { | 79 async_test(function(t) { |
| 79 var req = createGetAllRequest(t, 'out-of-line', connection, 'C'); | 80 var req = createGetAllRequest(t, 'out-of-line', connection, 'C'); |
| 80 req.onsuccess = t.step_func(function(evt) { | 81 req.onsuccess = t.step_func(function(evt) { |
| 81 var data = evt.target.result; | 82 var data = evt.target.result; |
| 82 assert_class_string(data, 'Array', 'result should be an array'); | 83 assert_class_string(data, 'Array', 'result should be an array'); |
| 83 assert_equals(data.length, 1); | 84 assert_array_equals(data.map(e => e.ch), ['c']); |
| 84 assert_object_equals(data[0], {ch: 'c', upper: 'C'}); | 85 assert_array_equals(data.map(e => e.upper), ['C']); |
| 85 t.done(); | 86 t.done(); |
| 86 }); | 87 }); |
| 87 }, 'Single item get'); | 88 }, 'Single item get'); |
| 88 | 89 |
| 89 async_test(function(t) { | 90 async_test(function(t) { |
| 90 var req = createGetAllRequest(t, 'empty', connection); | 91 var req = createGetAllRequest(t, 'empty', connection); |
| 91 req.onsuccess = t.step_func(function(evt) { | 92 req.onsuccess = t.step_func(function(evt) { |
| 92 assert_array_equals(evt.target.result, [], | 93 assert_array_equals(evt.target.result, [], |
| 93 'getAll() on empty object store should return an empty array'); | 94 'getAll() on empty object store should return an empty array'); |
| 94 t.done(); | 95 t.done(); |
| 95 }); | 96 }); |
| 96 }, 'Empty object store'); | 97 }, 'Empty object store'); |
| 97 | 98 |
| 98 async_test(function(t) { | 99 async_test(function(t) { |
| 99 var req = createGetAllRequest(t, 'out-of-line', connection); | 100 var req = createGetAllRequest(t, 'out-of-line', connection); |
| 100 req.onsuccess = t.step_func(function(evt) { | 101 req.onsuccess = t.step_func(function(evt) { |
| 101 var data = evt.target.result; | 102 var data = evt.target.result; |
| 102 assert_class_string(data, 'Array', 'result should be an array'); | 103 assert_class_string(data, 'Array', 'result should be an array'); |
| 103 assert_equals(data.length, 26); | 104 assert_array_equals(data.map(e => e.ch), alphabet); |
| 104 assert_object_equals(data[0], {ch: 'a', upper: 'A'}); | 105 assert_array_equals(data.map(e => e.upper), |
| 105 assert_object_equals(data[25], {ch: 'z', upper: 'Z'}); | 106 alphabet.map(e => e.toUpperCase())); |
| 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('')); |
|
cmumford
2015/08/21 17:32:40
Yes, much better approach.
| |
| 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), |
| 200 alphabet.map(e => e.toUpperCase())); | |
|
cmumford
2015/08/21 17:32:40
Nit: since it's used twice how about creating a gl
jsbell
2015/08/21 17:48:32
Done.
| |
| 201 t.done(); | |
| 202 }); | |
| 208 }, 'maxCount=0'); | 203 }, 'maxCount=0'); |
| 209 | 204 |
| 210 async_test(function(t) { | 205 async_test(function(t) { |
| 211 var req = createGetAllRequest(t, 'out-of-line-not-unique', connection, | 206 var req = createGetAllRequest(t, 'out-of-line-not-unique', connection, |
| 212 'first'); | 207 'first'); |
| 213 req.onsuccess = t.step_func(function(evt) { | 208 req.onsuccess = t.step_func(function(evt) { |
| 214 var data = evt.target.result; | 209 var data = evt.target.result; |
| 215 assert_class_string(data, 'Array', 'result should be an array'); | 210 assert_class_string(data, 'Array', 'result should be an array'); |
| 216 assert_equals(data.length, 13); | 211 assert_array_equals(data.map(e => e.ch), 'abcdefghijklm'.split('')); |
| 217 assert_object_equals(data[0], {ch: 'a', half: 'first'}); | 212 assert_true(data.every(e => e.half === 'first')); |
| 218 assert_object_equals(data[12], {ch: 'm', half: 'first'}); | |
| 219 t.done(); | 213 t.done(); |
| 220 }); | 214 }); |
| 221 }, 'Retrieve multiEntry key'); | 215 }, 'Retrieve multiEntry key'); |
| 222 | 216 |
| 223 async_test(function(t) { | 217 async_test(function(t) { |
| 224 var req = createGetAllRequest(t, 'out-of-line-multi', connection, | 218 var req = createGetAllRequest(t, 'out-of-line-multi', connection, |
| 225 'vowel'); | 219 'vowel'); |
| 226 req.onsuccess = t.step_func(function(evt) { | 220 req.onsuccess = t.step_func(function(evt) { |
| 227 var data = evt.target.result; | 221 var data = evt.target.result; |
| 228 assert_class_string(data, 'Array', 'result should be an array'); | 222 assert_class_string(data, 'Array', 'result should be an array'); |
| 229 assert_equals(data.length, 5); | 223 assert_array_equals(data.map(e => e.ch), ['a', 'e', 'i', 'o', 'u']); |
| 230 assert_object_equals(data[0], {ch: 'a', attribs: ['vowel', 'first']}); | 224 assert_array_equals(data[0].attribs, ['vowel', 'first']); |
| 231 assert_object_equals(data[1], {ch: 'e', attribs: ['vowel']}); | 225 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(); | 226 t.done(); |
| 236 }); | 227 }); |
| 237 }, 'Retrieve one key multiple values'); | 228 }, 'Retrieve one key multiple values'); |
| 238 }); | 229 }); |
| 239 | 230 |
| 240 </script> | 231 </script> |
| OLD | NEW |