diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /netwerk/test/unit/head_cache.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | netwerk/test/unit/head_cache.js | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/netwerk/test/unit/head_cache.js b/netwerk/test/unit/head_cache.js new file mode 100644 index 0000000000..7ec0e11f97 --- /dev/null +++ b/netwerk/test/unit/head_cache.js @@ -0,0 +1,137 @@ +"use strict"; + +var { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +function evict_cache_entries(where) { + var clearDisk = !where || where == "disk" || where == "all"; + var clearMem = !where || where == "memory" || where == "all"; + + var storage; + + if (clearMem) { + storage = Services.cache2.memoryCacheStorage( + Services.loadContextInfo.default + ); + storage.asyncEvictStorage(null); + } + + if (clearDisk) { + storage = Services.cache2.diskCacheStorage( + Services.loadContextInfo.default + ); + storage.asyncEvictStorage(null); + } +} + +function createURI(urispec) { + return Services.io.newURI(urispec); +} + +function getCacheStorage(where, lci) { + if (!lci) { + lci = Services.loadContextInfo.default; + } + switch (where) { + case "disk": + return Services.cache2.diskCacheStorage(lci); + case "memory": + return Services.cache2.memoryCacheStorage(lci); + case "pin": + return Services.cache2.pinningCacheStorage(lci); + } + return null; +} + +function asyncOpenCacheEntry(key, where, flags, lci, callback) { + key = createURI(key); + + function CacheListener() {} + CacheListener.prototype = { + QueryInterface: ChromeUtils.generateQI(["nsICacheEntryOpenCallback"]), + + onCacheEntryCheck(entry) { + if (typeof callback === "object") { + return callback.onCacheEntryCheck(entry); + } + return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; + }, + + onCacheEntryAvailable(entry, isnew, status) { + if (typeof callback === "object") { + // Root us at the callback + callback.__cache_listener_root = this; + callback.onCacheEntryAvailable(entry, isnew, status); + } else { + callback(status, entry); + } + }, + + run() { + var storage = getCacheStorage(where, lci); + storage.asyncOpenURI(key, "", flags, this); + }, + }; + + new CacheListener().run(); +} + +function syncWithCacheIOThread(callback, force) { + if (force) { + asyncOpenCacheEntry( + "http://nonexistententry/", + "disk", + Ci.nsICacheStorage.OPEN_READONLY, + null, + function (status, entry) { + Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND); + callback(); + } + ); + } else { + callback(); + } +} + +function get_device_entry_count(where, lci, continuation) { + var storage = getCacheStorage(where, lci); + if (!storage) { + continuation(-1, 0); + return; + } + + var visitor = { + onCacheStorageInfo(entryCount, consumption) { + executeSoon(function () { + continuation(entryCount, consumption); + }); + }, + }; + + // get the device entry count + storage.asyncVisitStorage(visitor, false); +} + +function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation) { + asyncOpenCacheEntry( + key, + where, + Ci.nsICacheStorage.OPEN_READONLY, + null, + function (status, entry) { + if (shouldExist) { + dump("TEST-INFO | checking cache key " + key + " exists @ " + where); + Assert.equal(status, Cr.NS_OK); + Assert.ok(!!entry); + } else { + dump( + "TEST-INFO | checking cache key " + key + " doesn't exist @ " + where + ); + Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND); + Assert.equal(null, entry); + } + continuation(); + } + ); +} |