summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/storage/estimate-indexeddb.https.any.js
blob: b0c6b944dd6ff83a00b38cac6d6c4e3ea8424979 (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
// META: title=StorageManager: estimate() for indexeddb

function indexedDbOpenRequest(t, dbname, upgrade_func) {
  return new Promise((resolve, reject) => {
    const openRequest = indexedDB.open(dbname);
    t.add_cleanup(() => {
      indexedDbDeleteRequest(dbname);
    });

    openRequest.onerror = () => {
      reject(openRequest.error);
    };
    openRequest.onsuccess = () => {
      resolve(openRequest.result);
    };
    openRequest.onupgradeneeded = event => {
      upgrade_func(openRequest.result);
    };
  });
}

function indexedDbDeleteRequest(name) {
  return new Promise((resolve, reject) => {
    const deleteRequest = indexedDB.deleteDatabase(name);
    deleteRequest.onerror = () => {
      reject(deleteRequest.error);
    };
    deleteRequest.onsuccess = () => {
      resolve();
    };
  });
}

function transactionPromise(txn) {
  return new Promise((resolve, reject) => {
    txn.onabort = () => {
      reject(txn.error);
    };
    txn.oncomplete = () => {
      resolve();
    };
  });
}

test(t => {
  assert_true('estimate' in navigator.storage);
  assert_equals(typeof navigator.storage.estimate, 'function');
  assert_true(navigator.storage.estimate() instanceof Promise);
}, 'estimate() method exists and returns a Promise');

promise_test(async t => {
  const estimate = await navigator.storage.estimate();
  assert_equals(typeof estimate, 'object');
  assert_true('usage' in estimate);
  assert_equals(typeof estimate.usage, 'number');
  assert_true('quota' in estimate);
  assert_equals(typeof estimate.quota, 'number');
}, 'estimate() resolves to dictionary with members');

promise_test(async t => {
  const arraySize = 1e6;
  const objectStoreName = "storageManager";
  const dbname = this.window ? window.location.pathname :
        "estimate-worker.https.html";

  await indexedDbDeleteRequest(dbname);
  let estimate = await navigator.storage.estimate();

  const usageBeforeCreate = estimate.usage;
  const db = await indexedDbOpenRequest(t, dbname, (db_to_upgrade) => {
    db_to_upgrade.createObjectStore(objectStoreName);
  });

  estimate = await navigator.storage.estimate();
  const usageAfterCreate = estimate.usage;

  assert_greater_than(
    usageAfterCreate, usageBeforeCreate,
    'estimated usage should increase after object store is created');

  const txn = db.transaction(objectStoreName, 'readwrite');
  const buffer = new ArrayBuffer(arraySize);
  const view = new Uint8Array(buffer);

  for (let i = 0; i < arraySize; i++) {
    view[i] = Math.floor(Math.random() * 255);
  }

  const testBlob = new Blob([buffer], {type: "binary/random"});
  txn.objectStore(objectStoreName).add(testBlob, 1);

  await transactionPromise(txn);

  estimate = await navigator.storage.estimate();
  const usageAfterPut = estimate.usage;
  assert_greater_than(
    usageAfterPut, usageAfterCreate,
    'estimated usage should increase after large value is stored');

  db.close();
}, 'estimate() shows usage increase after large value is stored');