summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/browser_isactiveintab.js
blob: bd189f1826212b89a8ec76a40f9685b2df72f56f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function documentURL(origin, html) {
  let params = new URLSearchParams();
  params.append("html", html.trim());
  return `${origin}/document-builder.sjs?${params.toString()}`;
}

add_task(async function checkIsActiveInTab() {
  // This test creates a few tricky situations with navigation and iframes and
  // examines the results of various ways you might think to check if a page
  // is currently visible, and confirms that isActiveInTab works, even if the
  // others don't.

  // Make a top level page with two nested iframes.
  const IFRAME2_URL = documentURL("https://example.com", `<h1>iframe2</h1>`);
  const IFRAME1_URL = documentURL(
    "https://example.com",
    `<iframe src=${JSON.stringify(IFRAME2_URL)}></iframe><h1>iframe1</h1>`
  );
  const TEST_URL = documentURL(
    "https://example.com",
    `<iframe src=${JSON.stringify(IFRAME1_URL)}></iframe><h1>top window</h1>`
  );

  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
    let topBC1 = browser.browsingContext;
    let topWindowGlobal1 = topBC1.currentWindowGlobal;

    is(
      browser.browsingContext.children.length,
      1,
      "only one child for top window"
    );
    let iframe1 = browser.browsingContext.children[0];
    let iframeWindowGlobal1a = iframe1.currentWindowGlobal;

    is(iframe1.children.length, 1, "only one child for iframe");
    let iframe2 = iframe1.children[0];
    let iframeWindowGlobal2 = iframe2.currentWindowGlobal;

    ok(topWindowGlobal1.isActiveInTab, "top window global is active in tab");
    ok(iframeWindowGlobal1a.isActiveInTab, "topmost iframe is active in tab");
    ok(iframeWindowGlobal2.isActiveInTab, "innermost iframe is active in tab");

    // Do a same-origin navigation on the topmost iframe.
    let iframe1bURI =
      "https://example.com/browser/dom/ipc/tests/file_dummy.html";
    let loadedIframe = BrowserTestUtils.browserLoaded(
      browser,
      true,
      iframe1bURI
    );
    await SpecialPowers.spawn(
      iframe1,
      [iframe1bURI],
      async function (_iframe1bURI) {
        content.location = _iframe1bURI;
      }
    );
    await loadedIframe;

    ok(
      topWindowGlobal1.isActiveInTab,
      "top window global is still active in tab"
    );

    let iframeWindowGlobal1b = iframe1.currentWindowGlobal;
    isnot(
      iframeWindowGlobal1a,
      iframeWindowGlobal1b,
      "navigating changed the iframe's current window"
    );

    // This tests the !CanSend() case but unfortunately not the
    // `bc->GetCurrentWindowGlobal() != this` case. Apparently the latter will
    // only hold temporarily, so that is likely hard to test.
    ok(
      !iframeWindowGlobal1a.isActiveInTab,
      "topmost iframe is not active in tab"
    );

    ok(iframeWindowGlobal1b.isActiveInTab, "new iframe is active in tab");

    is(
      iframe2.currentWindowGlobal,
      iframeWindowGlobal2,
      "innermost iframe current window global has not changed"
    );

    ok(
      iframeWindowGlobal2.isCurrentGlobal,
      "innermost iframe is still the current global for its BC"
    );

    // With a same-origin navigation, this hits the !bc->AncestorsAreCurrent()
    // case. (With a cross-origin navigation, this hits the !CanSend() case.)
    ok(
      !iframeWindowGlobal2.isActiveInTab,
      "innermost iframe is not active in tab"
    );

    // Load a new page into the tab to test the behavior when a page is in
    // the BFCache.
    let newTopURI = "https://example.com/browser/dom/ipc/tests/file_dummy.html";
    let loadedTop2 = BrowserTestUtils.browserLoaded(browser, false, newTopURI);
    await BrowserTestUtils.startLoadingURIString(browser, newTopURI);
    await loadedTop2;

    isnot(browser.browsingContext, topBC1, "Navigated to a new BC");

    is(
      topBC1.currentWindowGlobal,
      topWindowGlobal1,
      "old top window is still the current window global for the old BC"
    );
    ok(topWindowGlobal1.isInBFCache, "old top window's BC is in the BFCache");
    ok(!topWindowGlobal1.isCurrentGlobal, "old top frame isn't current");
    ok(!topWindowGlobal1.isActiveInTab, "old top frame not active in tab");

    is(
      iframe1.currentWindowGlobal,
      iframeWindowGlobal1b,
      "old iframe is still the current window global for the BC"
    );
    ok(!iframeWindowGlobal1b.isCurrentGlobal, "newer top iframe isn't current");
    ok(
      iframeWindowGlobal1b.isInBFCache,
      "old top window's BC is in the BFCache"
    );
    ok(iframe1.ancestorsAreCurrent, "ancestors of iframe are current");
    ok(
      !iframeWindowGlobal1b.isActiveInTab,
      "newer top iframe is active in not active in tab after top level navigation"
    );
  });
});