Skip to content

Commit

Permalink
Bug 1662434 - Show correct sheet count for PDF printers when numCopie…
Browse files Browse the repository at this point in the history
…s is set r=emalysz

Depends on D89796

Differential Revision: https://phabricator.services.mozilla.com/D89797
  • Loading branch information
mstriemer committed Sep 15, 2020
1 parent bb7122d commit 8514fcf
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
4 changes: 4 additions & 0 deletions toolkit/components/printing/content/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ var PrintEventHandler = {
this._updatePrintPreviewTask = createDeferredTask(async () => {
await initialPreviewDone;
await this._updatePrintPreview();
document.dispatchEvent(new CustomEvent("preview-updated"));
}, 0);

document.dispatchEvent(
Expand Down Expand Up @@ -913,6 +914,9 @@ var PrintSettingsViewProxy = {
p => p.name != PrintUtils.SAVE_TO_PDF_PRINTER
)?.value
);

case "numCopies":
return this.get(target, "willSaveToFile") ? 1 : target.numCopies;
}
return target[name];
},
Expand Down
1 change: 1 addition & 0 deletions toolkit/components/printing/tests/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ support-files =
file_page_change_print_original_2.html
skip-if = os == "mac"

[browser_sheet_count.js]
[browser_window_print.js]
support-files =
file_window_print.html
Expand Down
70 changes: 70 additions & 0 deletions toolkit/components/printing/tests/browser_sheet_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function getSheetCount(el) {
return el.ownerDocument.l10n.getAttributes(el).args.sheetCount;
}

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

let sheetCount = helper.get("sheet-count");
let { id } = helper.doc.l10n.getAttributes(sheetCount);
is(id, "printui-sheets-count", "The l10n id is correct");
let initialSheetCount = getSheetCount(sheetCount);
ok(initialSheetCount >= 1, "There is an initial sheet count");

await helper.openMoreSettings();

let scaleRadio = helper.get("percent-scale-choice");
await helper.waitForPreview(() => helper.click(scaleRadio));

let percentScale = helper.get("percent-scale");
await helper.waitForPreview(() => helper.text(percentScale, "200"));

let zoomedSheetCount = getSheetCount(sheetCount);
ok(zoomedSheetCount > initialSheetCount, "The sheet count increased");

// Since we're using the Save to PDF printer, the numCopies element should
// be hidden and its value ignored.
let numCopies = helper.get("copies-count");
ok(BrowserTestUtils.is_hidden(numCopies), "numCopies element is hidden");
helper.dispatchSettingsChange({
numCopies: 4,
});
is(
getSheetCount(sheetCount),
zoomedSheetCount,
"numCopies is ignored for Save to PDF printer"
);

// TODO: Ideally, this test would set numCopies=4 for a "real" printer and
// verify that the value is ignored when switching to the PDF printer. Since
// we don't have any "real" printers set up for testing yet, fake the printer
// and force the component to update.
let { settings, viewSettings } = helper;
is(viewSettings.numCopies, 1, "numCopies is 1 in viewSettings");
settings.outputFormat = Ci.nsIPrintSettings.kOutputFormatNative;
settings.printerName = "My real printer";
is(viewSettings.numCopies, 4, "numCopies is 4 in viewSettings");

// Manually update the components.
helper.get("print").update(viewSettings);
sheetCount.update(viewSettings);
numCopies.update(viewSettings);

// numCopies is now visible and sheetCount is multiplied by numCopies.
ok(BrowserTestUtils.is_visible(numCopies), "numCopies element is visible");
is(numCopies.value, "4", "numCopies displays the correct value");
is(
getSheetCount(sheetCount),
zoomedSheetCount * 4,
"numCopies is used when using a non-PDF printer"
);

await helper.closeDialog();
});
});
53 changes: 50 additions & 3 deletions toolkit/components/printing/tests/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class PrintHelper {

await SpecialPowers.popPrefEnv();

// Reset all of the other printing prefs to their default.
for (let name of Services.prefs.getChildList("print.")) {
Services.prefs.clearUserPref(name);
}

return taskReturn;
}

Expand Down Expand Up @@ -76,9 +81,7 @@ class PrintHelper {
}

get _printBrowser() {
let dialog = this.dialog;
ok(dialog, "The dialog exists");
return dialog._frame;
return this.dialog._frame;
}

get doc() {
Expand All @@ -89,7 +92,51 @@ class PrintHelper {
return this._printBrowser.contentWindow;
}

get(id) {
return this.doc.getElementById(id);
}

get sourceURI() {
return this.win.PrintEventHandler.originalSourceCurrentURI;
}

async waitForPreview(changeFn) {
changeFn();
await BrowserTestUtils.waitForEvent(this.doc, "preview-updated");
}

click(el) {
EventUtils.synthesizeMouseAtCenter(el, {}, this.win);
}

text(el, text) {
this.click(el);
el.value = "";
EventUtils.sendString(text, this.win);
}

async openMoreSettings() {
this.click(this.get("more-settings").firstElementChild);
await this.awaitAnimationFrame();
}

dispatchSettingsChange(settings) {
this.doc.dispatchEvent(
new CustomEvent("update-print-settings", {
detail: settings,
})
);
}

get settings() {
return this.win.PrintEventHandler.settings;
}

get viewSettings() {
return this.win.PrintEventHandler.viewSettings;
}

awaitAnimationFrame() {
return new Promise(resolve => this.win.requestAnimationFrame(resolve));
}
}

0 comments on commit 8514fcf

Please sign in to comment.