Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / index-getall.html
blob400e1cd27eb031eb850fede083eaf4c7d5f6ac61
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>
7 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
8 var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
10 function doSetup(dbName, dbVersion, onsuccess) {
11 var delete_request = indexedDB.deleteDatabase(dbName);
12 delete_request.onerror = function() {
13 assert_unreached('deleteDatabase should not fail');
15 delete_request.onsuccess = function(e) {
16 var req = indexedDB.open(dbName, dbVersion);
17 req.onsuccess = onsuccess;
18 req.onerror = function() {
19 assert_unreached('open should not fail');
21 req.onupgradeneeded = function(evt) {
22 var connection = evt.target.result;
24 var store = connection.createObjectStore('generated',
25 {autoIncrement: true, keyPath: 'id'});
26 var index = store.createIndex('test_idx', 'upper');
27 alphabet.forEach(function(letter) {
28 store.put({ch: letter, upper: letter.toUpperCase()});
29 });
31 store = connection.createObjectStore('out-of-line', null);
32 index = store.createIndex('test_idx', 'upper');
33 alphabet.forEach(function(letter) {
34 store.put({ch: letter, upper: letter.toUpperCase()}, letter);
35 });
37 store = connection.createObjectStore('out-of-line-not-unique', null);
38 index = store.createIndex('test_idx', 'half');
39 alphabet.forEach(function(letter) {
40 if (letter <= 'm')
41 store.put({ch: letter, half: 'first'}, letter);
42 else
43 store.put({ch: letter, half: 'second'}, letter);
44 });
46 store = connection.createObjectStore('out-of-line-multi', null);
47 index = store.createIndex('test_idx', 'attribs', {multiEntry: true});
48 alphabet.forEach(function(letter) {
49 attrs = [];
50 if (['a', 'e', 'i', 'o', 'u'].indexOf(letter) != -1)
51 attrs.push('vowel');
52 else
53 attrs.push('consonant');
54 if (letter == 'a')
55 attrs.push('first');
56 if (letter == 'z')
57 attrs.push('last');
58 store.put({ch: letter, attribs: attrs}, letter);
59 });
61 store = connection.createObjectStore('empty', null);
62 index = store.createIndex('test_idx', 'upper');
67 function createGetAllRequest(t, storeName, connection, range, maxCount) {
68 var transaction = connection.transaction(storeName, 'readonly');
69 var store = transaction.objectStore(storeName);
70 var index = store.index('test_idx');
71 // TODO(cmumford): Simplify once crbug.com/335871 is fixed.
72 var req = maxCount !== undefined ? index.getAll(range, maxCount) :
73 range !== undefined ? index.getAll(range) : index.getAll();
74 req.onerror = t.unreached_func('getAll request should succeed');
75 return req;
78 doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) {
79 var connection = evt.target.result;
80 async_test(function(t) {
81 var req = createGetAllRequest(t, 'out-of-line', connection, 'C');
82 req.onsuccess = t.step_func(function(evt) {
83 var data = evt.target.result;
84 assert_class_string(data, 'Array', 'result should be an array');
85 assert_array_equals(data.map(e => e.ch), ['c']);
86 assert_array_equals(data.map(e => e.upper), ['C']);
87 t.done();
88 });
89 }, 'Single item get');
91 async_test(function(t) {
92 var req = createGetAllRequest(t, 'empty', connection);
93 req.onsuccess = t.step_func(function(evt) {
94 assert_array_equals(evt.target.result, [],
95 'getAll() on empty object store should return an empty array');
96 t.done();
97 });
98 }, 'Empty object store');
100 async_test(function(t) {
101 var req = createGetAllRequest(t, 'out-of-line', connection);
102 req.onsuccess = t.step_func(function(evt) {
103 var data = evt.target.result;
104 assert_class_string(data, 'Array', 'result should be an array');
105 assert_array_equals(data.map(e => e.ch), alphabet);
106 assert_array_equals(data.map(e => e.upper), ALPHABET);
107 t.done();
109 }, 'Get all keys');
111 async_test(function(t) {
112 var req = createGetAllRequest(t, 'out-of-line', connection, undefined,
113 10);
114 req.onsuccess = t.step_func(function(evt) {
115 var data = evt.target.result;
116 assert_class_string(data, 'Array', 'result should be an array');
117 assert_array_equals(data.map(e => e.ch), 'abcdefghij'.split(''));
118 assert_array_equals(data.map(e => e.upper), 'ABCDEFGHIJ'.split(''));
119 t.done();
121 }, 'maxCount=10');
123 async_test(function(t) {
124 var req = createGetAllRequest(t, 'out-of-line', connection,
125 IDBKeyRange.bound('G', 'M'));
126 req.onsuccess = t.step_func(function(evt) {
127 var data = evt.target.result;
128 assert_array_equals(data.map(e => e.ch), 'ghijklm'.split(''));
129 assert_array_equals(data.map(e => e.upper), 'GHIJKLM'.split(''));
130 t.done();
132 }, 'Get bound range');
134 async_test(function(t) {
135 var req = createGetAllRequest(t, 'out-of-line', connection,
136 IDBKeyRange.bound('G', 'M'), 3);
137 req.onsuccess = t.step_func(function(evt) {
138 var data = evt.target.result;
139 assert_class_string(data, 'Array', 'result should be an array');
140 assert_array_equals(data.map(e => e.ch), 'ghi'.split(''));
141 assert_array_equals(data.map(e => e.upper), 'GHI'.split(''));
142 t.done();
144 }, 'Get bound range with maxCount');
146 async_test(function(t) {
147 var req = createGetAllRequest(t, 'out-of-line', connection,
148 IDBKeyRange.bound('G', 'K', false, true));
149 req.onsuccess = t.step_func(function(evt) {
150 var data = evt.target.result;
151 assert_class_string(data, 'Array', 'result should be an array');
152 assert_array_equals(data.map(e => e.ch), 'ghij'.split(''));
153 assert_array_equals(data.map(e => e.upper), 'GHIJ'.split(''));
154 t.done();
156 }, 'Get upper excluded');
158 async_test(function(t) {
159 var req = createGetAllRequest(t, 'out-of-line', connection,
160 IDBKeyRange.bound('G', 'K', true, false));
161 req.onsuccess = t.step_func(function(evt) {
162 var data = evt.target.result;
163 assert_class_string(data, 'Array', 'result should be an array');
164 assert_array_equals(data.map(e => e.ch), 'hijk'.split(''));
165 assert_array_equals(data.map(e => e.upper), 'HIJK'.split(''));
166 t.done();
168 }, 'Get lower excluded');
170 async_test(function(t) {
171 var req = createGetAllRequest(t, 'generated',
172 connection, IDBKeyRange.bound(4, 15), 3);
173 req.onsuccess = t.step_func(function(evt) {
174 var data = evt.target.result;
175 assert_true(Array.isArray(data));
176 assert_equals(data.length, 0);
177 t.done();
179 }, 'Get bound range (generated) with maxCount');
181 async_test(function(t) {
182 var req = createGetAllRequest(t, 'out-of-line',
183 connection, "Doesn't exist");
184 req.onsuccess = t.step_func(function(evt) {
185 assert_array_equals(evt.target.result, [],
186 'getAll() using a nonexistent key should return an empty array');
187 t.done();
188 req.onerror = t.unreached_func('getAll request should succeed');
190 }, 'Non existent key');
192 async_test(function(t) {
193 var req = createGetAllRequest(t, 'out-of-line', connection,
194 undefined, 0);
195 req.onsuccess = t.step_func(function(evt) {
196 var data = evt.target.result;
197 assert_class_string(data, 'Array', 'result should be an array');
198 assert_array_equals(data.map(e => e.ch), alphabet);
199 assert_array_equals(data.map(e => e.upper), ALPHABET);
200 t.done();
202 }, 'maxCount=0');
204 async_test(function(t) {
205 var req = createGetAllRequest(t, 'out-of-line-not-unique', connection,
206 'first');
207 req.onsuccess = t.step_func(function(evt) {
208 var data = evt.target.result;
209 assert_class_string(data, 'Array', 'result should be an array');
210 assert_array_equals(data.map(e => e.ch), 'abcdefghijklm'.split(''));
211 assert_true(data.every(e => e.half === 'first'));
212 t.done();
214 }, 'Retrieve multiEntry key');
216 async_test(function(t) {
217 var req = createGetAllRequest(t, 'out-of-line-multi', connection,
218 'vowel');
219 req.onsuccess = t.step_func(function(evt) {
220 var data = evt.target.result;
221 assert_class_string(data, 'Array', 'result should be an array');
222 assert_array_equals(data.map(e => e.ch), ['a', 'e', 'i', 'o', 'u']);
223 assert_array_equals(data[0].attribs, ['vowel', 'first']);
224 assert_true(data.every(e => e.attribs[0] === 'vowel'));
225 t.done();
227 }, 'Retrieve one key multiple values');
230 </script>