summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbcursor-continuePrimaryKey.htm
blob: 0de88c0aefc93d551acd314f17164f51c6ea948c (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
<!doctype html>
<meta charset="utf8">
<title>IndexedDB: IDBCursor method continuePrimaryKey()</title>
<link rel="help"
      href="http://w3c.github.io/IndexedDB/#dom-idbcursor-continueprimarykey">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support.js"></script>

<script>
'use strict';

indexeddb_test(
    (t, db, txn) => {
      const store = db.createObjectStore('store');
      const index = store.createIndex('index', 'indexKey', {multiEntry: true});

      store.put({indexKey: ['a', 'b']}, 1);
      store.put({indexKey: ['a', 'b']}, 2);
      store.put({indexKey: ['a', 'b']}, 3);
      store.put({indexKey: ['b']}, 4);

      const expectedIndexEntries = [
        {key: "a", primaryKey: 1},
        {key: "a", primaryKey: 2},
        {key: "a", primaryKey: 3},
        {key: "b", primaryKey: 1},
        {key: "b", primaryKey: 2},
        {key: "b", primaryKey: 3},
        {key: "b", primaryKey: 4},
      ];

      const request = index.openCursor();
      request.onerror = t.unreached_func('IDBIndex.openCursor should not fail');
      request.onsuccess = t.step_func(() => {
        const cursor = request.result;
        const expectedEntry = expectedIndexEntries.shift();
        if (expectedEntry) {
          assert_equals(cursor.key, expectedEntry.key,
              'The index entry keys should reflect the object store contents');
          assert_equals(cursor.primaryKey, expectedEntry.primaryKey,
              'The index entry primary keys should reflect the object store ' +
              'contents');
          cursor.continue();
        } else {
          assert_equals(cursor, null,
              'The index should not have entries that do not reflect the ' +
              'object store contents');
        }
      });
    },
    (t, db) => {
      const testCases = [
        // Continuing index key
        { call: cursor => { cursor.continue(); },
          result: { key: "a", primaryKey: 2 } },
        { call: cursor => { cursor.continue('a'); },
          exception: 'DataError' },
        { call: cursor => { cursor.continue('b'); },
          result: { key: "b", primaryKey: 1 } },
        { call: cursor => { cursor.continue('c'); }, result: null },

        // Called w/ index key and primary key:
        { call: cursor => { cursor.continuePrimaryKey('a', 3); },
          result: { key: 'a', primaryKey: 3 } },
        { call: cursor => { cursor.continuePrimaryKey('a', 4); },
          result: { key: 'b', primaryKey: 1 } },
        { call: cursor => { cursor.continuePrimaryKey('b', 1); },
          result: {key: 'b', primaryKey: 1} },
        { call: cursor => { cursor.continuePrimaryKey('b', 4); },
          result: {key: 'b', primaryKey: 4} },
        { call: cursor => { cursor.continuePrimaryKey('b', 5); },
          result: null },
        { call: cursor => { cursor.continuePrimaryKey('c', 1); },
          result: null },

        // Called w/ primary key but w/o index key
        { call: cursor => { cursor.continuePrimaryKey(null, 1); },
          exception: 'DataError' },
        { call: cursor => { cursor.continuePrimaryKey(null, 2); },
          exception: 'DataError' },
        { call: cursor => { cursor.continuePrimaryKey(null, 3); },
          exception: 'DataError' },
        { call: cursor => { cursor.continuePrimaryKey(null, 4); },
          exception: 'DataError' },
        { call: cursor => { cursor.continuePrimaryKey(null, 5); },
          exception: 'DataError' },

        // Called w/ index key but w/o primary key
        { call: cursor => { cursor.continuePrimaryKey('a', null); },
          exception: 'DataError' },
      ];

      const verifyContinueCalls = () => {
        if (!testCases.length) {
          t.done();
          return;
        }

        const testCase = testCases.shift();

        const txn = db.transaction('store', 'readonly', {durability: 'relaxed'});
        txn.oncomplete = t.step_func(verifyContinueCalls);

        const request = txn.objectStore('store').index('index').openCursor();
        let calledContinue = false;
        request.onerror =
            t.unreached_func('IDBIndex.openCursor should not fail');
        request.onsuccess = t.step_func(() => {
          const cursor = request.result;
          if (calledContinue) {
            if (testCase.result) {
              assert_equals(cursor.key, testCase.result.key,
                  `${testCase.call.toString()} - result key`);
              assert_equals(cursor.primaryKey, testCase.result.primaryKey,
                  `${testCase.call.toString()} - result primary key`);
            } else {
              assert_equals(cursor, null);
            }
          } else {
            calledContinue = true;
            if('exception' in testCase) {
              assert_throws_dom(
                  testCase.exception, () => { testCase.call(cursor); },
                  testCase.call.toString());
            } else {
              testCase.call(cursor);
            }
          }
        });
      };
      verifyContinueCalls();
    });
</script>