summaryrefslogtreecommitdiffstats
path: root/toolkit/components/printing/tests/browser_page_change_print_original.js
blob: 4b85820466bb80a5ccfc1181a1e6c1a84c26e55f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// This file spawns content tasks.
/* global content */

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.com"
);

/**
 * 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(async function pp_after_orientation_change() {
  await SpecialPowers.pushPrefEnv({
    set: [["print.tab_modal.enabled", false]],
  });

  const URI = TEST_PATH + "file_page_change_print_original_1.html";
  // 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 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    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 = PrintHelper.waitForOldPrintPreview(ppBrowser);
  document.getElementById("cmd_printPreview").doCommand();
  await printPreviewEntered;

  // Assert that we are showing the original page
  await SpecialPowers.spawn(ppBrowser, [], async function() {
    is(
      content.document.body.textContent.trim(),
      "INITIAL PAGE",
      "Should have initial page print previewed."
    );
  });

  await 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.getElementById("print-preview-toolbar");

  printPreviewEntered = PrintHelper.waitForOldPrintPreview(ppBrowser);
  printPreviewToolbar.orient(orientToSwitchTo);
  await printPreviewEntered;

  // Check that we're still showing the original page.
  await SpecialPowers.spawn(ppBrowser, [], async function() {
    is(
      content.document.body.textContent.trim(),
      "INITIAL PAGE",
      "Should still have initial page print previewed."
    );
  });

  // Check that the other tab is definitely showing the new page:
  await SpecialPowers.spawn(browserToPrint, [], async function() {
    is(
      content.document.body.textContent.trim(),
      "REPLACED PAGE!",
      "Original page should have changed."
    );
  });

  PrintUtils.exitPrintPreview();

  BrowserTestUtils.removeTab(tab);
});