Bug 497543 - Part 4 - Tests and test suite; r=dietrich

This commit is contained in:
Tim Taubert 2012-01-19 16:01:43 +01:00
parent e72129d811
commit 9a921d766f
4 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,21 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = browser/components/thumbnails/test
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_BROWSER_FILES = \
browser_thumbnails_cache.js \
browser_thumbnails_capture.js \
head.js \
$(NULL)
libs:: $(_BROWSER_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)

View File

@ -0,0 +1,34 @@
/* 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

@ -0,0 +1,38 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* These tests ensure that capturing a site's screenshot to a canvas actually
* 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");
// 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");
// 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);
}

View File

@ -0,0 +1,93 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource:///modules/PageThumbs.jsm");
registerCleanupFunction(function () {
while (gBrowser.tabs.length > 1)
gBrowser.removeTab(gBrowser.tabs[1]);
});
/**
* Provide the default test function to start our test runner.
*/
function test() {
TestRunner.run();
}
/**
* The test runner that controls the execution flow of our tests.
*/
let TestRunner = {
/**
* Starts the test runner.
*/
run: function () {
waitForExplicitFinish();
this._iter = runTests();
this.next();
},
/**
* Runs the next available test or finishes if there's no test left.
*/
next: function () {
try {
TestRunner._iter.next();
} catch (e if e instanceof StopIteration) {
finish();
}
}
};
/**
* Continues the current test execution.
*/
function next() {
TestRunner.next();
}
/**
* Creates a new tab with the given URI.
* @param aURI The URI that's loaded in the tab.
*/
function addTab(aURI) {
let tab = gBrowser.selectedTab = gBrowser.addTab(aURI);
whenBrowserLoaded(tab.linkedBrowser);
}
/**
* Loads a new URI into the currently selected tab.
* @param aURI The URI to load.
*/
function navigateTo(aURI) {
let browser = gBrowser.selectedTab.linkedBrowser;
whenBrowserLoaded(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.
*/
function whenBrowserLoaded(aBrowser) {
aBrowser.addEventListener("load", function onLoad() {
aBrowser.removeEventListener("load", onLoad, true);
executeSoon(next);
}, true);
}
/**
* Checks the top-left pixel of a given canvas' 2d context for a given color.
* @param aContext The 2D context of a 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 comparing the pixel color.
*/
function checkCanvasColor(aContext, aRed, aGreen, aBlue, aMessage) {
let [r, g, b] = aContext.getImageData(0, 0, 1, 1).data;
ok(r == aRed && g == aGreen && b == aBlue, aMessage);
}