Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_bug248970_cache.js
blob68272e686c6113a73a2a5533db9b6622872cd00b
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 // names for cache devices
8 const kDiskDevice = "disk";
9 const kMemoryDevice = "memory";
11 const kCacheA = "http://cache/A";
12 const kCacheA2 = "http://cache/A2";
13 const kCacheB = "http://cache/B";
14 const kTestContent = "test content";
16 const entries = [
17 // key content device should exist after leaving PB
18 [kCacheA, kTestContent, kMemoryDevice, true],
19 [kCacheA2, kTestContent, kDiskDevice, false],
20 [kCacheB, kTestContent, kDiskDevice, true],
23 var store_idx;
24 var store_cb = null;
26 function store_entries(cb) {
27 if (cb) {
28 store_cb = cb;
29 store_idx = 0;
32 if (store_idx == entries.length) {
33 executeSoon(store_cb);
34 return;
37 asyncOpenCacheEntry(
38 entries[store_idx][0],
39 entries[store_idx][2],
40 Ci.nsICacheStorage.OPEN_TRUNCATE,
41 Services.loadContextInfo.custom(false, {
42 privateBrowsingId: entries[store_idx][3] ? 0 : 1,
43 }),
44 store_data
48 var store_data = function (status, entry) {
49 Assert.equal(status, Cr.NS_OK);
50 var os = entry.openOutputStream(0, entries[store_idx][1].length);
52 var written = os.write(entries[store_idx][1], entries[store_idx][1].length);
53 if (written != entries[store_idx][1].length) {
54 do_throw(
55 "os.write has not written all data!\n" +
56 " Expected: " +
57 entries[store_idx][1].length +
58 "\n" +
59 " Actual: " +
60 written +
61 "\n"
64 os.close();
65 store_idx++;
66 executeSoon(store_entries);
69 var check_idx;
70 var check_cb = null;
71 var check_pb_exited;
72 function check_entries(cb, pbExited) {
73 if (cb) {
74 check_cb = cb;
75 check_idx = 0;
76 check_pb_exited = pbExited;
79 if (check_idx == entries.length) {
80 executeSoon(check_cb);
81 return;
84 asyncOpenCacheEntry(
85 entries[check_idx][0],
86 entries[check_idx][2],
87 Ci.nsICacheStorage.OPEN_READONLY,
88 Services.loadContextInfo.custom(false, {
89 privateBrowsingId: entries[check_idx][3] ? 0 : 1,
90 }),
91 check_data
95 var check_data = function (status, entry) {
96 var cont = function () {
97 check_idx++;
98 executeSoon(check_entries);
101 if (!check_pb_exited || entries[check_idx][3]) {
102 Assert.equal(status, Cr.NS_OK);
103 var is = entry.openInputStream(0);
104 pumpReadStream(is, function (read) {
105 Assert.equal(read, entries[check_idx][1]);
106 cont();
108 } else {
109 Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
110 cont();
114 function run_test() {
115 // Simulate a profile dir for xpcshell
116 do_get_profile();
118 // Start off with an empty cache
119 evict_cache_entries();
121 // Store cache-A, cache-A2, cache-B and cache-C
122 store_entries(run_test2);
124 do_test_pending();
127 function run_test2() {
128 // Check if cache-A, cache-A2, cache-B and cache-C are available
129 check_entries(run_test3, false);
132 function run_test3() {
133 // Simulate all private browsing instances being closed
134 Services.obs.notifyObservers(null, "last-pb-context-exited");
136 // Make sure the memory device is not empty
137 get_device_entry_count(kMemoryDevice, null, function (count) {
138 Assert.equal(count, 1);
139 // Check if cache-A is gone, and cache-B and cache-C are still available
140 check_entries(do_test_finished, true);