/* Any copyright is dedicated to the Public Domain. * http://creativecommons.org/publicdomain/zero/1.0/ */ "use strict"; async function openPageSetupDialog() { let printSettings = Cc["@mozilla.org/gfx/printsettings-service;1"] .getService(Ci.nsIPrintSettingsService) .createNewPrintSettings(); printSettings.printerName = "Mozilla Save to PDF"; printSettings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF; let paramBlock = Cc["@mozilla.org/embedcomp/dialogparam;1"].createInstance( Ci.nsIDialogParamBlock ); let win = window.openDialog( "chrome://global/content/printPageSetup.xhtml", "_blank", "chrome,dialog,titlebar", printSettings, paramBlock ); await BrowserTestUtils.waitForEvent(win, "load"); await TestUtils.waitForCondition( () => win.printPageSetupDialogLoaded, "Waiting for the dialog to initialize" ); return { win, printSettings, paramBlock }; } function dispatchInput(win, input, value) { input.value = value; input.dispatchEvent(new win.Event("input", { bubbles: true })); } add_setup(async function () { await SpecialPowers.pushPrefEnv({ set: [["print.save_print_settings", false]], }); }); add_task(async function test_page_setup_controls() { let { win, printSettings, paramBlock } = await openPageSetupDialog(); let doc = win.document; let topInput = doc.getElementById("topInput"); dispatchInput(win, topInput, "1.5x"); is(topInput.value, "1.5", "Margin input is sanitized"); is( doc.getElementById("marginTop").style.height, "0.15in", "Margin preview is updated" ); let scalingInput = doc.getElementById("scalingInput"); dispatchInput(win, scalingInput, "9a0"); is(scalingInput.value, "90", "Scaling input is sanitized"); let shrinkToFit = doc.getElementById("shrinkToFit"); let wasChecked = shrinkToFit.checked; shrinkToFit.click(); isnot(shrinkToFit.checked, wasChecked, "Shrink to fit was toggled"); is( scalingInput.disabled, shrinkToFit.checked, "Scaling input disabled state follows shrink to fit" ); if (shrinkToFit.checked) { shrinkToFit.click(); } let landscape = doc.getElementById("landscape"); let portrait = doc.getElementById("portrait"); let marginPage = doc.getElementById("marginPage"); let { width, height } = marginPage.style; let other = doc.getElementById("orientation").selectedItem == landscape ? portrait : landscape; other.click(); is(marginPage.style.width, height, "Page preview width was swapped"); is(marginPage.style.height, width, "Page preview height was swapped"); let hLeftOption = doc.getElementById("hLeftOption"); let promptPromise = BrowserTestUtils.promiseAlertDialogOpen( null, "chrome://global/content/commonDialog.xhtml", { callback(dialogWin) { dialogWin.document.getElementById("loginTextbox").value = "Custom header"; dialogWin.document.querySelector("dialog").acceptDialog(); }, } ); hLeftOption.selectedIndex = 6; hLeftOption.dispatchEvent(new win.CustomEvent("command", { bubbles: true })); await promptPromise; await TestUtils.waitForCondition( () => hLeftOption.custom == "Custom header", "Waiting for the custom header value" ); let closed = BrowserTestUtils.domWindowClosed(win); doc.querySelector("dialog").acceptDialog(); await closed; is(paramBlock.GetInt(0), 1, "Dialog was accepted"); is(printSettings.headerStrLeft, "Custom header", "Custom header was saved"); is(printSettings.scaling, 0.9, "Scaling was saved"); }); add_task(async function test_page_setup_cancel() { let { win, paramBlock } = await openPageSetupDialog(); let closed = BrowserTestUtils.domWindowClosed(win); win.document.querySelector("dialog").cancelDialog(); await closed; is(paramBlock.GetInt(0), 0, "Dialog was cancelled"); });