Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / objectstore-getallkeys.html
blob8ca26eac529be274ab0c62da1df9e305668a6f27
1 <!DOCTYPE html>
2 <title>IndexedDB: Test IDBObjectStore.getAllKeys.</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 createGetAllKeysRequest(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.getAllKeys(range, maxCount) :
44 range !== undefined ? store.getAllKeys(range) :
45 store.getAllKeys();
46 req.onerror = t.unreached_func('getAllKeys request should succeed');
47 return req;
50 doSetup(location.pathname + '-IDBObjectStore.getAllKeys', 1, function(evt) {
51 var connection = evt.target.result;
52 async_test(function(t) {
53 var req = createGetAllKeysRequest(t, 'out-of-line', connection, 'c');
54 req.onsuccess = t.step_func(function(evt) {
55 assert_array_equals(evt.target.result, ['c']);
56 t.done();
57 });
58 }, 'Single item get');
60 async_test(function(t) {
61 var req = createGetAllKeysRequest(t, 'generated', connection, 3);
62 req.onsuccess = t.step_func(function(evt) {
63 var data = evt.target.result;
64 assert_true(Array.isArray(data));
65 assert_array_equals(data, [3]);
66 t.done();
67 });
68 }, 'Single item get (generated key)');
70 async_test(function(t) {
71 var req = createGetAllKeysRequest(t, 'empty', connection);
72 req.onsuccess = t.step_func(function(evt) {
73 assert_array_equals(evt.target.result, [],
74 'getAllKeys() on empty object store should return an empty ' +
75 'array');
76 t.done();
77 });
78 }, 'getAllKeys on empty object store');
80 async_test(function(t) {
81 var req = createGetAllKeysRequest(t, 'out-of-line', connection);
82 req.onsuccess = t.step_func(function(evt) {
83 assert_array_equals(evt.target.result, alphabet);
84 t.done();
85 });
86 }, 'Get all values');
88 async_test(function(t) {
89 var req = createGetAllKeysRequest(t, 'out-of-line', connection, undefined,
90 10);
91 req.onsuccess = t.step_func(function(evt) {
92 assert_array_equals(evt.target.result,
93 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
94 t.done();
95 });
96 }, 'Test maxCount');
98 async_test(function(t) {
99 var req = createGetAllKeysRequest(t, 'out-of-line', connection,
100 IDBKeyRange.bound('g', 'm'));
101 req.onsuccess = t.step_func(function(evt) {
102 assert_array_equals(evt.target.result,
103 ['g', 'h', 'i', 'j', 'k', 'l', 'm']);
104 t.done();
106 }, 'Get bound range');
108 async_test(function(t) {
109 var req = createGetAllKeysRequest(t, 'out-of-line', connection,
110 IDBKeyRange.bound('g', 'm'), 3);
111 req.onsuccess = t.step_func(function(evt) {
112 assert_array_equals(evt.target.result, ['g', 'h', 'i']);
113 t.done();
115 }, 'Get bound range with maxCount');
117 async_test(function(t) {
118 var req = createGetAllKeysRequest(t, 'out-of-line', connection,
119 IDBKeyRange.bound('g', 'k', false, true));
120 req.onsuccess = t.step_func(function(evt) {
121 assert_array_equals(evt.target.result, ['g', 'h', 'i', 'j']);
122 t.done();
124 }, 'Get upper excluded');
126 async_test(function(t) {
127 var req = createGetAllKeysRequest(t, 'out-of-line', connection,
128 IDBKeyRange.bound('g', 'k', true, false));
129 req.onsuccess = t.step_func(function(evt) {
130 assert_array_equals(evt.target.result, ['h', 'i', 'j', 'k']);
131 t.done();
133 }, 'Get lower excluded');
135 async_test(function(t) {
136 var req = createGetAllKeysRequest(t, 'generated', connection,
137 IDBKeyRange.bound(4, 15), 3);
138 req.onsuccess = t.step_func(function(evt) {
139 var data = evt.target.result;
140 assert_true(Array.isArray(data));
141 assert_array_equals(data, [4, 5, 6]);
142 t.done();
144 }, 'Get bound range (generated) with maxCount');
146 async_test(function(t) {
147 var req = createGetAllKeysRequest(t, 'out-of-line', connection,
148 "Doesn't exist");
149 req.onsuccess = t.step_func(function(evt) {
150 assert_array_equals(evt.target.result, [],
151 'getAllKeys() using a nonexistent key should return an ' +
152 'empty array');
153 t.done();
155 req.onerror = t.unreached_func('getAllKeys request should succeed');
156 }, 'Non existent key');
158 async_test(function(t) {
159 var req = createGetAllKeysRequest(t, 'out-of-line', connection, undefined,
161 req.onsuccess = t.step_func(function(evt) {
162 assert_array_equals(evt.target.result, alphabet);
163 t.done();
165 }, 'zero maxCount');
168 </script>