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

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

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

    files: {
      "tab.js": function () {
        let url = document.location.href;

        browser.tabs.getCurrent(currentTab => {
          browser.test.assertEq(
            currentTab.url,
            url,
            "getCurrent in non-active background tab"
          );

          // Activate the tab.
          browser.tabs.onActivated.addListener(function listener({ tabId }) {
            if (tabId == currentTab.id) {
              browser.tabs.onActivated.removeListener(listener);

              browser.tabs.getCurrent(currentTab => {
                browser.test.assertEq(
                  currentTab.id,
                  tabId,
                  "in active background tab"
                );
                browser.test.assertEq(
                  currentTab.url,
                  url,
                  "getCurrent in non-active background tab"
                );

                browser.test.sendMessage("tab-finished");
              });
            }
          });
          browser.tabs.update(currentTab.id, { active: true });
        });
      },

      "popup.js": function () {
        browser.tabs.getCurrent(tab => {
          browser.test.assertEq(tab, undefined, "getCurrent in popup script");
          browser.test.sendMessage("popup-finished");
        });
      },

      "tab.html": `<head><meta charset="utf-8"><script src="tab.js"></script></head>`,
      "popup.html": `<head><meta charset="utf-8"><script src="popup.js"></script></head>`,
    },

    background: function () {
      browser.tabs.getCurrent(tab => {
        browser.test.assertEq(
          tab,
          undefined,
          "getCurrent in background script"
        );
        browser.test.sendMessage("background-finished");
      });

      browser.tabs.create({ url: "tab.html", active: false });
    },
  });

  await extension.startup();

  await extension.awaitMessage("background-finished");
  await extension.awaitMessage("tab-finished");

  clickBrowserAction(extension);
  await awaitExtensionPanel(extension);
  await extension.awaitMessage("popup-finished");
  await closeBrowserAction(extension);

  // The extension tab is automatically closed when the extension unloads.
  await extension.unload();
});