summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_history_triggeringprincipal_viewsource.js
blob: 808232460c277f7e0bb95a721101bb8c8858bb2a (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
"use strict";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://example.com"
);
const HTML_URI = TEST_PATH + "dummy_page.html";
const VIEW_SRC_URI = "view-source:" + HTML_URI;

add_task(async function() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.navigation.requireUserInteraction", false]],
  });

  info("load baseline html in new tab");
  await BrowserTestUtils.withNewTab(HTML_URI, async function(aBrowser) {
    is(
      gBrowser.selectedBrowser.currentURI.spec,
      HTML_URI,
      "sanity check to make sure html loaded"
    );

    info("right-click -> view-source of html");
    let vSrcCtxtMenu = document.getElementById("contentAreaContextMenu");
    let popupPromise = BrowserTestUtils.waitForEvent(
      vSrcCtxtMenu,
      "popupshown"
    );
    BrowserTestUtils.synthesizeMouseAtCenter(
      "body",
      { type: "contextmenu", button: 2 },
      aBrowser
    );
    await popupPromise;
    let tabPromise = BrowserTestUtils.waitForNewTab(gBrowser, VIEW_SRC_URI);
    let vSrcItem = vSrcCtxtMenu.querySelector("#context-viewsource");
    vSrcCtxtMenu.activateItem(vSrcItem);
    let tab = await tabPromise;
    is(
      gBrowser.selectedBrowser.currentURI.spec,
      VIEW_SRC_URI,
      "loading view-source of html succeeded"
    );

    info("load html file again before going .back()");
    let loadPromise = BrowserTestUtils.browserLoaded(
      tab.linkedBrowser,
      false,
      HTML_URI
    );
    await SpecialPowers.spawn(tab.linkedBrowser, [HTML_URI], HTML_URI => {
      content.document.location = HTML_URI;
    });
    await loadPromise;
    is(
      gBrowser.selectedBrowser.currentURI.spec,
      HTML_URI,
      "loading html another time succeeded"
    );

    info(
      "click .back() to view-source of html again and make sure the history entry has a triggeringPrincipal"
    );
    let backCtxtMenu = document.getElementById("contentAreaContextMenu");
    popupPromise = BrowserTestUtils.waitForEvent(backCtxtMenu, "popupshown");
    BrowserTestUtils.synthesizeMouseAtCenter(
      "body",
      { type: "contextmenu", button: 2 },
      aBrowser
    );
    await popupPromise;
    loadPromise = BrowserTestUtils.waitForContentEvent(
      tab.linkedBrowser,
      "pageshow"
    );
    let backItem = backCtxtMenu.querySelector("#context-back");
    backCtxtMenu.activateItem(backItem);
    await loadPromise;
    is(
      gBrowser.selectedBrowser.currentURI.spec,
      VIEW_SRC_URI,
      "clicking .back() to view-source of html succeeded"
    );

    BrowserTestUtils.removeTab(tab);
  });
});