summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex_getAllKeys.html
blob: 5640bfdee7eed532a582f753a358c0879c37bff1 (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
<!DOCTYPE html>
<title>IndexedDB: Test IDBIndex.getAllKeys.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support.js"></script>
<script>

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

function getall_test(func, name) {
  indexeddb_test(
    function(t, connection, tx) {
      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-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.toUpperCase());
      });

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

function createGetAllKeysRequest(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.getAllKeys(range, maxCount);
    req.onerror = t.unreached_func('getAllKeys request should succeed');
    return req;
}

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection, 'C');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_array_equals(evt.target.result, ['c']);
          t.done();
      });
    }, 'Single item get');

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

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, alphabet,
              'getAllKeys() should return a..z');
          t.done();
      });
    }, 'Get all keys');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'generated', connection);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result,
              [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],
              'getAllKeys() should return 1..26');
          t.done();
      });
    }, 'Get all generated keys');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection, undefined,
                                    10);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result,
                             'abcdefghij'.split(''),
                             'getAllKeys() should return a..j');
          t.done();
      });
    }, 'maxCount=10');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection,
                                    IDBKeyRange.bound('G', 'M'));
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result,
                              'ghijklm'.split(''),
                              'getAllKeys() should return g..m');
          t.done();
      });
    }, 'Get bound range');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection,
                                    IDBKeyRange.bound('G', 'M'), 3);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result,
                             ['g', 'h', 'i'],
                             'getAllKeys() should return g..i');
          t.done();
      });
    }, 'Get bound range with maxCount');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection,
          IDBKeyRange.bound('G', 'K', false, true));
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result,
                             ['g', 'h', 'i', 'j'],
                             'getAllKeys() should return g..j');
          t.done();
      });
    }, 'Get upper excluded');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection,
          IDBKeyRange.bound('G', 'K', true, false));
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result,
                             ['h', 'i', 'j', 'k'],
                             'getAllKeys() should return h..k');
          t.done();
      });
    }, 'Get lower excluded');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'generated',
          connection, IDBKeyRange.bound(4, 15), 3);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, [],
                              'getAllKeys() should return []');
          t.done();
      });
    }, 'Get bound range (generated) with maxCount');

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

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line', connection,
          undefined, 0);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, alphabet,
              'getAllKeys() should return a..z');
          t.done();
      });
    }, 'maxCount=0');

getall_test(function(t, connection) {
      var req = createGetAllKeysRequest(t, 'out-of-line-multi', connection,
                                        'vowel');
      req.onsuccess = t.step_func(function(evt) {
        assert_array_equals(evt.target.result, ['A','E','I','O','U'])
        t.done();
      });
      req.onerror = t.unreached_func('getAllKeys request should succeed');
    }, 'Retrieve multiEntry keys');

</script>