Bug 1653392 - Basic tab modal print UI test r?emalysz,sfoster
Summary:
Differential Revision:
https://phabricator.services.mozilla.com/D84245
Depends on D84244
Test Plan:
Reviewers: emalysz, sfoster
Subscribers:
Bug #: 1653392
Differential Diff: PHID-DIFF-24pjtqyjyf27v4bzdlg5
--- a/toolkit/components/printing/content/print.js
+++ b/toolkit/components/printing/content/print.js
@@ -15,17 +15,17 @@ document.addEventListener(
window.addEventListener(
"unload",
e => {
document.textContent = "";
},
{ once: true }
);
-const PrintEventHandler = {
+var PrintEventHandler = {
init() {
this.sourceBrowser = this.getSourceBrowser();
this.settings = PrintUtils.getPrintSettings();
document.addEventListener("print", e => this.print({ silent: true }));
document.addEventListener("update-print-setting", e =>
this.updateSetting(e.detail)
);
--- a/toolkit/components/printing/tests/browser.ini
+++ b/toolkit/components/printing/tests/browser.ini
@@ -1,8 +1,17 @@
+[DEFAULT]
+support-files =
+ head.js
+
+[browser_modal_print.js]
+support-files =
+ simplifyArticleSample.html
+skip-if = os == "mac" || (verify && (os == 'win' || os == 'linux'))
+
[browser_page_change_print_original.js]
support-files =
file_page_change_print_original_1.html
file_page_change_print_original_2.html
skip-if = os == "mac"
[browser_preview_print_simplify_non_article.js]
support-files =
new file mode 100644
--- /dev/null
+++ b/toolkit/components/printing/tests/browser_modal_print.js
@@ -0,0 +1,74 @@
+function assertExpectedPrintPage(helper) {
+ let printBrowser = helper.win.PrintEventHandler.sourceBrowser;
+ is(
+ printBrowser,
+ gBrowser.selectedBrowser,
+ "The current browser is being printed"
+ );
+ is(
+ printBrowser.currentURI.spec,
+ PrintHelper.defaultTestPageUrl,
+ "The URL of the browser is the one we expect"
+ );
+}
+
+add_task(async function testModalPrintDialog() {
+ await PrintHelper.withTestPage(async helper => {
+ helper.assertDialogHidden();
+
+ await helper.startPrint();
+
+ helper.assertDialogVisible();
+
+ // Check that we're printing the right page.
+ assertExpectedPrintPage(helper);
+
+ // Close the dialog with Escape.
+ await helper.withClosingFn(() => {
+ EventUtils.synthesizeKey("VK_ESCAPE", {}, helper.win);
+ });
+
+ helper.assertDialogHidden();
+ });
+});
+
+add_task(async function testPrintMultiple() {
+ await PrintHelper.withTestPage(async helper => {
+ helper.assertDialogHidden();
+
+ // First print as usual.
+ await helper.startPrint();
+ helper.assertDialogVisible();
+ assertExpectedPrintPage(helper);
+
+ // Trigger the command a few more times, verify the overlay still exists.
+ await helper.startPrint();
+ helper.assertDialogVisible();
+ await helper.startPrint();
+ helper.assertDialogVisible();
+ await helper.startPrint();
+ helper.assertDialogVisible();
+
+ // Verify it's still the correct page.
+ assertExpectedPrintPage(helper);
+
+ // Make sure we clean up, ideally this would be handled by the helper.
+ await helper.closeDialog();
+ });
+});
+
+add_task(async function testCancelButton() {
+ await PrintHelper.withTestPage(async helper => {
+ helper.assertDialogHidden();
+ await helper.startPrint();
+ helper.assertDialogVisible();
+
+ let cancelButton = helper.doc.querySelector("button[name=cancel]");
+ ok(cancelButton, "Got the cancel button");
+ await helper.withClosingFn(() =>
+ EventUtils.synthesizeMouseAtCenter(cancelButton, {}, helper.win)
+ );
+
+ helper.assertDialogHidden();
+ });
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/printing/tests/head.js
@@ -0,0 +1,82 @@
+const PRINT_DOCUMENT_URI = "chrome://global/content/print.html";
+
+class PrintHelper {
+ static async withTestPage(testFn) {
+ await SpecialPowers.pushPrefEnv({
+ set: [["print.tab_modal.enabled", true]],
+ });
+
+ let taskReturn = await BrowserTestUtils.withNewTab(
+ this.defaultTestPageUrl,
+ async function(browser) {
+ await testFn(new PrintHelper(browser));
+ }
+ );
+
+ await SpecialPowers.popPrefEnv();
+
+ return taskReturn;
+ }
+
+ static get defaultTestPageUrl() {
+ const testPath = getRootDirectory(gTestPath).replace(
+ "chrome://mochitests/content",
+ "http://example.com"
+ );
+ return testPath + "simplifyArticleSample.html";
+ }
+
+ constructor(sourceBrowser) {
+ this.sourceBrowser = sourceBrowser;
+ }
+
+ async startPrint() {
+ document.getElementById("cmd_print").doCommand();
+ let dialog = await TestUtils.waitForCondition(() => this._dialogs[0]);
+ await dialog._dialog._dialogReady;
+ }
+
+ async withClosingFn(closeFn) {
+ await closeFn();
+ await this._dialogs[0]._dialog._closingPromise;
+ }
+
+ async closeDialog() {
+ await this.withClosingFn(() => this._dialogs[0]._dialog.close());
+ }
+
+ assertDialogHidden() {
+ is(this._dialogs.length, 0, "There are no print dialogs");
+ }
+
+ assertDialogVisible() {
+ is(this._dialogs.length, 1, "There is one print dialog");
+ BrowserTestUtils.is_visible(this._dialogs[0], "The dialog is visible");
+ }
+
+ get _container() {
+ return this.sourceBrowser.ownerGlobal.gBrowser.getBrowserContainer(
+ this.sourceBrowser
+ );
+ }
+
+ get _dialogs() {
+ return this._container.querySelectorAll(".printDialogContainer");
+ }
+
+ get _printBrowser() {
+ let dialogs = this._dialogs;
+ is(dialogs.length, 1, "There's one dialog");
+ let frame = dialogs[0].querySelector(".dialogFrame");
+ ok(frame, "Found the print dialog frame");
+ return frame;
+ }
+
+ get doc() {
+ return this._printBrowser.contentDocument;
+ }
+
+ get win() {
+ return this._printBrowser.contentWindow;
+ }
+}