From 932628981ff555aec490a2c287ccbf96c0af4758 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Thu, 24 May 2012 12:22:46 -0400 Subject: [PATCH] updating tests --- test/mozcentral/Makefile.in | 1 + test/mozcentral/browser_pdfjs_main.js | 23 +--------- test/mozcentral/browser_pdfjs_zoom.js | 63 +++++++++++++++++++++++++++ test/mozcentral/head.js | 11 ++--- 4 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 test/mozcentral/browser_pdfjs_zoom.js diff --git a/test/mozcentral/Makefile.in b/test/mozcentral/Makefile.in index 8c9face17..fd40574ef 100644 --- a/test/mozcentral/Makefile.in +++ b/test/mozcentral/Makefile.in @@ -14,6 +14,7 @@ include $(topsrcdir)/config/rules.mk _BROWSER_TEST_FILES = \ head.js \ browser_pdfjs_main.js \ + browser_pdfjs_zoom.js \ file_pdfjs_test.pdf \ $(NULL) diff --git a/test/mozcentral/browser_pdfjs_main.js b/test/mozcentral/browser_pdfjs_main.js index d3f5dd646..5f07e6512 100644 --- a/test/mozcentral/browser_pdfjs_main.js +++ b/test/mozcentral/browser_pdfjs_main.js @@ -32,7 +32,7 @@ function test() { ok(outerContainer.classList.contains('sidebarOpen'), 'sidebar opens on click'); // Thumbnails are created asynchronously - wait for them - waitForElement(document, 'canvas#thumbnail2', function(error) { + waitForElement(document, 'canvas#thumbnail2', function(error, thumbnail) { if (error) finish(); @@ -42,7 +42,6 @@ function test() { var pageNumber = document.querySelector('input#pageNumber'); is(parseInt(pageNumber.value), 1, 'initial page is 1'); - var thumbnail = document.querySelector('canvas#thumbnail2'); ok(thumbnail, 'thumbnail2 is available'); if (thumbnail) { thumbnail.click(); @@ -73,25 +72,7 @@ function test() { var viewBookmark = document.querySelector('a#viewBookmark'); viewBookmark.click(); ok(viewBookmark.href.length > 0, 'viewBookmark button has href'); - - // - // Zoom in/out - // - var zoomOut = document.querySelector('button.zoomOut'), - zoomIn = document.querySelector('button.zoomIn'); - - // Zoom in - var oldWidth = document.querySelector('canvas#page1').width; - zoomIn.click(); - var newWidth = document.querySelector('canvas#page1').width; - ok(oldWidth < newWidth, 'zooming in increases page width (old: '+oldWidth+', new: '+newWidth+')'); - - // Zoom out - var oldWidth = document.querySelector('canvas#page1').width; - zoomOut.click(); - var newWidth = document.querySelector('canvas#page1').width; - ok(oldWidth > newWidth, 'zooming out decreases page width (old: '+oldWidth+', new: '+newWidth+')'); - + finish(); }); }, true, true); diff --git a/test/mozcentral/browser_pdfjs_zoom.js b/test/mozcentral/browser_pdfjs_zoom.js new file mode 100644 index 000000000..a6445da2a --- /dev/null +++ b/test/mozcentral/browser_pdfjs_zoom.js @@ -0,0 +1,63 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const RELATIVE_DIR = "browser/extensions/pdfjs/test/"; +const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; + +function test() { + waitForExplicitFinish(); + + var tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf"); + var newTabBrowser = gBrowser.getBrowserForTab(tab); + newTabBrowser.addEventListener("pagechange", function onPageChange() { + newTabBrowser.removeEventListener("pagechange", onPageChange, true); + + var document = newTabBrowser.contentDocument, + window = newTabBrowser.contentWindow; + + // + // Zoom in/out + // + var zoomOut = document.querySelector('button.zoomOut'), + zoomIn = document.querySelector('button.zoomIn'); + + var newWidth, oldWidth; + + // We need to query the DOM every time since zoom in/out operations destroy the canvas element + waitForElement(document, 'canvas#page1', function(error, page1) { + if (error) + finish(); + + oldWidth = page1.width; + zoomIn.click(); + + waitForElement(document, 'canvas#page1', function(error, page1) { + if (error) + finish(); + + newWidth = page1.width; + ok(oldWidth < newWidth, 'zooming in increases page width (old: '+oldWidth+', new: '+newWidth+')'); + + // Zoom out + oldWidth = newWidth; + zoomOut.click(); + + waitForElement(document, 'canvas#page1', function(error, page1) { + if (error) + finish(); + + newWidth = page1.width; + ok(oldWidth > newWidth, 'zooming out decreases page width (old: '+oldWidth+', new: '+newWidth+')'); + + finish(); + }); + }); + }); + + }, true, true); + + registerCleanupFunction(function() { + gBrowser.removeTab(tab); + }); +} diff --git a/test/mozcentral/head.js b/test/mozcentral/head.js index 6ee421b2f..0636ba4e9 100644 --- a/test/mozcentral/head.js +++ b/test/mozcentral/head.js @@ -1,19 +1,20 @@ -// Waits for element 'el' to exist in the DOM of 'doc' before executing 'callback' +// Waits for element 'sel' to exist in the DOM of 'doc' before executing 'callback' // Useful when elements are created asynchronously, e.g. after a Web Worker task -function waitForElement(doc, el, callback) { +function waitForElement(doc, sel, callback) { var time = 0, interval = 10, timeout = 5000; var checkEl = setInterval(function() { - if (doc.querySelector(el)) { + var el = doc.querySelector(sel); + if (el) { clearInterval(checkEl); - if (callback) callback(); + if (callback) callback(null, el); } time += interval; if (time > timeout) { - ok(false, 'waitForElement timed out on element: '+el); + ok(false, 'waitForElement timed out on element: '+sel); clearInterval(checkEl); if (callback) callback(true); }