summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/storage/storagemanager-estimate.https.any.js
blob: c2f5c569dc528cb8e58e8c057ba95ce1354463a7 (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
// META: title=StorageManager: estimate()

test(function(t) {
    assert_true(navigator.storage.estimate() instanceof Promise);
}, 'estimate() method returns a Promise');

promise_test(function(t) {
    return navigator.storage.estimate().then(function(result) {
        assert_equals(typeof result, 'object');
        assert_true('usage' in result);
        assert_equals(typeof result.usage, 'number');
        assert_true('quota' in result);
        assert_equals(typeof result.quota, 'number');
    });
}, 'estimate() resolves to dictionary with members');

promise_test(function(t) {
    const large_value = new Uint8Array(1e6);
    const dbname = `db-${location}-${t.name}`;
    let db, before, after;

    indexedDB.deleteDatabase(dbname);
    return new Promise((resolve, reject) => {
            const open = indexedDB.open(dbname);
            open.onerror = () => { reject(open.error); };
            open.onupgradeneeded = () => {
                const connection = open.result;
                connection.createObjectStore('store');
            };
            open.onsuccess = () => {
                const connection = open.result;
                t.add_cleanup(() => {
                    connection.close();
                    indexedDB.deleteDatabase(dbname);
                });
                resolve(connection);
            };
        })
        .then(connection => {
            db = connection;
            return navigator.storage.estimate();
        })
        .then(estimate => {
            before = estimate.usage;
            return new Promise((resolve, reject) => {
                const tx = db.transaction('store', 'readwrite');
                tx.objectStore('store').put(large_value, 'key');
                tx.onabort = () => { reject(tx.error); };
                tx.oncomplete = () => { resolve(); };
            });
        })
        .then(() => {
            return navigator.storage.estimate();
        })
        .then(estimate => {
            after = estimate.usage;
            assert_greater_than(after, before,
                                'estimated usage should increase');
        });
}, 'estimate() shows usage increase after 1MB IndexedDB record is stored');