Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
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 // names for cache devices
6 const kDiskDevice = "disk";
7 const kMemoryDevice = "memory";
8 const kOfflineDevice = "appcache";
10 const kCacheA = "http://cache/A";
11 const kCacheA2 = "http://cache/A2";
12 const kCacheB = "http://cache/B";
13 const kCacheC = "http://cache/C";
14 const kTestContent = "test content";
16 function make_input_stream_scriptable(input) {
17 var wrapper = Cc["@mozilla.org/scriptableinputstream;1"].
18 createInstance(Ci.nsIScriptableInputStream);
19 wrapper.init(input);
20 return wrapper;
21 }
23 const entries = [
24 // key content device should exist after leaving PB
25 [kCacheA, kTestContent, kMemoryDevice, true],
26 [kCacheA2, kTestContent, kDiskDevice, false],
27 [kCacheB, kTestContent, kDiskDevice, true],
28 [kCacheC, kTestContent, kOfflineDevice, true]
29 ]
31 var store_idx;
32 var store_cb = null;
33 var appCache = null;
35 function store_entries(cb)
36 {
37 if (cb) {
38 store_cb = cb;
39 store_idx = 0;
40 }
42 if (store_idx == entries.length) {
43 do_execute_soon(store_cb);
44 return;
45 }
47 asyncOpenCacheEntry(entries[store_idx][0],
48 entries[store_idx][2],
49 Ci.nsICacheStorage.OPEN_TRUNCATE,
50 LoadContextInfo.custom(!entries[store_idx][3]),
51 store_data,
52 appCache);
53 }
55 var store_data = function(status, entry) {
56 do_check_eq(status, Cr.NS_OK);
57 var os = entry.openOutputStream(0);
59 var written = os.write(entries[store_idx][1], entries[store_idx][1].length);
60 if (written != entries[store_idx][1].length) {
61 do_throw("os.write has not written all data!\n" +
62 " Expected: " + entries[store_idx][1].length + "\n" +
63 " Actual: " + written + "\n");
64 }
65 os.close();
66 entry.close();
67 store_idx++;
68 do_execute_soon(store_entries);
69 };
71 var check_idx;
72 var check_cb = null;
73 var check_pb_exited;
74 function check_entries(cb, pbExited)
75 {
76 if (cb) {
77 check_cb = cb;
78 check_idx = 0;
79 check_pb_exited = pbExited;
80 }
82 if (check_idx == entries.length) {
83 do_execute_soon(check_cb);
84 return;
85 }
87 asyncOpenCacheEntry(entries[check_idx][0],
88 entries[check_idx][2],
89 Ci.nsICacheStorage.OPEN_READONLY,
90 LoadContextInfo.custom(!entries[check_idx][3]),
91 check_data,
92 appCache);
93 }
95 var check_data = function (status, entry) {
96 var cont = function() {
97 check_idx++;
98 do_execute_soon(check_entries);
99 }
101 if (!check_pb_exited || entries[check_idx][3]) {
102 do_check_eq(status, Cr.NS_OK);
103 var is = entry.openInputStream(0);
104 pumpReadStream(is, function(read) {
105 entry.close();
106 do_check_eq(read, entries[check_idx][1]);
107 cont();
108 });
109 } else {
110 do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
111 cont();
112 }
113 };
115 function run_test() {
116 // Simulate a profile dir for xpcshell
117 do_get_profile();
119 appCache = Cc["@mozilla.org/network/application-cache-service;1"].
120 getService(Ci.nsIApplicationCacheService).
121 getApplicationCache("fake-client-id|fake-group-id");
123 // Start off with an empty cache
124 evict_cache_entries();
126 // Store cache-A, cache-A2, cache-B and cache-C
127 store_entries(run_test2);
129 do_test_pending();
130 }
132 function run_test2() {
133 // Check if cache-A, cache-A2, cache-B and cache-C are available
134 check_entries(run_test3, false);
135 }
137 function run_test3() {
138 // Simulate all private browsing instances being closed
139 var obsvc = Cc["@mozilla.org/observer-service;1"].
140 getService(Ci.nsIObserverService);
141 obsvc.notifyObservers(null, "last-pb-context-exited", null);
143 // Make sure the memory device is not empty
144 get_device_entry_count(kMemoryDevice, null, function(count) {
145 do_check_eq(count, 1);
146 // Check if cache-A is gone, and cache-B and cache-C are still available
147 check_entries(do_test_finished, true);
148 });
149 }