summaryrefslogtreecommitdiffstats
path: root/toolkit/components/viewsource/test/browser/browser_viewsource_newwindow.js
blob: 8a38d94719f16d59660563b354ebaedbd64404a7 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * Waits for a View Source window to be opened at a particular
 * URL.
 *
 * @param {string} expectedURL The view-source: URL that's expected.
 * @resolves {DOM Window} The window that was opened.
 * @returns {Promise}
 */
async function waitForNewViewSourceWindow(expectedURL) {
  let win = await BrowserTestUtils.domWindowOpened();
  await BrowserTestUtils.waitForEvent(win, "EndSwapDocShells", true);
  let browser = win.gBrowser.selectedBrowser;
  if (browser.currentURI.spec != expectedURL) {
    await BrowserTestUtils.browserLoaded(browser, false, expectedURL);
  }
  return win;
}

/**
 * When view_source.tab is set to false, view source should
 * open in new browser window instead of new tab.
 */
add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["view_source.tab", false]],
  });

  const PAGE = "http://example.com/";
  await BrowserTestUtils.withNewTab(
    {
      url: PAGE,
      gBrowser,
    },
    async browser => {
      let winPromise = waitForNewViewSourceWindow("view-source:" + PAGE);
      BrowserViewSource(browser);
      let win = await winPromise;

      ok(win, "View Source opened up in a new window.");
      await BrowserTestUtils.closeWindow(win);
    }
  );
});

/**
 * When view_source.tab is set to false, view partial source
 * should open up in new browser window instead of new tab.
 */
add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["view_source.tab", false]],
  });

  const para = "<p>test</p>";
  const source = `<html><body>${para}</body></html>`;
  await BrowserTestUtils.withNewTab(
    {
      url: "data:text/html," + source,
      gBrowser,
    },
    async browser => {
      let winPromise = waitForNewViewSourceWindow(
        "view-source:data:text/html;charset=utf-8,%3Cp%3E%EF%B7%90test%EF%B7%AF%3C%2Fp%3E"
      );
      await SpecialPowers.spawn(
        gBrowser.selectedBrowser,
        [],
        async function (arg) {
          let element = content.document.querySelector("p");
          content.getSelection().selectAllChildren(element);
        }
      );

      let contentAreaContextMenuPopup = document.getElementById(
        "contentAreaContextMenu"
      );
      let popupShownPromise = BrowserTestUtils.waitForEvent(
        contentAreaContextMenuPopup,
        "popupshown"
      );
      await BrowserTestUtils.synthesizeMouseAtCenter(
        "p",
        { type: "contextmenu", button: 2 },
        gBrowser.selectedBrowser
      );
      await popupShownPromise;

      let popupHiddenPromise = BrowserTestUtils.waitForEvent(
        contentAreaContextMenuPopup,
        "popuphidden"
      );
      let item = document.getElementById("context-viewpartialsource-selection");
      contentAreaContextMenuPopup.activateItem(item);
      await popupHiddenPromise;
      dump("Before winPromise");
      let win = await winPromise;
      dump("After winPromise");
      ok(win, "View Partial Source opened up in a new window.");
      await BrowserTestUtils.closeWindow(win);
    }
  );
});