summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex_getAll.html
blob: 507b6c9f860eb6d9aa8a2f0d95306d4a0165da38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<title>IndexedDB: Test IDBIndex.getAll.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
setup({explicit_done: true});

var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');

function doSetup(dbName, dbVersion, onsuccess) {
  var delete_request = indexedDB.deleteDatabase(dbName);
  delete_request.onerror = function() {
    assert_unreached('deleteDatabase should not fail');
  };
  delete_request.onsuccess = function(e) {
    var req = indexedDB.open(dbName, dbVersion);
    req.onsuccess = onsuccess;
    req.onerror = function() {
      assert_unreached('open should not fail');
    };
    req.onupgradeneeded = function(evt) {
      var connection = evt.target.result;

      var store = connection.createObjectStore('generated',
            {autoIncrement: true, keyPath: 'id'});
      var index = store.createIndex('test_idx', 'upper');
      alphabet.forEach(function(letter) {
        store.put({ch: letter, upper: letter.toUpperCase()});
      });

      store = connection.createObjectStore('out-of-line', null);
      index = store.createIndex('test_idx', 'upper');
      alphabet.forEach(function(letter) {
        store.put({ch: letter, upper: letter.toUpperCase()}, letter);
      });

      store = connection.createObjectStore('out-of-line-not-unique', null);
      index = store.createIndex('test_idx', 'half');
      alphabet.forEach(function(letter) {
        if (letter <= 'm')
          store.put({ch: letter, half: 'first'}, letter);
        else
          store.put({ch: letter, half: 'second'}, letter);
      });

      store = connection.createObjectStore('out-of-line-multi', null);
      index = store.createIndex('test_idx', 'attribs', {multiEntry: true});
      alphabet.forEach(function(letter) {
        attrs = [];
        if (['a', 'e', 'i', 'o', 'u'].indexOf(letter) != -1)
          attrs.push('vowel');
        else
          attrs.push('consonant');
        if (letter == 'a')
          attrs.push('first');
        if (letter == 'z')
          attrs.push('last');
        store.put({ch: letter, attribs: attrs}, letter);
      });

      store = connection.createObjectStore('empty', null);
      index = store.createIndex('test_idx', 'upper');
    };
  };
}

function createGetAllRequest(t, storeName, connection, range, maxCount) {
    var transaction = connection.transaction(storeName, 'readonly');
    var store = transaction.objectStore(storeName);
    var index = store.index('test_idx');
    var req = index.getAll(range, maxCount);
    req.onerror = t.unreached_func('getAll request should succeed');
    return req;
}

doSetup(location.pathname + '-IDBIndex.getAll', 1, function(evt) {
    var connection = evt.target.result;
    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection, 'C');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), ['c']);
          assert_array_equals(data.map(function(e) { return e.upper; }), ['C']);
          t.done();
      });
    }, 'Single item get');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'empty', connection);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, [],
              'getAll() on empty object store should return an empty array');
          t.done();
      });
    }, 'Empty object store');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), alphabet);
          assert_array_equals(data.map(function(e) { return e.upper; }), ALPHABET);
          t.done();
      });
    }, 'Get all keys');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection, undefined,
                                    10);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'abcdefghij'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'ABCDEFGHIJ'.split(''));
          t.done();
      });
    }, 'maxCount=10');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
                                    IDBKeyRange.bound('G', 'M'));
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_array_equals(data.map(function(e) { return e.ch; }), 'ghijklm'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'GHIJKLM'.split(''));
          t.done();
      });
    }, 'Get bound range');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
                                    IDBKeyRange.bound('G', 'M'), 3);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'ghi'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'GHI'.split(''));
          t.done();
      });
    }, 'Get bound range with maxCount');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
          IDBKeyRange.bound('G', 'K', false, true));
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'ghij'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'GHIJ'.split(''));
          t.done();
      });
    }, 'Get upper excluded');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
          IDBKeyRange.bound('G', 'K', true, false));
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'hijk'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'HIJK'.split(''));
          t.done();
      });
    }, 'Get lower excluded');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'generated',
          connection, IDBKeyRange.bound(4, 15), 3);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_true(Array.isArray(data));
          assert_equals(data.length, 0);
          t.done();
      });
    }, 'Get bound range (generated) with maxCount');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line',
          connection, "Doesn't exist");
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, [],
              'getAll() using a nonexistent key should return an empty array');
          t.done();
      req.onerror = t.unreached_func('getAll request should succeed');
      });
    }, 'Non existent key');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
          undefined, 0);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), alphabet);
          assert_array_equals(data.map(function(e) { return e.upper; }), ALPHABET);
          t.done();
      });
    }, 'maxCount=0');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line-not-unique', connection,
                                    'first');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'abcdefghijklm'.split(''));
          assert_true(data.every(function(e) { return e.half === 'first'; }));
          t.done();
      });
    }, 'Retrieve multiEntry key');

    async_test(function(t) {
      var req = createGetAllRequest(t, 'out-of-line-multi', connection,
                                    'vowel');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), ['a', 'e', 'i', 'o', 'u']);
          assert_array_equals(data[0].attribs, ['vowel', 'first']);
          assert_true(data.every(function(e) { return e.attribs[0] === 'vowel'; }));
          t.done();
      });
    }, 'Retrieve one key multiple values');

    // Explicit done needed in case async_test body fails synchronously.
    done();
});

</script>