Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_navigation.js
blob9616f078203be548470e127e9eb746840cf31944
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;
11 const TESTS = [
13 action: {
14 selector: "button#next",
15 event: "click",
17 expectedPage: 2,
18 message: "navigated to next page using NEXT button",
21 action: {
22 selector: "button#previous",
23 event: "click",
25 expectedPage: 1,
26 message: "navigated to previous page using PREV button",
29 action: {
30 selector: "button#next",
31 event: "click",
33 expectedPage: 2,
34 message: "navigated to next page using NEXT button",
37 action: {
38 selector: "input#pageNumber",
39 value: 1,
40 event: "change",
42 expectedPage: 1,
43 message: "navigated to first page using pagenumber",
46 action: {
47 selector: "#thumbnailView a:nth-child(4)",
48 event: "click",
50 expectedPage: 4,
51 message: "navigated to 4th page using thumbnail view",
54 action: {
55 selector: "#thumbnailView a:nth-child(2)",
56 event: "click",
58 expectedPage: 2,
59 message: "navigated to 2nd page using thumbnail view",
62 action: {
63 selector: "#viewer",
64 event: "keydown",
65 keyCode: 36,
67 expectedPage: 1,
68 message: "navigated to 1st page using 'home' key",
71 action: {
72 selector: "#viewer",
73 event: "keydown",
74 keyCode: 34,
76 expectedPage: 2,
77 message: "navigated to 2nd page using 'Page Down' key",
80 action: {
81 selector: "#viewer",
82 event: "keydown",
83 keyCode: 33,
85 expectedPage: 1,
86 message: "navigated to 1st page using 'Page Up' key",
89 action: {
90 selector: "#viewer",
91 event: "keydown",
92 keyCode: 39,
94 expectedPage: 2,
95 message: "navigated to 2nd page using 'right' key",
98 action: {
99 selector: "#viewer",
100 event: "keydown",
101 keyCode: 37,
103 expectedPage: 1,
104 message: "navigated to 1st page using 'left' key",
107 action: {
108 selector: "#viewer",
109 event: "keydown",
110 keyCode: 35,
112 expectedPage: 5,
113 message: "navigated to last page using 'home' key",
116 action: {
117 selector: "#outlineView .treeItem:nth-child(1) a",
118 event: "click",
120 expectedPage: 1,
121 message: "navigated to 1st page using outline view",
124 action: {
125 selector: "#outlineView .treeItem:nth-child(" + PDF_OUTLINE_ITEMS + ") a",
126 event: "click",
128 expectedPage: 4,
129 message: "navigated to 4th page using outline view",
132 action: {
133 selector: "input#pageNumber",
134 value: 5,
135 event: "change",
137 expectedPage: 5,
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(
145 "application/pdf",
146 "pdf"
149 // Make sure pdf.js is the default handler.
151 handlerInfo.alwaysAskBeforeHandling,
152 false,
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(
170 newTabBrowser,
171 "outlineloaded",
172 resolve,
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.
185 Assert.ok(
186 content.document.querySelector("div#viewer"),
187 "document content has viewer UI"
191 info("Wait for zoom to page fit");
192 await SpecialPowers.spawn(
193 newTabBrowser,
196 * The key navigation has to happen in page-fit, otherwise it won't scroll
197 * through a complete page
199 * @param document
200 * @returns {deferred.promise|*}
202 function setZoomToPageFit() {
203 const { document } = content;
204 return new Promise(resolve => {
205 document.addEventListener(
206 "pagerendered",
207 function () {
208 document.querySelector("#viewer").click();
209 resolve();
211 { once: true }
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");
226 Assert.equal(
227 pageNumber.value,
228 pageNumber.max,
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
240 * @param document
241 * @param window
242 * @param test
243 * @param callback
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) {
251 let deferred = {};
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
279 var ev;
280 if (test.action.event == "keydown") {
281 ev = new window.KeyboardEvent("keydown", {
282 bubbles: true,
283 cancelable: true,
284 view: null,
285 keyCode: test.action.keyCode,
286 charCode: 0,
288 el.dispatchEvent(ev);
289 } else {
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);