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

"use strict";

let pdfPrinterName = "Mozilla Save to PDF";
let fastPrinterName = "Fast";
let slowPrinterName = "Slow";

async function setupPrinters(helper) {
  helper.addMockPrinter(fastPrinterName);

  let resolvePrinterInfo;
  helper.addMockPrinter({
    name: slowPrinterName,
    printerInfoPromise: new Promise(resolve => {
      resolvePrinterInfo = resolve;
    }),
  });

  await SpecialPowers.pushPrefEnv({
    set: [["print.printer_Slow.print_orientation", 1]],
  });

  return resolvePrinterInfo;
}

async function changeDestination(helper, dir) {
  let picker = helper.get("printer-picker");
  let changed = BrowserTestUtils.waitForEvent(picker, "change");

  let pickerOpened = BrowserTestUtils.waitForSelectPopupShown(window);
  picker.focus();
  EventUtils.sendKey("space", helper.win);
  await pickerOpened;
  EventUtils.sendKey(dir, window);
  EventUtils.sendKey("return", window);
  await changed;
}

function assertFormEnabled(form) {
  for (let element of form.elements) {
    if (element.hasAttribute("disallowed")) {
      ok(element.disabled, `${element.id} is disallowed`);
    } else {
      ok(!element.disabled, `${element.id} is enabled`);
    }
  }
}

function assertFormDisabled(form) {
  for (let element of form.elements) {
    if (element.id == "printer-picker" || element.id == "cancel-button") {
      ok(!element.disabled, `${element.id} is enabled`);
    } else {
      ok(element.disabled, `${element.id} is disabled`);
    }
  }
}

add_task(async function testSlowDestinationChange() {
  await PrintHelper.withTestPage(async helper => {
    let resolvePrinterInfo = await setupPrinters(helper);
    await helper.startPrint();

    let destinationPicker = helper.get("printer-picker");
    let printForm = helper.get("print");

    info("Changing to fast printer should change settings");
    await helper.assertSettingsChanged(
      { printerName: pdfPrinterName, orientation: 0 },
      { printerName: fastPrinterName, orientation: 0 },
      async () => {
        await changeDestination(helper, "down");
        is(destinationPicker.value, fastPrinterName, "Fast printer selected");
        // Wait one frame so the print settings promises resolve.
        await helper.awaitAnimationFrame();
        assertFormEnabled(printForm);
      }
    );

    info("Changing to slow printer should not change settings yet");
    await helper.assertSettingsNotChanged(
      { printerName: fastPrinterName, orientation: 0 },
      async () => {
        await changeDestination(helper, "down");
        is(destinationPicker.value, slowPrinterName, "Slow printer selected");
        // Wait one frame, since the settings are blocked on resolvePrinterInfo
        // the settings shouldn't change.
        await helper.awaitAnimationFrame();
        assertFormDisabled(printForm);
      }
    );

    await helper.assertSettingsChanged(
      { printerName: fastPrinterName, orientation: 0 },
      { printerName: slowPrinterName, orientation: 1 },
      async () => {
        resolvePrinterInfo();
        await helper.waitForSettingsEvent();
        assertFormEnabled(printForm);
      }
    );

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

add_task(async function testSwitchAwayFromSlowDestination() {
  await PrintHelper.withTestPage(async helper => {
    let resolvePrinterInfo = await setupPrinters(helper);
    await helper.startPrint();

    let destinationPicker = helper.get("printer-picker");
    let printForm = helper.get("print");

    // Load the fast printer.
    await helper.waitForSettingsEvent(async () => {
      await changeDestination(helper, "down");
    });
    await helper.awaitAnimationFrame();
    assertFormEnabled(printForm);

    // "Load" the slow printer.
    await changeDestination(helper, "down");
    is(destinationPicker.value, slowPrinterName, "Slow printer selected");
    // Wait an animation frame, since there's no settings event.
    await helper.awaitAnimationFrame();
    assertFormDisabled(printForm);

    // Switch back to the fast printer.
    await helper.waitForSettingsEvent(async () => {
      await changeDestination(helper, "up");
    });
    helper.assertSettingsMatch({
      printerName: fastPrinterName,
      orientation: 0,
    });

    await helper.awaitAnimationFrame();
    assertFormEnabled(printForm);

    // Let the slow printer settings resolve, the orientation shouldn't change.
    resolvePrinterInfo();
    // Wait so the settings event can trigger, if this case isn't handled.
    await helper.awaitAnimationFrame();
    helper.assertSettingsMatch({
      printerName: fastPrinterName,
      orientation: 0,
    });
    assertFormEnabled(printForm);

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