summaryrefslogtreecommitdiffstats
path: root/toolkit/components/printing/tests/browser_modal_print.js
blob: c3e3f94874a8e6029b052363c99a9141814b3a28 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

function assertExpectedPrintPage(helper) {
  is(
    helper.sourceURI,
    PrintHelper.defaultTestPageUrlHTTPS,
    "The URL of the browser is the one we expect"
  );
}

add_task(async function testModalPrintDialog() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    helper.assertDialogClosed();

    await helper.startPrint();

    helper.assertDialogOpen();

    // Check that we're printing the right page.
    assertExpectedPrintPage(helper);

    // Close the dialog with Escape.
    await helper.withClosingFn(() => {
      EventUtils.synthesizeKey("VK_ESCAPE", {}, helper.win);
    });

    helper.assertDialogClosed();
  });
});

add_task(async function testPrintMultiple() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    helper.assertDialogClosed();

    // First print as usual.
    await helper.startPrint();
    helper.assertDialogOpen();
    assertExpectedPrintPage(helper);

    // Trigger the command a few more times, verify the overlay still exists.
    ignoreAllUncaughtExceptions(true);
    for (let i = 0; i < 3; ++i) {
      try {
        await helper.startPrint();
      } finally {
        helper.assertDialogOpen();
      }
    }
    ignoreAllUncaughtExceptions(false);

    // Verify it's still the correct page.
    assertExpectedPrintPage(helper);

    // Make sure we clean up, ideally this would be handled by the helper.
    await TestUtils.waitForTick();
    await helper.closeDialog();
  });
});

add_task(async function testCancelButton() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    helper.assertDialogClosed();
    await helper.startPrint();
    helper.assertDialogOpen();

    let cancelButton = helper.doc.querySelector("button[name=cancel]");
    ok(cancelButton, "Got the cancel button");
    await helper.withClosingFn(() =>
      EventUtils.synthesizeMouseAtCenter(cancelButton, {}, helper.win)
    );

    helper.assertDialogClosed();
  });
});

add_task(async function testTabOrder() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    helper.assertDialogClosed();
    await helper.startPrint();
    helper.assertDialogOpen();

    const printerPicker = helper.doc.getElementById("printer-picker");
    is(
      helper.doc.activeElement,
      printerPicker,
      "Initial focus on printer picker"
    );

    const previewBrowser = document.querySelector(".printPreviewBrowser");
    ok(previewBrowser, "Got the print preview browser");

    let focused;
    let navigationShadowRoot = document.querySelector(
      ".printPreviewNavigation"
    ).shadowRoot;
    for (let buttonId of [
      "navigateEnd",
      "navigateNext",
      "navigatePrevious",
      "navigateHome",
    ]) {
      let button = navigationShadowRoot.getElementById(buttonId);
      focused = BrowserTestUtils.waitForEvent(button, "focus");
      await EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
      await focused;
    }

    focused = BrowserTestUtils.waitForEvent(previewBrowser, "focus");
    await EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
    await focused;
    ok(true, "Print preview focused after shift+tab through the paginator");

    focused = BrowserTestUtils.waitForEvent(gNavToolbox, "focus", true);
    EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
    await focused;
    ok(true, "Toolbox focused after shift+tab");

    focused = BrowserTestUtils.waitForEvent(previewBrowser, "focus");
    EventUtils.synthesizeKey("KEY_Tab");
    await focused;
    ok(true, "Print preview focused after tab");

    for (let buttonId of [
      "navigateHome",
      "navigatePrevious",
      "navigateNext",
      "navigateEnd",
    ]) {
      let button = navigationShadowRoot.getElementById(buttonId);
      focused = BrowserTestUtils.waitForEvent(button, "focus");
      await EventUtils.synthesizeKey("KEY_Tab");
      await focused;
    }
    focused = BrowserTestUtils.waitForEvent(printerPicker, "focus");
    EventUtils.synthesizeKey("KEY_Tab");
    await focused;
    ok(true, "Printer picker focused after tab");

    const lastButtonId =
      AppConstants.platform == "win" ? "cancel-button" : "print-button";
    const lastButton = helper.doc.getElementById(lastButtonId);

    focused = BrowserTestUtils.waitForEvent(lastButton, "focus");
    lastButton.focus();
    await focused;
    ok(true, "Last button focused");

    focused = BrowserTestUtils.waitForEvent(gNavToolbox, "focus", true);
    EventUtils.synthesizeKey("KEY_Tab");
    await focused;
    ok(true, "Toolbox focused after tab");

    focused = BrowserTestUtils.waitForEvent(lastButton, "focus");
    EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
    await focused;
    ok(true, "Last button focused after shift+tab");

    await helper.withClosingFn(() => {
      EventUtils.synthesizeKey("VK_ESCAPE", {});
    });

    helper.assertDialogClosed();
  });
});

