1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3 /* eslint-disable mozilla/no-arbitrary-setTimeout */
5 requestLongerTimeout(2);
7 const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
8 const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
10 const PDF_OUTLINE_ITEMS = 17;
14 selector: "button#next",
18 message: "navigated to next page using NEXT button",
22 selector: "button#previous",
26 message: "navigated to previous page using PREV button",
30 selector: "button#next",
34 message: "navigated to next page using NEXT button",
38 selector: "input#pageNumber",
43 message: "navigated to first page using pagenumber",
47 selector: "#thumbnailView a:nth-child(4)",
51 message: "navigated to 4th page using thumbnail view",
55 selector: "#thumbnailView a:nth-child(2)",
59 message: "navigated to 2nd page using thumbnail view",
68 message: "navigated to 1st page using 'home' key",
77 message: "navigated to 2nd page using 'Page Down' key",
86 message: "navigated to 1st page using 'Page Up' key",
95 message: "navigated to 2nd page using 'right' key",
104 message: "navigated to 1st page using 'left' key",
113 message: "navigated to last page using 'home' key",
117 selector: "#outlineView .treeItem:nth-child(1) a",
121 message: "navigated to 1st page using outline view",
125 selector: "#outlineView .treeItem:nth-child(" + PDF_OUTLINE_ITEMS + ") a",
129 message: "navigated to 4th page using outline view",
133 selector: "input#pageNumber",
138 message: "navigated to 5th page using pagenumber",
142 add_task(async function test() {
143 let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
144 let handlerInfo = mimeService.getFromTypeAndExtension(
149 // Make sure pdf.js is the default handler.
151 handlerInfo.alwaysAskBeforeHandling,
153 "pdf handler defaults to always-ask is false"
156 handlerInfo.preferredAction,
157 Ci.nsIHandlerInfo.handleInternally,
158 "pdf handler defaults to internal"
161 info("Pref action: " + handlerInfo.preferredAction);
163 await BrowserTestUtils.withNewTab(
164 { gBrowser, url: "about:blank" },
165 async function (newTabBrowser) {
166 // Outline Items gets appended to the document later on we have to
167 // wait for them before we start to navigate though document
168 const outlinePromise = new Promise(resolve => {
169 BrowserTestUtils.addContentEventListener(
173 { capture: false, once: true, wantUntrusted: true }
177 info("Wait for document to load");
178 await waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf");
180 info("Wait for outline items");
181 await outlinePromise;
183 await SpecialPowers.spawn(newTabBrowser, [], async function () {
184 // Check if PDF is opened with internal viewer.
186 content.document.querySelector("div#viewer"),
187 "document content has viewer UI"
191 info("Wait for zoom to page fit");
192 await SpecialPowers.spawn(
196 * The key navigation has to happen in page-fit, otherwise it won't scroll
197 * through a complete page
200 * @returns {deferred.promise|*}
202 function setZoomToPageFit() {
203 const { document
} = content
;
204 return new Promise(resolve
=> {
205 document
.addEventListener(
208 document
.querySelector("#viewer").click();
214 const select
= document
.querySelector("select#scaleSelect");
215 select
.selectedIndex
= 2;
216 select
.dispatchEvent(new content
.Event("change"));
221 info("PDF loaded successfully: run tests");
222 await
runTests(newTabBrowser
);
224 await SpecialPowers
.spawn(newTabBrowser
, [], async
function () {
225 let pageNumber
= content
.document
.querySelector("input#pageNumber");
229 "Document is left on the last page"
237 * As the page changes asynchronously, we have to wait for the event after
238 * we trigger the action so we will be at the expected page number after each action
245 async
function runTests(browser
) {
246 await SpecialPowers
.spawn(browser
, [TESTS
], async
function (contentTESTS
) {
247 let window
= content
;
248 let document
= window
.document
;
250 for (let test
of contentTESTS
) {
252 deferred
.promise
= new Promise((resolve
, reject
) => {
253 deferred
.resolve
= resolve
;
254 deferred
.reject
= reject
;
257 let pageNumber
= document
.querySelector("input#pageNumber");
259 // Add an event-listener to wait for page to change, afterwards resolve the promise
260 let timeout
= window
.setTimeout(() => deferred
.reject(), 5000);
261 window
.addEventListener("pagechanging", function pageChange() {
262 if (pageNumber
.value
== test
.expectedPage
) {
263 window
.removeEventListener("pagechanging", pageChange
);
264 window
.clearTimeout(timeout
);
265 deferred
.resolve(+pageNumber
.value
);
269 // Get the element and trigger the action for changing the page
270 var el
= document
.querySelector(test
.action
.selector
);
271 Assert
.ok(el
, "Element '" + test
.action
.selector
+ "' has been found");
273 // The value option is for input case
274 if (test
.action
.value
) {
275 el
.value
= test
.action
.value
;
278 // Dispatch the event for changing the page
280 if (test
.action
.event
== "keydown") {
281 ev
= new window
.KeyboardEvent("keydown", {
285 keyCode
: test
.action
.keyCode
,
288 el
.dispatchEvent(ev
);
290 ev
= new content
.Event(test
.action
.event
);
292 el
.dispatchEvent(ev
);
294 let pgNumber
= await deferred
.promise
;
295 Assert
.equal(pgNumber
, test
.expectedPage
, test
.message
);
299 await
waitForPdfJSClose(browser
);