summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbcursor_continue_delete_objectstore.htm
blob: 86b4abf113eea27e62e9bbb60fba23e4efb64da5 (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
<!DOCTYPE html>
<title>IDBObjectStore.delete() and IDBCursor.continue() - object store - remove a record from the object store while iterating cursor</title>
<link rel="author" title="Mozilla" href="https://www.mozilla.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support.js"></script>

<script>
    /* The goal here is to test that any prefetching of cursor values performs
     * correct invalidation of prefetched data.  This test is motivated by the
     * particularities of the Firefox implementation of preloading, and is
     * specifically motivated by an edge case when prefetching prefetches at
     * least 2 extra records and at most determines whether a mutation is
     * potentially relevant based on current cursor position and direction and
     * does not test for key equivalence.  Future implementations may want to
     * help refine this test if their cursors are more clever.
     *
     * Step-wise we:
     * - Open a cursor, returning key 0.
     * - When the cursor request completes, without yielding control:
     *   - Issue a delete() call that won't actually delete anything but looks
     *     relevant.  This should purge prefetched records 1 and 2.
     *   - Issue a continue() which should result in record 1 being fetched
     *     again and record 2 being prefetched again.
     *   - Delete record 2.  Unless there's a synchronously available source
     *     of truth, the data from continue() above will not be present and
     *     we'll expect the implementation to need to set a flag to invalidate
     *     the prefetched data when it arrives.
     * - When the cursor request completes, validate we got record 1 and issue
     *   a continue.
     * - When the request completes, we should have a null cursor result value
     *   because 2 was deleted.
     */
    var db,
      count = 0,
      t = async_test(),
      records = [ { pKey: "primaryKey_0" },
                  { pKey: "primaryKey_1" },
                  { pKey: "primaryKey_2" } ];

    // This is a key that is not present in the database, but that is known to
    // be relevant to a forward iteration of the above keys by comparing to be
    // greater than all of them.
    var plausibleFutureKey = "primaryKey_9";

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function(e) {
        db = e.target.result;

        var objStore = db.createObjectStore("test", { keyPath: "pKey" });

        for (var i = 0; i < records.length; i++)
            objStore.add(records[i]);
    };

    open_rq.onsuccess = t.step_func(CursorDeleteRecord);


    function CursorDeleteRecord(e) {
        var txn = db.transaction("test", "readwrite", {durability: 'relaxed'}),
          object_store = txn.objectStore("test"),
          cursor_rq = object_store.openCursor();

        var iteration = 0;

        cursor_rq.onsuccess = t.step_func(function(e) {
            var cursor = e.target.result;

            switch (iteration) {
            case 0:
                object_store.delete(plausibleFutureKey);
                assert_true(cursor != null, "cursor valid");
                assert_equals(cursor.value.pKey, records[iteration].pKey);
                cursor.continue();
                object_store.delete(records[2].pKey);
                break;
            case 1:
                assert_true(cursor != null, "cursor valid");
                assert_equals(cursor.value.pKey, records[iteration].pKey);
                cursor.continue();
                break;
            case 2:
                assert_equals(cursor, null, "cursor no longer valid");
                break;
            };
            iteration++;
        });

        txn.oncomplete = t.step_func(VerifyRecordWasDeleted);
    }


    function VerifyRecordWasDeleted(e) {
        var cursor_rq = db.transaction("test", "readonly", {durability: 'relaxed'})
                          .objectStore("test")
                          .openCursor();

        cursor_rq.onsuccess = t.step_func(function(e) {
            var cursor = e.target.result;

            if (!cursor) {
                assert_equals(count, 2, 'count');
                t.done();
            }

            assert_equals(cursor.value.pKey, records[count].pKey);
            count++;
            cursor.continue();
        });
    }

</script>

<div id="log"></div>