| OLD | NEW |
| (Empty) |
| 1 <!DOCTYPE html> | |
| 2 <title>IndexedDB: Test IDBIndex.getAll.</title> | |
| 3 <script src="../../../resources/testharness.js"></script> | |
| 4 <script src="../../../resources/testharnessreport.js"></script> | |
| 5 <script> | |
| 6 setup({explicit_done: true}); | |
| 7 | |
| 8 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
| 9 var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); | |
| 10 | |
| 11 function doSetup(dbName, dbVersion, onsuccess) { | |
| 12 var delete_request = indexedDB.deleteDatabase(dbName); | |
| 13 delete_request.onerror = function() { | |
| 14 assert_unreached('deleteDatabase should not fail'); | |
| 15 }; | |
| 16 delete_request.onsuccess = function(e) { | |
| 17 var req = indexedDB.open(dbName, dbVersion); | |
| 18 req.onsuccess = onsuccess; | |
| 19 req.onerror = function() { | |
| 20 assert_unreached('open should not fail'); | |
| 21 }; | |
| 22 req.onupgradeneeded = function(evt) { | |
| 23 var connection = evt.target.result; | |
| 24 | |
| 25 var store = connection.createObjectStore('generated', | |
| 26 {autoIncrement: true, keyPath: 'id'}); | |
| 27 var index = store.createIndex('test_idx', 'upper'); | |
| 28 alphabet.forEach(function(letter) { | |
| 29 store.put({ch: letter, upper: letter.toUpperCase()}); | |
| 30 }); | |
| 31 | |
| 32 store = connection.createObjectStore('out-of-line', null); | |
| 33 index = store.createIndex('test_idx', 'upper'); | |
| 34 alphabet.forEach(function(letter) { | |
| 35 store.put({ch: letter, upper: letter.toUpperCase()}, letter); | |
| 36 }); | |
| 37 | |
| 38 store = connection.createObjectStore('out-of-line-not-unique', null); | |
| 39 index = store.createIndex('test_idx', 'half'); | |
| 40 alphabet.forEach(function(letter) { | |
| 41 if (letter <= 'm') | |
| 42 store.put({ch: letter, half: 'first'}, letter); | |
| 43 else | |
| 44 store.put({ch: letter, half: 'second'}, letter); | |
| 45 }); | |
| 46 | |
| 47 store = connection.createObjectStore('out-of-line-multi', null); | |
| 48 index = store.createIndex('test_idx', 'attribs', {multiEntry: true}); | |
| 49 alphabet.forEach(function(letter) { | |
| 50 attrs = []; | |
| 51 if (['a', 'e', 'i', 'o', 'u'].indexOf(letter) != -1) | |
| 52 attrs.push('vowel'); | |
| 53 else | |
| 54 attrs.push('consonant'); | |
| 55 if (letter == 'a') | |
| 56 attrs.push('first'); | |
| 57 if (letter == 'z') | |
| 58 attrs.push('last'); | |
| 59 store.put({ch: letter, attribs: attrs}, letter); | |
| 60 }); | |
| 61 | |
| 62 store = connection.createObjectStore('empty', null); | |
| 63 index = store.createIndex('test_idx', 'upper'); | |
| 64 }; | |
| 65 }; | |
| 66 } | |
| 67 | |
| 68 function createGetAllRequest(t, storeName, connection, range, maxCount) { | |
| 69 var transaction = connection.transaction(storeName, 'readonly'); | |
| 70 var store = transaction.objectStore(storeName); | |
| 71 var index = store.index('test_idx'); | |
| 72 var req = index.getAll(range, maxCount); | |
| 73 req.onerror = t.unreached_func('getAll request should succeed'); | |
| 74 return req; | |
| 75 } | |
| 76 | |
| 77 doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) { | |
| 78 var connection = evt.target.result; | |
| 79 async_test(function(t) { | |
| 80 var req = createGetAllRequest(t, 'out-of-line', connection, 'C'); | |
| 81 req.onsuccess = t.step_func(function(evt) { | |
| 82 var data = evt.target.result; | |
| 83 assert_class_string(data, 'Array', 'result should be an array'); | |
| 84 assert_array_equals(data.map(function(e) { return e.ch; }), ['c']); | |
| 85 assert_array_equals(data.map(function(e) { return e.upper; }), ['C']); | |
| 86 t.done(); | |
| 87 }); | |
| 88 }, 'Single item get'); | |
| 89 | |
| 90 async_test(function(t) { | |
| 91 var req = createGetAllRequest(t, 'empty', connection); | |
| 92 req.onsuccess = t.step_func(function(evt) { | |
| 93 assert_array_equals(evt.target.result, [], | |
| 94 'getAll() on empty object store should return an empty array'); | |
| 95 t.done(); | |
| 96 }); | |
| 97 }, 'Empty object store'); | |
| 98 | |
| 99 async_test(function(t) { | |
| 100 var req = createGetAllRequest(t, 'out-of-line', connection); | |
| 101 req.onsuccess = t.step_func(function(evt) { | |
| 102 var data = evt.target.result; | |
| 103 assert_class_string(data, 'Array', 'result should be an array'); | |
| 104 assert_array_equals(data.map(function(e) { return e.ch; }), alphabet); | |
| 105 assert_array_equals(data.map(function(e) { return e.upper; }), ALPHABE
T); | |
| 106 t.done(); | |
| 107 }); | |
| 108 }, 'Get all keys'); | |
| 109 | |
| 110 async_test(function(t) { | |
| 111 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, | |
| 112 10); | |
| 113 req.onsuccess = t.step_func(function(evt) { | |
| 114 var data = evt.target.result; | |
| 115 assert_class_string(data, 'Array', 'result should be an array'); | |
| 116 assert_array_equals(data.map(function(e) { return e.ch; }), 'abcdefghi
j'.split('')); | |
| 117 assert_array_equals(data.map(function(e) { return e.upper; }), 'ABCDEF
GHIJ'.split('')); | |
| 118 t.done(); | |
| 119 }); | |
| 120 }, 'maxCount=10'); | |
| 121 | |
| 122 async_test(function(t) { | |
| 123 var req = createGetAllRequest(t, 'out-of-line', connection, | |
| 124 IDBKeyRange.bound('G', 'M')); | |
| 125 req.onsuccess = t.step_func(function(evt) { | |
| 126 var data = evt.target.result; | |
| 127 assert_array_equals(data.map(function(e) { return e.ch; }), 'ghijklm'.
split('')); | |
| 128 assert_array_equals(data.map(function(e) { return e.upper; }), 'GHIJKL
M'.split('')); | |
| 129 t.done(); | |
| 130 }); | |
| 131 }, 'Get bound range'); | |
| 132 | |
| 133 async_test(function(t) { | |
| 134 var req = createGetAllRequest(t, 'out-of-line', connection, | |
| 135 IDBKeyRange.bound('G', 'M'), 3); | |
| 136 req.onsuccess = t.step_func(function(evt) { | |
| 137 var data = evt.target.result; | |
| 138 assert_class_string(data, 'Array', 'result should be an array'); | |
| 139 assert_array_equals(data.map(function(e) { return e.ch; }), 'ghi'.spli
t('')); | |
| 140 assert_array_equals(data.map(function(e) { return e.upper; }), 'GHI'.s
plit('')); | |
| 141 t.done(); | |
| 142 }); | |
| 143 }, 'Get bound range with maxCount'); | |
| 144 | |
| 145 async_test(function(t) { | |
| 146 var req = createGetAllRequest(t, 'out-of-line', connection, | |
| 147 IDBKeyRange.bound('G', 'K', false, true)); | |
| 148 req.onsuccess = t.step_func(function(evt) { | |
| 149 var data = evt.target.result; | |
| 150 assert_class_string(data, 'Array', 'result should be an array'); | |
| 151 assert_array_equals(data.map(function(e) { return e.ch; }), 'ghij'.spl
it('')); | |
| 152 assert_array_equals(data.map(function(e) { return e.upper; }), 'GHIJ'.
split('')); | |
| 153 t.done(); | |
| 154 }); | |
| 155 }, 'Get upper excluded'); | |
| 156 | |
| 157 async_test(function(t) { | |
| 158 var req = createGetAllRequest(t, 'out-of-line', connection, | |
| 159 IDBKeyRange.bound('G', 'K', true, false)); | |
| 160 req.onsuccess = t.step_func(function(evt) { | |
| 161 var data = evt.target.result; | |
| 162 assert_class_string(data, 'Array', 'result should be an array'); | |
| 163 assert_array_equals(data.map(function(e) { return e.ch; }), 'hijk'.spl
it('')); | |
| 164 assert_array_equals(data.map(function(e) { return e.upper; }), 'HIJK'.
split('')); | |
| 165 t.done(); | |
| 166 }); | |
| 167 }, 'Get lower excluded'); | |
| 168 | |
| 169 async_test(function(t) { | |
| 170 var req = createGetAllRequest(t, 'generated', | |
| 171 connection, IDBKeyRange.bound(4, 15), 3); | |
| 172 req.onsuccess = t.step_func(function(evt) { | |
| 173 var data = evt.target.result; | |
| 174 assert_true(Array.isArray(data)); | |
| 175 assert_equals(data.length, 0); | |
| 176 t.done(); | |
| 177 }); | |
| 178 }, 'Get bound range (generated) with maxCount'); | |
| 179 | |
| 180 async_test(function(t) { | |
| 181 var req = createGetAllRequest(t, 'out-of-line', | |
| 182 connection, "Doesn't exist"); | |
| 183 req.onsuccess = t.step_func(function(evt) { | |
| 184 assert_array_equals(evt.target.result, [], | |
| 185 'getAll() using a nonexistent key should return an empty array'); | |
| 186 t.done(); | |
| 187 req.onerror = t.unreached_func('getAll request should succeed'); | |
| 188 }); | |
| 189 }, 'Non existent key'); | |
| 190 | |
| 191 async_test(function(t) { | |
| 192 var req = createGetAllRequest(t, 'out-of-line', connection, | |
| 193 undefined, 0); | |
| 194 req.onsuccess = t.step_func(function(evt) { | |
| 195 var data = evt.target.result; | |
| 196 assert_class_string(data, 'Array', 'result should be an array'); | |
| 197 assert_array_equals(data.map(function(e) { return e.ch; }), alphabet); | |
| 198 assert_array_equals(data.map(function(e) { return e.upper; }), ALPHABE
T); | |
| 199 t.done(); | |
| 200 }); | |
| 201 }, 'maxCount=0'); | |
| 202 | |
| 203 async_test(function(t) { | |
| 204 var req = createGetAllRequest(t, 'out-of-line-not-unique', connection, | |
| 205 'first'); | |
| 206 req.onsuccess = t.step_func(function(evt) { | |
| 207 var data = evt.target.result; | |
| 208 assert_class_string(data, 'Array', 'result should be an array'); | |
| 209 assert_array_equals(data.map(function(e) { return e.ch; }), 'abcdefghi
jklm'.split('')); | |
| 210 assert_true(data.every(function(e) { return e.half === 'first'; })); | |
| 211 t.done(); | |
| 212 }); | |
| 213 }, 'Retrieve multiEntry key'); | |
| 214 | |
| 215 async_test(function(t) { | |
| 216 var req = createGetAllRequest(t, 'out-of-line-multi', connection, | |
| 217 'vowel'); | |
| 218 req.onsuccess = t.step_func(function(evt) { | |
| 219 var data = evt.target.result; | |
| 220 assert_class_string(data, 'Array', 'result should be an array'); | |
| 221 assert_array_equals(data.map(function(e) { return e.ch; }), ['a', 'e',
'i', 'o', 'u']); | |
| 222 assert_array_equals(data[0].attribs, ['vowel', 'first']); | |
| 223 assert_true(data.every(function(e) { return e.attribs[0] === 'vowel';
})); | |
| 224 t.done(); | |
| 225 }); | |
| 226 }, 'Retrieve one key multiple values'); | |
| 227 | |
| 228 // Explicit done needed in case async_test body fails synchronously. | |
| 229 done(); | |
| 230 }); | |
| 231 | |
| 232 </script> | |
| OLD | NEW |