summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_viewSourceInTabOnViewSource.js
blob: 26c040324aa8ff88a4773581146b8dd6cecc04d2 (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
function wait_while_tab_is_busy() {
  return new Promise(resolve => {
    let progressListener = {
      onStateChange(aWebProgress, aRequest, aStateFlags) {
        if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
          gBrowser.removeProgressListener(this);
          setTimeout(resolve, 0);
        }
      },
    };
    gBrowser.addProgressListener(progressListener);
  });
}

// This function waits for the tab to stop being busy instead of waiting for it
// to load, since the _elementsForViewSource change happens at that time.
var with_new_tab_opened = async function (options, taskFn) {
  let busyPromise = wait_while_tab_is_busy();
  let tab = await BrowserTestUtils.openNewForegroundTab(
    options.gBrowser,
    options.url,
    false
  );
  await busyPromise;
  await taskFn(tab.linkedBrowser);
  gBrowser.removeTab(tab);
};

add_task(async function test_regular_page() {
  function test_expect_view_source_enabled() {
    for (let element of [...XULBrowserWindow._elementsForViewSource]) {
      ok(!element.hasAttribute("disabled"), "View Source should be enabled");
    }
  }

  await with_new_tab_opened(
    {
      gBrowser,
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      url: "http://example.com",
    },
    test_expect_view_source_enabled
  );
});

add_task(async function test_view_source_page() {
  function test_expect_view_source_disabled() {
    for (let element of [...XULBrowserWindow._elementsForViewSource]) {
      ok(element.hasAttribute("disabled"), "View Source should be disabled");
    }
  }

  await with_new_tab_opened(
    {
      gBrowser,
      url: "view-source:http://example.com",
    },
    test_expect_view_source_disabled
  );
});