diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /dom/cache/test/mochitest/test_cache_shrink.html | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/cache/test/mochitest/test_cache_shrink.html')
-rw-r--r-- | dom/cache/test/mochitest/test_cache_shrink.html | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/dom/cache/test/mochitest/test_cache_shrink.html b/dom/cache/test/mochitest/test_cache_shrink.html new file mode 100644 index 0000000000..387d180fef --- /dev/null +++ b/dom/cache/test/mochitest/test_cache_shrink.html @@ -0,0 +1,139 @@ +<!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +<!DOCTYPE HTML> +<html> +<head> + <title>Test Cache with QuotaManager Restart</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="large_url_list.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<script class="testbody" type="text/javascript"> +function setupTestIframe() { + return new Promise(function(resolve) { + var iframe = document.createElement("iframe"); + iframe.src = "empty.html"; + iframe.onload = function() { + window.caches = iframe.contentWindow.caches; + resolve(); + }; + document.body.appendChild(iframe); + }); +} + +function clearStorage() { + return new Promise(function(resolve, reject) { + var qms = SpecialPowers.Services.qms; + var principal = SpecialPowers.wrap(document).nodePrincipal; + var request = qms.clearStoragesForPrincipal(principal); + var cb = SpecialPowers.wrapCallback(resolve); + request.callback = cb; + }); +} + +function storageUsage() { + return new Promise(function(resolve, reject) { + var qms = SpecialPowers.Services.qms; + var principal = SpecialPowers.wrap(document).nodePrincipal; + var cb = SpecialPowers.wrapCallback(function(request) { + var result = request.result; + resolve(result.usage); + }); + qms.getUsageForPrincipal(principal, cb); + }); +} + +function resetStorage() { + return new Promise(function(resolve, reject) { + var qms = SpecialPowers.Services.qms; + var request = qms.reset(); + var cb = SpecialPowers.wrapCallback(resolve); + request.callback = cb; + }); +} + +function gc() { + return new Promise(function(resolve, reject) { + SpecialPowers.exactGC(resolve); + }); +} + +SimpleTest.waitForExplicitFinish(); +SpecialPowers.pushPrefEnv({ + "set": [["dom.caches.enabled", true], + ["dom.caches.testing.enabled", true], + ["dom.quotaManager.testing", true], + ["privacy.partition.always_partition_third_party_non_cookie_storage", false]], +}, async function() { + // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5) + // Acquire storage access permission here so that the Cache API is avaialable + SpecialPowers.wrap(document).notifyUserGestureActivation(); + await SpecialPowers.addPermission("storageAccessAPI", true, window.location.href); + await SpecialPowers.wrap(document).requestStorageAccess(); + + var name = "foo"; + var cache = null; + var initialUsage = 0; + var fullUsage = 0; + var endUsage = 0; + // start from a fresh origin directory so other tests do not influence our + // results + setupTestIframe().then(function() { + return clearStorage(); + }).then(function() { + return storageUsage(); + }).then(function(usage) { + is(0, usage, "disk usage should be zero to start"); + return caches.open(name); + }).then(function(c) { + cache = c; + return storageUsage(); + }).then(function(usage) { + initialUsage = usage; + return Promise.all(largeUrlList.map(function(url) { + return cache.put(new Request(url), new Response()); + })); + }).then(function() { + return cache.keys(); + }).then(function(keyList) { + is(keyList.length, largeUrlList.length, "Large URL list is stored in cache"); + cache = null; + // Ensure the Cache DOM object is gone before proceeding. If its alive + // it will keep the related entries on-disk as well. + return gc(); + }).then(function() { + // reset the quota manager storage to ensure the DB connection is flushed + return resetStorage(); + }).then(function() { + return storageUsage(); + }).then(function(usage) { + fullUsage = usage; + ok(fullUsage > initialUsage, "disk usage should have grown"); + return caches.delete(name); + }).then(function(result) { + ok(result, "cache should be deleted"); + // This is a bit superfluous, but its necessary to make sure the Cache is + // fully deleted before we proceed. The deletion actually takes place in + // two async steps. We don't want to resetStorage() until the second step + // has taken place. This extra Cache operation ensure that all the + // runnables have been flushed through the threads, etc. + return caches.has(name); + }).then(function(result) { + ok(!result, "cache should not exist in storage"); + // reset the quota manager storage to ensure the DB connection is flushed + return resetStorage(); + }).then(function() { + return storageUsage(); + }).then(function(usage) { + endUsage = usage; + dump("### ### initial:" + initialUsage + ", full:" + fullUsage + + ", end:" + endUsage + "\n"); + ok(endUsage < (fullUsage / 2), "disk usage should have shrank significantly"); + ok(endUsage > initialUsage, "disk usage should not shrink back to orig size"); + SimpleTest.finish(); + }); +}); +</script> +</body> +</html> |