summaryrefslogtreecommitdiffstats
path: root/toolkit/components/printing/tests/browser_preview_navigation.js
blob: b4c09201856f6d1e728f9e941e8a4d97797df71a (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function compare10nArgs(elem, expectedValues) {
  let l10nArgs = elem.ownerDocument.l10n.getAttributes(elem).args;
  for (let [name, value] of Object.entries(expectedValues)) {
    if (value !== l10nArgs[name]) {
      info(
        `compare10nArgs, expected ${name}: ${value}, actual: ${l10nArgs[name]}`
      );
      return false;
    }
  }
  return true;
}

async function waitForPageStatusUpdate(elem, expected, message) {
  await TestUtils.waitForCondition(
    () => compare10nArgs(elem, expected),
    message
  );
}

async function waitUntilVisible(elem, visible = true) {
  await TestUtils.waitForCondition(
    () =>
      BrowserTestUtils.isVisible(elem) && getComputedStyle(elem).opacity == "1",
    "Waiting for element to be visible and have opacity:1"
  );
}

async function waitUntilTransparent(elem) {
  // Note that is_visible considers a fully transparent element "visible"
  await TestUtils.waitForCondition(
    () => getComputedStyle(elem).opacity == "0",
    "Waiting for element to be have opacity:0"
  );
}

async function mouseMoveAndWait(elem) {
  let mouseMovePromise = BrowserTestUtils.waitForEvent(elem, "mousemove");
  EventUtils.synthesizeMouseAtCenter(elem, { type: "mousemove" });
  await mouseMovePromise;
  await TestUtils.waitForTick();
}

add_task(async function testToolbarVisibility() {
  // move the mouse to a known position
  await mouseMoveAndWait(gURLBar.textbox);

  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();

    let previewStack = document.querySelector(".previewStack");

    // The toolbar has 0 opacity until we hover or focus it
    is(
      getComputedStyle(helper.paginationElem).opacity,
      "0",
      "Initially transparent"
    );

    let visiblePromise = waitUntilVisible(helper.paginationElem);
    helper.paginationElem.shadowRoot.querySelector("#navigateEnd").focus();
    await visiblePromise;
    is(
      getComputedStyle(helper.paginationElem).opacity,
      "1",
      "Opaque with button focused"
    );

    await EventUtils.synthesizeKey("KEY_Tab", {});
    await waitUntilTransparent(helper.paginationElem);
    is(
      getComputedStyle(helper.paginationElem).opacity,
      "0",
      "Returns to transparent"
    );

    visiblePromise = waitUntilVisible(helper.paginationElem);
    info("Waiting for mousemove event, and for the toolbar to become opaque");
    await mouseMoveAndWait(previewStack);
    await visiblePromise;
    is(getComputedStyle(helper.paginationElem).opacity, "1", "Opaque toolbar");

    // put the mouse back where it won't interfere with later tests
    await mouseMoveAndWait(gURLBar.textbox);
    await helper.closeDialog();
  });
});

add_task(async function testPreviewSheetCount() {
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();

    // We have to wait for the first _updatePrintPreview to get the sheet count
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Paginator indicates the correct number of sheets"
    );

    // then switch to page range 1-1 and verify page count changes
    await helper.dispatchSettingsChange({
      pageRanges: ["1", "1"],
    });
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 1 },
      "Indicates the updated number of sheets"
    );

    await helper.closeDialog();
  }, "longerArticle.html");
});

add_task(async function testPreviewScroll() {
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();

    // Wait for the first _updatePrintPreview before interacting with the preview
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Paginator indicates the correct number of sheets"
    );
    let previewBrowser = helper.currentPrintPreviewBrowser;

    // scroll down the document
    // and verify the indicator is updated correctly
    await SpecialPowers.spawn(previewBrowser, [], async function () {
      const { ContentTaskUtils } = ChromeUtils.importESModule(
        "resource://testing-common/ContentTaskUtils.sys.mjs"
      );
      const EventUtils = ContentTaskUtils.getEventUtils(content);
      content.focus();
      EventUtils.synthesizeKey("VK_PAGE_DOWN", {}, content);
    });
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 2, sheetCount: 3 },
      "Indicator updates on scroll"
    );

    // move focus before closing the dialog
    helper.get("cancel-button").focus();
    await helper.closeDialog();
  }, "longerArticle.html");
});

