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

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

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

add_task(async function print_selection() {
  let i = 0;
  for (let 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];
        await SpecialPowers.spawn(frameBC, [], () => {
          let element = content.document.getElementById("other");
          content.focus();
          content.getSelection().selectAllChildren(element);
        });

        let helper = new PrintHelper(browser);

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

        await waitForPreviewVisible();

        let previewBrowser = document.querySelector(
          ".printPreviewBrowser[previewtype='selection']"
        );
        let previewText = () => getPreviewText(previewBrowser);
        // The preview process is async, wait for it to not be empty.
        let textContent = await TestUtils.waitForCondition(previewText);
        is(textContent, "other text", "Correct content loaded");

        let printSelect = document
          .querySelector(".printSettingsBrowser")
          .contentDocument.querySelector("#source-version-selection-radio");
        ok(
          BrowserTestUtils.is_visible(printSelect),
          "Print selection checkbox is shown"
        );
        ok(printSelect.checked, "Print selection checkbox is checked");

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

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

  await BrowserTestUtils.withNewTab("about:support", async function (browser) {
    ok(!browser.isRemote, "Page loaded in parent process");
    let selectedText = await SpecialPowers.spawn(
      browser.browsingContext,
      [],
      () => {
        let element = content.document.querySelector("h1");
        content.focus();
        content.getSelection().selectAllChildren(element);
        return element.textContent;
      }
    );
    ok(selectedText, "There is selected text");

    let helper = new PrintHelper(browser);

    // If you change this, change nsContextMenu.printSelection() too.
    PrintUtils.startPrintWindow(browser.browsingContext, {
      printSelectionOnly: true,
    });

    await waitForPreviewVisible();

    let previewBrowser = document.querySelector(
      ".printPreviewBrowser[previewtype='selection']"
    );
    let previewText = () => getPreviewText(previewBrowser);
    // The preview process is async, wait for it to not be empty.
    let textContent = await TestUtils.waitForCondition(previewText);
    is(textContent, selectedText, "Correct content loaded");

    let printSelect = document
      .querySelector(".printSettingsBrowser")
      .contentDocument.querySelector("#source-version-selection-radio");
    ok(
      BrowserTestUtils.is_visible(printSelect),
      "Print selection checkbox is shown"
    );
    ok(printSelect.checked, "Print selection checkbox is checked");

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

add_task(async function no_print_selection() {
  // Ensures the print selection checkbox is hidden if nothing is selected
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();
    await helper.openMoreSettings();

    let printSelect = helper.get("source-version-selection");
    ok(
      BrowserTestUtils.is_hidden(printSelect),
      "Print selection checkbox is hidden"
    );
    await helper.closeDialog();
  });
});

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

    await helper.startPrint();
    await helper.openMoreSettings();
    let printSource = helper.get("source-version-source-radio");
    ok(printSource.checked, "Print source radio is checked");
    let printSelect = helper.get("source-version-selection-radio");
    ok(!printSelect.checked, "Print selection radio is not checked");

    function getCurrentBrowser(previewType) {
      let browser = document.querySelector(
        `.printPreviewBrowser[previewtype="${previewType}"]`
      );
      is(
        browser.parentElement.getAttribute("previewtype"),
        previewType,
        "Expected browser is showing"
      );
      return browser;
    }

    let selectedText = "Article title";
    let fullText = await getPreviewText(getCurrentBrowser("source"));

    helper.assertSettingsMatch({
      printSelectionOnly: false,
    });

    await helper.assertSettingsChanged(
      { printSelectionOnly: false },
      { printSelectionOnly: true },
      async () => {
        await helper.waitForPreview(() => helper.click(printSelect));
        let text = await getPreviewText(getCurrentBrowser("selection"));
        is(text, selectedText, "Correct content loaded");
      }
    );

    await helper.assertSettingsChanged(
      { printSelectionOnly: true },
      { printSelectionOnly: false },
      async () => {
        await helper.waitForPreview(() => helper.click(printSource));
        let text = await getPreviewText(getCurrentBrowser("source"));
        is(text, fullText, "Correct content loaded");
      }
    );

    await helper.closeDialog();
  });
});

add_task(async function open_system_print_with_selection_and_pdf() {
  await BrowserTestUtils.withNewTab(
    "data:text/html," + sources[0],
    async function (browser) {
      let frameBC = browser.browsingContext.children[0];
      await SpecialPowers.spawn(frameBC, [], () => {
        let element = content.document.getElementById("other");
        content.focus();
        content.getSelection().selectAllChildren(element);
      });

      let helper = new PrintHelper(browser);

      // Add another printer so the system dialog link is shown on Windows.
      helper.addMockPrinter("A printer");

      PrintUtils.startPrintWindow(frameBC, {});

      await waitForPreviewVisible();

      // Ensure that the PDF printer is selected since the way settings are
      // cloned is different in this case.
      is(
        helper.settings.printerName,
        "Mozilla Save to PDF",
        "Mozilla Save to PDF is the current printer."
      );

      await helper.setupMockPrint();

      helper.click(helper.get("open-dialog-link"));
      await helper.withClosingFn(() => {
        helper.resolveShowSystemDialog();
        helper.resolvePrint();
      });

      ok(
        helper.systemDialogOpenedWithSelection,
        "Expect system print dialog to be notified of selection"
      );

      PrintHelper.resetPrintPrefs();
    }
  );
});