summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_dbg_multiple-windows.js
blob: 2e2013479c6ba0cce9661e3457f611bd4442fc0e (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Make sure that the debugger attaches to the right tab when multiple windows
 * are open.
 */

var {
  DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");
var {
  DevToolsClient,
} = require("resource://devtools/client/devtools-client.js");

const TAB1_URL = "data:text/html;charset=utf-8,first-tab";
const TAB2_URL = "data:text/html;charset=utf-8,second-tab";

add_task(async function () {
  DevToolsServer.init();
  DevToolsServer.registerAllActors();

  const transport = DevToolsServer.connectPipe();
  const client = new DevToolsClient(transport);
  const [type] = await client.connect();
  is(type, "browser", "Root actor should identify itself as a browser.");

  const tab = await addTab(TAB1_URL);
  await testFirstTab(client, tab);
  const win = await addWindow(TAB2_URL);
  await testNewWindow(client, win);
  testFocusFirst(client);
  await testRemoveTab(client, win, tab);
  await client.close();
});

async function testFirstTab(client, tab) {
  ok(!!tab, "Second tab created.");

  const tabs = await client.mainRoot.listTabs();
  const targetFront = tabs.find(grip => grip.url == TAB1_URL);
  ok(targetFront, "Should find a target actor for the first tab.");

  ok(!tabs[0].selected, "The previously opened tab isn't selected.");
  ok(tabs[1].selected, "The first tab is selected.");
}

async function testNewWindow(client, win) {
  ok(!!win, "Second window created.");

  win.focus();

  const topWindow = Services.wm.getMostRecentWindow("navigator:browser");
  is(topWindow, win, "The second window is on top.");

  if (Services.focus.activeWindow != win) {
    await new Promise(resolve => {
      win.addEventListener(
        "activate",
        function onActivate(event) {
          if (event.target != win) {
            return;
          }
          win.removeEventListener("activate", onActivate, true);
          resolve();
        },
        true
      );
    });
  }

  const tabs = await client.mainRoot.listTabs();
  ok(!tabs[0].selected, "The previously opened tab isn't selected.");
  ok(!tabs[1].selected, "The first tab isn't selected.");
  ok(tabs[2].selected, "The second tab is selected.");
}

async function testFocusFirst(client) {
  const tab = window.gBrowser.selectedTab;
  await ContentTask.spawn(tab.linkedBrowser, null, async function () {
    const onFocus = new Promise(resolve => {
      content.addEventListener("focus", resolve, { once: true });
    });
    await onFocus;
  });

  const tabs = await client.mainRoot.listTabs();
  ok(!tabs[0].selected, "The previously opened tab isn't selected.");
  ok(!tabs[1].selected, "The first tab is selected after focusing on i.");
  ok(tabs[2].selected, "The second tab isn't selected.");
}

async function testRemoveTab(client, win, tab) {
  win.close();

  // give it time to close
  await new Promise(resolve => executeSoon(resolve));
  await continue_remove_tab(client, tab);
}

async function continue_remove_tab(client, tab) {
  removeTab(tab);

  const tabs = await client.mainRoot.listTabs();

  // Verify that tabs are no longer included in listTabs.
  const foundTab1 = tabs.some(grip => grip.url == TAB1_URL);
  const foundTab2 = tabs.some(grip => grip.url == TAB2_URL);
  ok(!foundTab1, "Tab1 should be gone.");
  ok(!foundTab2, "Tab2 should be gone.");

  ok(tabs[0].selected, "The previously opened tab is selected.");
}

async function addWindow(url) {
  info("Adding window: " + url);
  const onNewWindow = BrowserTestUtils.waitForNewWindow({ url });
  window.open(url, "_blank", "noopener");
  return onNewWindow;
}