netwerk/test/unit/test_bug248970_cache.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug248970_cache.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// names for cache devices
     1.9 +const kDiskDevice = "disk";
    1.10 +const kMemoryDevice = "memory";
    1.11 +const kOfflineDevice = "appcache";
    1.12 +
    1.13 +const kCacheA = "http://cache/A";
    1.14 +const kCacheA2 = "http://cache/A2";
    1.15 +const kCacheB = "http://cache/B";
    1.16 +const kCacheC = "http://cache/C";
    1.17 +const kTestContent = "test content";
    1.18 +
    1.19 +function make_input_stream_scriptable(input) {
    1.20 +  var wrapper = Cc["@mozilla.org/scriptableinputstream;1"].
    1.21 +                createInstance(Ci.nsIScriptableInputStream);
    1.22 +  wrapper.init(input);
    1.23 +  return wrapper;
    1.24 +}
    1.25 +
    1.26 +const entries = [
    1.27 +// key       content       device          should exist after leaving PB
    1.28 +  [kCacheA,  kTestContent, kMemoryDevice,  true],
    1.29 +  [kCacheA2, kTestContent, kDiskDevice,    false],
    1.30 +  [kCacheB,  kTestContent, kDiskDevice,    true],
    1.31 +  [kCacheC,  kTestContent, kOfflineDevice, true]
    1.32 +]
    1.33 +
    1.34 +var store_idx;
    1.35 +var store_cb = null;
    1.36 +var appCache = null;
    1.37 +
    1.38 +function store_entries(cb)
    1.39 +{
    1.40 +  if (cb) {
    1.41 +    store_cb = cb;
    1.42 +    store_idx = 0;
    1.43 +  }
    1.44 +
    1.45 +  if (store_idx == entries.length) {
    1.46 +    do_execute_soon(store_cb);
    1.47 +    return;
    1.48 +  }
    1.49 +
    1.50 +  asyncOpenCacheEntry(entries[store_idx][0],
    1.51 +                      entries[store_idx][2],
    1.52 +                      Ci.nsICacheStorage.OPEN_TRUNCATE,
    1.53 +                      LoadContextInfo.custom(!entries[store_idx][3]),
    1.54 +                      store_data,
    1.55 +                      appCache);
    1.56 +}
    1.57 +
    1.58 +var store_data = function(status, entry) {
    1.59 +  do_check_eq(status, Cr.NS_OK);
    1.60 +  var os = entry.openOutputStream(0);
    1.61 +
    1.62 +  var written = os.write(entries[store_idx][1], entries[store_idx][1].length);
    1.63 +  if (written != entries[store_idx][1].length) {
    1.64 +    do_throw("os.write has not written all data!\n" +
    1.65 +             "  Expected: " + entries[store_idx][1].length  + "\n" +
    1.66 +             "  Actual: " + written + "\n");
    1.67 +  }
    1.68 +  os.close();
    1.69 +  entry.close();
    1.70 +  store_idx++;
    1.71 +  do_execute_soon(store_entries);
    1.72 +};
    1.73 +
    1.74 +var check_idx;
    1.75 +var check_cb = null;
    1.76 +var check_pb_exited;
    1.77 +function check_entries(cb, pbExited)
    1.78 +{
    1.79 +  if (cb) {
    1.80 +    check_cb = cb;
    1.81 +    check_idx = 0;
    1.82 +    check_pb_exited = pbExited;
    1.83 +  }
    1.84 +
    1.85 +  if (check_idx == entries.length) {
    1.86 +    do_execute_soon(check_cb);
    1.87 +    return;
    1.88 +  }
    1.89 +
    1.90 +  asyncOpenCacheEntry(entries[check_idx][0],
    1.91 +                      entries[check_idx][2],
    1.92 +                      Ci.nsICacheStorage.OPEN_READONLY,
    1.93 +                      LoadContextInfo.custom(!entries[check_idx][3]),
    1.94 +                      check_data,
    1.95 +                      appCache);
    1.96 +}
    1.97 +
    1.98 +var check_data = function (status, entry) {
    1.99 +  var cont = function() {
   1.100 +    check_idx++;
   1.101 +    do_execute_soon(check_entries);
   1.102 +  }
   1.103 +
   1.104 +  if (!check_pb_exited || entries[check_idx][3]) {
   1.105 +    do_check_eq(status, Cr.NS_OK);
   1.106 +    var is = entry.openInputStream(0);
   1.107 +    pumpReadStream(is, function(read) {
   1.108 +      entry.close();
   1.109 +      do_check_eq(read, entries[check_idx][1]);
   1.110 +      cont();
   1.111 +    });
   1.112 +  } else {
   1.113 +    do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
   1.114 +    cont();
   1.115 +  }
   1.116 +};
   1.117 +
   1.118 +function run_test() {
   1.119 +  // Simulate a profile dir for xpcshell
   1.120 +  do_get_profile();
   1.121 +
   1.122 +  appCache = Cc["@mozilla.org/network/application-cache-service;1"].
   1.123 +             getService(Ci.nsIApplicationCacheService).
   1.124 +             getApplicationCache("fake-client-id|fake-group-id");
   1.125 +
   1.126 +  // Start off with an empty cache
   1.127 +  evict_cache_entries();
   1.128 +
   1.129 +  // Store cache-A, cache-A2, cache-B and cache-C
   1.130 +  store_entries(run_test2);
   1.131 +
   1.132 +  do_test_pending();
   1.133 +}
   1.134 +
   1.135 +function run_test2() {
   1.136 +  // Check if cache-A, cache-A2, cache-B and cache-C are available
   1.137 +  check_entries(run_test3, false);
   1.138 +}
   1.139 +
   1.140 +function run_test3() {
   1.141 +  // Simulate all private browsing instances being closed
   1.142 +  var obsvc = Cc["@mozilla.org/observer-service;1"].
   1.143 +    getService(Ci.nsIObserverService);
   1.144 +  obsvc.notifyObservers(null, "last-pb-context-exited", null);
   1.145 +
   1.146 +  // Make sure the memory device is not empty
   1.147 +  get_device_entry_count(kMemoryDevice, null, function(count) {
   1.148 +    do_check_eq(count, 1);
   1.149 +    // Check if cache-A is gone, and cache-B and cache-C are still available
   1.150 +    check_entries(do_test_finished, true);
   1.151 +  });
   1.152 +}

mercurial