Bug 1159385 - Make PDFJS browser-chrome tests e10s compatible. r=yury
authorBlake Kaplan <mrbkap@gmail.com>
Tue, 17 Nov 2015 14:20:00 -0800
changeset 273079 ccb27d336e71839d10d08f9bd850da4c7da382c7
parent 273078 bc9e3945f94e3fc92b493346a3d7222dc8a04ab7
child 273080 8c5d3e1ee51053e15337f5fa363098a3f606adb8
push id29693
push usercbook@mozilla.com
push dateWed, 18 Nov 2015 13:50:33 +0000
treeherdermozilla-central@1d6155d7e6c9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyury
bugs1159385
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1159385 - Make PDFJS browser-chrome tests e10s compatible. r=yury
browser/extensions/pdfjs/test/browser.ini
browser/extensions/pdfjs/test/browser_pdfjs_main.js
browser/extensions/pdfjs/test/browser_pdfjs_navigation.js
browser/extensions/pdfjs/test/browser_pdfjs_views.js
browser/extensions/pdfjs/test/browser_pdfjs_zoom.js
browser/extensions/pdfjs/test/head.js
testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
--- a/browser/extensions/pdfjs/test/browser.ini
+++ b/browser/extensions/pdfjs/test/browser.ini
@@ -1,12 +1,10 @@
 [DEFAULT]
-support-files = file_pdfjs_test.pdf
+support-files =
+  file_pdfjs_test.pdf
+  head.js
 
 [browser_pdfjs_main.js]
-skip-if = e10s # Bug 1159385
 [browser_pdfjs_navigation.js]
-skip-if = e10s # Bug 1159385
 [browser_pdfjs_savedialog.js]
 [browser_pdfjs_views.js]
-skip-if = e10s # Bug 1159385
 [browser_pdfjs_zoom.js]
