test_storage_manager_estimate.js (2563B)
1 var testGenerator = testSteps(); 2 3 function* testSteps() { 4 const name = this.window 5 ? window.location.pathname 6 : "test_storage_manager_estimate.js"; 7 const objectStoreName = "storagesManager"; 8 const arraySize = 1e6; 9 10 ok("estimate" in navigator.storage, "Has estimate function"); 11 is(typeof navigator.storage.estimate, "function", "estimate is function"); 12 ok( 13 navigator.storage.estimate() instanceof Promise, 14 "estimate() method exists and returns a Promise" 15 ); 16 17 navigator.storage.estimate().then(estimation => { 18 testGenerator.next(estimation.usage); 19 }); 20 21 let before = yield undefined; 22 23 let request = indexedDB.open(name, 1); 24 request.onerror = errorHandler; 25 request.onupgradeneeded = grabEventAndContinueHandler; 26 request.onsuccess = continueToNextStep; 27 let event = yield undefined; 28 29 let db = event.target.result; 30 db.onerror = errorHandler; 31 32 let objectStore = db.createObjectStore(objectStoreName, {}); 33 yield undefined; 34 35 navigator.storage.estimate().then(estimation => { 36 testGenerator.next(estimation.usage); 37 }); 38 let usageAfterCreate = yield undefined; 39 ok( 40 usageAfterCreate > before, 41 "estimated usage must increase after createObjectStore" 42 ); 43 44 let txn = db.transaction(objectStoreName, "readwrite"); 45 objectStore = txn.objectStore(objectStoreName); 46 objectStore.put(new Uint8Array(arraySize), "k"); 47 txn.oncomplete = continueToNextStep; 48 txn.onabort = errorHandler; 49 txn.onerror = errorHandler; 50 event = yield undefined; 51 52 navigator.storage.estimate().then(estimation => { 53 testGenerator.next(estimation.usage); 54 }); 55 let usageAfterPut = yield undefined; 56 ok( 57 usageAfterPut > usageAfterCreate, 58 "estimated usage must increase after putting large object" 59 ); 60 db.close(); 61 62 finishTest(); 63 } 64 65 /* exported setup */ 66 async function setup(isXOrigin) { 67 // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5) 68 // Acquire storage access permission here so that the iframe has 69 // first-party access to the storage estimate. Without this, it is 70 // isolated and this test will always fail 71 if (isXOrigin) { 72 await SpecialPowers.pushPrefEnv({ 73 set: [ 74 [ 75 "privacy.partition.always_partition_third_party_non_cookie_storage", 76 false, 77 ], 78 ], 79 }); 80 SpecialPowers.wrap(document).notifyUserGestureActivation(); 81 await SpecialPowers.addPermission( 82 "storageAccessAPI", 83 true, 84 window.location.href 85 ); 86 await SpecialPowers.wrap(document).requestStorageAccess(); 87 } 88 runTest(); 89 }