summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pdfjs/test/browser_pdfjs_zoom.js
blob: e00dede42285c943c71552ca7628c4e8d5c2ab47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

requestLongerTimeout(2);

const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;

const TESTS = [
  {
    action: {
      selector: "button#zoomIn",
      event: "click",
    },
    expectedZoom: 1, // 1 - zoom in
    message: "Zoomed in using the '+' (zoom in) button",
  },

  {
    action: {
      selector: "button#zoomOut",
      event: "click",
    },
    expectedZoom: -1, // -1 - zoom out
    message: "Zoomed out using the '-' (zoom out) button",
  },

  {
    action: {
      keyboard: true,
      keyCode: 61,
      event: "+",
    },
    expectedZoom: 1, // 1 - zoom in
    message: "Zoomed in using the CTRL++ keys",
  },

  {
    action: {
      keyboard: true,
      keyCode: 109,
      event: "-",
    },
    expectedZoom: -1, // -1 - zoom out
    message: "Zoomed out using the CTRL+- keys",
  },

  {
    action: {
      selector: "select#scaleSelect",
      index: 5,
      event: "change",
    },
    expectedZoom: -1, // -1 - zoom out
    message: "Zoomed using the zoom picker",
  },
];

add_task(async function test() {
  let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
  let handlerInfo = mimeService.getFromTypeAndExtension(
    "application/pdf",
    "pdf"
  );

  // Make sure pdf.js is the default handler.
  is(
    handlerInfo.alwaysAskBeforeHandling,
    false,
    "pdf handler defaults to always-ask is false"
  );
  is(
    handlerInfo.preferredAction,
    Ci.nsIHandlerInfo.handleInternally,
    "pdf handler defaults to internal"
  );

  info("Pref action: " + handlerInfo.preferredAction);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (newTabBrowser) {
      await waitForPdfJS(
        newTabBrowser,
        TESTROOT + "file_pdfjs_test.pdf#zoom=100"
      );

      await SpecialPowers.spawn(
        newTabBrowser,
        [TESTS],
        async function (contentTESTS) {
          let document = content.document;

          function waitForRender() {
            return new Promise(resolve => {
              document.addEventListener(
                "pagerendered",
                function onPageRendered(e) {
                  if (e.detail.pageNumber !== 1) {
                    return;
                  }

                  document.removeEventListener(
                    "pagerendered",
                    onPageRendered,
                    true
                  );
                  resolve();
                },
                true
              );
            });
          }

          // check that PDF is opened with internal viewer
          Assert.ok(
            content.document.querySelector("div#viewer"),
            "document content has viewer UI"
          );

          let initialWidth, previousWidth;
          initialWidth = previousWidth = parseInt(
            content.getComputedStyle(
              content.document.querySelector("div.page[data-page-number='1']")
            ).width
          );

          for (let subTest of contentTESTS) {
            // We zoom using an UI element
            var ev;
            if (subTest.action.selector) {
              // Get the element and trigger the action for changing the zoom
              var el = document.querySelector(subTest.action.selector);
              Assert.ok(
                el,
                "Element '" + subTest.action.selector + "' has been found"
              );

              if (subTest.action.index) {
                el.selectedIndex = subTest.action.index;
              }

              // Dispatch the event for changing the zoom
              ev = new content.Event(subTest.action.event);
            } else {
              // We zoom using keyboard
              // Simulate key press
              ev = new content.KeyboardEvent("keydown", {
                key: subTest.action.event,
                keyCode: subTest.action.keyCode,
                ctrlKey: true,
              });
              el = content;
            }

            el.dispatchEvent(ev);
            await waitForRender();

            var pageZoomScale =
              content.document.querySelector("select#scaleSelect");

            // The zoom value displayed in the zoom select
            var zoomValue =
              pageZoomScale.options[pageZoomScale.selectedIndex].innerHTML;

            let pageContainer = content.document.querySelector(
              "div.page[data-page-number='1']"
            );
            let actualWidth = parseInt(
              content.getComputedStyle(pageContainer).width
            );

            // the actual zoom of the PDF document
            let computedZoomValue =
              parseInt((actualWidth / initialWidth).toFixed(2) * 100) + "%";
            Assert.equal(
              computedZoomValue,
              zoomValue,
              "Content has correct zoom"
            );

            // Check that document zooms in the expected way (in/out)
            let zoom = (actualWidth - previousWidth) * subTest.expectedZoom;
            Assert.ok(zoom > 0, subTest.message);

            previousWidth = actualWidth;
          }

          var viewer = content.wrappedJSObject.PDFViewerApplication;
          await viewer.close();
        }
      );
    }
  );
});

// Performs a SpecialPowers.spawn round-trip to ensure that any setup
// that needs to be done in the content process by any pending tasks has
// a chance to complete before continuing.
function waitForRoundTrip(browser) {
  return SpecialPowers.spawn(browser, [], () => {});
}

async function waitForRenderAndGetWidth(newTabBrowser) {
  return SpecialPowers.spawn(newTabBrowser, [], async function () {
    function waitForRender(document) {
      return new Promise(resolve => {
        document.addEventListener(
          "pagerendered",
          function onPageRendered(e) {
            if (e.detail.pageNumber !== 1) {
              return;
            }

            document.removeEventListener("pagerendered", onPageRendered, true);
            resolve();
          },
          true
        );
      });
    }
    // check that PDF is opened with internal viewer
    Assert.ok(
      content.document.querySelector("div#viewer"),
      "document content has viewer UI"
    );

    await waitForRender(content.document);

    return parseInt(
      content.getComputedStyle(
        content.document.querySelector("div.page[data-page-number='1']")
      ).width
    );
  });
}

add_task(async function test_browser_zoom() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (newTabBrowser) {
      await waitForPdfJS(newTabBrowser, TESTROOT + "file_pdfjs_test.pdf");

      const initialWidth = await waitForRenderAndGetWidth(newTabBrowser);

      // Zoom in
      let newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
      await waitForRoundTrip(newTabBrowser);
      FullZoom.enlarge();
      Assert.greater(
        await newWidthPromise,
        initialWidth,
        "Zoom in makes the page bigger."
      );

      // Reset
      newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
      await waitForRoundTrip(newTabBrowser);
      FullZoom.reset();
      is(await newWidthPromise, initialWidth, "Zoom reset restores page.");

      // Zoom out
      newWidthPromise = waitForRenderAndGetWidth(newTabBrowser);
      await waitForRoundTrip(newTabBrowser);
      FullZoom.reduce();
      Assert.less(
        await newWidthPromise,
        initialWidth,
        "Zoom out makes the page smaller."
      );

      // Clean-up after the PDF viewer.
      await SpecialPowers.spawn(newTabBrowser, [], function () {
        const viewer = content.wrappedJSObject.PDFViewerApplication;
        return viewer.close();
      });
    }
  );
});