-skip-if = e10s # Bug 1159385
--- a/browser/extensions/pdfjs/test/browser_pdfjs_main.js
+++ b/browser/extensions/pdfjs/test/browser_pdfjs_main.js
@@ -1,98 +1,67 @@
 /* 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() {
-  var tab;
-
+add_task(function* test() {
   let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
   let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
 
   // Make sure pdf.js is the default handler.
   is(handlerInfo.alwaysAskBeforeHandling, false, 'pdf handler defaults to always-ask is false');
   is(handlerInfo.preferredAction, Ci.nsIHandlerInfo.handleInternally, 'pdf handler defaults to internal');
 
   info('Pref action: ' + handlerInfo.preferredAction);
 
-  waitForExplicitFinish();
-  registerCleanupFunction(function() {
-    gBrowser.removeTab(tab);
-  });
+  yield BrowserTestUtils.withNewTab({ gBrowser: gBrowser, url: "about:blank" },
+    function* (newTabBrowser) {
+      yield waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf");
 
-  tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf");
-  var newTabBrowser = gBrowser.getBrowserForTab(tab);
-  newTabBrowser.addEventListener("load", function eventHandler() {
-    newTabBrowser.removeEventListener("load", eventHandler, true);
-
-    var document = newTabBrowser.contentDocument,
-        window = newTabBrowser.contentWindow;
+      ok(gBrowser.isFindBarInitialized(), "Browser FindBar initialized!");
 
-    // Runs tests after all 'load' event handlers have fired off
-    window.addEventListener("documentload", function() {
-      runTests(document, window, tab, function () {
-        closePDFViewer(window, finish);
-      });
-    }, false, true);
-  }, true);
-}
-
-
-function runTests(document, window, tab, callback) {
+      yield ContentTask.spawn(newTabBrowser, null, function* () {
+        //
+        // Overall sanity tests
+        //
+        ok(content.document.querySelector('div#viewer'), "document content has viewer UI");
+        ok('PDFJS' in content.wrappedJSObject, "window content has PDFJS object");
 
-  //
-  // Overall sanity tests
-  //
-  ok(document.querySelector('div#viewer'), "document content has viewer UI");
-  ok('PDFJS' in window.wrappedJSObject, "window content has PDFJS object");
-  ok('PDFViewerApplication' in window.wrappedJSObject,
-     "window content has viewer object");
+        //
+        // Sidebar: open
+        //
+        var sidebar = content.document.querySelector('button#sidebarToggle'),
+            outerContainer = content.document.querySelector('div#outerContainer');
 
-  //
-  // Browser Find
-  //
-  ok(gBrowser.isFindBarInitialized(tab), "Browser FindBar initialized!");
+        sidebar.click();
+        ok(outerContainer.classList.contains('sidebarOpen'), "sidebar opens on click");
 
-  //
-  // Sidebar: open
-  //
-  var sidebar = document.querySelector('button#sidebarToggle'),
-      outerContainer = document.querySelector('div#outerContainer');
-
-  sidebar.click();
-  ok(outerContainer.classList.contains('sidebarOpen'), 'sidebar opens on click');
+        //
+        // Sidebar: close
+        //
+        sidebar.click();
+        ok(!outerContainer.classList.contains('sidebarOpen'), "sidebar closes on click");
 
-  //
-  // Sidebar: close
-  //
-  sidebar.click();
-  ok(!outerContainer.classList.contains('sidebarOpen'), 'sidebar closes on click');
-
-  //
-  // Page change from prev/next buttons
-  //
-  var prevPage = document.querySelector('button#previous'),
-      nextPage = document.querySelector('button#next');
+        //
+        // Page change from prev/next buttons
+        //
+        var prevPage = content.document.querySelector('button#previous'),
+            nextPage = content.document.querySelector('button#next');
 
-  var pageNumber = document.querySelector('input#pageNumber');
-  is(parseInt(pageNumber.value), 1, 'initial page is 1');
-
-  //
-  // Bookmark button
-  //
-  var viewBookmark = document.querySelector('a#viewBookmark');
-  viewBookmark.click();
-  ok(viewBookmark.href.length > 0, 'viewBookmark button has href');
+        var pgNumber = content.document.querySelector('input#pageNumber').value;
+        is(parseInt(pgNumber, 10), 1, 'initial page is 1');
 
-  callback();
-}
+        //
+        // Bookmark button
+        //
+        var viewBookmark = content.document.querySelector('a#viewBookmark');
+        viewBookmark.click();
 
-/**
- * Destroys PDF.js viewer opened document.
- */
-function closePDFViewer(window, callback) {
-  var viewer = window.wrappedJSObject.PDFViewerApplication;
-  viewer.close().then(callback);
-}
+        ok(viewBookmark.href.length > 0, "viewBookmark button has href");
+
+        var viewer = content.wrappedJSObject.PDFViewerApplication;
+        yield viewer.close();
+      });
+    });
+});
--- a/browser/extensions/pdfjs/test/browser_pdfjs_navigation.js
+++ b/browser/extensions/pdfjs/test/browser_pdfjs_navigation.js
@@ -134,172 +134,148 @@ const TESTS = [
       value: 5,
       event: "change"
     },
     expectedPage: 5,
     message: "navigated to 5th page using pagenumber"
   }
 ];
 
