From 9a921d766fbebedd0756603d86b5b573f5383567 Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Thu, 19 Jan 2012 16:01:43 +0100 Subject: [PATCH] Bug 497543 - Part 4 - Tests and test suite; r=dietrich --- .../components/thumbnails/test/Makefile.in | 21 +++++ .../test/browser_thumbnails_cache.js | 34 +++++++ .../test/browser_thumbnails_capture.js | 38 ++++++++ browser/components/thumbnails/test/head.js | 93 +++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100644 browser/components/thumbnails/test/Makefile.in create mode 100644 browser/components/thumbnails/test/browser_thumbnails_cache.js create mode 100644 browser/components/thumbnails/test/browser_thumbnails_capture.js create mode 100644 browser/components/thumbnails/test/head.js diff --git a/browser/components/thumbnails/test/Makefile.in b/browser/components/thumbnails/test/Makefile.in new file mode 100644 index 000000000000..014bf0828a31 --- /dev/null +++ b/browser/components/thumbnails/test/Makefile.in @@ -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) diff --git a/browser/components/thumbnails/test/browser_thumbnails_cache.js b/browser/components/thumbnails/test/browser_thumbnails_cache.js new file mode 100644 index 000000000000..c0917f74fffd --- /dev/null +++ b/browser/components/thumbnails/test/browser_thumbnails_cache.js @@ -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 element to compare + * its pixel colors. + */ +function runTests() { + // Create a new tab with a red background. + yield addTab("data:text/html,"); + 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," + + ""); + + 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"); +} diff --git a/browser/components/thumbnails/test/browser_thumbnails_capture.js b/browser/components/thumbnails/test/browser_thumbnails_capture.js new file mode 100644 index 000000000000..38886159659d --- /dev/null +++ b/browser/components/thumbnails/test/browser_thumbnails_capture.js @@ -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,"); + checkCurrentThumbnailColor(255, 0, 0, "we have a red thumbnail"); + + // Load a page with a green background. + yield navigateTo("data:text/html,"); + checkCurrentThumbnailColor(0, 255, 0, "we have a green thumbnail"); + + // Load a page with a blue background. + yield navigateTo("data:text/html,"); + 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); +} diff --git a/browser/components/thumbnails/test/head.js b/browser/components/thumbnails/test/head.js new file mode 100644 index 000000000000..65eb8c4003b2 --- /dev/null +++ b/browser/components/thumbnails/test/head.js @@ -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); +}