summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/browser/browser_TabManager.js
blob: 9229d1762bf32901a7637a1cf39b09fd6015743b (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

const FRAME_URL = "https://example.com/document-builder.sjs?html=frame";
const FRAME_MARKUP = `<iframe src="${encodeURI(FRAME_URL)}"></iframe>`;
const TEST_URL = `https://example.com/document-builder.sjs?html=${encodeURI(
  FRAME_MARKUP
)}`;

add_task(async function test_getBrowsingContextById() {
  const browser = gBrowser.selectedBrowser;

  is(TabManager.getBrowsingContextById(null), null);
  is(TabManager.getBrowsingContextById(undefined), null);
  is(TabManager.getBrowsingContextById("wrong-id"), null);

  info(`Navigate to ${TEST_URL}`);
  const loaded = BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.loadURIString(browser, TEST_URL);
  await loaded;

  const contexts = browser.browsingContext.getAllBrowsingContextsInSubtree();
  is(contexts.length, 2, "Top context has 1 child");

  const topContextId = TabManager.getIdForBrowsingContext(contexts[0]);
  is(TabManager.getBrowsingContextById(topContextId), contexts[0]);
  const childContextId = TabManager.getIdForBrowsingContext(contexts[1]);
  is(TabManager.getBrowsingContextById(childContextId), contexts[1]);
});

add_task(async function test_addTab_focus() {
  let tabsCount = gBrowser.tabs.length;

  let newTab1, newTab2, newTab3;
  try {
    newTab1 = await TabManager.addTab({ focus: true });

    ok(gBrowser.tabs.includes(newTab1), "A new tab was created");
    is(gBrowser.tabs.length, tabsCount + 1);
    is(gBrowser.selectedTab, newTab1, "Tab added with focus: true is selected");

    newTab2 = await TabManager.addTab({ focus: false });

    ok(gBrowser.tabs.includes(newTab2), "A new tab was created");
    is(gBrowser.tabs.length, tabsCount + 2);
    is(
      gBrowser.selectedTab,
      newTab1,
      "Tab added with focus: false is not selected"
    );

    newTab3 = await TabManager.addTab();

    ok(gBrowser.tabs.includes(newTab3), "A new tab was created");
    is(gBrowser.tabs.length, tabsCount + 3);
    is(
      gBrowser.selectedTab,
      newTab1,
      "Tab added with no focus parameter is not selected (defaults to false)"
    );
  } finally {
    gBrowser.removeTab(newTab1);
    gBrowser.removeTab(newTab2);
    gBrowser.removeTab(newTab3);
  }
});

add_task(async function test_addTab_referenceTab() {
  let tab1, tab2, tab3, tab4;
  try {
    tab1 = await TabManager.addTab();
    // Add a second tab with no referenceTab, should be added at the end.
    tab2 = await TabManager.addTab();
    // Add a third tab with tab1 as referenceTab, should be added right after tab1.
    tab3 = await TabManager.addTab({ referenceTab: tab1 });
    // Add a fourth tab with tab2 as referenceTab, should be added right after tab2.
    tab4 = await TabManager.addTab({ referenceTab: tab2 });

    // Check that the tab order is as expected: tab1 > tab3 > tab2 > tab4
    const tab1Index = gBrowser.tabs.indexOf(tab1);
    is(gBrowser.tabs[tab1Index + 1], tab3);
    is(gBrowser.tabs[tab1Index + 2], tab2);
    is(gBrowser.tabs[tab1Index + 3], tab4);
  } finally {
    gBrowser.removeTab(tab1);
    gBrowser.removeTab(tab2);
    gBrowser.removeTab(tab3);
    gBrowser.removeTab(tab4);
  }
});

add_task(async function test_addTab_window() {
  const win1 = await BrowserTestUtils.openNewBrowserWindow();
  const win2 = await BrowserTestUtils.openNewBrowserWindow();
  try {
    // openNewBrowserWindow should ensure the new window is focused.
    is(Services.wm.getMostRecentBrowserWindow(null), win2);

    const newTab1 = await TabManager.addTab({ window: win1 });
    is(
      newTab1.ownerGlobal,
      win1,
      "The new tab was opened in the specified window"
    );

    const newTab2 = await TabManager.addTab({ window: win2 });
    is(
      newTab2.ownerGlobal,
      win2,
      "The new tab was opened in the specified window"
    );

    const newTab3 = await TabManager.addTab();
    is(
      newTab3.ownerGlobal,
      win2,
      "The new tab was opened in the foreground window"
    );
  } finally {
    await BrowserTestUtils.closeWindow(win1);
    await BrowserTestUtils.closeWindow(win2);
  }
});

add_task(async function test_getTabForBrowsingContext() {
  const tab = await TabManager.addTab();
  try {
    const browser = tab.linkedBrowser;

    info(`Navigate to ${TEST_URL}`);
    const loaded = BrowserTestUtils.browserLoaded(browser);
    BrowserTestUtils.loadURIString(browser, TEST_URL);
    await loaded;

    const contexts = browser.browsingContext.getAllBrowsingContextsInSubtree();
    is(TabManager.getTabForBrowsingContext(contexts[0]), tab);
    is(TabManager.getTabForBrowsingContext(contexts[1]), tab);
    is(TabManager.getTabForBrowsingContext(null), null);
  } finally {
    gBrowser.removeTab(tab);
  }
});