1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* Test to ensure that image loading/decoding notifications are always
* delivered async, and in the order we expect.
*
* Must be included from a file that has a uri of the image to test defined in
* var uri.
*/
/* import-globals-from image_load_helpers.js */
const { HttpServer } = ChromeUtils.importESModule(
"resource://testing-common/httpd.sys.mjs"
);
const { NetUtil } = ChromeUtils.importESModule(
"resource://gre/modules/NetUtil.sys.mjs"
);
const ReferrerInfo = Components.Constructor(
"@mozilla.org/referrer-info;1",
"nsIReferrerInfo",
"init"
);
var server = new HttpServer();
server.registerDirectory("/", do_get_file(""));
server.registerContentType("sjs", "sjs");
server.start(-1);
load("image_load_helpers.js");
var requests = [];
/* global uri */
// Return a closure that holds on to the listener from the original
// imgIRequest, and compares its results to the cloned one.
function getCloneStopCallback(original_listener) {
return function cloneStop(listener) {
Assert.equal(original_listener.state, listener.state);
// Sanity check to make sure we didn't accidentally use the same listener
// twice.
Assert.notEqual(original_listener, listener);
do_test_finished();
};
}
// Make sure that cloned requests get all the same callbacks as the original,
// but they aren't synchronous right now.
function checkClone(other_listener, aRequest) {
do_test_pending();
// For as long as clone notification is synchronous, we can't test the clone state reliably.
var listener = new ImageListener(
null,
function (foo, bar) {
do_test_finished();
} /* getCloneStopCallback(other_listener)*/
);
listener.synchronous = false;
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var clone = aRequest.clone(outer);
requests.push({ request: clone, locked: false });
}
// Ensure that all the callbacks were called on aRequest.
function checkSizeAndLoad(listener, aRequest) {
Assert.notEqual(listener.state & SIZE_AVAILABLE, 0);
Assert.notEqual(listener.state & LOAD_COMPLETE, 0);
do_test_finished();
}
function secondLoadDone(oldlistener, aRequest) {
do_test_pending();
try {
var staticrequest = aRequest.getStaticRequest();
// For as long as clone notification is synchronous, we can't test the
// clone state reliably.
var listener = new ImageListener(null, checkSizeAndLoad);
listener.synchronous = false;
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var staticrequestclone = staticrequest.clone(outer);
requests.push({ request: staticrequestclone, locked: false });
} catch (e) {
// We can't create a static request. Most likely the request we started
// with didn't load successfully.
do_test_finished();
}
run_loadImageWithChannel_tests();
do_test_finished();
}
// Load the request a second time. This should come from the image cache, and
// therefore would be at most risk of being served synchronously.
function checkSecondLoad() {
do_test_pending();
var listener = new ImageListener(checkClone, secondLoadDone);
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var referrerInfo = new ReferrerInfo(
Ci.nsIReferrerInfo.NO_REFERRER_WHEN_DOWNGRADE,
true,
null
);
requests.push({
request: gCurrentLoader.loadImageXPCOM(
uri,
null,
referrerInfo,
null,
null,
outer,
null,
0,
null
),
locked: false,
});
listener.synchronous = false;
}
function firstLoadDone(oldlistener, aRequest) {
checkSecondLoad(uri);
do_test_finished();
}
// Return a closure that allows us to check the stream listener's status when the
// image finishes loading.
function getChannelLoadImageStopCallback(streamlistener, next) {
return function channelLoadStop(imglistener, aRequest) {
next();
do_test_finished();
};
}
// Load the request a second time. This should come from the image cache, and
// therefore would be at most risk of being served synchronously.
function checkSecondChannelLoad() {
do_test_pending();
var channel = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true });
var channellistener = new ChannelListener();
channel.asyncOpen(channellistener);
var listener = new ImageListener(
null,
getChannelLoadImageStopCallback(channellistener, all_done_callback)
);
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var outlistener = {};
requests.push({
request: gCurrentLoader.loadImageWithChannelXPCOM(
channel,
outer,
null,
outlistener
),
locked: false,
});
channellistener.outputListener = outlistener.value;
listener.synchronous = false;
}
function run_loadImageWithChannel_tests() {
// To ensure we're testing what we expect to, create a new loader and cache.
gCurrentLoader = Cc["@mozilla.org/image/loader;1"].createInstance(
Ci.imgILoader
);
do_test_pending();
var channel = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true });
var channellistener = new ChannelListener();
channel.asyncOpen(channellistener);
var listener = new ImageListener(
null,
getChannelLoadImageStopCallback(channellistener, checkSecondChannelLoad)
);
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var outlistener = {};
requests.push({
request: gCurrentLoader.loadImageWithChannelXPCOM(
channel,
outer,
null,
outlistener
),
locked: false,
});
channellistener.outputListener = outlistener.value;
listener.synchronous = false;
}
function all_done_callback() {
server.stop(function () {
do_test_finished();
});
}
function startImageCallback(otherCb) {
return function (listener, request) {
// Make sure we can load the same image immediately out of the cache.
do_test_pending();
var listener2 = new ImageListener(null, function (foo, bar) {
do_test_finished();
});
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener2);
var referrerInfo = new ReferrerInfo(
Ci.nsIReferrerInfo.NO_REFERRER_WHEN_DOWNGRADE,
true,
null
);
requests.push({
request: gCurrentLoader.loadImageXPCOM(
uri,
null,
referrerInfo,
null,
null,
outer,
null,
0,
null
),
locked: false,
});
listener2.synchronous = false;
// Now that we've started another load, chain to the callback.
otherCb(listener, request);
};
}
var gCurrentLoader;
function cleanup() {
for (let { request, locked } of requests) {
if (locked) {
try {
request.unlockImage();
} catch (e) {}
}
request.cancelAndForgetObserver(0);
}
}
function run_test() {
registerCleanupFunction(cleanup);
gCurrentLoader = Cc["@mozilla.org/image/loader;1"].createInstance(
Ci.imgILoader
);
do_test_pending();
var listener = new ImageListener(
startImageCallback(checkClone),
firstLoadDone
);
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var referrerInfo = new ReferrerInfo(
Ci.nsIReferrerInfo.NO_REFERRER_WHEN_DOWNGRADE,
true,
null
);
var req = gCurrentLoader.loadImageXPCOM(
uri,
null,
referrerInfo,
null,
null,
outer,
null,
0,
null
);
// Ensure that we don't cause any mayhem when we lock an image.
req.lockImage();
requests.push({ request: req, locked: true });
listener.synchronous = false;
}
|