Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / head_cache.js
blob7ec0e11f97e68428c3561ec7eafaaa8e7dee28a4
1 "use strict";
3 var { XPCOMUtils } = ChromeUtils.importESModule(
4   "resource://gre/modules/XPCOMUtils.sys.mjs"
5 );
7 function evict_cache_entries(where) {
8   var clearDisk = !where || where == "disk" || where == "all";
9   var clearMem = !where || where == "memory" || where == "all";
11   var storage;
13   if (clearMem) {
14     storage = Services.cache2.memoryCacheStorage(
15       Services.loadContextInfo.default
16     );
17     storage.asyncEvictStorage(null);
18   }
20   if (clearDisk) {
21     storage = Services.cache2.diskCacheStorage(
22       Services.loadContextInfo.default
23     );
24     storage.asyncEvictStorage(null);
25   }
28 function createURI(urispec) {
29   return Services.io.newURI(urispec);
32 function getCacheStorage(where, lci) {
33   if (!lci) {
34     lci = Services.loadContextInfo.default;
35   }
36   switch (where) {
37     case "disk":
38       return Services.cache2.diskCacheStorage(lci);
39     case "memory":
40       return Services.cache2.memoryCacheStorage(lci);
41     case "pin":
42       return Services.cache2.pinningCacheStorage(lci);
43   }
44   return null;
47 function asyncOpenCacheEntry(key, where, flags, lci, callback) {
48   key = createURI(key);
50   function CacheListener() {}
51   CacheListener.prototype = {
52     QueryInterface: ChromeUtils.generateQI(["nsICacheEntryOpenCallback"]),
54     onCacheEntryCheck(entry) {
55       if (typeof callback === "object") {
56         return callback.onCacheEntryCheck(entry);
57       }
58       return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
59     },
61     onCacheEntryAvailable(entry, isnew, status) {
62       if (typeof callback === "object") {
63         // Root us at the callback
64         callback.__cache_listener_root = this;
65         callback.onCacheEntryAvailable(entry, isnew, status);
66       } else {
67         callback(status, entry);
68       }
69     },
71     run() {
72       var storage = getCacheStorage(where, lci);
73       storage.asyncOpenURI(key, "", flags, this);
74     },
75   };
77   new CacheListener().run();
80 function syncWithCacheIOThread(callback, force) {
81   if (force) {
82     asyncOpenCacheEntry(
83       "http://nonexistententry/",
84       "disk",
85       Ci.nsICacheStorage.OPEN_READONLY,
86       null,
87       function (status, entry) {
88         Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
89         callback();
90       }
91     );
92   } else {
93     callback();
94   }
97 function get_device_entry_count(where, lci, continuation) {
98   var storage = getCacheStorage(where, lci);
99   if (!storage) {
100     continuation(-1, 0);
101     return;
102   }
104   var visitor = {
105     onCacheStorageInfo(entryCount, consumption) {
106       executeSoon(function () {
107         continuation(entryCount, consumption);
108       });
109     },
110   };
112   // get the device entry count
113   storage.asyncVisitStorage(visitor, false);
116 function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation) {
117   asyncOpenCacheEntry(
118     key,
119     where,
120     Ci.nsICacheStorage.OPEN_READONLY,
121     null,
122     function (status, entry) {
123       if (shouldExist) {
124         dump("TEST-INFO | checking cache key " + key + " exists @ " + where);
125         Assert.equal(status, Cr.NS_OK);
126         Assert.ok(!!entry);
127       } else {
128         dump(
129           "TEST-INFO | checking cache key " + key + " doesn't exist @ " + where
130         );
131         Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
132         Assert.equal(null, entry);
133       }
134       continuation();
135     }
136   );