Bug 1653392 - Basic tab modal print UI test r=emalysz,sfoster
authorMark Striemer <mstriemer@mozilla.com>
Sat, 01 Aug 2020 15:14:14 +0000
changeset 543031 f1ea26e4b3b7851b34efbae7e1ad9ee3d810eaae
parent 543030 a09b54a535f4154e2070b60f68888182cf0cd809
child 543032 93617011a15817e10794bbb6e1b1080f63d3de7a
push id123177
push usermstriemer@mozilla.com
push dateSat, 01 Aug 2020 15:42:11 +0000
treeherderautoland@f0120d6d9c98 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemalysz, sfoster
bugs1653392
milestone81.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1653392 - Basic tab modal print UI test r=emalysz,sfoster Differential Revision: https://phabricator.services.mozilla.com/D84245
toolkit/components/printing/content/print.js
toolkit/components/printing/tests/browser.ini
toolkit/components/printing/tests/browser_modal_print.js
toolkit/components/printing/tests/head.js
--- 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 = true # Bug 1656688
+
 [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;
+  }
+}