Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / resources / image-preload-helper.js
blob27ea17d143abaad3f1cd1f0b9144dee27b9b2804
1 // This is used by several tests to help get images reliably preloaded.
3 // Given a node, loads all urls specified in style declarations
4 // attached to the node or it's decendants.
5 // imageCount specifies the number of images we expect to find (to try to add some
6 // protection against brittleness due to imperfect url parsing, since other missing a preload
7 // will typically result in a test that fails only occasionally).
8 // If failPattern is specified, then any url that matches the regex
9 // will be expected to fail to load.
10 function preloadImagesFromStyle(rootNode, imageCount, onComplete, failPattern) {
11 var basePath = location.href.substring(0, location.href.lastIndexOf('/') + 1);
12 var nodes = rootNode.querySelectorAll('[style]');
13 var imagesToLoad = [];
14 var seenUrls = {};
15 for (var i = 0; i < nodes.length; i++) {
16 var urls = nodes[i].style.cssText.split(/url\w*\(([^)]*)\)/);
17 for (var j = 1; j < urls.length; j += 2) {
18 // Attempt to convert URL to a relative path in order to have deterministic error messages.
19 var url = urls[j];
20 if (url.indexOf(basePath) == 0)
21 url = url.substring(basePath.length);
23 var error = false;
24 if (failPattern && failPattern.test(url))
25 error = true;
26 if (url in seenUrls)
27 continue;
28 seenUrls[url] = true;
29 imagesToLoad.push({url: url, error: error});
33 if (imageCount != imagesToLoad.length) {
34 var msg = 'Found the following ' + imagesToLoad.length + ' images, when expecting ' + imageCount + ': ';
35 for (var i = 0; i < imagesToLoad.length; i++) {
36 msg += '\n' + imagesToLoad[i].url;
38 testFailed(msg);
41 loadImages(imagesToLoad, onComplete);
44 // For each object in the given array, attempt to load the image specified by the
45 // url property. If the error property is specified and true, then the load is
46 // expected to fail. Once all loads have completed or failed, onComplete is invoked.
47 function loadImages(imagesToLoad, onComplete) {
49 var imagesLeftToLoad = imagesToLoad.length;
51 function onImageLoad(url, success, e) {
52 // This debug output order is non-deterministic - only show when not running in DRT
53 if (!window.testRunner)
54 debug( 'Event "' + e.type + '": ' + url);
56 if (!success)
57 testFailed('Got unexpected \'' + e.type + '\' event for image: ' + url);
59 imagesLeftToLoad--;
60 if (imagesLeftToLoad == 0) {
61 onComplete();
63 if (imagesLeftToLoad < 0)
64 testFailed('Got more load/error callbacks than expected.');
67 for (var i = 0; i < imagesToLoad.length; i++) {
68 var img = new Image();
69 var expectError = imagesToLoad[i].error;
70 img.addEventListener('load', onImageLoad.bind(undefined, imagesToLoad[i].url, !expectError));
71 img.addEventListener('error', onImageLoad.bind(undefined, imagesToLoad[i].url, !!expectError));
72 img.src = imagesToLoad[i].url;