1 <!-- Any copyright is dedicated to the Public Domain.
2 - http://creativecommons.org/publicdomain/zero/1.0/ -->
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" />
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;
21 document.body.appendChild(iframe);
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;
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);
43 qms.getUsageForPrincipal(principal, cb);
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;
57 return new Promise(function(resolve, reject) {
58 SpecialPowers.exactGC(resolve);
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]],
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();
79 // start from a fresh origin directory so other tests do not influence our
81 setupTestIframe().then(function() {
82 return clearStorage();
84 return storageUsage();
85 }).then(function(usage) {
86 is(
0, usage,
"disk usage should be zero to start");
87 return caches.open(name);
90 return storageUsage();
91 }).then(function(usage) {
93 return Promise.all(largeUrlList.map(function(url) {
94 return cache.put(new Request(url), new Response());
98 }).then(function(keyList) {
99 is(keyList.length, largeUrlList.length,
"Large URL list is stored in cache");
101 // Ensure the Cache DOM object is gone before proceeding. If its alive
102 // it will keep the related entries on-disk as well.
105 // reset the quota manager storage to ensure the DB connection is flushed
106 return resetStorage();
108 return storageUsage();
109 }).then(function(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();
126 return storageUsage();
127 }).then(function(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");