function SeleniumHelpers() {
var self = this,
nr_images_loaded = 0,
nr_expected_loads = 0,
has_srcset_support = ('srcset' in document.createElement('img'));
// No need to stop the interval timer.
setInterval(function () {
var images = document.getElementsByTagName('img'), i, image;
function implies(a, b) { return !a || b; }
nr_images_loaded = 0;
nr_expected_loads = 0;
for (i = 0; i < images.length; i += 1) {
image = images[i];
if (image.naturalWidth) {
nr_images_loaded += 1;
}
if (!image.hasAttribute('will_fail')) {
// FIXME: get rid of this conditional when we
// implement srcset emulation in the TC.
if (implies(image.hasAttribute('srcset'), has_srcset_support)) {
nr_expected_loads += 1;
}
}
}
}, 100);
self.verifyImageLoad = function () {
// Some browsers are more tolerant of malformed srcset spec:
// e.g., Chrome, hence the >=.
return nr_images_loaded >= nr_expected_loads;
};
self.verifySrcSet = function () {
var nr_expected_loads = 0, nr_actual_loads = 0, images, i;
images = document.getElementsByTagName('img');
if (!has_srcset_support) {
// Not an HTML5 browser ---> no support for srcset.
return true;
}
for (i = 0; i < images.length; i += 1) {
if (!images[i].getAttribute('will_fail') && images[i].hasAttribute('srcset')) {
nr_expected_loads += 1;
if (images[i].getAttribute('srcset')) {
nr_actual_loads += 1;
}
}
}
return nr_actual_loads === nr_expected_loads;
};
}
testcase.image = new SeleniumHelpers();