2 <title>IndexedDB: Test IDBObjectStore.getAllKeys.
</title>
3 <script src=
"../../resources/testharness.js"></script>
4 <script src=
"../../resources/testharnessreport.js"></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
});
29 store
= connection
.createObjectStore('out-of-line', null);
30 alphabet
.forEach(function(letter
) {
31 store
.put('value-' + letter
, letter
);
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
) :
46 req
.onerror
= t
.unreached_func('getAllKeys request should succeed');
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']);
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]);
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 ' +
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
);
88 async_test(function(t
) {
89 var req
= createGetAllKeysRequest(t
, 'out-of-line', connection
, undefined,
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']);
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']);
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']);
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']);
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']);
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]);
144 }, 'Get bound range (generated) with maxCount');
146 async_test(function(t
) {
147 var req
= createGetAllKeysRequest(t
, 'out-of-line', connection
,
149 req
.onsuccess
= t
.step_func(function(evt
) {
150 assert_array_equals(evt
.target
.result
, [],
151 'getAllKeys() using a nonexistent key should return an ' +
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
);