summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_browsingContext-01.js
blob: 95831d25672469626cb63f4909c89349bf8dedd3 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const URL = "about:blank";

async function getBrowsingContextId(browser, id) {
  return SpecialPowers.spawn(browser, [id], async function (id) {
    let contextId = content.window.docShell.browsingContext.id;

    let frames = [content.window];
    while (frames.length) {
      let frame = frames.pop();
      let target = frame.document.getElementById(id);
      if (target) {
        contextId = target.docShell.browsingContext.id;
        break;
      }

      frames = frames.concat(Array.from(frame.frames));
    }

    return contextId;
  });
}

async function addFrame(browser, id, parentId) {
  return SpecialPowers.spawn(
    browser,
    [{ parentId, id }],
    async function ({ parentId, id }) {
      let parent = null;
      if (parentId) {
        let frames = [content.window];
        while (frames.length) {
          let frame = frames.pop();
          let target = frame.document.getElementById(parentId);
          if (target) {
            parent = target.contentWindow.document.body;
            break;
          }
          frames = frames.concat(Array.from(frame.frames));
        }
      } else {
        parent = content.document.body;
      }

      let frame = await new Promise(resolve => {
        let frame = content.document.createElement("iframe");
        frame.id = id || "";
        frame.url = "about:blank";
        frame.onload = () => resolve(frame);
        parent.appendChild(frame);
      });

      return frame.contentWindow.docShell.browsingContext.id;
    }
  );
}

async function removeFrame(browser, id) {
  return SpecialPowers.spawn(browser, [id], async function (id) {
    let frames = [content.window];
    while (frames.length) {
      let frame = frames.pop();
      let target = frame.document.getElementById(id);
      if (target) {
        target.remove();
        break;
      }

      frames = frames.concat(Array.from(frame.frames));
    }
  });
}

function getBrowsingContextById(id) {
  return BrowsingContext.get(id);
}

add_task(async function () {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: URL },
    async function (browser) {
      let topId = await getBrowsingContextId(browser, "");
      let topContext = getBrowsingContextById(topId);
      isnot(topContext, null);
      is(topContext.parent, null);
      is(
        topId,
        browser.browsingContext.id,
        "<browser> has the correct browsingContext"
      );
      is(
        browser.browserId,
        topContext.browserId,
        "browsing context should have a correct <browser> id"
      );

      let id0 = await addFrame(browser, "frame0");
      let browsingContext0 = getBrowsingContextById(id0);
      isnot(browsingContext0, null);
      is(browsingContext0.parent, topContext);

      await removeFrame(browser, "frame0");

      is(topContext.children.indexOf(browsingContext0), -1);

      // TODO(farre): Handle browsingContext removal [see Bug 1486719].
      todo_isnot(browsingContext0.parent, topContext);
    }
  );
});

add_task(async function () {
  // If Fission is disabled, the pref is no-op.
  await SpecialPowers.pushPrefEnv({ set: [["fission.bfcacheInParent", true]] });

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url:
        getRootDirectory(gTestPath).replace(
          "chrome://mochitests/content",
          // eslint-disable-next-line @microsoft/sdl/no-insecure-url
          "http://example.com"
        ) + "dummy_page.html",
    },
    async function (browser) {
      let path = getRootDirectory(gTestPath).replace(
        "chrome://mochitests/content",
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://example.com"
      );
      await SpecialPowers.spawn(browser, [path], async function (path) {
        var bc = new content.BroadcastChannel("browser_browsingContext");
        function waitForMessage(command) {
          let p = new Promise(resolve => {
            bc.addEventListener("message", e => resolve(e), { once: true });
          });
          command();
          return p;
        }

        // Open a new window and wait for the message.
        let e1 = await waitForMessage(_ =>
          content.window.open(path + "onpageshow_message.html", "", "noopener")
        );

        is(e1.data, "pageshow", "Got page show");

        let e2 = await waitForMessage(_ => bc.postMessage("createiframe"));
        is(e2.data.framesLength, 1, "Here we should have an iframe");

        let e3 = await waitForMessage(_ => bc.postMessage("nextpage"));

        is(e3.data.event, "load");
        is(e3.data.framesLength, 0, "Here there shouldn't be an iframe");

        // Return to the previous document. N.B. we expect to trigger
        // BFCache here, hence we wait for pageshow.
        let e4 = await waitForMessage(_ => bc.postMessage("back"));

        is(e4.data, "pageshow");

        let e5 = await waitForMessage(_ => bc.postMessage("queryframes"));
        is(e5.data.framesLength, 1, "And again there should be an iframe");

        is(e5.outerWindowId, e2.outerWindowId, "BF cache cached outer window");
        is(e5.browsingContextId, e2.browsingContextId, "BF cache cached BC");

        let e6 = await waitForMessage(_ => bc.postMessage("close"));
        is(e6.data, "closed");

        bc.close();
      });
    }
  );
});