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

const frameSource = "<a href='about:mozilla'>some text</a>";
const SOURCES = [
  `Something else <iframe id="f" srcdoc="${frameSource}"></iframe>`,
  `Something else <iframe id="f" src="https://example.com/document-builder.sjs?html=${frameSource}"></iframe>`,
];

async function getPreviewText(previewBrowser) {
  return SpecialPowers.spawn(previewBrowser, [], function () {
    return content.document.body.textContent;
  });
}

add_task(async function print_frame() {
  let i = 0;
  for (const source of SOURCES) {
    is(
      document.querySelector(".printPreviewBrowser"),
      null,
      "There shouldn't be any print preview browser"
    );

    await BrowserTestUtils.withNewTab(
      "data:text/html," + source,
      async function (browser) {
        let frameBC = browser.browsingContext.children[0];
        let helper = new PrintHelper(browser);

        // If you change this, change nsContextMenu.printFrame() too.
        PrintUtils.startPrintWindow(frameBC, {
          printFrameOnly: true,
        });

        // Wait for the dialog to be fully ready. The initial preview will be
        // done at this point.
        await helper.waitForDialog();

        let textContent = await getPreviewText(
          helper.currentPrintPreviewBrowser
        );
        is(textContent, "some text", "Correct content loaded");

        let file = helper.mockFilePicker(`browser_print_frame-${i++}.pdf`);
        await helper.assertPrintToFile(file, () => {
          helper.click(helper.get("print-button"));
        });
        PrintHelper.resetPrintPrefs();
      }
    );
  }
});