diff --git a/test/lib/www/load-images.html b/test/lib/www/load-images.html new file mode 100644 index 000000000..cf7335e35 --- /dev/null +++ b/test/lib/www/load-images.html @@ -0,0 +1,9 @@ + + + + + + logo + phantomjs + + diff --git a/test/module/webpage/load-images-no.js b/test/module/webpage/load-images-no.js new file mode 100644 index 000000000..51b5fed3e --- /dev/null +++ b/test/module/webpage/load-images-no.js @@ -0,0 +1,50 @@ +//! phantomjs: --load-images=no +"use strict"; + +// Compiled for debug, this tests for leaks when not loading images. + +var url = TEST_HTTP_BASE + 'load-images.html'; + +async_test(function testLoadImagesNo () { + var page = require('webpage').create(); + + var numImagesRequested = 0; + var numImagesLoaded = 0; + + page.onResourceRequested = this.step_func(function (resource) { + if (/\.png$/.test(resource.url)) ++numImagesRequested; + }); + + page.onResourceReceived = this.step_func(function (resource) { + if (resource.stage === 'end' && /\.png$/.test(resource.url)) ++numImagesLoaded; + }); + + function checkImageCounts () + { + var imageCounts = page.evaluate (function countImages() { + "use strict"; + try + { + var images = document.getElementsByTagName('img'); + var imageCounts = {}; + imageCounts.numImages = images.length; + imageCounts.numComplete = Array.prototype.reduce.call(images, function(n, image) { return n + (image.complete? 1 : 0); }, 0); + return imageCounts; + } + catch(err) + { + return undefined; + } + }); + assert_equals(imageCounts.numImages, 2); + assert_equals(imageCounts.numComplete, 0); + assert_equals(numImagesRequested, 0); + assert_equals(numImagesLoaded, 0); + } + + page.open(url, this.step_func_done (function pageOpened(status) { + assert_equals(status, "success"); + checkImageCounts(); + page.release(); + })); +}, "load a page two images with --load-images=no"); diff --git a/test/module/webpage/load-images-yes.js b/test/module/webpage/load-images-yes.js new file mode 100644 index 000000000..7f5ba9c63 --- /dev/null +++ b/test/module/webpage/load-images-yes.js @@ -0,0 +1,48 @@ +//! phantomjs: --load-images=yes +"use strict"; + +var url = TEST_HTTP_BASE + 'load-images.html'; + +async_test(function testLoadImagesYes () { + var page = require('webpage').create(); + + var numImagesRequested = 0; + var numImagesLoaded = 0; + + page.onResourceRequested = this.step_func(function (resource) { + if (/\.png$/.test(resource.url)) ++numImagesRequested; + }); + + page.onResourceReceived = this.step_func(function (resource) { + if (resource.stage === 'end' && /\.png$/.test(resource.url)) ++numImagesLoaded; + }); + + function checkImageCounts () + { + var imageCounts = page.evaluate (function countImages() { + "use strict"; + try + { + var images = document.getElementsByTagName('img'); + var imageCounts = {}; + imageCounts.numImages = images.length; + imageCounts.numComplete = Array.prototype.reduce.call(images, function(n, image) { return n + (image.complete? 1 : 0); }, 0); + return imageCounts; + } + catch(err) + { + return undefined; + } + }); + assert_equals(imageCounts.numImages, 2); + assert_equals(imageCounts.numComplete, 2); + assert_equals(numImagesRequested, 2); + assert_equals(numImagesLoaded, 2); + } + + page.open(url, this.step_func_done (function pageOpened(status) { + assert_equals(status, "success"); + checkImageCounts(); + page.release(); + })); +}, "load a page two images with --load-images=yes");