async function testPrintWithEnter(testFn, filename) {
  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();

    let file = helper.mockFilePicker(filename);
    await testFn(helper);
    await helper.assertPrintToFile(file, () => {
      EventUtils.sendKey("return", helper.win);
      const cancelButton = helper.doc.querySelector(`button[name="cancel"]`);
      ok(!cancelButton.disabled, "Cancel button is not disabled");
      const printButton = helper.doc.querySelector(`button[name="print"]`);
      ok(printButton.disabled, "Print button is disabled");
    });
  });
}

add_task(async function testEnterAfterLoadPrints() {
  info("Test print without moving focus");
  await testPrintWithEnter(() => {}, "print_initial_focus.pdf");
});

add_task(async function testEnterPrintsFromPageRangeSelect() {
  info("Test print from page range select");
  await testPrintWithEnter(helper => {
    let pageRangePicker = helper.get("range-picker");
    pageRangePicker.focus();
    is(
      helper.doc.activeElement,
      pageRangePicker,
      "Page range select is focused"
    );
  }, "print_page_range_select.pdf");
});

add_task(async function testEnterPrintsFromOrientation() {
  info("Test print on Enter from focused orientation input");
  await testPrintWithEnter(helper => {
    let portrait = helper.get("portrait");
    portrait.focus();
    is(helper.doc.activeElement, portrait, "Portrait is focused");
  }, "print_orientation_focused.pdf");
});

add_task(async function testPrintOnNewWindowDoesntClose() {
  info("Test that printing doesn't close a window with a single tab");
  let win = await BrowserTestUtils.openNewBrowserWindow();

  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();
    let file = helper.mockFilePicker("print_new_window_close.pdf");
    await helper.assertPrintToFile(file, () => {
      EventUtils.sendKey("return", helper.win);
    });
  });
  ok(!win.closed, "Shouldn't be closed");
  await BrowserTestUtils.closeWindow(win);
  await SpecialPowers.popPrefEnv();
});

add_task(async function testPrintProgressIndicator() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();

    helper.setupMockPrint();

    let progressIndicator = helper.get("print-progress");
    ok(progressIndicator.hidden, "Progress indicator is hidden");

    let indicatorShown = BrowserTestUtils.waitForAttributeRemoval(
      "hidden",
      progressIndicator
    );
    helper.click(helper.get("print-button"));
    await indicatorShown;

    ok(!progressIndicator.hidden, "Progress indicator is shown on print start");

    await helper.withClosingFn(async () => {
      await helper.resolvePrint();
    });
  });
});

add_task(async function testPageSizePortrait() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();

    let orientation = helper.get("orientation");
    ok(orientation.hidden, "Orientation selector is hidden");

    is(
      helper.settings.orientation,
      Ci.nsIPrintSettings.kPortraitOrientation,
      "Orientation set to portrait"
    );
  }, "file_portrait.html");
});

add_task(async function testPageSizeLandscape() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();

    let orientation = helper.get("orientation");
    ok(orientation.hidden, "Orientation selector is hidden");

    is(
      helper.settings.orientation,
      Ci.nsIPrintSettings.kLandscapeOrientation,
      "Orientation set to landscape"
    );
  }, "file_landscape.html");
});

add_task(async function testFirstPageSizePortrait() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();

    let orientation = helper.get("orientation");
    ok(orientation.hidden, "Orientation selector is hidden");

    is(
      helper.settings.orientation,
      Ci.nsIPrintSettings.kPortraitOrientation,
      "Orientation set to portrait"
    );
  }, "file_first_portrait.html");
});

add_task(async function testFirstPageSizeLandscape() {
  await PrintHelper.withTestPageHTTPS(async helper => {
    await helper.startPrint();

    let orientation = helper.get("orientation");
    ok(orientation.hidden, "Orientation selector is hidden");

    is(
      helper.settings.orientation,
      Ci.nsIPrintSettings.kLandscapeOrientation,
      "Orientation set to landscape"
    );
  }, "file_first_landscape.html");
});