storage_bucket_object.tentative.https.any.js (5300B)
1 // META: title=Buckets API: Tests for the StorageBucket object. 2 // META: global=window,worker 3 4 'use strict'; 5 6 promise_test(async testCase => { 7 const bucket = await navigator.storageBuckets.open('bucket_name'); 8 testCase.add_cleanup(async () => { 9 await navigator.storageBuckets.delete('bucket_name'); 10 }); 11 const persisted = await bucket.persisted(); 12 assert_false(persisted); 13 14 // Also verify that the promise is rejected after the bucket is deleted. 15 await navigator.storageBuckets.delete('bucket_name'); 16 await promise_rejects_dom(testCase, 'UnknownError', bucket.persisted()); 17 }, 'persisted() should default to false'); 18 19 promise_test(async testCase => { 20 const bucket = await navigator.storageBuckets.open('bucket_name'); 21 testCase.add_cleanup(async () => { 22 await navigator.storageBuckets.delete('bucket_name'); 23 }); 24 const estimate = await bucket.estimate(); 25 assert_greater_than(estimate.quota, 0); 26 assert_equals(estimate.usage, 0); 27 28 const cacheName = 'attachments'; 29 const cacheKey = 'receipt1.txt'; 30 var inboxCache = await bucket.caches.open(cacheName); 31 await inboxCache.put(cacheKey, new Response('bread x 2')) 32 33 const estimate2 = await bucket.estimate(); 34 assert_greater_than(estimate2.quota, 0); 35 assert_less_than(estimate.usage, estimate2.usage); 36 }, 'estimate() should retrieve quota usage'); 37 38 promise_test(async testCase => { 39 const bucket = await navigator.storageBuckets.open( 40 'bucket_name', { durability: 'strict' }); 41 testCase.add_cleanup(async () => { 42 await navigator.storageBuckets.delete('bucket_name'); 43 }); 44 45 const durability = await bucket.durability(); 46 assert_equals('strict', durability); 47 48 await navigator.storageBuckets.delete('bucket_name'); 49 await promise_rejects_dom(testCase, 'UnknownError', bucket.durability()); 50 }, 'durability() should retrieve bucket durability specified during creation'); 51 52 promise_test(async testCase => { 53 const bucket = await navigator.storageBuckets.open('bucket_name'); 54 testCase.add_cleanup(async () => { 55 await navigator.storageBuckets.delete('bucket_name'); 56 }); 57 58 const durability = await bucket.durability(); 59 assert_equals('relaxed', durability); 60 }, 'Bucket durability defaults to relaxed'); 61 62 promise_test(async testCase => { 63 const oneYear = 365 * 24 * 60 * 60 * 1000; 64 const expiresDate = Date.now() + oneYear; 65 const bucket = await navigator.storageBuckets.open( 66 'bucket_name', { expires: expiresDate }); 67 testCase.add_cleanup(async () => { 68 await navigator.storageBuckets.delete('bucket_name'); 69 }); 70 71 const expires = await bucket.expires(); 72 assert_equals(expires, expiresDate); 73 }, 'expires() should retrieve expires date'); 74 75 promise_test(async testCase => { 76 const bucket = await navigator.storageBuckets.open('bucket_name'); 77 testCase.add_cleanup(async () => { 78 await navigator.storageBuckets.delete('bucket_name'); 79 }); 80 81 const expires = await bucket.expires(); 82 assert_equals(expires, null); 83 84 await navigator.storageBuckets.delete('bucket_name'); 85 await promise_rejects_dom(testCase, 'UnknownError', bucket.expires()); 86 }, 'expires() should be defaulted to null'); 87 88 promise_test(async testCase => { 89 const bucket = await navigator.storageBuckets.open('bucket_name'); 90 testCase.add_cleanup(async () => { 91 await navigator.storageBuckets.delete('bucket_name'); 92 }); 93 94 const oneYear = 365 * 24 * 60 * 60 * 1000; 95 const expiresDate = Date.now() + oneYear; 96 await bucket.setExpires(expiresDate); 97 98 const expires = await bucket.expires(); 99 assert_equals(expires, expiresDate); 100 101 await navigator.storageBuckets.delete('bucket_name'); 102 await promise_rejects_dom(testCase, 'UnknownError', bucket.setExpires(expiresDate)); 103 }, 'setExpires() should set bucket expires date'); 104 105 promise_test(async testCase => { 106 const oneDay = 24 * 60 * 60 * 1000; 107 const expiresDate = Date.now() + oneDay; 108 const bucket = await navigator.storageBuckets.open('bucket_name', { 109 expires: expiresDate 110 }); 111 testCase.add_cleanup(async () => { 112 await navigator.storageBuckets.delete('bucket_name'); 113 }); 114 let expires = await bucket.expires(); 115 assert_equals(expires, expiresDate); 116 117 const oneYear = 365 * oneDay; 118 const newExpiresDate = Date.now() + oneYear; 119 await bucket.setExpires(newExpiresDate); 120 121 expires = await bucket.expires(); 122 assert_equals(expires, newExpiresDate); 123 }, 'setExpires() should update expires date'); 124 125 promise_test(async testCase => { 126 const bucket = await navigator.storageBuckets.open( 127 'bucket_name', { durability: 'strict' }); 128 testCase.add_cleanup(async () => { 129 await navigator.storageBuckets.delete('bucket_name'); 130 }); 131 132 const same_bucket = await navigator.storageBuckets.open('bucket_name'); 133 const durability = await bucket.durability(); 134 const other_durability = await same_bucket.durability(); 135 assert_equals(durability, other_durability); 136 137 // Delete the bucket and remake it. 138 await navigator.storageBuckets.delete('bucket_name'); 139 const remade_bucket = await navigator.storageBuckets.open('bucket_name'); 140 await promise_rejects_dom(testCase, 'UnknownError', bucket.durability()); 141 const remade_durability = await remade_bucket.durability(); 142 assert_not_equals(remade_durability, durability); 143 }, 'two handles can refer to the same bucket, and a bucket name can be reused after deletion');