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;
12 selector: "#zoomInButton",
15 expectedZoom: 1, // 1 - zoom in
16 message: "Zoomed in using the '+' (zoom in) button",
21 selector: "#zoomOutButton",
24 expectedZoom: -1, // -1 - zoom out
25 message: "Zoomed out using the '-' (zoom out) button",
34 expectedZoom: 1, // 1 - zoom in
35 message: "Zoomed in using the CTRL++ keys",
44 expectedZoom: -1, // -1 - zoom out
45 message: "Zoomed out using the CTRL+- keys",
50 selector: "select#scaleSelect",
54 expectedZoom: -1, // -1 - zoom out
55 message: "Zoomed using the zoom picker",
59 add_task(async function test() {
60 let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
61 let handlerInfo = mimeService.getFromTypeAndExtension(
66 // Make sure pdf.js is the default handler.
68 handlerInfo.alwaysAskBeforeHandling,
70 "pdf handler defaults to always-ask is false"
73 handlerInfo.preferredAction,
74 Ci.nsIHandlerInfo.handleInternally,
75 "pdf handler defaults to internal"
78 info("Pref action: " + handlerInfo.preferredAction);
80 await BrowserTestUtils.withNewTab(
81 { gBrowser, url: "about:blank" },
82 async function (newTabBrowser) {
85 TESTROOT + "file_pdfjs_test.pdf#zoom=100"
88 await SpecialPowers.spawn(
91 async function (contentTESTS) {
92 let document = content.document;
94 function waitForRender() {
95 return new Promise(resolve => {
96 document.addEventListener(
98 function onPageRendered(e) {
99 if (e.detail.pageNumber !== 1) {
103 document.removeEventListener(
115 // check that PDF is opened with internal viewer
117 content.document.querySelector("div#viewer"),
118 "document content has viewer UI"
121 let initialWidth, previousWidth;
122 initialWidth = previousWidth = parseInt(
123 content.getComputedStyle(
124 content.document.querySelector("div.page[data-page-number='1']")
128 for (let subTest of contentTESTS) {
129 // We zoom using an UI element
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);
136 "Element '" + subTest.action.selector + "' has been found"
139 if (subTest.action.index) {
140 el.selectedIndex = subTest.action.index;
143 // Dispatch the event for changing the zoom
144 ev = new content.Event(subTest.action.event);
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,
156 el.dispatchEvent(ev);
157 await waitForRender();
160 content.document.querySelector("select#scaleSelect");
162 // The zoom value displayed in the zoom select
164 pageZoomScale.options[pageZoomScale.selectedIndex].innerHTML;
166 let pageContainer = content.document.querySelector(
167 "div.page[data-page-number='1']"
169 let actualWidth = parseInt(
170 content.getComputedStyle(pageContainer).width
173 // the actual zoom of the PDF document
174 let computedZoomValue =
175 parseInt((actualWidth / initialWidth).toFixed(2) * 100) + "%";
179 "Content has correct zoom"
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;
190 await waitForPdfJSClose(newTabBrowser);
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(
208 function onPageRendered(e) {
209 if (e.detail.pageNumber !== 1) {
213 document.removeEventListener("pagerendered", onPageRendered, true);
220 // check that PDF is opened with internal viewer
222 content.document.querySelector("div#viewer"),
223 "document content has viewer UI"
226 await waitForRender(content.document);
229 content.getComputedStyle(
230 content.document.querySelector("div.page[data-page-number='1']")
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(
242 TESTROOT + "file_pdfjs_test.pdf"
244 await BrowserTestUtils.waitForContentEvent(
252 const initialWidth = await waitForRenderAndGetWidth(newTabBrowser);
256 let newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
257 await waitForRoundTrip(newTabBrowser);
260 await newWidthPromise,
262 "Zoom in makes the page bigger."
266 newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
267 await waitForRoundTrip(newTabBrowser);
269 is(await newWidthPromise, initialWidth, "Zoom reset restores page.");
272 newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
273 await waitForRoundTrip(newTabBrowser);
276 await newWidthPromise,
278 "Zoom out makes the page smaller."
281 // Clean-up after the PDF viewer.
282 await waitForPdfJSClose(newTabBrowser);