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

"use strict";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);

const TEST_PATH_SITE = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://test1.example.com"
);

add_task(async function test_print_blocks() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  await BrowserTestUtils.withNewTab(
    `${TEST_PATH}file_window_print.html`,
    async function (browser) {
      info(
        "Waiting for the first window.print() to run and ensure we're showing the preview..."
      );

      let helper = new PrintHelper(browser);
      await helper.waitForDialog();

      {
        let [before, afterFirst] = await SpecialPowers.spawn(
          browser,
          [],
          () => {
            return [
              !!content.document.getElementById("before-print"),
              !!content.document.getElementById("after-first-print"),
            ];
          }
        );

        ok(before, "Content before printing should be in the DOM");
        ok(!afterFirst, "Shouldn't have returned yet from window.print()");
      }

      gBrowser.getTabDialogBox(browser).abortAllDialogs();

      await helper.waitForDialog();

      {
        let [before, afterFirst, afterSecond] = await SpecialPowers.spawn(
          browser,
          [],
          () => {
            return [
              !!content.document.getElementById("before-print"),
              !!content.document.getElementById("after-first-print"),
              !!content.document.getElementById("after-second-print"),
            ];
          }
        );

        ok(before, "Content before printing should be in the DOM");
        ok(afterFirst, "Should be in the second print already");
        ok(afterSecond, "Shouldn't have blocked if we have mozPrintCallbacks");
      }
    }
  );
});

add_task(async function test_print_delayed_during_load() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  await BrowserTestUtils.withNewTab(
    `${TEST_PATH}file_window_print_delayed_during_load.html`,
    async function (browser) {
      info(
        "Waiting for the first window.print() to run and ensure we're showing the preview..."
      );

      let helper = new PrintHelper(browser);
      await helper.waitForDialog();

      // The print dialog is open, should be open after onload.
      {
        let duringLoad = await SpecialPowers.spawn(browser, [], () => {
          return !!content.document.getElementById("added-during-load");
        });
        ok(duringLoad, "Print should've been delayed");
      }

      gBrowser.getTabDialogBox(browser).abortAllDialogs();

      is(typeof browser.isConnected, "boolean");
      await BrowserTestUtils.waitForCondition(() => !browser.isConnected);
      ok(true, "Tab should've been closed after printing");
    }
  );
});

add_task(async function test_print_on_sandboxed_frame() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  await BrowserTestUtils.withNewTab(
    `${TEST_PATH}file_window_print_sandboxed_iframe.html`,
    async function (browser) {
      info(
        "Waiting for the first window.print() to run and ensure we're showing the preview..."
      );

      let helper = new PrintHelper(browser);
      await helper.waitForDialog();

      isnot(
        document.querySelector(".printPreviewBrowser"),
        null,
        "Should open the print preview correctly"
      );
      gBrowser.getTabDialogBox(browser).abortAllDialogs();
    }
  );
});

add_task(async function test_print_another_iframe_and_remove() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  await BrowserTestUtils.withNewTab(
    `${TEST_PATH}file_window_print_another_iframe_and_remove.html`,
    async function (browser) {
      let firstFrame = browser.browsingContext.children[0];
      info("Clicking on the button in the first iframe");
      BrowserTestUtils.synthesizeMouse("button", 0, 0, {}, firstFrame);

      await new PrintHelper(browser).waitForDialog();

      isnot(
        document.querySelector(".printPreviewBrowser"),
        null,
        "Should open the print preview correctly"
      );
      gBrowser.getTabDialogBox(browser).abortAllDialogs();
    }
  );
});

add_task(async function test_window_print_coop_site() {
  for (const base of [TEST_PATH, TEST_PATH_SITE]) {
    const url = `${base}file_coop_header2.html`;
    is(
      document.querySelector(".printPreviewBrowser"),
      null,
      "There shouldn't be any print preview browser"
    );
    await BrowserTestUtils.withNewTab(url, async function (browser) {
      await new PrintHelper(browser).waitForDialog();

      ok(true, "Shouldn't crash");
      gBrowser.getTabDialogBox(browser).abortAllDialogs();
    });
  }
});

