summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex-cross-realm-methods.html
blob: 1e431d3772cd1022eec43a1fddb8e67c78140dda (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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Cross-realm IDBIndex methods from detached iframe work as expected</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=resources/support.js></script>

<body>
<script>
"use strict";
const INDEX_LOWER = 1000;
const INDEX_UPPER = 1001;
const INDEX_RANGE = IDBKeyRange.bound(INDEX_LOWER, INDEX_UPPER);

const testCases = [
    {
        methodName: "get",
        arguments: [INDEX_LOWER],
        validateResult: e => {
            assert_equals(e.target.result.indexedKey, INDEX_LOWER);
        },
    },
    {
        methodName: "getKey",
        arguments: [INDEX_UPPER],
        validateResult: e => {
            assert_equals(e.target.result, INDEX_UPPER);
        },
    },
    {
        methodName: "getAll",
        arguments: [INDEX_RANGE],
        validateResult: e => {
            assert_array_equals(e.target.result.map(v => v.indexedKey), [INDEX_LOWER, INDEX_UPPER]);
        },
    },
    {
        methodName: "getAllKeys",
        arguments: [INDEX_RANGE],
        validateResult: e => {
            assert_array_equals(e.target.result, [INDEX_LOWER, INDEX_UPPER]);
        },
    },
    {
        methodName: "count",
        arguments: [INDEX_RANGE],
        validateResult: e => {
            assert_equals(e.target.result, 2);
        },
    },
    {
        methodName: "openCursor",
        arguments: [],
        validateResult: e => {
            const cursor = e.target.result;
            assert_true(cursor instanceof IDBCursor);
            assert_equals(cursor.value.indexedKey, INDEX_LOWER);
        },
    },
    {
        methodName: "openKeyCursor",
        arguments: [],
        validateResult: e => {
            const cursor = e.target.result;
            assert_true(cursor instanceof IDBCursor);
            assert_equals(cursor.key, INDEX_LOWER);
        },
    },
];

for (const testCase of testCases) {
    async_test(t => {
        const iframe = document.createElement("iframe");
        iframe.onload = t.step_func(() => {
            const method = iframe.contentWindow.IDBIndex.prototype[testCase.methodName];
            iframe.remove();

            let db;
            const open_rq = createdb(t);
            open_rq.onupgradeneeded = t.step_func(e => {
                db = e.target.result;
                const objectStore = db.createObjectStore("store");
                objectStore.createIndex("index", "indexedKey");
                objectStore.add({ indexedKey: INDEX_LOWER }, INDEX_LOWER);
                objectStore.add({ indexedKey: INDEX_UPPER }, INDEX_UPPER);
            });

            open_rq.onsuccess = t.step_func(() => {
                const index = db.transaction("store", "readonly", {durability: 'relaxed'}).objectStore("store").index("index");
                const rq = method.call(index, ...testCase.arguments);
                rq.onsuccess = t.step_func_done(e => {
                    testCase.validateResult(e);
                });
            });
        });
        document.body.append(iframe);
    }, `Cross-realm IDBIndex::${testCase.methodName}() method from detached <iframe> works as expected`);
}
</script>