Bug 1329220 - Fix print preview source browser confusion. r=mconley, a=jcristau
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Mon, 09 Jan 2017 22:04:05 +0000
changeset 353725 8b41783ea267fa925414de5e63e03bb34ba7ff73
parent 353724 5ec6cfbdd4e1d46903134eff4d4e96655ff6edc8
child 353726 5c6671c08dbb19a4e873452d9eb1475db380bca6
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley, jcristau
bugs1329220, 1308621
milestone52.0a2
Bug 1329220 - Fix print preview source browser confusion. r=mconley, a=jcristau Restore overwriting of this._sourceBrowser as it was before bug 1308621 to ensure we continue to use the same DOM to do print previewing (and eventually printing). Bug 1308621 already changed the enterPrintPreview code to rely on the member _sourceBrowser variable, which will be updated to point to the print preview browser if/when print preview is being reinitialized for the same page (because one of the print settings changes). We need to do this to avoid re-initializing off the original browser, which may now have navigated or be displaying something else entirely. This also updates the 'simplified' mode code to rely on the extant _originalURL member to avoid displaying the page URL as about:blank after a settings change. MozReview-Commit-ID: DZ1kT7Mb0mS
toolkit/components/printing/content/printUtils.js
toolkit/components/printing/moz.build
toolkit/components/printing/tests/browser.ini
toolkit/components/printing/tests/browser_page_change_print_original.js
--- a/toolkit/components/printing/content/printUtils.js
+++ b/toolkit/components/printing/content/printUtils.js
@@ -200,18 +200,20 @@ var PrintUtils = {
       this._originalURL = this._sourceBrowser.currentURI.spec;
 
       // Here we log telemetry data for when the user enters print preview.
       this.logTelemetry("PRINT_PREVIEW_OPENED_COUNT");
     } else {
       // collapse the browser here -- it will be shown in
       // enterPrintPreview; this forces a reflow which fixes display
       // issues in bug 267422.
-      let ppBrowser = this._listener.getPrintPreviewBrowser();
-      ppBrowser.collapsed = true;
+      // We use the print preview browser as the source browser to avoid
+      // re-initializing print preview with a document that might now have changed.
+      this._sourceBrowser = this._listener.getPrintPreviewBrowser();
+      this._sourceBrowser.collapsed = true;
 
       // If the user transits too quickly within preview and we have a pending
       // progress dialog, we will close it before opening a new one.
       if (this._webProgressPP && this._webProgressPP.value) {
         this._webProgressPP.value.onStateChange(null, null,
           Components.interfaces.nsIWebProgressListener.STATE_STOP, 0);
       }
     }
@@ -529,17 +531,17 @@ var PrintUtils = {
           spMM.removeMessageListener("Printing:Preview:ReaderModeReady", onReaderReady);
           sendEnterPreviewMessage(simplifiedBrowser, true);
         });
 
         // Here, we send down a message to simplified browser in order to parse
         // the original page. After we have parsed it, content will tell parent
         // that the document is ready for print previewing.
         spMM.sendAsyncMessage("Printing:Preview:ParseDocument", {
-          URL: this._sourceBrowser.currentURI.spec,
+          URL: this._originalURL,
           windowID: this._sourceBrowser.outerWindowID,
         });
 
         // Here we log telemetry data for when the user enters simplify mode.
         this.logTelemetry("PRINT_PREVIEW_SIMPLIFY_PAGE_OPENED_COUNT");
       }
     } else {
       sendEnterPreviewMessage(this._sourceBrowser, false);
--- a/toolkit/components/printing/moz.build
+++ b/toolkit/components/printing/moz.build
@@ -1,10 +1,14 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 JAR_MANIFESTS += ['jar.mn']
 
+BROWSER_CHROME_MANIFESTS += [
+    'tests/browser.ini'
+]
+
 with Files('**'):
     BUG_COMPONENT = ('Toolkit', 'Printing')
new file mode 100644
--- /dev/null
+++ b/toolkit/components/printing/tests/browser.ini
@@ -0,0 +1,2 @@
+[browser_page_change_print_original.js]
+skip-if = os == "mac"
new file mode 100644
--- /dev/null
+++ b/toolkit/components/printing/tests/browser_page_change_print_original.js
@@ -0,0 +1,57 @@
+/**
+ * Verify that if the page contents change after print preview is initialized,
+ * and we re-initialize print preview (e.g. by changing page orientation),
+ * we still show (and will therefore print) the original contents.
+ */
+add_task(function* pp_after_orientation_change() {
+  const DATA_URI = `data:text/html,<script>window.onafterprint = function() { setTimeout("window.location = 'data:text/plain,REPLACED PAGE!'", 0); }</script><pre>INITIAL PAGE</pre>`;
+  // Can only do something if we have a print preview UI:
+  if (AppConstants.platform != "win" && AppConstants.platform != "linux") {
+    ok(true, "Can't test if there's no print preview.");
+    return;
+  }
+
+  // Ensure we get a browserStopped for this browser
+  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, DATA_URI, false, true);
+  let browserToPrint = tab.linkedBrowser;
+  let ppBrowser = PrintPreviewListener.getPrintPreviewBrowser();
+
+  // Get a promise now that resolves when the original tab's location changes.
+  let originalTabNavigated = BrowserTestUtils.browserStopped(browserToPrint);
+
+  // Enter print preview:
+  let printPreviewEntered = BrowserTestUtils.waitForMessage(ppBrowser.messageManager, "Printing:Preview:Entered");
+  document.getElementById("cmd_printPreview").doCommand();
+  yield printPreviewEntered;
+
+  // Assert that we are showing the original page
+  yield ContentTask.spawn(ppBrowser, null, function* () {
+    is(content.document.body.textContent, "INITIAL PAGE", "Should have initial page print previewed.");
+  });
+
+  yield originalTabNavigated;
+
+  // Change orientation and wait for print preview to re-enter:
+  let orient = PrintUtils.getPrintSettings().orientation;
+  let orientToSwitchTo = orient != Ci.nsIPrintSettings.kPortraitOrientation ?
+    "portrait" : "landscape";
+  let printPreviewToolbar = document.querySelector("toolbar[printpreview=true]");
+
+  printPreviewEntered = BrowserTestUtils.waitForMessage(ppBrowser.messageManager, "Printing:Preview:Entered");
+  printPreviewToolbar.orient(orientToSwitchTo);
+  yield printPreviewEntered;
+
+  // Check that we're still showing the original page.
+  yield ContentTask.spawn(ppBrowser, null, function* () {
+    is(content.document.body.textContent, "INITIAL PAGE", "Should still have initial page print previewed.");
+  });
+
+  // Check that the other tab is definitely showing the new page:
+  yield ContentTask.spawn(browserToPrint, null, function* () {
+    is(content.document.body.textContent, "REPLACED PAGE!", "Original page should have changed.");
+  });
+
+  PrintUtils.exitPrintPreview();
+
+  yield BrowserTestUtils.removeTab(tab);
+});