add_task(async function testPreviewNavigationCommands() {
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();

    // Wait for the first _updatePrintPreview before interacting with the preview
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Paginator indicates the correct number of sheets"
    );

    // click the navigation buttons
    // and verify the indicator is updated correctly
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 2, sheetCount: 3 },
      "Indicator updates on navigation to next"
    );

    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigatePrevious"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Indicator updates on navigation to previous"
    );

    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateEnd"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 3, sheetCount: 3 },
      "Indicator updates on navigation to end"
    );

    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateHome"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Indicator updates on navigation to start"
    );

    // Test rapid clicks on the navigation buttons
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 3, sheetCount: 3 },
      "2 successive 'next' clicks correctly update the sheet indicator"
    );

    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigatePrevious"),
      {}
    );
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigatePrevious"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "2 successive 'previous' clicks correctly update the sheet indicator"
    );

    // move focus before closing the dialog
    helper.get("cancel-button").focus();
    await helper.closeDialog();
  }, "longerArticle.html");
});

add_task(async function testMultiplePreviewNavigation() {
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();
    const tab1 = gBrowser.selectedTab;

    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Indicator has the correct initial sheetCount"
    );

    const tab2 = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      PrintHelper.defaultTestPageUrl
    );
    let helper2 = new PrintHelper(tab2.linkedBrowser);
    await helper2.startPrint();

    let [previewBrowser1, previewBrowser2] = document.querySelectorAll(
      ".printPreviewBrowser[previewtype='source']"
    );
    ok(previewBrowser1 && previewBrowser2, "There are 2 preview browsers");

    let [toolbar1, toolbar2] = document.querySelectorAll(
      ".printPreviewNavigation"
    );
    ok(toolbar1 && toolbar2, "There are 2 preview navigation toolbars");
    is(
      toolbar1.previewBrowser,
      previewBrowser1,
      "toolbar1 has the correct previewBrowser"
    );
    ok(
      compare10nArgs(helper.paginationSheetIndicator, {
        sheetNum: 1,
        sheetCount: 3,
      }),
      "First toolbar has the correct content"
    );

    is(
      toolbar2.previewBrowser,
      previewBrowser2,
      "toolbar2 has the correct previewBrowser"
    );
    ok(
      compare10nArgs(helper2.paginationSheetIndicator, {
        sheetNum: 1,
        sheetCount: 1,
      }),
      "2nd toolbar has the correct content"
    );

    // Switch back to the first tab and ensure the correct preview navigation is updated when clicked
    await BrowserTestUtils.switchTab(gBrowser, tab1);

    EventUtils.synthesizeMouseAtCenter(
      toolbar1.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 2, sheetCount: 3 },
      "Indicator updates on navigation multiple"
    );

    gBrowser.removeTab(tab2);
  }, "longerArticle.html");
});

add_task(async function testPreviewNavigationSelection() {
  await PrintHelper.withTestPage(async helper => {
    await SpecialPowers.spawn(helper.sourceBrowser, [], async function () {
      let element = content.document.querySelector("#page-2");
      content.window.getSelection().selectAllChildren(element);
    });

    await helper.startPrint();

    // Wait for the first _updatePrintPreview before interacting with the preview
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Paginator indicates the correct number of sheets"
    );

    // click a navigation button
    // and verify the indicator is updated correctly
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 2, sheetCount: 3 },
      "Indicator updates on navigation next selection"
    );

    await helper.openMoreSettings();
    let printSelect = helper.get("source-version-selection-radio");
    await helper.waitForPreview(() => helper.click(printSelect));

    // Wait for the first _updatePrintPreview before interacting with the preview
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 2 },
      "Paginator indicates the correct number of sheets"
    );

    // click a navigation button
    // and verify the indicator is updated correctly
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 2, sheetCount: 2 },
      "Indicator updates on navigation next selection 2"
    );

    // move focus before closing the dialog
    helper.get("cancel-button").focus();
    await helper.closeDialog();
  }, "longerArticle.html");
});

