summaryrefslogtreecommitdiffstats
path: root/remote/shared/listeners/test/browser/browser_BrowsingContextListener.js
blob: 9a08df7857b64323ada5506f5f52cc4586cff4ed (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { BrowsingContextListener } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/listeners/BrowsingContextListener.sys.mjs"
);

add_task(async function test_attachedOnNewTab() {
  const listener = new BrowsingContextListener();
  const attached = listener.once("attached");

  listener.startListening();

  const tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  const { browsingContext, why } = await attached;

  is(
    browsingContext.id,
    tab.linkedBrowser.browsingContext.id,
    "Received expected browsing context"
  );
  is(why, "attach", "Browsing context has been attached");

  listener.stopListening();
  gBrowser.removeTab(tab);
});

add_task(async function test_attachedValidEmbedderElement() {
  const listener = new BrowsingContextListener();

  let hasEmbedderElement = false;
  listener.on(
    "attached",
    (evtName, { browsingContext }) => {
      hasEmbedderElement = !!browsingContext.embedderElement;
    },
    { once: true }
  );

  listener.startListening();

  const tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  ok(
    hasEmbedderElement,
    "Attached browsing context has a valid embedder element"
  );

  listener.stopListening();
  gBrowser.removeTab(tab);
});

add_task(async function test_discardedOnCloseTab() {
  const listener = new BrowsingContextListener();
  const discarded = listener.once("discarded");

  const tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  const browsingContext = tab.linkedBrowser.browsingContext;
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  listener.startListening();
  gBrowser.removeTab(tab);
  const { browsingContext: discardedBrowsingContext, why } = await discarded;

  is(
    discardedBrowsingContext.id,
    browsingContext.id,
    "Received expected browsing context"
  );
  is(why, "discard", "Browsing context has been discarded");

  listener.stopListening();
});

add_task(async function test_replaceTopLevelOnNavigation() {
  const listener = new BrowsingContextListener();
  const attached = listener.once("attached");
  const discarded = listener.once("discarded");

  const tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  const browsingContext = tab.linkedBrowser.browsingContext;
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  listener.startListening();

  await loadURL(tab.linkedBrowser, "about:mozilla");

  const discardEvent = await discarded;
  const attachEvent = await attached;

  is(
    discardEvent.browsingContext.id,
    browsingContext.id,
    "Received expected browsing context for discarded"
  );
  is(discardEvent.why, "replace", "Browsing context has been replaced");

  is(
    attachEvent.browsingContext.id,
    tab.linkedBrowser.browsingContext.id,
    "Received expected browsing context for attached"
  );
  is(discardEvent.why, "replace", "Browsing context has been replaced");

  isnot(
    discardEvent.browsingContext,
    attachEvent.browsingContext,
    "Got different browsing contexts"
  );

  listener.stopListening();
  gBrowser.removeTab(gBrowser.selectedTab);
});