Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / objectstore-getall.html
blobfc4f5d11a63acdcdf7f57650e608d465d10f7277
1 <!DOCTYPE html>
2 <title>IndexedDB: Test IDBObjectStore.getAll.</title>
3 <script src="../../resources/testharness.js"></script>
4 <script src="../../resources/testharnessreport.js"></script>
5 <script>
7 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
9 function doSetup(dbName, dbVersion, onsuccess) {
10 var delete_request = indexedDB.deleteDatabase(dbName);
11 delete_request.onerror = function() {
12 assert_unreached('deleteDatabase should not fail');
14 delete_request.onsuccess = function(e) {
15 var req = indexedDB.open(dbName, dbVersion);
16 req.onsuccess = onsuccess;
17 req.onerror = function() {
18 assert_unreached('open should not fail');
20 req.onupgradeneeded = function(evt) {
21 var connection = evt.target.result;
23 var store = connection.createObjectStore('generated',
24 {autoIncrement: true, keyPath: 'id'});
25 alphabet.forEach(function(letter) {
26 store.put({ch: letter});
27 });
29 store = connection.createObjectStore('out-of-line', null);
30 alphabet.forEach(function(letter) {
31 store.put('value-' + letter, letter);
32 });
34 store = connection.createObjectStore('empty', null);
39 function createGetAllRequest(t, storeName, connection, range, maxCount) {
40 var transaction = connection.transaction(storeName, 'readonly');
41 var store = transaction.objectStore(storeName);
42 // TODO(cmumford): Simplify once crbug.com/335871 is closed.
43 var req = maxCount !== undefined ? store.getAll(range, maxCount) :
44 range !== undefined ? store.getAll(range) : store.getAll();
45 req.onerror = t.unreached_func('getAll request should succeed');
46 return req;
49 doSetup(location.pathname + '-IDBObjectStore.getAll', 1, function(evt) {
50 var connection = evt.target.result;
51 async_test(function(t) {
52 var req = createGetAllRequest(t, 'out-of-line', connection, 'c');
53 req.onsuccess = t.step_func(function(evt) {
54 assert_array_equals(evt.target.result, ['value-c']);
55 t.done();
56 });
57 }, 'Single item get');
59 async_test(function(t) {
60 var req = createGetAllRequest(t, 'generated', connection, 3);
61 req.onsuccess = t.step_func(function(evt) {
62 var data = evt.target.result;
63 assert_true(Array.isArray(data));
64 assert_equals(data.length, 1);
65 assert_equals(data[0].id, 3);
66 assert_equals(data[0].ch, 'c');
67 t.done();
68 });
69 }, 'Single item get (generated key)');
71 async_test(function(t) {
72 var req = createGetAllRequest(t, 'empty', connection);
73 req.onsuccess = t.step_func(function(evt) {
74 assert_array_equals(evt.target.result, [],
75 'getAll() on empty object store should return an empty array');
76 t.done();
77 });
78 }, 'getAll on empty object store');
80 async_test(function(t) {
81 var req = createGetAllRequest(t, 'out-of-line', connection);
82 req.onsuccess = t.step_func(function(evt) {
83 assert_array_equals(evt.target.result,
84 alphabet.map(c => 'value-' + c));
85 t.done();
86 });
87 }, 'Get all values');
89 async_test(function(t) {
90 var req = createGetAllRequest(t, 'out-of-line', connection, undefined,
91 10);
92 req.onsuccess = t.step_func(function(evt) {
93 assert_array_equals(evt.target.result,
94 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
95 .map(c => 'value-' + c));
96 t.done();
97 });
98 }, 'Test maxCount');
100 async_test(function(t) {
101 var req = createGetAllRequest(t, 'out-of-line', connection,
102 IDBKeyRange.bound('g', 'm'));
103 req.onsuccess = t.step_func(function(evt) {
104 assert_array_equals(evt.target.result,
105 ['g', 'h', 'i', 'j', 'k', 'l', 'm']
106 .map(c => 'value-' + c));
107 t.done();
109 }, 'Get bound range');
111 async_test(function(t) {
112 var req = createGetAllRequest(t, 'out-of-line', connection,
113 IDBKeyRange.bound('g', 'm'), 3);
114 req.onsuccess = t.step_func(function(evt) {
115 assert_array_equals(evt.target.result, ['g', 'h', 'i']
116 .map(c => 'value-' + c));
117 t.done();
119 }, 'Get bound range with maxCount');
121 async_test(function(t) {
122 var req = createGetAllRequest(t, 'out-of-line', connection,
123 IDBKeyRange.bound('g', 'k', false, true));
124 req.onsuccess = t.step_func(function(evt) {
125 assert_array_equals(evt.target.result, ['g', 'h', 'i', 'j']
126 .map(c => 'value-' + c));
127 t.done();
129 }, 'Get upper excluded');
131 async_test(function(t) {
132 var req = createGetAllRequest(t, 'out-of-line', connection,
133 IDBKeyRange.bound('g', 'k', true, false));
134 req.onsuccess = t.step_func(function(evt) {
135 assert_array_equals(evt.target.result, ['h', 'i', 'j', 'k']
136 .map(c => 'value-' + c));
137 t.done();
139 }, 'Get lower excluded');
141 async_test(function(t) {
142 var req = createGetAllRequest(t, 'generated', connection,
143 IDBKeyRange.bound(4, 15), 3);
144 req.onsuccess = t.step_func(function(evt) {
145 var data = evt.target.result;
146 assert_true(Array.isArray(data));
147 assert_array_equals(data.map(e => e.ch), ['d', 'e', 'f']);
148 assert_array_equals(data.map(e => e.id), [4, 5, 6]);
149 t.done();
151 }, 'Get bound range (generated) with maxCount');
153 async_test(function(t) {
154 var req = createGetAllRequest(t, 'out-of-line', connection,
155 "Doesn't exist");
156 req.onsuccess = t.step_func(function(evt) {
157 assert_array_equals(evt.target.result, [],
158 'getAll() using a nonexistent key should return an empty array');
159 t.done();
161 req.onerror = t.unreached_func('getAll request should succeed');
162 }, 'Non existent key');
164 async_test(function(t) {
165 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, 0);
166 req.onsuccess = t.step_func(function(evt) {
167 assert_array_equals(evt.target.result,
168 alphabet.map(c => 'value-' + c));
169 t.done();
171 }, 'zero maxCount');
174 </script>