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

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

const TOPIC = "browsing-context-attached";

async function observeAttached(callback) {
  let attached = new Map();

  function observer(subject, topic, data) {
    is(topic, TOPIC, "observing correct topic");
    ok(BrowsingContext.isInstance(subject), "subject to be a BrowsingContext");
    is(typeof data, "string", "data to be a String");
    info(`*** bc id=${subject.id}, why=${data}`);
    attached.set(subject.id, { browsingContext: subject, why: data });
  }

  Services.obs.addObserver(observer, TOPIC);
  try {
    await callback();
    return attached;
  } finally {
    Services.obs.removeObserver(observer, TOPIC);
  }
}

add_task(async function toplevelForNewWindow() {
  let win;

  let attached = await observeAttached(async () => {
    win = await BrowserTestUtils.openNewBrowserWindow();
  });

  ok(
    attached.has(win.browsingContext.id),
    "got notification for window's chrome browsing context"
  );
  is(
    attached.get(win.browsingContext.id).why,
    "attach",
    "expected reason for chrome browsing context"
  );

  ok(
    attached.has(win.gBrowser.selectedBrowser.browsingContext.id),
    "got notification for toplevel browsing context"
  );
  is(
    attached.get(win.gBrowser.selectedBrowser.browsingContext.id).why,
    "attach",
    "expected reason for toplevel browsing context"
  );

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function toplevelForNewTab() {
  let tab;

  let attached = await observeAttached(async () => {
    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  });

  ok(
    !attached.has(window.browsingContext.id),
    "no notification for the current window's chrome browsing context"
  );
  ok(
    attached.has(tab.linkedBrowser.browsingContext.id),
    "got notification for toplevel browsing context"
  );
  is(
    attached.get(tab.linkedBrowser.browsingContext.id).why,
    "attach",
    "expected reason for toplevel browsing context"
  );

  BrowserTestUtils.removeTab(tab);
});

add_task(async function subframe() {
  let browsingContext;
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);

  let attached = await observeAttached(async () => {
    browsingContext = await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
      let iframe = content.document.createElement("iframe");
      content.document.body.appendChild(iframe);
      iframe.contentWindow.location = "https://example.com/";
      return iframe.browsingContext;
    });
  });

  ok(
    !attached.has(window.browsingContext.id),
    "no notification for the current window's chrome browsing context"
  );
  ok(
    !attached.has(tab.linkedBrowser.browsingContext.id),
    "no notification for toplevel browsing context"
  );
  ok(
    attached.has(browsingContext.id),
    "got notification for frame's browsing context"
  );
  is(
    attached.get(browsingContext.id).why,
    "attach",
    "expected reason for frame's browsing context"
  );

  BrowserTestUtils.removeTab(tab);
});

add_task(async function toplevelReplacedBy() {
  let tab;

  let attached = await observeAttached(async () => {
    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
  });

  const firstContext = tab.linkedBrowser.browsingContext;
  ok(
    attached.has(firstContext.id),
    "got notification for initial toplevel browsing context"
  );
  is(
    attached.get(firstContext.id).why,
    "attach",
    "expected reason for initial toplevel browsing context"
  );

  attached = await observeAttached(async () => {
    await loadURI(TEST_PATH);
  });
  const secondContext = tab.linkedBrowser.browsingContext;
  ok(
    attached.has(secondContext.id),
    "got notification for replaced toplevel browsing context"
  );
  isnot(secondContext, firstContext, "browsing context to be replaced");
  is(
    attached.get(secondContext.id).why,
    "replace",
    "expected reason for replaced toplevel browsing context"
  );
  is(
    secondContext.browserId,
    firstContext.browserId,
    "browserId has been kept"
  );

  attached = await observeAttached(async () => {
    await loadURI("about:robots");
  });
  const thirdContext = tab.linkedBrowser.browsingContext;
  ok(
    attached.has(thirdContext.id),
    "got notification for replaced toplevel browsing context"
  );
  isnot(thirdContext, secondContext, "browsing context to be replaced");
  is(
    attached.get(thirdContext.id).why,
    "replace",
    "expected reason for replaced toplevel browsing context"
  );
  is(
    thirdContext.browserId,
    secondContext.browserId,
    "browserId has been kept"
  );

  BrowserTestUtils.removeTab(tab);
});