OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <title>StorageManager: estimate()</title> | |
3 <script src="../../resources/testharness.js"></script> | |
4 <script src="../../resources/testharnessreport.js"></script> | |
5 <script> | |
6 | |
7 test(function(t) { | |
8 assert_true('estimate' in window.navigator.storage); | |
9 assert_equals(typeof window.navigator.storage.estimate, 'function'); | |
10 assert_true(window.navigator.storage.estimate() instanceof Promise); | |
11 }, 'estimate() method exists and returns a Promise'); | |
12 | |
13 promise_test(function(t) { | |
14 return window.navigator.storage.estimate().then(function(result) { | |
15 assert_true(typeof result === 'object'); | |
16 assert_true('usage' in result); | |
17 assert_equals(typeof result.usage, 'number'); | |
18 assert_true('quota' in result); | |
19 assert_equals(typeof result.quota, 'number'); | |
20 }); | |
21 }, 'estimate() resolves to dictionary with members'); | |
22 | |
23 | |
24 // Simple Promise wrappers for IndexedDB to make async testing easier. | |
25 function deleteDB(name) { | |
26 return new Promise(function(resolve, reject) { | |
27 var del = indexedDB.deleteDatabase(name); | |
28 del.onerror = function() { reject(del.error); }; | |
29 del.onsuccess = function() { resolve(); }; | |
30 }); | |
31 } | |
32 function openDB(name, upgrade) { | |
33 return new Promise(function(resolve, reject) { | |
34 var open = indexedDB.open(name); | |
35 open.onerror = function() { reject(open.error); }; | |
36 open.onupgradeneeded = function() { upgrade(open.result); }; | |
37 open.onsuccess = function() { resolve(open.result); }; | |
38 }); | |
39 } | |
40 function transactionToPromise(tx) { | |
41 return new Promise(function(resolve, reject) { | |
42 tx.onabort = function() { reject(tx.error); }; | |
43 tx.oncomplete = function() { resolve(); }; | |
44 }); | |
45 } | |
46 | |
47 promise_test(function(t) { | |
48 var large_value = new Uint8Array(1e6); | |
49 var dbname = 'db' + window.location; | |
50 var db, before, after; | |
51 | |
52 return deleteDB(name) | |
53 .then(() => openDB(name, db => { db.createObjectStore('store'); })) | |
54 .then(connection => { | |
55 db = connection; | |
56 return navigator.storage.estimate(); | |
57 }) | |
58 .then(estimate => { | |
59 before = estimate.usage; | |
60 var tx = db.transaction('store', 'readwrite'); | |
61 tx.objectStore('store').put(large_value, 'k'); | |
62 return transactionToPromise(tx); | |
63 }) | |
64 .then(() => { | |
65 return navigator.storage.estimate(); | |
66 }) | |
67 .then(estimate => { | |
68 after = estimate.usage; | |
69 assert_greater_than(after, before, | |
70 'estimated usage should increase'); | |
71 // This assertion may not hold, e.g. if the implementation compresse
s | |
72 // the data or pre-allocate blocks larger than the value being store
d. | |
73 assert_greater_than_equal(after - before, large_value.length, | |
74 'increase should be at least as large as value stored'); | |
75 | |
76 db.close(); | |
77 return deleteDB(name); | |
78 }); | |
79 }, 'estimate() shows usage increase after large value is stored'); | |
80 | |
81 </script> | |
OLD | NEW |