-function test() {
-  var tab;
-
+add_task(function* test() {
   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
   let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
 
   // Make sure pdf.js is the default handler.
   is(handlerInfo.alwaysAskBeforeHandling, false, 'pdf handler defaults to always-ask is false');
   is(handlerInfo.preferredAction, Ci.nsIHandlerInfo.handleInternally, 'pdf handler defaults to internal');
 
   info('Pref action: ' + handlerInfo.preferredAction);
 
-  waitForExplicitFinish();
-  registerCleanupFunction(function() {
-    gBrowser.removeTab(tab);
-  });
-
-  tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf");
-  gBrowser.selectedTab = tab;
+  yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:blank" },
+    function* (newTabBrowser) {
+      yield waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf");
 
-  var newTabBrowser = gBrowser.getBrowserForTab(tab);
-  newTabBrowser.addEventListener("load", function eventHandler() {
-    newTabBrowser.removeEventListener("load", eventHandler, true);
-
-    var document = newTabBrowser.contentDocument,
-        window = newTabBrowser.contentWindow;
+      yield ContentTask.spawn(newTabBrowser, null, function* () {
+        // Check if PDF is opened with internal viewer
+        ok(content.document.querySelector('div#viewer'), "document content has viewer UI");
+        ok('PDFJS' in content.wrappedJSObject, "window content has PDFJS object");
+      });
 
-    // Runs tests after all 'load' event handlers have fired off
-    window.addEventListener("documentload", function() {
-      runTests(document, window, function () {
-        var pageNumber = document.querySelector('input#pageNumber');
+      yield ContentTask.spawn(newTabBrowser, null, contentSetUp);
+
+      yield Task.spawn(runTests(newTabBrowser));
+
+      yield ContentTask.spawn(newTabBrowser, null, function*() {
+        let pageNumber = content.document.querySelector('input#pageNumber');
         is(pageNumber.value, pageNumber.max, "Document is left on the last page");
-        finish();
       });
-    }, false, true);
-  }, true);
-}
+    });
+});
 
-function runTests(document, window, finish) {
-  // Check if PDF is opened with internal viewer
-  ok(document.querySelector('div#viewer'), "document content has viewer UI");
-  ok('PDFJS' in window.wrappedJSObject, "window content has PDFJS object");
+function* contentSetUp() {
+  /**
+   * Outline Items gets appended to the document later on we have to
+   * wait for them before we start to navigate though document
+   *
+   * @param document
+   * @returns {deferred.promise|*}
+   */
+  function waitForOutlineItems(document) {
+    return new Promise((resolve, reject) => {
+      document.addEventListener("outlineloaded", function outlineLoaded(evt) {
+        document.removeEventListener("outlineloaded", outlineLoaded);
+        var outlineCount = evt.detail.outlineCount;
 
-  // Wait for outline items, the start the navigation actions
-  waitForOutlineItems(document).then(function () {
-    // The key navigation has to happen in page-fit, otherwise it won't scroll
-    // trough a complete page
-    setZoomToPageFit(document).then(function () {
-      runNextTest(document, window, finish);
-    }, function () {
-      ok(false, "Current scale has been set to 'page-fit'");
-      finish();
+        if (document.querySelectorAll(".outlineItem").length === outlineCount) {
+          resolve();
+        } else {
+          reject();
+        }
+      });
     });
-  }, function () {
-    ok(false, "Outline items have been found");
-    finish();
-  });
+  }
+
+  /**
+   * The key navigation has to happen in page-fit, otherwise it won't scroll
+   * through a complete page
+   *
+   * @param document
+   * @returns {deferred.promise|*}
+   */
+  function setZoomToPageFit(document) {
+    return new Promise((resolve) => {
+      document.addEventListener("pagerendered", function onZoom(e) {
+        document.removeEventListener("pagerendered", onZoom);
+        document.querySelector("#viewer").click();
+        resolve();
+      });
+
+      var select = document.querySelector("select#scaleSelect");
+      select.selectedIndex = 2;
+      select.dispatchEvent(new Event("change"));
+    });
+  }
+
+  yield waitForOutlineItems(content.document);
+  yield setZoomToPageFit(content.document);
 }
 
 /**
  * As the page changes asynchronously, we have to wait for the event after
  * we trigger the action so we will be at the expected page number after each action
  *
  * @param document
  * @param window
  * @param test
  * @param callback
  */
-function runNextTest(document, window, endCallback) {
-  var test = TESTS.shift(),
-      deferred = Promise.defer(),
-      pageNumber = document.querySelector('input#pageNumber');
+function* runTests(browser) {
+  yield ContentTask.spawn(browser, TESTS, function* (TESTS) {
+    let window = content;
+    let document = window.document;
+
+    for (let test of TESTS) {
+      let deferred = {};
+      deferred.promise = new Promise((resolve, reject) => {
+        deferred.resolve = resolve;
+        deferred.reject = reject;
+      });
 
-  // Add an event-listener to wait for page to change, afterwards resolve the promise
-  var timeout = window.setTimeout(() => deferred.reject(), 5000);
-  window.addEventListener('pagechange', function pageChange() {
-    if (pageNumber.value == test.expectedPage) {
-      window.removeEventListener('pagechange', pageChange);
-      window.clearTimeout(timeout);
-      deferred.resolve(pageNumber.value);
-    }
-  });
+      let pageNumber = document.querySelector('input#pageNumber');
 
-  // Get the element and trigger the action for changing the page
-  var el = document.querySelector(test.action.selector);
-  ok(el, "Element '" + test.action.selector + "' has been found");
-
-  // The value option is for input case
-  if (test.action.value)
-    el.value = test.action.value;
+      // Add an event-listener to wait for page to change, afterwards resolve the promise
+      let timeout = window.setTimeout(() => deferred.reject(), 5000);
+      window.addEventListener('pagechange', function pageChange() {
+        if (pageNumber.value == test.expectedPage) {
+          window.removeEventListener('pagechange', pageChange);
+          window.clearTimeout(timeout);
+          deferred.resolve(+pageNumber.value);
+        }
+      });
 
-  // Dispatch the event for changing the page
-  if (test.action.event == "keydown") {
-    var ev = document.createEvent("KeyboardEvent");
-        ev.initKeyEvent("keydown", true, true, null, false, false, false, false,
-                        test.action.keyCode, 0);
-    el.dispatchEvent(ev);
-  }
-  else {
-    var ev = new Event(test.action.event);
-  }
-  el.dispatchEvent(ev);
+      // Get the element and trigger the action for changing the page
+      var el = document.querySelector(test.action.selector);
+      ok(el, "Element '" + test.action.selector + "' has been found");
 
+      // The value option is for input case
+      if (test.action.value)
+        el.value = test.action.value;
 
-  // When the promise gets resolved we call the next test if there are any left
-  // or else we call the final callback which will end the test
-  deferred.promise.then(function (pgNumber) {
-    is(pgNumber, test.expectedPage, test.message);
+      // Dispatch the event for changing the page
+      if (test.action.event == "keydown") {
+        var ev = document.createEvent("KeyboardEvent");
+            ev.initKeyEvent("keydown", true, true, null, false, false, false, false,
+                            test.action.keyCode, 0);
+        el.dispatchEvent(ev);
+      }
+      else {
+        var ev = new Event(test.action.event);
+      }
+      el.dispatchEvent(ev);
 
-    if (TESTS.length)
-      runNextTest(document, window, endCallback);
-    else
-      endCallback();
-  }, function () {
-    ok(false, "Test '" + test.message + "' failed with timeout.");
-    endCallback();
+      let pgNumber = yield deferred.promise;
+      is(pgNumber, test.expectedPage, test.message);
+    }
+
+    var viewer = content.wrappedJSObject.PDFViewerApplication;
+    yield viewer.close();
   });
 }
-
-/**
- * Outline Items gets appended to the document latter on we have to
- * wait for them before we start to navigate though document
- *
- * @param document
- * @returns {deferred.promise|*}
- */
-function waitForOutlineItems(document) {
-  var deferred = Promise.defer();
-  document.addEventListener("outlineloaded", function outlineLoaded(evt) {
-    document.removeEventListener("outlineloaded", outlineLoaded);
-    var outlineCount = evt.detail.outlineCount;
-
-    if (document.querySelectorAll(".outlineItem").length === outlineCount) {
-      deferred.resolve();
-    } else {
-      deferred.reject();
-    }
-  });
-
-  return deferred.promise;
-}
-
-/**
- * The key navigation has to happen in page-fit, otherwise it won't scroll
- * trough a complete page
- *
- * @param document
- * @returns {deferred.promise|*}
- */
-function setZoomToPageFit(document) {
-  var deferred = Promise.defer();
-  document.addEventListener("pagerendered", function onZoom(e) {
-    document.removeEventListener("pagerendered", onZoom);
-    document.querySelector("#viewer").click();
-    deferred.resolve();
-  });
-
-  var select = document.querySelector("select#scaleSelect");
-  select.selectedIndex = 2;
-  select.dispatchEvent(new Event("change"));
-
-  return deferred.promise;
-}
-
--- a/browser/extensions/pdfjs/test/browser_pdfjs_views.js
+++ b/browser/extensions/pdfjs/test/browser_pdfjs_views.js
@@ -1,86 +1,61 @@
 /* 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() {
-  var tab;
-
+add_task(function* test() {
   let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
   let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
 
   // Make sure pdf.js is the default handler.
   is(handlerInfo.alwaysAskBeforeHandling, false, 'pdf handler defaults to always-ask is false');
   is(handlerInfo.preferredAction, Ci.nsIHandlerInfo.handleInternally, 'pdf handler defaults to internal');
 
   info('Pref action: ' + handlerInfo.preferredAction);
 
-  waitForExplicitFinish();
-  registerCleanupFunction(function() {
-    gBrowser.removeTab(tab);
-  });
+  yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:blank" },
+    function* (browser) {
+      // check that PDF is opened with internal viewer
+      yield waitForPdfJS(browser, TESTROOT + "file_pdfjs_test.pdf");
 
-  tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf");
-  var newTabBrowser = gBrowser.getBrowserForTab(tab);
-  newTabBrowser.addEventListener("load", function eventHandler() {
-    newTabBrowser.removeEventListener("load", eventHandler, true);
-
-    var document = newTabBrowser.contentDocument,
-        window = newTabBrowser.contentWindow;
+      yield ContentTask.spawn(browser, null, function* () {
+        ok(content.document.querySelector('div#viewer'), "document content has viewer UI");
+        ok('PDFJS' in content.wrappedJSObject, "window content has PDFJS object");
 
-    // Runs tests after all 'load' event handlers have fired off
-    window.addEventListener("documentload", function() {
-      runTests(document, window, function () {
-        closePDFViewer(window, finish);
-      });
-    }, false, true);
-  }, true);
-}
+        //open sidebar
+        var sidebar = content.document.querySelector('button#sidebarToggle');
+        var outerContainer = content.document.querySelector('div#outerContainer');
 
-function runTests(document, window, callback) {
-  // check that PDF is opened with internal viewer
-  ok(document.querySelector('div#viewer'), "document content has viewer UI");
-  ok('PDFJS' in window.wrappedJSObject, "window content has PDFJS object");
+        sidebar.click();
+        ok(outerContainer.classList.contains('sidebarOpen'), 'sidebar opens on click');
 
-  //open sidebar
-  var sidebar = document.querySelector('button#sidebarToggle');
-  var outerContainer = document.querySelector('div#outerContainer');
-
-  sidebar.click();
-  ok(outerContainer.classList.contains('sidebarOpen'), 'sidebar opens on click');
+        // check that thumbnail view is open
+        var thumbnailView = content.document.querySelector('div#thumbnailView');
+        var outlineView = content.document.querySelector('div#outlineView');
 
-  // check that thumbnail view is open
-  var thumbnailView = document.querySelector('div#thumbnailView');
-  var outlineView = document.querySelector('div#outlineView');
-
-  is(thumbnailView.getAttribute('class'), null, 'Initial view is thumbnail view');
-  is(outlineView.getAttribute('class'), 'hidden', 'Outline view is hidden initially');
+        is(thumbnailView.getAttribute('class'), null, 'Initial view is thumbnail view');
+        is(outlineView.getAttribute('class'), 'hidden', 'Outline view is hidden initially');
 
-  //switch to outline view
-  var viewOutlineButton = document.querySelector('button#viewOutline');
-  viewOutlineButton.click();
+        //switch to outline view
+        var viewOutlineButton = content.document.querySelector('button#viewOutline');
+        viewOutlineButton.click();
 
-  is(outlineView.getAttribute('class'), '', 'Outline view is visible when selected');
-  is(thumbnailView.getAttribute('class'), 'hidden', 'Thumbnail view is hidden when outline is selected');
+        is(thumbnailView.getAttribute('class'), 'hidden', 'Thumbnail view is hidden when outline is selected');
+        is(outlineView.getAttribute('class'), '', 'Outline view is visible when selected');
 
-  //switch back to thumbnail view
-  var viewThumbnailButton = document.querySelector('button#viewThumbnail');
-  viewThumbnailButton.click();
+        //switch back to thumbnail view
+        var viewThumbnailButton = content.document.querySelector('button#viewThumbnail');
+        viewThumbnailButton.click();
 
-  is(thumbnailView.getAttribute('class'), '', 'Thumbnail view is visible when selected');
-  is(outlineView.getAttribute('class'), 'hidden', 'Outline view is hidden when thumbnail is selected');
-
-  sidebar.click();
+        is(thumbnailView.getAttribute('class'), '', 'Thumbnail view is visible when selected');
+        is(outlineView.getAttribute('class'), 'hidden', 'Outline view is hidden when thumbnail is selected');
 
-  callback();
-}
+        sidebar.click();
 
-/**
- * Destroys PDF.js viewer opened document.
- */
-function closePDFViewer(window, callback) {
-  var viewer = window.wrappedJSObject.PDFViewerApplication;
-  viewer.close().then(callback);
-}
+        var viewer = content.wrappedJSObject.PDFViewerApplication;
+        yield viewer.close();
+      });
+    });
+});
--- a/browser/extensions/pdfjs/test/browser_pdfjs_zoom.js
+++ b/browser/extensions/pdfjs/test/browser_pdfjs_zoom.js
@@ -23,25 +23,27 @@ const TESTS = [
     },
     expectedZoom: -1, // -1 - zoom out
     message: "Zoomed out using the '-' (zoom out) button"
   },
 
   {
     action: {
       keyboard: true,
+      keyCode: 61,
       event: "+"
     },
     expectedZoom: 1, // 1 - zoom in
     message: "Zoomed in using the CTRL++ keys"
   },
 
   {
     action: {
       keyboard: true,
+      keyCode: 109,
       event: "-"
     },
     expectedZoom: -1, // -1 - zoom out
     message: "Zoomed out using the CTRL+- keys"
   },
 
   {
     action: {
@@ -49,137 +51,102 @@ const TESTS = [
       index: 5,
       event: "change"
     },
     expectedZoom: -1, // -1 - zoom out
     message: "Zoomed using the zoom picker"
   }
 ];
 
