Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_zoom.js
blob9cac0794a6bc3985f51c7b2e58b133fab72a8636
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 requestLongerTimeout(2);
6 const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
7 const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
9 const TESTS = [
10   {
11     action: {
12       selector: "#zoomInButton",
13       event: "click",
14     },
15     expectedZoom: 1, // 1 - zoom in
16     message: "Zoomed in using the '+' (zoom in) button",
17   },
19   {
20     action: {
21       selector: "#zoomOutButton",
22       event: "click",
23     },
24     expectedZoom: -1, // -1 - zoom out
25     message: "Zoomed out using the '-' (zoom out) button",
26   },
28   {
29     action: {
30       keyboard: true,
31       keyCode: 61,
32       event: "+",
33     },
34     expectedZoom: 1, // 1 - zoom in
35     message: "Zoomed in using the CTRL++ keys",
36   },
38   {
39     action: {
40       keyboard: true,
41       keyCode: 109,
42       event: "-",
43     },
44     expectedZoom: -1, // -1 - zoom out
45     message: "Zoomed out using the CTRL+- keys",
46   },
48   {
49     action: {
50       selector: "select#scaleSelect",
51       index: 5,
52       event: "change",
53     },
54     expectedZoom: -1, // -1 - zoom out
55     message: "Zoomed using the zoom picker",
56   },
59 add_task(async function test() {
60   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
61   let handlerInfo = mimeService.getFromTypeAndExtension(
62     "application/pdf",
63     "pdf"
64   );
66   // Make sure pdf.js is the default handler.
67   is(
68     handlerInfo.alwaysAskBeforeHandling,
69     false,
70     "pdf handler defaults to always-ask is false"
71   );
72   is(
73     handlerInfo.preferredAction,
74     Ci.nsIHandlerInfo.handleInternally,
75     "pdf handler defaults to internal"
76   );
78   info("Pref action: " + handlerInfo.preferredAction);
80   await BrowserTestUtils.withNewTab(
81     { gBrowser, url: "about:blank" },
82     async function (newTabBrowser) {
83       await waitForPdfJS(
84         newTabBrowser,
85         TESTROOT + "file_pdfjs_test.pdf#zoom=100"
86       );
88       await SpecialPowers.spawn(
89         newTabBrowser,
90         [TESTS],
91         async function (contentTESTS) {
92           let document = content.document;
94           function waitForRender() {
95             return new Promise(resolve => {
96               document.addEventListener(
97                 "pagerendered",
98                 function onPageRendered(e) {
99                   if (e.detail.pageNumber !== 1) {
100                     return;
101                   }
103                   document.removeEventListener(
104                     "pagerendered",
105                     onPageRendered,
106                     true
107                   );
108                   resolve();
109                 },
110                 true
111               );
112             });
113           }
115           // check that PDF is opened with internal viewer
116           Assert.ok(
117             content.document.querySelector("div#viewer"),
118             "document content has viewer UI"
119           );
121           let initialWidth, previousWidth;
122           initialWidth = previousWidth = parseInt(
123             content.getComputedStyle(
124               content.document.querySelector("div.page[data-page-number='1']")
125             ).width
126           );
128           for (let subTest of contentTESTS) {
129             // We zoom using an UI element
130             var ev;
131             if (subTest.action.selector) {
132               // Get the element and trigger the action for changing the zoom
133               var el = document.querySelector(subTest.action.selector);
134               Assert.ok(
135                 el,
136                 "Element '" + subTest.action.selector + "' has been found"
137               );
139               if (subTest.action.index) {
140                 el.selectedIndex = subTest.action.index;
141               }
143               // Dispatch the event for changing the zoom
144               ev = new content.Event(subTest.action.event);
145             } else {
146               // We zoom using keyboard
147               // Simulate key press
148               ev = new content.KeyboardEvent("keydown", {
149                 key: subTest.action.event,
150                 keyCode: subTest.action.keyCode,
151                 ctrlKey: true,
152               });
153               el = content;
154             }
156             el.dispatchEvent(ev);
157             await waitForRender();
159             var pageZoomScale =
160               content.document.querySelector("select#scaleSelect");
162             // The zoom value displayed in the zoom select
163             var zoomValue =
164               pageZoomScale.options[pageZoomScale.selectedIndex].innerHTML;
166             let pageContainer = content.document.querySelector(
167               "div.page[data-page-number='1']"
168             );
169             let actualWidth = parseInt(
170               content.getComputedStyle(pageContainer).width
171             );
173             // the actual zoom of the PDF document
174             let computedZoomValue =
175               parseInt((actualWidth / initialWidth).toFixed(2) * 100) + "%";
176             Assert.equal(
177               computedZoomValue,
178               zoomValue,
179               "Content has correct zoom"
180             );
182             // Check that document zooms in the expected way (in/out)
183             let zoom = (actualWidth - previousWidth) * subTest.expectedZoom;
184             Assert.ok(zoom > 0, subTest.message);
186             previousWidth = actualWidth;
187           }
188         }
189       );
190       await waitForPdfJSClose(newTabBrowser);
191     }
192   );
195 // Performs a SpecialPowers.spawn round-trip to ensure that any setup
196 // that needs to be done in the content process by any pending tasks has
197 // a chance to complete before continuing.
198 function waitForRoundTrip(browser) {
199   return SpecialPowers.spawn(browser, [], () => {});
202 async function waitForRenderAndGetWidth(newTabBrowser) {
203   return SpecialPowers.spawn(newTabBrowser, [], async function () {
204     function waitForRender(document) {
205       return new Promise(resolve => {
206         document.addEventListener(
207           "pagerendered",
208           function onPageRendered(e) {
209             if (e.detail.pageNumber !== 1) {
210               return;
211             }
213             document.removeEventListener("pagerendered", onPageRendered, true);
214             resolve();
215           },
216           true
217         );
218       });
219     }
220     // check that PDF is opened with internal viewer
221     Assert.ok(
222       content.document.querySelector("div#viewer"),
223       "document content has viewer UI"
224     );
226     await waitForRender(content.document);
228     return parseInt(
229       content.getComputedStyle(
230         content.document.querySelector("div.page[data-page-number='1']")
231       ).width
232     );
233   });
236 add_task(async function test_browser_zoom() {
237   await BrowserTestUtils.withNewTab(
238     { gBrowser, url: "about:blank" },
239     async function (newTabBrowser) {
240       const promise = waitForPdfJS(
241         newTabBrowser,
242         TESTROOT + "file_pdfjs_test.pdf"
243       );
244       await BrowserTestUtils.waitForContentEvent(
245         newTabBrowser,
246         "documentloaded",
247         false,
248         null,
249         true
250       );
252       const initialWidth = await waitForRenderAndGetWidth(newTabBrowser);
253       await promise;
255       // Zoom in
256       let newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
257       await waitForRoundTrip(newTabBrowser);
258       FullZoom.enlarge();
259       Assert.greater(
260         await newWidthPromise,
261         initialWidth,
262         "Zoom in makes the page bigger."
263       );
265       // Reset
266       newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
267       await waitForRoundTrip(newTabBrowser);
268       FullZoom.reset();
269       is(await newWidthPromise, initialWidth, "Zoom reset restores page.");
271       // Zoom out
272       newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
273       await waitForRoundTrip(newTabBrowser);
274       FullZoom.reduce();
275       Assert.less(
276         await newWidthPromise,
277         initialWidth,
278         "Zoom out makes the page smaller."
279       );
281       // Clean-up after the PDF viewer.
282       await waitForPdfJSClose(newTabBrowser);
283     }
284   );