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

"use strict";

add_task(async function testPDFPrinterSettings() {
  await PrintHelper.withTestPage(async helper => {
    // Set some bad prefs
    await SpecialPowers.pushPrefEnv({
      set: [
        ["print.print_to_file", false],
        ["print.print_in_color", false],
        ["print.printer_Mozilla_Save_to_PDF.print_to_file", false],
        ["print.printer_Mozilla_Save_to_PDF.print_in_color", false],
      ],
    });

    await helper.startPrint();
    await helper.awaitAnimationFrame();

    // Verify we end up with sane settings
    let { settings } = helper;

    Assert.equal(
      settings.outputDestination,
      Ci.nsIPrintSettings.kOutputDestinationFile,
      "Check the current settings have file destination"
    );
    ok(
      settings.printInColor,
      "Check the current settings have a truthy printInColor for the PDF printer"
    );
    is(
      settings.outputFormat,
      Ci.nsIPrintSettings.kOutputFormatPDF,
      "The PDF printer has the correct outputFormat"
    );

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

add_task(async function testPDFCancel() {
  await PrintHelper.withTestPage(async helper => {
    await helper.startPrint();
    helper.mockFilePickerCancel();
    let form = helper.doc.querySelector("#print");

    // retrieve all elements other than cancel button
    let elements = [];
    for (let element of form.elements) {
      if (element.name != "cancel") {
        elements.push(element);
      }
    }
    let getDisabledStates = () => elements.map(el => el.disabled);
    let initialDisabledStates = getDisabledStates();

    ok(
      initialDisabledStates.some(disabled => !disabled),
      "At least one enabled form element before submitting"
    );
    let getShownDisabledStates = new Promise(resolve => {
      MockFilePicker.showCallback = () => resolve(getDisabledStates());
    });

    EventUtils.sendKey("return", helper.win);

    let shownDisabledStates = await getShownDisabledStates;
    ok(shownDisabledStates, "Got disabled states while shown");
    ok(
      shownDisabledStates.every(disabled => disabled),
      "All elements were disabled when showing picker"
    );
    let cancelButton = helper.doc.querySelector(`button[name="cancel"]`);
    ok(!cancelButton.disabled, "Cancel button is still enabled");

    let saveButton = form.querySelector("#print-button");
    await BrowserTestUtils.waitForAttributeRemoval("disabled", saveButton);
    helper.assertDialogOpen();

    is(
      getDisabledStates().every(
        (disabledState, index) => disabledState === initialDisabledStates[index]
      ),
      true,
      "Previous disabled states match after returning to preview"
    );

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

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

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

    helper.mockFilePicker("pdfFile.pdf");
    let filePath = PathUtils.join(
      Services.dirsvc.get("TmpD", Ci.nsIFile).path,
      "pdfFile.pdf"
    );

    await helper.withClosingFn(() => {
      EventUtils.sendKey("return", helper.win);
    });

    try {
      Services.prefs.getStringPref(
        "print.printer_Mozilla_Save_to_PDF.print_to_filename"
      );
      ok(false, "Should have cleared the filename pref");
    } catch (ex) {
      ok(true, "Cleared the filename pref");
    }

    is(await IOUtils.exists(filePath), true, "Saved pdf file exists");
    ok(await IOUtils.read(filePath), "Saved pdf file is not empty");
  });
});