OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>IndexedDB: Test IDBObjectStore.getAll.</title> | 2 <title>IndexedDB: Test IDBObjectStore.getAll.</title> |
3 <script src="../../resources/testharness.js"></script> | 3 <script src="../../resources/testharness.js"></script> |
4 <script src="../../resources/testharnessreport.js"></script> | 4 <script src="../../resources/testharnessreport.js"></script> |
5 <script> | 5 <script> |
6 | 6 |
7 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
8 | |
7 function doSetup(dbName, dbVersion, onsuccess) { | 9 function doSetup(dbName, dbVersion, onsuccess) { |
8 var delete_request = indexedDB.deleteDatabase(dbName); | 10 var delete_request = indexedDB.deleteDatabase(dbName); |
9 delete_request.onerror = function() { | 11 delete_request.onerror = function() { |
10 assert_unreached('deleteDatabase should not fail'); | 12 assert_unreached('deleteDatabase should not fail'); |
11 }; | 13 }; |
12 delete_request.onsuccess = function(e) { | 14 delete_request.onsuccess = function(e) { |
13 var req = indexedDB.open(dbName, dbVersion); | 15 var req = indexedDB.open(dbName, dbVersion); |
14 req.onsuccess = onsuccess; | 16 req.onsuccess = onsuccess; |
15 req.onerror = function() { | 17 req.onerror = function() { |
16 assert_unreached('open should not fail'); | 18 assert_unreached('open should not fail'); |
17 }; | 19 }; |
18 req.onupgradeneeded = function(evt) { | 20 req.onupgradeneeded = function(evt) { |
19 var connection = evt.target.result; | 21 var connection = evt.target.result; |
20 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
21 | 22 |
22 var store = connection.createObjectStore('generated', | 23 var store = connection.createObjectStore('generated', |
23 {autoIncrement: true, keyPath: 'id'}); | 24 {autoIncrement: true, keyPath: 'id'}); |
24 alphabet.forEach(function(letter) { | 25 alphabet.forEach(function(letter) { |
25 store.put({ch: letter}); | 26 store.put({ch: letter}); |
26 }); | 27 }); |
27 | 28 |
28 store = connection.createObjectStore('out-of-line', null); | 29 store = connection.createObjectStore('out-of-line', null); |
29 alphabet.forEach(function(letter) { | 30 alphabet.forEach(function(letter) { |
30 store.put(letter, letter); | 31 store.put('value-' + letter, letter); |
jsbell
2015/08/21 17:16:38
FYI, I made this change so that getAll and getAllK
| |
31 }); | 32 }); |
32 | 33 |
33 store = connection.createObjectStore('empty', null); | 34 store = connection.createObjectStore('empty', null); |
34 }; | 35 }; |
35 }; | 36 }; |
36 } | 37 } |
37 | 38 |
38 function createGetAllRequest(t, storeName, connection, range, maxCount) { | 39 function createGetAllRequest(t, storeName, connection, range, maxCount) { |
39 var transaction = connection.transaction(storeName, 'readonly'); | 40 var transaction = connection.transaction(storeName, 'readonly'); |
40 var store = transaction.objectStore(storeName); | 41 var store = transaction.objectStore(storeName); |
41 // TODO(cmumford): Simplify once crbug.com/335871 is closed. | 42 // TODO(cmumford): Simplify once crbug.com/335871 is closed. |
42 var req = maxCount !== undefined ? store.getAll(range, maxCount) : | 43 var req = maxCount !== undefined ? store.getAll(range, maxCount) : |
43 range !== undefined ? store.getAll(range) : store.getAll(); | 44 range !== undefined ? store.getAll(range) : store.getAll(); |
44 req.onerror = t.unreached_func('getAll request should succeed'); | 45 req.onerror = t.unreached_func('getAll request should succeed'); |
45 return req; | 46 return req; |
46 } | 47 } |
47 | 48 |
48 doSetup(location.pathname + '-IDBObjectStore.getAll', 1, function(evt) { | 49 doSetup(location.pathname + '-IDBObjectStore.getAll', 1, function(evt) { |
49 var connection = evt.target.result; | 50 var connection = evt.target.result; |
50 async_test(function(t) { | 51 async_test(function(t) { |
51 var req = createGetAllRequest(t, 'out-of-line', connection, 'c'); | 52 var req = createGetAllRequest(t, 'out-of-line', connection, 'c'); |
52 req.onsuccess = t.step_func(function(evt) { | 53 req.onsuccess = t.step_func(function(evt) { |
53 assert_array_equals(evt.target.result, ['c']); | 54 assert_array_equals(evt.target.result, ['value-c']); |
54 t.done(); | 55 t.done(); |
55 }); | 56 }); |
56 }, 'Single item get'); | 57 }, 'Single item get'); |
57 | 58 |
58 async_test(function(t) { | 59 async_test(function(t) { |
59 var req = createGetAllRequest(t, 'generated', connection, 3); | 60 var req = createGetAllRequest(t, 'generated', connection, 3); |
60 req.onsuccess = t.step_func(function(evt) { | 61 req.onsuccess = t.step_func(function(evt) { |
61 var data = evt.target.result; | 62 var data = evt.target.result; |
62 assert_true(Array.isArray(data)); | 63 assert_true(Array.isArray(data)); |
63 assert_equals(data.length, 1); | 64 assert_equals(data.length, 1); |
64 assert_object_equals(data[0], {ch: 'c', id: 3}); | 65 assert_equals(data[0].id, 3); |
66 assert_equals(data[0].ch, 'c'); | |
65 t.done(); | 67 t.done(); |
66 }); | 68 }); |
67 }, 'Single item get (generated key)'); | 69 }, 'Single item get (generated key)'); |
68 | 70 |
69 async_test(function(t) { | 71 async_test(function(t) { |
70 var req = createGetAllRequest(t, 'empty', connection); | 72 var req = createGetAllRequest(t, 'empty', connection); |
71 req.onsuccess = t.step_func(function(evt) { | 73 req.onsuccess = t.step_func(function(evt) { |
72 assert_array_equals(evt.target.result, [], | 74 assert_array_equals(evt.target.result, [], |
73 'getAll() on empty object store should return an empty array'); | 75 'getAll() on empty object store should return an empty array'); |
74 t.done(); | 76 t.done(); |
75 }); | 77 }); |
76 }, 'getAll on empty object store'); | 78 }, 'getAll on empty object store'); |
77 | 79 |
78 async_test(function(t) { | 80 async_test(function(t) { |
79 var req = createGetAllRequest(t, 'out-of-line', connection); | 81 var req = createGetAllRequest(t, 'out-of-line', connection); |
80 req.onsuccess = t.step_func(function(evt) { | 82 req.onsuccess = t.step_func(function(evt) { |
81 assert_array_equals(evt.target.result, | 83 assert_array_equals(evt.target.result, |
82 'abcdefghijklmnopqrstuvwxyz'.split('')); | 84 alphabet.map(c => 'value-' + c)); |
83 t.done(); | 85 t.done(); |
84 }); | 86 }); |
85 }, 'Get all values'); | 87 }, 'Get all values'); |
86 | 88 |
87 async_test(function(t) { | 89 async_test(function(t) { |
88 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, | 90 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, |
89 10); | 91 10); |
90 req.onsuccess = t.step_func(function(evt) { | 92 req.onsuccess = t.step_func(function(evt) { |
91 assert_array_equals(evt.target.result, | 93 assert_array_equals(evt.target.result, |
92 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']); | 94 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] |
95 .map(c => 'value-' + c)); | |
93 t.done(); | 96 t.done(); |
94 }); | 97 }); |
95 }, 'Test maxCount'); | 98 }, 'Test maxCount'); |
96 | 99 |
97 async_test(function(t) { | 100 async_test(function(t) { |
98 var req = createGetAllRequest(t, 'out-of-line', connection, | 101 var req = createGetAllRequest(t, 'out-of-line', connection, |
99 IDBKeyRange.bound('g', 'm')); | 102 IDBKeyRange.bound('g', 'm')); |
100 req.onsuccess = t.step_func(function(evt) { | 103 req.onsuccess = t.step_func(function(evt) { |
101 assert_array_equals(evt.target.result, | 104 assert_array_equals(evt.target.result, |
102 ['g', 'h', 'i', 'j', 'k', 'l', 'm']); | 105 ['g', 'h', 'i', 'j', 'k', 'l', 'm'] |
106 .map(c => 'value-' + c)); | |
103 t.done(); | 107 t.done(); |
104 }); | 108 }); |
105 }, 'Get bound range'); | 109 }, 'Get bound range'); |
106 | 110 |
107 async_test(function(t) { | 111 async_test(function(t) { |
108 var req = createGetAllRequest(t, 'out-of-line', connection, | 112 var req = createGetAllRequest(t, 'out-of-line', connection, |
109 IDBKeyRange.bound('g', 'm'), 3); | 113 IDBKeyRange.bound('g', 'm'), 3); |
110 req.onsuccess = t.step_func(function(evt) { | 114 req.onsuccess = t.step_func(function(evt) { |
111 assert_array_equals(evt.target.result, ['g', 'h', 'i']); | 115 assert_array_equals(evt.target.result, ['g', 'h', 'i'] |
116 .map(c => 'value-' + c)); | |
112 t.done(); | 117 t.done(); |
113 }); | 118 }); |
114 }, 'Get bound range with maxCount'); | 119 }, 'Get bound range with maxCount'); |
115 | 120 |
116 async_test(function(t) { | 121 async_test(function(t) { |
117 var req = createGetAllRequest(t, 'out-of-line', connection, | 122 var req = createGetAllRequest(t, 'out-of-line', connection, |
118 IDBKeyRange.bound('g', 'k', false, true)); | 123 IDBKeyRange.bound('g', 'k', false, true)); |
119 req.onsuccess = t.step_func(function(evt) { | 124 req.onsuccess = t.step_func(function(evt) { |
120 assert_array_equals(evt.target.result, ['g', 'h', 'i', 'j']); | 125 assert_array_equals(evt.target.result, ['g', 'h', 'i', 'j'] |
126 .map(c => 'value-' + c)); | |
121 t.done(); | 127 t.done(); |
122 }); | 128 }); |
123 }, 'Get upper excluded'); | 129 }, 'Get upper excluded'); |
124 | 130 |
125 async_test(function(t) { | 131 async_test(function(t) { |
126 var req = createGetAllRequest(t, 'out-of-line', connection, | 132 var req = createGetAllRequest(t, 'out-of-line', connection, |
127 IDBKeyRange.bound('g', 'k', true, false)); | 133 IDBKeyRange.bound('g', 'k', true, false)); |
128 req.onsuccess = t.step_func(function(evt) { | 134 req.onsuccess = t.step_func(function(evt) { |
129 assert_array_equals(evt.target.result, ['h', 'i', 'j', 'k']); | 135 assert_array_equals(evt.target.result, ['h', 'i', 'j', 'k'] |
136 .map(c => 'value-' + c)); | |
130 t.done(); | 137 t.done(); |
131 }); | 138 }); |
132 }, 'Get lower excluded'); | 139 }, 'Get lower excluded'); |
133 | 140 |
134 async_test(function(t) { | 141 async_test(function(t) { |
135 var req = createGetAllRequest(t, 'generated', connection, | 142 var req = createGetAllRequest(t, 'generated', connection, |
136 IDBKeyRange.bound(4, 15), 3); | 143 IDBKeyRange.bound(4, 15), 3); |
137 req.onsuccess = t.step_func(function(evt) { | 144 req.onsuccess = t.step_func(function(evt) { |
138 var data = evt.target.result; | 145 var data = evt.target.result; |
139 assert_true(Array.isArray(data)); | 146 assert_true(Array.isArray(data)); |
140 assert_equals(data.length, 3); | 147 assert_array_equals(data.map(e => e.ch), ['d', 'e', 'f']); |
141 assert_object_equals(data[0], {ch: 'd', id: 4}); | 148 assert_array_equals(data.map(e => e.id), [4, 5, 6]); |
cmumford
2015/08/21 17:32:40
In general I really like the way you used map in t
jsbell
2015/08/21 17:48:32
IMHO we don't to be that paranoid everywhere, and
| |
142 assert_object_equals(data[1], {ch: 'e', id: 5}); | |
143 assert_object_equals(data[2], {ch: 'f', id: 6}); | |
144 t.done(); | 149 t.done(); |
145 }); | 150 }); |
146 }, 'Get bound range (generated) with maxCount'); | 151 }, 'Get bound range (generated) with maxCount'); |
147 | 152 |
148 async_test(function(t) { | 153 async_test(function(t) { |
149 var req = createGetAllRequest(t, 'out-of-line', connection, | 154 var req = createGetAllRequest(t, 'out-of-line', connection, |
150 "Doesn't exist"); | 155 "Doesn't exist"); |
151 req.onsuccess = t.step_func(function(evt) { | 156 req.onsuccess = t.step_func(function(evt) { |
152 assert_array_equals(evt.target.result, [], | 157 assert_array_equals(evt.target.result, [], |
153 'getAll() using a nonexistent key should return an empty array'); | 158 'getAll() using a nonexistent key should return an empty array'); |
154 t.done(); | 159 t.done(); |
155 }); | 160 }); |
156 req.onerror = t.unreached_func('getAll request should succeed'); | 161 req.onerror = t.unreached_func('getAll request should succeed'); |
157 }, 'Non existent key'); | 162 }, 'Non existent key'); |
158 | 163 |
159 async_test(function(t) { | 164 async_test(function(t) { |
160 var transaction = connection.transaction('out-of-line', 'readonly'); | 165 var req = createGetAllRequest(t, 'out-of-line', connection, undefined, 0); |
161 var store = transaction.objectStore('out-of-line'); | 166 req.onsuccess = t.step_func(function(evt) { |
162 assert_throws(new TypeError(), function () { | 167 assert_array_equals(evt.target.result, alphabet.map(c => 'value-' + c) ); |
cmumford
2015/08/21 17:32:40
Do you want to keep this < 80 chars for w3c submis
jsbell
2015/08/21 17:48:32
Done. (Here and elsewhere)
| |
163 store.getAll(undefined, 0); | 168 t.done(); |
164 }, 'getAll() with maxCount=0 should throw TypeError'); | 169 }); |
165 t.done(); | |
166 }, 'zero maxCount'); | 170 }, 'zero maxCount'); |
167 }); | 171 }); |
168 | 172 |
169 </script> | 173 </script> |
OLD | NEW |