tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_cache_shrink.html (4501B)


      1 <!-- Any copyright is dedicated to the Public Domain.
      2   - http://creativecommons.org/publicdomain/zero/1.0/ -->
      3 <!DOCTYPE HTML>
      4 <html>
      5 <head>
      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" />
     10 </head>
     11 <body>
     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;
     19      resolve();
     20    };
     21    document.body.appendChild(iframe);
     22  });
     23 }
     24 
     25 function clearStorage() {
     26  return new Promise(function(resolve) {
     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;
     32  });
     33 }
     34 
     35 function storageUsage() {
     36  return new Promise(function(resolve) {
     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);
     42    });
     43    qms.getUsageForPrincipal(principal, cb);
     44  });
     45 }
     46 
     47 function resetStorage() {
     48  return new Promise(function(resolve) {
     49    var qms = SpecialPowers.Services.qms;
     50    var principal = SpecialPowers.wrap(document).nodePrincipal;
     51    var request = qms.resetStoragesForPrincipal(principal);
     52    var cb = SpecialPowers.wrapCallback(resolve);
     53    request.callback = cb;
     54  });
     55 }
     56 
     57 function gc() {
     58  return new Promise(function(resolve) {
     59    SpecialPowers.exactGC(resolve);
     60  });
     61 }
     62 
     63 SimpleTest.waitForExplicitFinish();
     64 SpecialPowers.pushPrefEnv({
     65  "set": [["dom.caches.enabled", true],
     66          ["dom.caches.testing.enabled", true],
     67          ["dom.quotaManager.testing", true],],
     68 },  function() {
     69  var name = "foo";
     70  var cache = null;
     71  var initialUsage = 0;
     72  var fullUsage = 0;
     73  var endUsage = 0;
     74  // start from a fresh origin directory so other tests do not influence our
     75  // results
     76  setupTestIframe().then(function() {
     77    return clearStorage();
     78  }).then(function() {
     79    return storageUsage();
     80  }).then(function(usage) {
     81    is(0, usage, "disk usage should be zero to start");
     82    return caches.open(name);
     83  }).then(function(c) {
     84    cache = c;
     85    return storageUsage();
     86  }).then(function(usage) {
     87    initialUsage = usage;
     88    return Promise.all(largeUrlList.map(function(url) {
     89      return cache.put(new Request(url), new Response());
     90    }));
     91  }).then(function() {
     92    return cache.keys();
     93  }).then(function(keyList) {
     94    is(keyList.length, largeUrlList.length, "Large URL list is stored in cache");
     95    cache = null;
     96    // Ensure the Cache DOM object is gone before proceeding.  If its alive
     97    // it will keep the related entries on-disk as well.
     98    return gc();
     99  }).then(function() {
    100    // reset the quota manager storage to ensure the DB connection is flushed
    101    return resetStorage();
    102  }).then(function() {
    103    return storageUsage();
    104  }).then(function(usage) {
    105    fullUsage = usage;
    106    ok(fullUsage > initialUsage, "disk usage should have grown");
    107    return caches.delete(name);
    108  }).then(function(result) {
    109    ok(result, "cache should be deleted");
    110    // This is a bit superfluous, but its necessary to make sure the Cache is
    111    // fully deleted before we proceed.  The deletion actually takes place in
    112    // two async steps.  We don't want to resetStorage() until the second step
    113    // has taken place.  This extra Cache operation ensure that all the
    114    // runnables have been flushed through the threads, etc.
    115    return caches.has(name);
    116  }).then(function(result) {
    117    ok(!result, "cache should not exist in storage");
    118    // reset the quota manager storage to ensure the DB connection is flushed
    119    return resetStorage();
    120  }).then(function() {
    121    return storageUsage();
    122  }).then(function(usage) {
    123    endUsage = usage;
    124    dump("### ### initial:" + initialUsage + ", full:" + fullUsage +
    125         ", end:" + endUsage + "\n");
    126    ok(endUsage < (fullUsage / 2), "disk usage should have shrank significantly");
    127    ok(endUsage > initialUsage, "disk usage should not shrink back to orig size");
    128    SimpleTest.finish();
    129  });
    130 });
    131 </script>
    132 </body>
    133 </html>