netwerk/test/unit/head_cache.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/head_cache.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
     1.5 +Components.utils.import('resource://gre/modules/LoadContextInfo.jsm');
     1.6 +
     1.7 +var _CSvc;
     1.8 +function get_cache_service() {
     1.9 +  if (_CSvc)
    1.10 +    return _CSvc;
    1.11 +
    1.12 +  return _CSvc = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
    1.13 +                           .getService(Components.interfaces.nsICacheStorageService);
    1.14 +}
    1.15 +
    1.16 +function evict_cache_entries(where)
    1.17 +{
    1.18 +  var clearDisk = (!where || where == "disk" || where == "all");
    1.19 +  var clearMem = (!where || where == "memory" || where == "all");
    1.20 +  var clearAppCache = (where == "appcache");
    1.21 +
    1.22 +  var svc = get_cache_service();
    1.23 +  var storage;
    1.24 +
    1.25 +  if (clearMem) {
    1.26 +    storage = svc.memoryCacheStorage(LoadContextInfo.default);
    1.27 +    storage.asyncEvictStorage(null);
    1.28 +  }
    1.29 +
    1.30 +  if (clearDisk) {
    1.31 +    storage = svc.diskCacheStorage(LoadContextInfo.default, false);
    1.32 +    storage.asyncEvictStorage(null);
    1.33 +  }
    1.34 +
    1.35 +  if (clearAppCache) {
    1.36 +    storage = svc.appCacheStorage(LoadContextInfo.default, null);
    1.37 +    storage.asyncEvictStorage(null);
    1.38 +  }
    1.39 +}
    1.40 +
    1.41 +function createURI(urispec)
    1.42 +{
    1.43 +  var ioServ = Components.classes["@mozilla.org/network/io-service;1"]
    1.44 +                         .getService(Components.interfaces.nsIIOService);
    1.45 +  return ioServ.newURI(urispec, null, null);
    1.46 +}
    1.47 +
    1.48 +function getCacheStorage(where, lci, appcache)
    1.49 +{
    1.50 +  if (!lci) lci = LoadContextInfo.default;
    1.51 +  var svc = get_cache_service();
    1.52 +  switch (where) {
    1.53 +    case "disk": return svc.diskCacheStorage(lci, false);
    1.54 +    case "memory": return svc.memoryCacheStorage(lci);
    1.55 +    case "appcache": return svc.appCacheStorage(lci, appcache);
    1.56 +  }
    1.57 +  return null;
    1.58 +}
    1.59 +
    1.60 +function asyncOpenCacheEntry(key, where, flags, lci, callback, appcache)
    1.61 +{
    1.62 +  key = createURI(key);
    1.63 +
    1.64 +  function CacheListener() { }
    1.65 +  CacheListener.prototype = {
    1.66 +    _appCache: appcache,
    1.67 +
    1.68 +    QueryInterface: function (iid) {
    1.69 +      if (iid.equals(Components.interfaces.nsICacheEntryOpenCallback) ||
    1.70 +          iid.equals(Components.interfaces.nsISupports))
    1.71 +        return this;
    1.72 +      throw Components.results.NS_ERROR_NO_INTERFACE;
    1.73 +    },
    1.74 +
    1.75 +    onCacheEntryCheck: function(entry, appCache) {
    1.76 +      if (typeof callback === "object")
    1.77 +        return callback.onCacheEntryCheck(entry, appCache);
    1.78 +      return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
    1.79 +    },
    1.80 +
    1.81 +    onCacheEntryAvailable: function (entry, isnew, appCache, status) {
    1.82 +      if (typeof callback === "object") {
    1.83 +        // Root us at the callback
    1.84 +        callback.__cache_listener_root = this;
    1.85 +        callback.onCacheEntryAvailable(entry, isnew, appCache, status);
    1.86 +      }
    1.87 +      else
    1.88 +        callback(status, entry, appCache);
    1.89 +    },
    1.90 +
    1.91 +    run: function () {
    1.92 +      var storage = getCacheStorage(where, lci, this._appCache);
    1.93 +      storage.asyncOpenURI(key, "", flags, this);
    1.94 +    }
    1.95 +  };
    1.96 +
    1.97 +  (new CacheListener()).run();
    1.98 +}
    1.99 +
   1.100 +function syncWithCacheIOThread(callback)
   1.101 +{
   1.102 +  if (!newCacheBackEndUsed()) {
   1.103 +    asyncOpenCacheEntry(
   1.104 +      "http://nonexistententry/", "disk", Ci.nsICacheStorage.OPEN_READONLY, null,
   1.105 +      function(status, entry) {
   1.106 +        do_check_eq(status, Components.results.NS_ERROR_CACHE_KEY_NOT_FOUND);
   1.107 +        callback();
   1.108 +      });
   1.109 +  }
   1.110 +  else {
   1.111 +    callback();
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +function get_device_entry_count(where, lci, continuation) {
   1.116 +  var storage = getCacheStorage(where, lci);
   1.117 +  if (!storage) {
   1.118 +    continuation(-1, 0);
   1.119 +    return;
   1.120 +  }
   1.121 +
   1.122 +  var visitor = {
   1.123 +    onCacheStorageInfo: function (entryCount, consumption) {
   1.124 +      do_execute_soon(function() {
   1.125 +        continuation(entryCount, consumption);
   1.126 +      });
   1.127 +    },
   1.128 +  };
   1.129 +
   1.130 +  // get the device entry count
   1.131 +  storage.asyncVisitStorage(visitor, false);
   1.132 +}
   1.133 +
   1.134 +function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation, appCache)
   1.135 +{
   1.136 +  asyncOpenCacheEntry(key, where, Ci.nsICacheStorage.OPEN_READONLY, null,
   1.137 +    function(status, entry) {
   1.138 +      if (shouldExist) {
   1.139 +        dump("TEST-INFO | checking cache key " + key + " exists @ " + where);
   1.140 +        do_check_eq(status, Cr.NS_OK);
   1.141 +        do_check_true(!!entry);
   1.142 +      } else {
   1.143 +        dump("TEST-INFO | checking cache key " + key + " doesn't exist @ " + where);
   1.144 +        do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
   1.145 +        do_check_null(entry);
   1.146 +      }
   1.147 +      continuation();
   1.148 +    }, appCache);
   1.149 +}

mercurial