PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/image/test/unit/async_load_tests.js

http://github.com/zpao/v8monkey
JavaScript | 209 lines | 130 code | 41 blank | 38 comment | 0 complexity | b3b5b02e123a19bfbc4e15399eec0d36 MD5 | raw file
  1/*
  2 * Test to ensure that image loading/decoding notifications are always
  3 * delivered async, and in the order we expect.
  4 *
  5 * Must be included from a file that has a uri of the image to test defined in
  6 * var uri.
  7 */
  8
  9do_load_httpd_js();
 10
 11var server = new nsHttpServer();
 12server.registerDirectory("/", do_get_file(''));
 13server.registerContentType("sjs", "sjs");
 14server.start(8088);
 15
 16load('image_load_helpers.js');
 17
 18var requests = [];
 19
 20// Return a closure that holds on to the listener from the original
 21// imgIRequest, and compares its results to the cloned one.
 22function getCloneStopCallback(original_listener)
 23{
 24  return function cloneStop(listener) {
 25    do_check_eq(original_listener.state, listener.state);
 26
 27    // Sanity check to make sure we didn't accidentally use the same listener
 28    // twice.
 29    do_check_neq(original_listener, listener);
 30    do_test_finished();
 31  }
 32}
 33
 34// Make sure that cloned requests get all the same callbacks as the original,
 35// but they aren't synchronous right now.
 36function checkClone(other_listener, aRequest)
 37{
 38  do_test_pending();
 39
 40  // For as long as clone notification is synchronous, we can't test the clone state reliably.
 41  var listener = new ImageListener(null, function(foo, bar) { do_test_finished(); } /*getCloneStopCallback(other_listener)*/);
 42  listener.synchronous = false;
 43  var clone = aRequest.clone(listener);
 44}
 45
 46// Ensure that all the callbacks were called on aRequest.
 47function checkAllCallbacks(listener, aRequest)
 48{
 49  do_check_eq(listener.state, ALL_BITS);
 50
 51  do_test_finished();
 52}
 53
 54function secondLoadDone(oldlistener, aRequest)
 55{
 56  do_test_pending();
 57
 58  try {
 59    var staticrequest = aRequest.getStaticRequest();
 60
 61    // For as long as clone notification is synchronous, we can't test the
 62    // clone state reliably.
 63    var listener = new ImageListener(null, checkAllCallbacks);
 64    listener.synchronous = false;
 65    var staticrequestclone = staticrequest.clone(listener);
 66  } catch(e) {
 67    // We can't create a static request. Most likely the request we started
 68    // with didn't load successfully.
 69    do_test_finished();
 70  }
 71
 72  run_loadImageWithChannel_tests();
 73
 74  do_test_finished();
 75}
 76
 77// Load the request a second time. This should come from the image cache, and
 78// therefore would be at most risk of being served synchronously.
 79function checkSecondLoad()
 80{
 81  do_test_pending();
 82
 83  var loader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
 84  var listener = new ImageListener(checkClone, secondLoadDone);
 85  requests.push(loader.loadImage(uri, null, null, null, null, listener, null, 0, null, null, null));
 86  listener.synchronous = false;
 87}
 88
 89function firstLoadDone(oldlistener, aRequest)
 90{
 91  checkSecondLoad(uri);
 92
 93  do_test_finished();
 94}
 95
 96// Return a closure that allows us to check the stream listener's status when the
 97// image starts loading.
 98function getChannelLoadImageStartCallback(streamlistener)
 99{
100  return function channelLoadStart(imglistener, aRequest) {
101    // We must not have received any status before we get this start callback.
102    // If we have, we've broken people's expectations by delaying events from a
103    // channel we were given.
104    do_check_eq(streamlistener.requestStatus, 0);
105
106    checkClone(imglistener, aRequest);
107  }
108}
109
110// Return a closure that allows us to check the stream listener's status when the
111// image finishes loading.
112function getChannelLoadImageStopCallback(streamlistener, next)
113{
114  return function channelLoadStop(imglistener, aRequest) {
115    // We absolutely must not get imgIDecoderObserver::onStopRequest after
116    // nsIRequestObserver::onStopRequest has fired. If we do that, we've broken
117    // people's expectations by delaying events from a channel we were given.
118    do_check_eq(streamlistener.requestStatus & STOP_REQUEST, 0);
119
120    next();
121
122    do_test_finished();
123  }
124}
125
126// Load the request a second time. This should come from the image cache, and
127// therefore would be at most risk of being served synchronously.
128function checkSecondChannelLoad()
129{
130  do_test_pending();
131
132  var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);  
133  var channel = ioService.newChannelFromURI(uri);
134  var channellistener = new ChannelListener();
135  channel.asyncOpen(channellistener, null);
136
137  var loader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
138  var listener = new ImageListener(getChannelLoadImageStartCallback(channellistener),
139                                   getChannelLoadImageStopCallback(channellistener,
140                                                                   all_done_callback));
141  var outlistener = {};
142  requests.push(loader.loadImageWithChannel(channel, listener, null, outlistener));
143  channellistener.outputListener = outlistener.value;
144
145  listener.synchronous = false;
146}
147
148function run_loadImageWithChannel_tests()
149{
150  // To ensure we're testing what we expect to, clear the content image cache
151  // between test runs.
152  var loader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
153  loader.QueryInterface(Ci.imgICache);
154  loader.clearCache(false);
155
156  do_test_pending();
157
158  var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);  
159  var channel = ioService.newChannelFromURI(uri);
160  var channellistener = new ChannelListener();
161  channel.asyncOpen(channellistener, null);
162
163  var loader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
164  var listener = new ImageListener(getChannelLoadImageStartCallback(channellistener),
165                                   getChannelLoadImageStopCallback(channellistener,
166                                                                   checkSecondChannelLoad));
167  var outlistener = {};
168  requests.push(loader.loadImageWithChannel(channel, listener, null, outlistener));
169  channellistener.outputListener = outlistener.value;
170
171  listener.synchronous = false;
172}
173
174function all_done_callback()
175{
176  server.stop(function() { do_test_finished(); });
177}
178
179function startImageCallback(otherCb)
180{
181  return function(listener, request)
182  {
183    var loader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
184
185    // Make sure we can load the same image immediately out of the cache.
186    do_test_pending();
187    var listener2 = new ImageListener(null, function(foo, bar) { do_test_finished(); });
188    requests.push(loader.loadImage(uri, null, null, null, null, listener2, null, 0, null, null, null));
189    listener2.synchronous = false;
190
191    // Now that we've started another load, chain to the callback.
192    otherCb(listener, request);
193  }
194}
195
196function run_test()
197{
198  var loader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
199
200  do_test_pending();
201  var listener = new ImageListener(startImageCallback(checkClone), firstLoadDone);
202  var req = loader.loadImage(uri, null, null, null, null, listener, null, 0, null, null, null);
203  requests.push(req);
204
205  // Ensure that we don't cause any mayhem when we lock an image.
206  req.lockImage();
207
208  listener.synchronous = false;
209}