add_task(async function test_window_print_iframe_remove_on_afterprint() {
  ok(
    !document.querySelector(".printPreviewBrowser"),
    "There shouldn't be any print preview browser"
  );
  await BrowserTestUtils.withNewTab(
    `${TEST_PATH}file_window_print_iframe_remove_on_afterprint.html`,
    async function (browser) {
      await new PrintHelper(browser).waitForDialog();
      let modalBefore = await SpecialPowers.spawn(browser, [], () => {
        return content.windowUtils.isInModalState();
      });

      ok(modalBefore, "The tab should be in modal state");

      // Clear the dialog.
      gBrowser.getTabDialogBox(browser).abortAllDialogs();

      let [modalAfter, hasIframe] = await SpecialPowers.spawn(
        browser,
        [],
        () => {
          return [
            content.windowUtils.isInModalState(),
            !!content.document.querySelector("iframe"),
          ];
        }
      );

      ok(!modalAfter, "Should've cleared the modal state properly");
      ok(!hasIframe, "Iframe should've been removed from the DOM");
    }
  );
});

// FIXME(emilio): This test doesn't use window.print(), why is it on this file?
add_task(async function test_focused_browsing_context() {
  await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    `${TEST_PATH}longerArticle.html`
  );

  let tabCount = gBrowser.tabs.length;
  document.getElementById("cmd_newNavigatorTab").doCommand();
  await TestUtils.waitForCondition(() => gBrowser.tabs.length == tabCount + 1);
  let newTabBrowser = gBrowser.selectedBrowser;
  is(newTabBrowser.documentURI.spec, "about:newtab", "newtab is loaded");

  let menuButton = document.getElementById("PanelUI-menu-button");
  menuButton.click();
  await BrowserTestUtils.waitForEvent(window.PanelUI.mainView, "ViewShown");

  let printButtonID = "appMenu-print-button2";

  document.getElementById(printButtonID).click();

  let dialog = await TestUtils.waitForCondition(
    () =>
      gBrowser
        .getTabDialogBox(newTabBrowser)
        .getTabDialogManager()
        ._dialogs.find(dlg => dlg._box.querySelector(".printSettingsBrowser")),
    "Wait for dialog"
  );
  await dialog._dialogReady;
  ok(dialog, "Dialog is available");
  await dialog._frame.contentWindow._initialized;
  await dialog.close();

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function test_print_with_oop_iframe() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  await BrowserTestUtils.withNewTab(
    `${TEST_PATH}file_window_print_oop_iframe.html`,
    async function (browser) {
      info(
        "Waiting for window.print() to run and ensure we're showing the preview..."
      );

      let helper = new PrintHelper(browser);
      await helper.waitForDialog();

      isnot(
        document.querySelector(".printPreviewBrowser"),
        null,
        "Should open the print preview correctly"
      );
      gBrowser.getTabDialogBox(browser).abortAllDialogs();
    }
  );
});

add_task(async function test_base_uri_srcdoc() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  const PARENT_URI = `${TEST_PATH}file_window_print_srcdoc_base_uri.html`;
  await BrowserTestUtils.withNewTab(PARENT_URI, async function (browser) {
    info(
      "Waiting for window.print() to run and ensure we're showing the preview..."
    );

    let helper = new PrintHelper(browser);
    await helper.waitForDialog();

    let previewBrowser = document.querySelector(".printPreviewBrowser");
    isnot(previewBrowser, null, "Should open the print preview correctly");

    let baseURI = await SpecialPowers.spawn(previewBrowser, [], () => {
      return content.document.baseURI;
    });

    is(baseURI, PARENT_URI, "srcdoc print document base uri should be right");

    gBrowser.getTabDialogBox(browser).abortAllDialogs();
  });
});

add_task(async function test_print_reentrant() {
  is(
    document.querySelector(".printPreviewBrowser"),
    null,
    "There shouldn't be any print preview browser"
  );

  const URI = `${TEST_PATH}file_window_print_reentrant.html`;
  await BrowserTestUtils.withNewTab(URI, async function (browser) {
    info(
      "Waiting for window.print() to run and ensure we're showing the preview..."
    );

    let helper = new PrintHelper(browser);
    await helper.waitForDialog();

    let previewBrowser = document.querySelector(".printPreviewBrowser");
    isnot(previewBrowser, null, "Should open the print preview correctly");

    gBrowser.getTabDialogBox(browser).abortAllDialogs();

    let count = await SpecialPowers.spawn(browser, [], () => {
      return parseInt(
        content.document.getElementById("before-print-count").innerText
      );
    });

    is(count, 1, "Should've fired beforeprint just once");
  });
});