Bug 721422 - [Page Thumbs] Re-enable tests and make them work with URI_DANGEROUS_TO_LOAD; r=dietrich

This commit is contained in:
Tim Taubert 2012-01-27 11:22:35 +01:00
parent e0a0dce3dd
commit 69050caecd
5 changed files with 85 additions and 71 deletions

View File

@ -18,10 +18,9 @@ EXTRA_PP_JS_MODULES = \
PageThumbs.jsm \
$(NULL)
# FIXME Bug 721422 - Re-enable tests and make them work with URI_DANGEROUS_TO_LOAD
#ifdef ENABLE_TESTS
# DIRS += test
#endif
ifdef ENABLE_TESTS
DIRS += test
endif
include $(topsrcdir)/config/rules.mk

View File

@ -12,7 +12,6 @@ include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_BROWSER_FILES = \
browser_thumbnails_cache.js \
browser_thumbnails_capture.js \
head.js \
$(NULL)

View File

@ -1,34 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* These tests ensure that saving a thumbnail to the cache works. They also
* retrieve the thumbnail and display it using an <img> element to compare
* its pixel colors.
*/
function runTests() {
// Create a new tab with a red background.
yield addTab("data:text/html,<body bgcolor=ff0000></body>");
let cw = gBrowser.selectedTab.linkedBrowser.contentWindow;
// Capture a thumbnail for the tab.
let canvas = PageThumbs.capture(cw);
// Store the tab into the thumbnail cache.
yield PageThumbs.store("key", canvas, next);
let {width, height} = canvas;
let thumb = PageThumbs.getThumbnailURL("key", width, height);
// Create a new tab with an image displaying the previously stored thumbnail.
yield addTab("data:text/html,<img src='" + thumb + "'/>" +
"<canvas width=" + width + " height=" + height + "/>");
cw = gBrowser.selectedTab.linkedBrowser.contentWindow;
let [img, canvas] = cw.document.querySelectorAll("img, canvas");
// Draw the image to a canvas and compare the pixel color values.
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
checkCanvasColor(ctx, 255, 0, 0, "we have a red image and canvas");
}

View File

@ -2,37 +2,19 @@
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* These tests ensure that capturing a site's screenshot to a canvas actually
* works.
* These tests ensure that capturing a sites's thumbnail, saving it and
* retrieving it from the cache works.
*/
function runTests() {
// Create a tab with a red background.
yield addTab("data:text/html,<body bgcolor=ff0000></body>");
checkCurrentThumbnailColor(255, 0, 0, "we have a red thumbnail");
yield captureAndCheckColor(255, 0, 0, "we have a red thumbnail");
// Load a page with a green background.
yield navigateTo("data:text/html,<body bgcolor=00ff00></body>");
checkCurrentThumbnailColor(0, 255, 0, "we have a green thumbnail");
yield captureAndCheckColor(0, 255, 0, "we have a green thumbnail");
// Load a page with a blue background.
yield navigateTo("data:text/html,<body bgcolor=0000ff></body>");
checkCurrentThumbnailColor(0, 0, 255, "we have a blue thumbnail");
}
/**
* Captures a thumbnail of the currently selected tab and checks the color of
* the resulting canvas.
* @param aRed The red component's intensity.
* @param aGreen The green component's intensity.
* @param aBlue The blue component's intensity.
* @param aMessage The info message to print when checking the pixel color.
*/
function checkCurrentThumbnailColor(aRed, aGreen, aBlue, aMessage) {
let tab = gBrowser.selectedTab;
let cw = tab.linkedBrowser.contentWindow;
let canvas = PageThumbs.capture(cw);
let ctx = canvas.getContext("2d");
checkCanvasColor(ctx, aRed, aGreen, aBlue, aMessage);
yield captureAndCheckColor(0, 0, 255, "we have a blue thumbnail");
}

View File

@ -8,6 +8,8 @@ registerCleanupFunction(function () {
gBrowser.removeTab(gBrowser.tabs[1]);
});
let cachedXULDocument;
/**
* Provide the default test function to start our test runner.
*/
@ -54,7 +56,7 @@ function next() {
*/
function addTab(aURI) {
let tab = gBrowser.selectedTab = gBrowser.addTab(aURI);
whenBrowserLoaded(tab.linkedBrowser);
whenLoaded(tab.linkedBrowser);
}
/**
@ -63,22 +65,88 @@ function addTab(aURI) {
*/
function navigateTo(aURI) {
let browser = gBrowser.selectedTab.linkedBrowser;
whenBrowserLoaded(browser);
whenLoaded(browser);
browser.loadURI(aURI);
}
/**
* Continues the current test execution when a load event for the given browser
* has been received
* @param aBrowser The browser to listen on.
* Continues the current test execution when a load event for the given element
* has been received.
* @param aElement The DOM element to listen on.
* @param aCallback The function to call when the load event was dispatched.
*/
function whenBrowserLoaded(aBrowser) {
aBrowser.addEventListener("load", function onLoad() {
aBrowser.removeEventListener("load", onLoad, true);
executeSoon(next);
function whenLoaded(aElement, aCallback) {
aElement.addEventListener("load", function onLoad() {
aElement.removeEventListener("load", onLoad, true);
executeSoon(aCallback || next);
}, true);
}
/**
* Captures a screenshot for the currently selected tab, stores it in the cache,
* retrieves it from the cache and compares pixel color values.
* @param aRed The red component's intensity.
* @param aGreen The green component's intensity.
* @param aBlue The blue component's intensity.
* @param aMessage The info message to print when comparing the pixel color.
*/
function captureAndCheckColor(aRed, aGreen, aBlue, aMessage) {
let window = gBrowser.selectedTab.linkedBrowser.contentWindow;
let key = Date.now();
let data = PageThumbs.capture(window);
// Store the thumbnail in the cache.
PageThumbs.store(key, data, function () {
let width = 100, height = 100;
let thumb = PageThumbs.getThumbnailURL(key, width, height);
getXULDocument(function (aDocument) {
let htmlns = "http://www.w3.org/1999/xhtml";
let img = aDocument.createElementNS(htmlns, "img");
img.setAttribute("src", thumb);
whenLoaded(img, function () {
let canvas = aDocument.createElementNS(htmlns, "canvas");
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
// Draw the image to a canvas and compare the pixel color values.
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
checkCanvasColor(ctx, aRed, aGreen, aBlue, aMessage);
next();
});
});
});
}
/**
* Passes a XUL document (created if necessary) to the given callback.
* @param aCallback The function to be called when the XUL document has been
* created. The first argument will be the document.
*/
function getXULDocument(aCallback) {
let hiddenWindow = Services.appShell.hiddenDOMWindow;
let doc = cachedXULDocument || hiddenWindow.document;
if (doc instanceof XULDocument) {
aCallback(cachedXULDocument = doc);
return;
}
let iframe = doc.createElement("iframe");
iframe.setAttribute("src", "chrome://global/content/mozilla.xhtml");
iframe.addEventListener("DOMContentLoaded", function onLoad() {
iframe.removeEventListener("DOMContentLoaded", onLoad, false);
aCallback(cachedXULDocument = iframe.contentDocument);
}, false);
doc.body.appendChild(iframe);
}
/**
* Checks the top-left pixel of a given canvas' 2d context for a given color.
* @param aContext The 2D context of a canvas.