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/. */
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";
17 // key content device should exist after leaving PB
18 [kCacheA
, kTestContent
, kMemoryDevice
, true],
19 [kCacheA2
, kTestContent
, kDiskDevice
, false],
20 [kCacheB
, kTestContent
, kDiskDevice
, true],
26 function store_entries(cb
) {
32 if (store_idx
== entries
.length
) {
33 executeSoon(store_cb
);
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,
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
) {
55 "os.write has not written all data!\n" +
57 entries
[store_idx
][1].length
+
66 executeSoon(store_entries
);
72 function check_entries(cb
, pbExited
) {
76 check_pb_exited
= pbExited
;
79 if (check_idx
== entries
.length
) {
80 executeSoon(check_cb
);
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,
95 var check_data = function (status
, entry
) {
96 var cont = function () {
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]);
109 Assert
.equal(status
, Cr
.NS_ERROR_CACHE_KEY_NOT_FOUND
);
114 function run_test() {
115 // Simulate a profile dir for xpcshell
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
);
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);