-var initialWidth; // the initial width of the PDF document
-var previousWidth; // the width of the PDF document at previous step/test
-
-function test() {
-  var tab;
+add_task(function* test() {
   let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"]
                        .getService(Ci.nsIHandlerService);
   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
   let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
 
   // Make sure pdf.js is the default handler.
   is(handlerInfo.alwaysAskBeforeHandling, false,
      'pdf handler defaults to always-ask is false');
   is(handlerInfo.preferredAction, Ci.nsIHandlerInfo.handleInternally,
     'pdf handler defaults to internal');
 
   info('Pref action: ' + handlerInfo.preferredAction);
 
-  waitForExplicitFinish();
-  registerCleanupFunction(function() {
-    gBrowser.removeTab(tab);
-  });
+  yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:blank" },
+    function* (newTabBrowser) {
+      yield waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf" + "#zoom=100");
 
-  tab = gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf");
-  var newTabBrowser = gBrowser.getBrowserForTab(tab);
-
-  newTabBrowser.addEventListener("load", function eventHandler() {
-    newTabBrowser.removeEventListener("load", eventHandler, true);
+      yield ContentTask.spawn(newTabBrowser, TESTS, function* (TESTS) {
+        let document = content.document;
 
-    var document = newTabBrowser.contentDocument,
-        window = newTabBrowser.contentWindow;
+        function waitForRender() {
+          return new Promise((resolve) => {
+            document.addEventListener("pagerendered", function onPageRendered(e) {
+              if(e.detail.pageNumber !== 1) {
+                return;
+              }
 
-    // Runs tests after all 'load' event handlers have fired off
-    window.addEventListener("documentload", function() {
-      initialWidth = parseInt(document.querySelector("div#pageContainer1").style.width);
-      previousWidth = initialWidth;
-      runTests(document, window, function () {
-        closePDFViewer(window, finish);
-      });
-    }, false, true);
-  }, true);
-}
+              document.removeEventListener("pagerendered", onPageRendered, true);
+              resolve();
+            }, true);
+          });
+        }
 
-function runTests(document, window, callback) {
-  // check that PDF is opened with internal viewer
-  ok(document.querySelector('div#viewer'), "document content has viewer UI");
-  ok('PDFJS' in window.wrappedJSObject, "window content has PDFJS object");
+        // check that PDF is opened with internal viewer
+        ok(content.document.querySelector('div#viewer'), "document content has viewer UI");
+        ok('PDFJS' in content.wrappedJSObject, "window content has PDFJS object");
 
-  // Start the zooming tests after the document is loaded
-  waitForDocumentLoad(document).then(function () {
-    zoomPDF(document, window, TESTS.shift(), callback);
-  });
-}
+        let initialWidth, previousWidth;
+        initialWidth = previousWidth =
+          parseInt(content.document.querySelector("div#pageContainer1").style.width);
 
-function waitForDocumentLoad(document) {
-  var deferred = Promise.defer();
-  var interval = setInterval(function () {
-    if (document.querySelector("div#pageContainer1") != null){
-      clearInterval(interval);
-      deferred.resolve();
-    }
-  }, 500);
+        for (let test of TESTS) {
+          // We zoom using an UI element
+          var ev;
+          if (test.action.selector) {
+            // Get the element and trigger the action for changing the zoom
+            var el = document.querySelector(test.action.selector);
+            ok(el, "Element '" + test.action.selector + "' has been found");
 
-  return deferred.promise;
-}
-
-function zoomPDF(document, window, test, endCallback) {
-  var renderedPage;
-
-  document.addEventListener("pagerendered", function onPageRendered(e) {
-    if(e.detail.pageNumber !== 1) {
-      return;
-    }
+            if (test.action.index){
+              el.selectedIndex = test.action.index;
+            }
 
-    document.removeEventListener("pagerendered", onPageRendered, true);
-
-    var pageZoomScale = document.querySelector('select#scaleSelect');
-
-    // The zoom value displayed in the zoom select
-    var zoomValue = pageZoomScale.options[pageZoomScale.selectedIndex].innerHTML;
-
-    let pageContainer = document.querySelector('div#pageContainer1');
-    let actualWidth  = parseInt(pageContainer.style.width);
-
-    // the actual zoom of the PDF document
-    let computedZoomValue = parseInt(((actualWidth/initialWidth).toFixed(2))*100) + "%";
-    is(computedZoomValue, zoomValue, "Content has correct zoom");
+            // Dispatch the event for changing the zoom
+            ev = new Event(test.action.event);
+          }
+          // We zoom using keyboard
+          else {
+            // Simulate key press
+            ev = new content.KeyboardEvent("keydown",
+                                           { key: test.action.event,
+                                             keyCode: test.action.keyCode,
+                                             ctrlKey: true });
+            el = content;
+          }
 
-    // Check that document zooms in the expected way (in/out)
-    let zoom = (actualWidth - previousWidth) * test.expectedZoom;
-    ok(zoom > 0, test.message);
+          el.dispatchEvent(ev);
+          yield waitForRender();
 
-    // Go to next test (if there is any) or finish
-    var nextTest = TESTS.shift();
-    if (nextTest) {
-      previousWidth = actualWidth;
-      zoomPDF(document, window, nextTest, endCallback);
-    }
-    else
-      endCallback();
-  }, true);
+          var pageZoomScale = content.document.querySelector('select#scaleSelect');
 
-  // We zoom using an UI element
-  if (test.action.selector) {
-    // Get the element and trigger the action for changing the zoom
-    var el = document.querySelector(test.action.selector);
-    ok(el, "Element '" + test.action.selector + "' has been found");
+          // The zoom value displayed in the zoom select
+          var zoomValue = pageZoomScale.options[pageZoomScale.selectedIndex].innerHTML;
 
-    if (test.action.index){
-      el.selectedIndex = test.action.index;
-    }
+          let pageContainer = content.document.querySelector('div#pageContainer1');
+          let actualWidth = parseInt(pageContainer.style.width);
+
+          // the actual zoom of the PDF document
+          let computedZoomValue = parseInt(((actualWidth/initialWidth).toFixed(2))*100) + "%";
+          is(computedZoomValue, zoomValue, "Content has correct zoom");
 
-    // Dispatch the event for changing the zoom
-    el.dispatchEvent(new Event(test.action.event));
-  }
-  // We zoom using keyboard
-  else {
-    // Simulate key press
-    EventUtils.synthesizeKey(test.action.event, { ctrlKey: true });
-  }
-}
+          // Check that document zooms in the expected way (in/out)
+          let zoom = (actualWidth - previousWidth) * test.expectedZoom;
+          ok(zoom > 0, test.message);
 
-/**
- * Destroys PDF.js viewer opened document.
- */
-function closePDFViewer(window, callback) {
-  var viewer = window.wrappedJSObject.PDFViewerApplication;
-  viewer.close().then(callback);
-}
+          previousWidth = actualWidth;
+        }
+
+        var viewer = content.wrappedJSObject.PDFViewerApplication;
+        yield viewer.close();
+      });
+    });
+});
new file mode 100644
--- /dev/null
+++ b/browser/extensions/pdfjs/test/head.js
@@ -0,0 +1,15 @@
+function waitForPdfJS(browser, url) {
+  // Runs tests after all 'load' event handlers have fired off
+  return ContentTask.spawn(browser, url, function* (url) {
+    yield new Promise((resolve) => {
+      // NB: Add the listener to the global object so that we receive the
+      // event fired from the new window.
+      addEventListener("documentload", function listener() {
+        removeEventListener("documentload", listener, false);
+        resolve();
+      }, false, true);
+
+      content.location = url;
+    });
+  });
+}
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -332,17 +332,17 @@ this.BrowserTestUtils = {
       win.close();
     });
   },
 
   /**
    * Waits for an event to be fired on a specified element.
    *
    * Usage:
-   *    let promiseEvent = BrowserTestUtil.waitForEvent(element, "eventName");
+   *    let promiseEvent = BrowserTestUtils.waitForEvent(element, "eventName");
    *    // Do some processing here that will cause the event to be fired
    *    // ...
    *    // Now yield until the Promise is fulfilled
    *    let receivedEvent = yield promiseEvent;
    *
    * @param {Element} subject
    *        The element that should receive the event.
    * @param {string} eventName