add_task(async function testPaginatorAfterSettingsUpdate() {
  const mockPrinterName = "Fake Printer";
  await PrintHelper.withTestPage(async helper => {
    helper.addMockPrinter(mockPrinterName);
    await helper.startPrint();

    // Wait for the first _updatePrintPreview before interacting with the preview
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Paginator indicates the correct number of sheets"
    );

    // click the navigation buttons
    // and verify the indicator is updated correctly
    EventUtils.synthesizeMouseAtCenter(
      helper.paginationElem.shadowRoot.querySelector("#navigateNext"),
      {}
    );
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 2, sheetCount: 3 },
      "Indicator updates on navigation next after update"
    );

    // Select a new printer
    await helper.dispatchSettingsChange({ printerName: mockPrinterName });
    await waitForPageStatusUpdate(
      helper.paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Indicator updates on navigation next after printer change"
    );
    ok(
      compare10nArgs(helper.paginationSheetIndicator, {
        sheetNum: 1,
        sheetCount: 3,
      }),
      "Sheet indicator has correct value"
    );

    // move focus before closing the dialog
    helper.get("cancel-button").focus();
    await helper.closeDialog();
  }, "longerArticle.html");
});

add_task(async function testTooltips() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.tooltipDelay", 0]] });
  const mockPrinterName = "Fake Printer";
  await PrintHelper.withTestPage(async helper => {
    helper.addMockPrinter(mockPrinterName);
    await helper.startPrint();

    let paginationElem = document.querySelector(".printPreviewNavigation");
    let paginationSheetIndicator =
      paginationElem.shadowRoot.querySelector("#sheetIndicator");

    // Wait for the first _updatePrintPreview before interacting with the preview
    await waitForPageStatusUpdate(
      paginationSheetIndicator,
      { sheetNum: 1, sheetCount: 3 },
      "Paginator indicates the correct number of sheets"
    );

    let awaitTooltipOpen = new Promise(resolve => {
      window.addEventListener(
        "popupshown",
        function (event) {
          resolve(event.originalTarget);
        },
        { once: true }
      );
    });

    let navigateEnd = paginationElem.shadowRoot.querySelector("#navigateEnd");
    info("Initial mouse move to end navigation button");
    EventUtils.synthesizeMouseAtCenter(navigateEnd, { type: "mousemove" });
    let tooltip = await awaitTooltipOpen;
    is(tooltip.label, navigateEnd.title, "Tooltip shows correct text");
    awaitTooltipOpen = new Promise(resolve => {
      window.addEventListener(
        "popupshown",
        function (event) {
          resolve(event.originalTarget);
        },
        { once: true }
      );
    });

    let navigateNext = paginationElem.shadowRoot.querySelector("#navigateNext");
    let navigateNextRect = navigateNext.getBoundingClientRect();
    info("Initial mouse move to next navigation button");
    EventUtils.synthesizeMouseAtCenter(navigateNext, { type: "mousemove" });
    info("Waiting");
    EventUtils.synthesizeMouse(
      navigateNext,
      navigateNextRect.width / 2 + 5,
      navigateNextRect.height / 2,
      { type: "mousemove" },
      window
    );
    tooltip = await awaitTooltipOpen;
    is(tooltip.label, navigateNext.title, "Tooltip shows correct text");

    // move focus before closing the dialog
    helper.get("cancel-button").focus();
    await helper.awaitAnimationFrame();
    await helper.closeDialog();
  }, "longerArticle.html");
});