Bug 1098246 - Enhance browser_pdfjs_navigation.js to navigate using arrows keys. r=mossop
authorCosmin Malutan <cosmin.malutan@softvision.ro>
Tue, 09 Dec 2014 06:29:00 +0100
changeset 219489 80f3ab1c6cf230969248a57b0435b8d8c37dac72
parent 219488 8132d858fefec2c174859d6e348226f416232d53
child 219490 9a4e416213ccb65946c03b518cc789610566063c
push idunknown
push userunknown
push dateunknown
reviewersmossop
bugs1098246
milestone37.0a1
Bug 1098246 - Enhance browser_pdfjs_navigation.js to navigate using arrows keys. r=mossop
browser/extensions/pdfjs/test/browser_pdfjs_navigation.js
--- a/browser/extensions/pdfjs/test/browser_pdfjs_navigation.js
+++ b/browser/extensions/pdfjs/test/browser_pdfjs_navigation.js
@@ -55,16 +55,70 @@ const TESTS = [
       selector: "#thumbnailView a:nth-child(2)",
       event: "click"
     },
     expectedPage: 2,
     message: "navigated to 2nd page using thumbnail view"
   },
   {
     action: {
+      selector: "#viewer",
+      event: "keydown",
+      keyCode: 36
+    },
+    expectedPage: 1,
+    message: "navigated to 1st page using 'home' key"
+  },
+  {
+    action: {
+      selector: "#viewer",
+      event: "keydown",
+      keyCode: 34
+    },
+    expectedPage: 2,
+    message: "navigated to 2nd page using 'Page Down' key"
+  },
+  {
+    action: {
+      selector: "#viewer",
+      event: "keydown",
+      keyCode: 33
+    },
+    expectedPage: 1,
+    message: "navigated to 1st page using 'Page Up' key"
+  },
+  {
+    action: {
+      selector: "#viewer",
+      event: "keydown",
+      keyCode: 39
+    },
+    expectedPage: 2,
+    message: "navigated to 2nd page using 'right' key"
+  },
+  {
+    action: {
+      selector: "#viewer",
+      event: "keydown",
+      keyCode: 37
+    },
+    expectedPage: 1,
+    message: "navigated to 1st page using 'left' key"
+  },
+  {
+    action: {
+      selector: "#viewer",
+      event: "keydown",
+      keyCode: 35
+    },
+    expectedPage: 5,
+    message: "navigated to last page using 'home' key"
+  },
+  {
+    action: {
       selector: ".outlineItem:nth-child(1) a",
       event: "click"
     },
     expectedPage: 1,
     message: "navigated to 1st page using outline view"
   },
   {
     action: {
@@ -125,17 +179,24 @@ function test() {
 
 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");
 
   // Wait for outline items, the start the navigation actions
   waitForOutlineItems(document).then(function () {
-    runNextTest(document, window, finish);
+    // 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 ste to 'page-fit'");
+      finish();
+    });
   }, function () {
     ok(false, "Outline items have ben found");
     finish();
   });
 }
 
 /**
  * As the page changes asynchronously, we have to wait for the event after
@@ -165,17 +226,27 @@ function runNextTest(document, window, e
   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;
 
   // Dispatch the event for changing the page
-  el.dispatchEvent(new Event(test.action.event));
+  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);
+
 
   // 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);
 
     if (TESTS.length)
       runNextTest(document, window, endCallback);
@@ -202,8 +273,32 @@ function waitForOutlineItems(document) {
       clearInterval(interval);
       clearTimeout(timeout);
       deferred.resolve();
     }
   }, 500);
 
   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), false;
+    document.querySelector("#viewer").click();
+    deferred.resolve();
+
+  }, false);
+
+  var select = document.querySelector("select#scaleSelect");
+  select.selectedIndex = 2;
+  select.dispatchEvent(new Event("change"));
+
+  return deferred.promise;
+}
+