Backed out 6 changesets (bug 1835907) for causing multiple failures. CLOSED TREE
[gecko.git] / dom / cache / test / mochitest / test_cache_shrink.html
blob65878f743841948c5bb4d63396982aad61bac1b0
1 <!-- Any copyright is dedicated to the Public Domain.
2 - http://creativecommons.org/publicdomain/zero/1.0/ -->
3 <!DOCTYPE HTML>
4 <html>
5 <head>
6 <title>Test Cache with QuotaManager Restart</title>
7 <script src="/tests/SimpleTest/SimpleTest.js"></script>
8 <script type="text/javascript" src="large_url_list.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body>
12 <script class="testbody" type="text/javascript">
13 function setupTestIframe() {
14 return new Promise(function(resolve) {
15 var iframe = document.createElement("iframe");
16 iframe.src = "empty.html";
17 iframe.onload = function() {
18 window.caches = iframe.contentWindow.caches;
19 resolve();
21 document.body.appendChild(iframe);
22 });
25 function clearStorage() {
26 return new Promise(function(resolve, reject) {
27 var qms = SpecialPowers.Services.qms;
28 var principal = SpecialPowers.wrap(document).nodePrincipal;
29 var request = qms.clearStoragesForPrincipal(principal);
30 var cb = SpecialPowers.wrapCallback(resolve);
31 request.callback = cb;
32 });
35 function storageUsage() {
36 return new Promise(function(resolve, reject) {
37 var qms = SpecialPowers.Services.qms;
38 var principal = SpecialPowers.wrap(document).nodePrincipal;
39 var cb = SpecialPowers.wrapCallback(function(request) {
40 var result = request.result;
41 resolve(result.usage);
42 });
43 qms.getUsageForPrincipal(principal, cb);
44 });
47 function resetStorage() {
48 return new Promise(function(resolve, reject) {
49 var qms = SpecialPowers.Services.qms;
50 var request = qms.reset();
51 var cb = SpecialPowers.wrapCallback(resolve);
52 request.callback = cb;
53 });
56 function gc() {
57 return new Promise(function(resolve, reject) {
58 SpecialPowers.exactGC(resolve);
59 });
62 SimpleTest.waitForExplicitFinish();
63 SpecialPowers.pushPrefEnv({
64 "set": [["dom.caches.testing.enabled", true],
65 ["dom.quotaManager.testing", true],
66 ["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
67 }, async function() {
68 // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
69 // Acquire storage access permission here so that the Cache API is avaialable
70 SpecialPowers.wrap(document).notifyUserGestureActivation();
71 await SpecialPowers.addPermission("storageAccessAPI", true, window.location.href);
72 await SpecialPowers.wrap(document).requestStorageAccess();
74 var name = "foo";
75 var cache = null;
76 var initialUsage = 0;
77 var fullUsage = 0;
78 var endUsage = 0;
79 // start from a fresh origin directory so other tests do not influence our
80 // results
81 setupTestIframe().then(function() {
82 return clearStorage();
83 }).then(function() {
84 return storageUsage();
85 }).then(function(usage) {
86 is(0, usage, "disk usage should be zero to start");
87 return caches.open(name);
88 }).then(function(c) {
89 cache = c;
90 return storageUsage();
91 }).then(function(usage) {
92 initialUsage = usage;
93 return Promise.all(largeUrlList.map(function(url) {
94 return cache.put(new Request(url), new Response());
95 }));
96 }).then(function() {
97 return cache.keys();
98 }).then(function(keyList) {
99 is(keyList.length, largeUrlList.length, "Large URL list is stored in cache");
100 cache = null;
101 // Ensure the Cache DOM object is gone before proceeding. If its alive
102 // it will keep the related entries on-disk as well.
103 return gc();
104 }).then(function() {
105 // reset the quota manager storage to ensure the DB connection is flushed
106 return resetStorage();
107 }).then(function() {
108 return storageUsage();
109 }).then(function(usage) {
110 fullUsage = usage;
111 ok(fullUsage > initialUsage, "disk usage should have grown");
112 return caches.delete(name);
113 }).then(function(result) {
114 ok(result, "cache should be deleted");
115 // This is a bit superfluous, but its necessary to make sure the Cache is
116 // fully deleted before we proceed. The deletion actually takes place in
117 // two async steps. We don't want to resetStorage() until the second step
118 // has taken place. This extra Cache operation ensure that all the
119 // runnables have been flushed through the threads, etc.
120 return caches.has(name);
121 }).then(function(result) {
122 ok(!result, "cache should not exist in storage");
123 // reset the quota manager storage to ensure the DB connection is flushed
124 return resetStorage();
125 }).then(function() {
126 return storageUsage();
127 }).then(function(usage) {
128 endUsage = usage;
129 dump("### ### initial:" + initialUsage + ", full:" + fullUsage +
130 ", end:" + endUsage + "\n");
131 ok(endUsage < (fullUsage / 2), "disk usage should have shrank significantly");
132 ok(endUsage > initialUsage, "disk usage should not shrink back to orig size");
133 SimpleTest.finish();
136 </script>
137 </body>
138 </html>