| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.net.ImageLoaderTest'); |
| 16 |
goog.setTestOnly('goog.net.ImageLoaderTest'); |
| 17 |
|
| 18 |
goog.require('goog.Promise'); |
| 19 |
goog.require('goog.Timer'); |
| 20 |
goog.require('goog.array'); |
| 21 |
goog.require('goog.dispose'); |
| 22 |
goog.require('goog.events'); |
| 23 |
goog.require('goog.events.Event'); |
| 24 |
goog.require('goog.events.EventType'); |
| 25 |
goog.require('goog.net.EventType'); |
| 26 |
goog.require('goog.net.ImageLoader'); |
| 27 |
goog.require('goog.object'); |
| 28 |
goog.require('goog.string'); |
| 29 |
goog.require('goog.testing.TestCase'); |
| 30 |
goog.require('goog.testing.jsunit'); |
| 31 |
goog.require('goog.testing.recordFunction'); |
| 32 |
|
| 33 |
|
| 34 |
var TEST_EVENT_TYPES = [ |
| 35 |
goog.events.EventType.LOAD, goog.net.EventType.COMPLETE, |
| 36 |
goog.net.EventType.ERROR |
| 37 |
]; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Mapping from test image file name to: |
| 42 |
* [expected width, expected height, expected event to be fired]. |
| 43 |
*/ |
| 44 |
var TEST_IMAGES = { |
| 45 |
'imageloader_testimg1.gif': [20, 20, goog.events.EventType.LOAD], |
| 46 |
'imageloader_testimg2.gif': [20, 20, goog.events.EventType.LOAD], |
| 47 |
'imageloader_testimg3.gif': [32, 32, goog.events.EventType.LOAD], |
| 48 |
|
| 49 |
'this-is-not-image-1.gif': [0, 0, goog.net.EventType.ERROR], |
| 50 |
'this-is-not-image-2.gif': [0, 0, goog.net.EventType.ERROR] |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
var startTime; |
| 55 |
var loader; |
| 56 |
|
| 57 |
|
| 58 |
function setUpPage() { |
| 59 |
// Increase the timeout to 5 seconds to allow more time for images to load. |
| 60 |
goog.testing.TestCase.getActiveTestCase().promiseTimeout = 5 * 1000; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function setUp() { |
| 65 |
startTime = goog.now(); |
| 66 |
|
| 67 |
loader = new goog.net.ImageLoader(); |
| 68 |
|
| 69 |
// Adds test images to the loader. |
| 70 |
var i = 0; |
| 71 |
for (var key in TEST_IMAGES) { |
| 72 |
var imageId = 'img_' + i++; |
| 73 |
loader.addImage(imageId, key); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
function tearDown() { |
| 79 |
goog.dispose(loader); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Tests loading image and disposing before loading completes. |
| 85 |
*/ |
| 86 |
function testDisposeInTheMiddleOfLoadingWorks() { |
| 87 |
var resolver = goog.Promise.withResolver(); |
| 88 |
|
| 89 |
goog.events.listen(loader, TEST_EVENT_TYPES, function(e) { |
| 90 |
assertFalse( |
| 91 |
'Handler is still invoked after loader is disposed.', |
| 92 |
loader.isDisposed()); |
| 93 |
|
| 94 |
switch (e.type) { |
| 95 |
case goog.net.EventType.COMPLETE: |
| 96 |
resolver.reject('This test should never get COMPLETE event.'); |
| 97 |
return; |
| 98 |
|
| 99 |
case goog.events.EventType.LOAD: |
| 100 |
case goog.net.EventType.ERROR: |
| 101 |
loader.dispose(); |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
// Make sure that handler is never called again after disposal before |
| 106 |
// marking test as successful. |
| 107 |
goog.Timer.callOnce(function() { resolver.resolve(); }, 500); |
| 108 |
}); |
| 109 |
|
| 110 |
loader.start(); |
| 111 |
return resolver.promise; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Tests loading of images until completion. |
| 117 |
*/ |
| 118 |
function testLoadingUntilCompletion() { |
| 119 |
var resolver = goog.Promise.withResolver(); |
| 120 |
var results = {}; |
| 121 |
goog.events.listen(loader, TEST_EVENT_TYPES, function(e) { |
| 122 |
switch (e.type) { |
| 123 |
case goog.events.EventType.LOAD: |
| 124 |
var image = e.target; |
| 125 |
results[image.src.substring(image.src.lastIndexOf('/') + 1)] = |
| 126 |
[image.naturalWidth, image.naturalHeight, e.type]; |
| 127 |
return; |
| 128 |
|
| 129 |
case goog.net.EventType.ERROR: |
| 130 |
var image = e.target; |
| 131 |
results[image.src.substring(image.src.lastIndexOf('/') + 1)] = |
| 132 |
[image.naturalWidth, image.naturalHeight, e.type]; |
| 133 |
return; |
| 134 |
|
| 135 |
case goog.net.EventType.COMPLETE: |
| 136 |
try { |
| 137 |
assertImagesAreCorrect(results); |
| 138 |
} catch (e) { |
| 139 |
resolver.reject(e); |
| 140 |
return; |
| 141 |
} |
| 142 |
resolver.resolve(); |
| 143 |
return; |
| 144 |
} |
| 145 |
}); |
| 146 |
|
| 147 |
loader.start(); |
| 148 |
return resolver.promise; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
function assertImagesAreCorrect(results) { |
| 153 |
assertEquals( |
| 154 |
goog.object.getCount(TEST_IMAGES), goog.object.getCount(results)); |
| 155 |
goog.object.forEach(TEST_IMAGES, function(value, key) { |
| 156 |
// Check if fires the COMPLETE event. |
| 157 |
assertTrue('Image is not loaded completely.', key in results); |
| 158 |
|
| 159 |
var image = results[key]; |
| 160 |
|
| 161 |
// Check image size. |
| 162 |
assertEquals('Image width is not correct', value[0], image[0]); |
| 163 |
assertEquals('Image length is not correct', value[1], image[1]); |
| 164 |
|
| 165 |
// Check if fired the correct event. |
| 166 |
assertEquals('Event *' + value[2] + '* must be fired', value[2], image[2]); |
| 167 |
}); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Overrides the loader's loadImage_ method so that it dispatches an image |
| 173 |
* loaded event immediately, causing any event listeners to receive them |
| 174 |
* synchronously. This allows tests to assume synchronous execution. |
| 175 |
*/ |
| 176 |
function makeLoaderSynchronous(loader) { |
| 177 |
var originalLoadImage = loader.loadImage_; |
| 178 |
loader.loadImage_ = function(request, id) { |
| 179 |
originalLoadImage.call(this, request, id); |
| 180 |
|
| 181 |
var event = new goog.events.Event(goog.events.EventType.LOAD); |
| 182 |
event.currentTarget = this.imageIdToImageMap_[id]; |
| 183 |
loader.onNetworkEvent_(event); |
| 184 |
}; |
| 185 |
|
| 186 |
// Make listen() a no-op. |
| 187 |
loader.handler_.listen = goog.nullFunction; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Verifies that if an additional image is added after start() was called, but |
| 193 |
* before COMPLETE was dispatched, no COMPLETE event is sent. Verifies COMPLETE |
| 194 |
* is finally sent when .start() is called again and all images have now |
| 195 |
* completed loading. |
| 196 |
*/ |
| 197 |
function testImagesAddedAfterStart() { |
| 198 |
// Use synchronous image loading. |
| 199 |
makeLoaderSynchronous(loader); |
| 200 |
|
| 201 |
// Add another image once the first images finishes loading. |
| 202 |
goog.events.listenOnce(loader, goog.events.EventType.LOAD, function() { |
| 203 |
loader.addImage('extra_image', 'extra_image.gif'); |
| 204 |
}); |
| 205 |
|
| 206 |
// Keep track of the total # of image loads. |
| 207 |
var loadRecordFn = goog.testing.recordFunction(); |
| 208 |
goog.events.listen(loader, goog.events.EventType.LOAD, loadRecordFn); |
| 209 |
|
| 210 |
// Keep track of how many times COMPLETE was dispatched. |
| 211 |
var completeRecordFn = goog.testing.recordFunction(); |
| 212 |
goog.events.listen(loader, goog.net.EventType.COMPLETE, completeRecordFn); |
| 213 |
|
| 214 |
// Start testing. |
| 215 |
loader.start(); |
| 216 |
assertEquals( |
| 217 |
'COMPLETE event should not have been dispatched yet: An image was ' + |
| 218 |
'added after the initial batch was started.', |
| 219 |
0, completeRecordFn.getCallCount()); |
| 220 |
assertEquals( |
| 221 |
'Just the test images should have loaded', |
| 222 |
goog.object.getCount(TEST_IMAGES), loadRecordFn.getCallCount()); |
| 223 |
|
| 224 |
loader.start(); |
| 225 |
assertEquals( |
| 226 |
'COMPLETE should have been dispatched once.', 1, |
| 227 |
completeRecordFn.getCallCount()); |
| 228 |
assertEquals( |
| 229 |
'All images should have been loaded', |
| 230 |
goog.object.getCount(TEST_IMAGES) + 1, loadRecordFn.getCallCount()); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Verifies that more images can be added after an upload starts, and start() |
| 236 |
* can be called for them, resulting in just one COMPLETE event once all the |
| 237 |
* images have completed. |
| 238 |
*/ |
| 239 |
function testImagesAddedAndStartedAfterStart() { |
| 240 |
// Use synchronous image loading. |
| 241 |
makeLoaderSynchronous(loader); |
| 242 |
|
| 243 |
// Keep track of the total # of image loads. |
| 244 |
var loadRecordFn = goog.testing.recordFunction(); |
| 245 |
goog.events.listen(loader, goog.events.EventType.LOAD, loadRecordFn); |
| 246 |
|
| 247 |
// Add more images once the first images finishes loading, and call start() |
| 248 |
// to get them going. |
| 249 |
goog.events.listenOnce(loader, goog.events.EventType.LOAD, function(e) { |
| 250 |
loader.addImage('extra_image', 'extra_image.gif'); |
| 251 |
loader.addImage('extra_image2', 'extra_image2.gif'); |
| 252 |
loader.start(); |
| 253 |
}); |
| 254 |
|
| 255 |
// Keep track of how many times COMPLETE was dispatched. |
| 256 |
var completeRecordFn = goog.testing.recordFunction(); |
| 257 |
goog.events.listen(loader, goog.net.EventType.COMPLETE, completeRecordFn); |
| 258 |
|
| 259 |
// Start testing. Make sure all 7 images loaded. |
| 260 |
loader.start(); |
| 261 |
assertEquals( |
| 262 |
'COMPLETE should have been dispatched once.', 1, |
| 263 |
completeRecordFn.getCallCount()); |
| 264 |
assertEquals( |
| 265 |
'All images should have been loaded', |
| 266 |
goog.object.getCount(TEST_IMAGES) + 2, loadRecordFn.getCallCount()); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* Verifies that if images are removed after loading has started, COMPLETE |
| 272 |
* is dispatched once the remaining images have finished. |
| 273 |
*/ |
| 274 |
function testImagesRemovedAfterStart() { |
| 275 |
// Use synchronous image loading. |
| 276 |
makeLoaderSynchronous(loader); |
| 277 |
|
| 278 |
// Remove 2 images once the first image finishes loading. |
| 279 |
goog.events.listenOnce(loader, goog.events.EventType.LOAD, function(e) { |
| 280 |
loader.removeImage( |
| 281 |
goog.array.peek(goog.object.getKeys(this.imageIdToRequestMap_))); |
| 282 |
loader.removeImage( |
| 283 |
goog.array.peek(goog.object.getKeys(this.imageIdToRequestMap_))); |
| 284 |
}); |
| 285 |
|
| 286 |
// Keep track of the total # of image loads. |
| 287 |
var loadRecordFn = goog.testing.recordFunction(); |
| 288 |
goog.events.listen(loader, goog.events.EventType.LOAD, loadRecordFn); |
| 289 |
|
| 290 |
// Keep track of how many times COMPLETE was dispatched. |
| 291 |
var completeRecordFn = goog.testing.recordFunction(); |
| 292 |
goog.events.listen(loader, goog.net.EventType.COMPLETE, completeRecordFn); |
| 293 |
|
| 294 |
// Start testing. Make sure only the 3 images remaining loaded. |
| 295 |
loader.start(); |
| 296 |
assertEquals( |
| 297 |
'COMPLETE should have been dispatched once.', 1, |
| 298 |
completeRecordFn.getCallCount()); |
| 299 |
assertEquals( |
| 300 |
'All images should have been loaded', |
| 301 |
goog.object.getCount(TEST_IMAGES) - 2, loadRecordFn.getCallCount()); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Verifies that the correct image attribute is set when using CORS requests. |
| 307 |
*/ |
| 308 |
function testSetsCorsAttribute() { |
| 309 |
// Use synchronous image loading. |
| 310 |
makeLoaderSynchronous(loader); |
| 311 |
|
| 312 |
// Verify the crossOrigin attribute of the requested images. |
| 313 |
goog.events.listen(loader, goog.events.EventType.LOAD, function(e) { |
| 314 |
var image = e.target; |
| 315 |
if (image.id == 'cors_request') { |
| 316 |
assertEquals( |
| 317 |
'CORS requested image should have a crossOrigin attribute set', |
| 318 |
'anonymous', image.crossOrigin); |
| 319 |
} else { |
| 320 |
assertTrue( |
| 321 |
'Non-CORS requested images should not have a crossOrigin attribute', |
| 322 |
goog.string.isEmptyOrWhitespace( |
| 323 |
goog.string.makeSafe(image.crossOrigin))); |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
// Make a new request for one of the images, this time using CORS. |
| 328 |
var srcs = goog.object.getKeys(TEST_IMAGES); |
| 329 |
loader.addImage( |
| 330 |
'cors_request', srcs[0], goog.net.ImageLoader.CorsRequestType.ANONYMOUS); |
| 331 |
loader.start(); |
| 332 |
} |