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);
7 indexedDbDeleteRequest(dbname);
10 openRequest.onerror = () => {
11 reject(openRequest.error);
13 openRequest.onsuccess = () => {
14 resolve(openRequest.result);
16 openRequest.onupgradeneeded = event => {
17 upgrade_func(openRequest.result);
22 function indexedDbDeleteRequest(name) {
23 return new Promise((resolve, reject) => {
24 const deleteRequest = indexedDB.deleteDatabase(name);
25 deleteRequest.onerror = () => {
26 reject(deleteRequest.error);
28 deleteRequest.onsuccess = () => {
34 function transactionPromise(txn) {
35 return new Promise((resolve, reject) => {
39 txn.oncomplete = () => {
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);
74 estimate = await navigator.storage.estimate();
75 const usageAfterCreate = estimate.usage;
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);
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;
97 usageAfterPut, usageAfterCreate,
98 'estimated usage should increase after large value is stored');
101 }, 'estimate() shows usage increase after large value is stored');