summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_currentWindow.js
blob: 1ea7fc6eae2176d984a3f3a82e6f332c252333c3 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function genericChecker() {
  let kind = "background";
  let path = window.location.pathname;
  if (path.includes("/popup.html")) {
    kind = "popup";
  } else if (path.includes("/page.html")) {
    kind = "page";
  }

  browser.test.onMessage.addListener((msg, ...args) => {
    if (msg == kind + "-check-current1") {
      browser.tabs.query(
        {
          currentWindow: true,
        },
        function (tabs) {
          browser.test.sendMessage("result", tabs[0].windowId);
        }
      );
    } else if (msg == kind + "-check-current2") {
      browser.tabs.query(
        {
          windowId: browser.windows.WINDOW_ID_CURRENT,
        },
        function (tabs) {
          browser.test.sendMessage("result", tabs[0].windowId);
        }
      );
    } else if (msg == kind + "-check-current3") {
      browser.windows.getCurrent(function (window) {
        browser.test.sendMessage("result", window.id);
      });
    } else if (msg == kind + "-open-page") {
      browser.tabs.create({
        windowId: args[0],
        url: browser.runtime.getURL("page.html"),
      });
    } else if (msg == kind + "-close-page") {
      browser.tabs.query(
        {
          windowId: args[0],
        },
        tabs => {
          let tab = tabs.find(tab => tab.url.includes("/page.html"));
          browser.tabs.remove(tab.id, () => {
            browser.test.sendMessage("closed");
          });
        }
      );
    }
  });
  browser.test.sendMessage(kind + "-ready");
}

add_task(async function () {
  let win1 = await BrowserTestUtils.openNewBrowserWindow();
  let win2 = await BrowserTestUtils.openNewBrowserWindow();

  await focusWindow(win2);

  BrowserTestUtils.loadURIString(win1.gBrowser.selectedBrowser, "about:robots");
  await BrowserTestUtils.browserLoaded(win1.gBrowser.selectedBrowser);

  BrowserTestUtils.loadURIString(win2.gBrowser.selectedBrowser, "about:config");
  await BrowserTestUtils.browserLoaded(win2.gBrowser.selectedBrowser);

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],

      browser_action: {
        default_popup: "popup.html",
        default_area: "navbar",
      },
    },

    files: {
      "page.html": `
      <!DOCTYPE html>
      <html><body>
      <script src="page.js"></script>
      </body></html>
      `,

      "page.js": genericChecker,

      "popup.html": `
      <!DOCTYPE html>
      <html><body>
      <script src="popup.js"></script>
      </body></html>
      `,

      "popup.js": genericChecker,
    },

    background: genericChecker,
  });

  await Promise.all([
    extension.startup(),
    extension.awaitMessage("background-ready"),
  ]);

  const {
    Management: {
      global: { windowTracker },
    },
  } = ChromeUtils.importESModule("resource://gre/modules/Extension.sys.mjs");

  let winId1 = windowTracker.getId(win1);
  let winId2 = windowTracker.getId(win2);

  async function checkWindow(kind, winId, name) {
    extension.sendMessage(kind + "-check-current1");
    is(
      await extension.awaitMessage("result"),
      winId,
      `${name} is on top (check 1) [${kind}]`
    );
    extension.sendMessage(kind + "-check-current2");
    is(
      await extension.awaitMessage("result"),
      winId,
      `${name} is on top (check 2) [${kind}]`
    );
    extension.sendMessage(kind + "-check-current3");
    is(
      await extension.awaitMessage("result"),
      winId,
      `${name} is on top (check 3) [${kind}]`
    );
  }

  async function triggerPopup(win, callback) {
    await clickBrowserAction(extension, win);
    await awaitExtensionPanel(extension, win);

    await extension.awaitMessage("popup-ready");

    await callback();

    closeBrowserAction(extension, win);
  }

  await focusWindow(win1);
  await checkWindow("background", winId1, "win1");
  await triggerPopup(win1, async function () {
    await checkWindow("popup", winId1, "win1");
  });

  await focusWindow(win2);
  await checkWindow("background", winId2, "win2");
  await triggerPopup(win2, async function () {
    await checkWindow("popup", winId2, "win2");
  });

  async function triggerPage(winId, name) {
    extension.sendMessage("background-open-page", winId);
    await extension.awaitMessage("page-ready");
    await checkWindow("page", winId, name);
    extension.sendMessage("background-close-page", winId);
    await extension.awaitMessage("closed");
  }

  await triggerPage(winId1, "win1");
  await triggerPage(winId2, "win2");

  await extension.unload();

  await BrowserTestUtils.closeWindow(win1);
  await BrowserTestUtils.closeWindow(win2);
});