Bug 1770047 [wpt PR 34117] - [Clipboard API] Clipboard Web Custom Formats implementat...
[gecko.git] / testing / web-platform / tests / storage / estimate-indexeddb.https.any.js
blobb0c6b944dd6ff83a00b38cac6d6c4e3ea8424979
1 // META: title=StorageManager: estimate() for indexeddb
3 function indexedDbOpenRequest(t, dbname, upgrade_func) {
4   return new Promise((resolve, reject) => {
5     const openRequest = indexedDB.open(dbname);
6     t.add_cleanup(() => {
7       indexedDbDeleteRequest(dbname);
8     });
10     openRequest.onerror = () => {
11       reject(openRequest.error);
12     };
13     openRequest.onsuccess = () => {
14       resolve(openRequest.result);
15     };
16     openRequest.onupgradeneeded = event => {
17       upgrade_func(openRequest.result);
18     };
19   });
22 function indexedDbDeleteRequest(name) {
23   return new Promise((resolve, reject) => {
24     const deleteRequest = indexedDB.deleteDatabase(name);
25     deleteRequest.onerror = () => {
26       reject(deleteRequest.error);
27     };
28     deleteRequest.onsuccess = () => {
29       resolve();
30     };
31   });
34 function transactionPromise(txn) {
35   return new Promise((resolve, reject) => {
36     txn.onabort = () => {
37       reject(txn.error);
38     };
39     txn.oncomplete = () => {
40       resolve();
41     };
42   });
45 test(t => {
46   assert_true('estimate' in navigator.storage);
47   assert_equals(typeof navigator.storage.estimate, 'function');
48   assert_true(navigator.storage.estimate() instanceof Promise);
49 }, 'estimate() method exists and returns a Promise');
51 promise_test(async t => {
52   const estimate = await navigator.storage.estimate();
53   assert_equals(typeof estimate, 'object');
54   assert_true('usage' in estimate);
55   assert_equals(typeof estimate.usage, 'number');
56   assert_true('quota' in estimate);
57   assert_equals(typeof estimate.quota, 'number');
58 }, 'estimate() resolves to dictionary with members');
60 promise_test(async t => {
61   const arraySize = 1e6;
62   const objectStoreName = "storageManager";
63   const dbname = this.window ? window.location.pathname :
64         "estimate-worker.https.html";
66   await indexedDbDeleteRequest(dbname);
67   let estimate = await navigator.storage.estimate();
69   const usageBeforeCreate = estimate.usage;
70   const db = await indexedDbOpenRequest(t, dbname, (db_to_upgrade) => {
71     db_to_upgrade.createObjectStore(objectStoreName);
72   });
74   estimate = await navigator.storage.estimate();
75   const usageAfterCreate = estimate.usage;
77   assert_greater_than(
78     usageAfterCreate, usageBeforeCreate,
79     'estimated usage should increase after object store is created');
81   const txn = db.transaction(objectStoreName, 'readwrite');
82   const buffer = new ArrayBuffer(arraySize);
83   const view = new Uint8Array(buffer);
85   for (let i = 0; i < arraySize; i++) {
86     view[i] = Math.floor(Math.random() * 255);
87   }
89   const testBlob = new Blob([buffer], {type: "binary/random"});
90   txn.objectStore(objectStoreName).add(testBlob, 1);
92   await transactionPromise(txn);
94   estimate = await navigator.storage.estimate();
95   const usageAfterPut = estimate.usage;
96   assert_greater_than(
97     usageAfterPut, usageAfterCreate,
98     'estimated usage should increase after large value is stored');
100   db.close();
101 }, 'estimate() shows usage increase after large value is stored');