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

add_task(async function test_openPopup_requires_user_interaction() {
  async function backgroundScript() {
    browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tabInfo) => {
      if (changeInfo.status != "complete") {
        return;
      }
      await browser.pageAction.show(tabId);

      await browser.test.assertRejects(
        browser.pageAction.openPopup(),
        "pageAction.openPopup may only be called from a user input handler",
        "The error is informative."
      );
      await browser.test.assertRejects(
        browser.sidebarAction.open(),
        "sidebarAction.open may only be called from a user input handler",
        "The error is informative."
      );
      await browser.test.assertRejects(
        browser.sidebarAction.close(),
        "sidebarAction.close may only be called from a user input handler",
        "The error is informative."
      );
      await browser.test.assertRejects(
        browser.sidebarAction.toggle(),
        "sidebarAction.toggle may only be called from a user input handler",
        "The error is informative."
      );

      browser.runtime.onMessage.addListener(async msg => {
        browser.test.assertEq(msg, "from-panel", "correct message received");
        browser.test.sendMessage("panel-opened");
      });

      browser.test.sendMessage("ready");
    });
    browser.tabs.create({ url: "tab.html" });
  }

  let extensionData = {
    background: backgroundScript,
    manifest: {
      browser_action: {
        default_popup: "panel.html",
      },
      page_action: {
        default_popup: "panel.html",
      },
      sidebar_action: {
        default_panel: "panel.html",
      },
    },
    // We don't want the panel open automatically, so need a non-default reason.
    startupReason: "APP_STARTUP",

    files: {
      "tab.html": `
      <!DOCTYPE html>
      <html><head><meta charset="utf-8"></head><body>
      <button id="openPageAction">openPageAction</button>
      <button id="openSidebarAction">openSidebarAction</button>
      <button id="closeSidebarAction">closeSidebarAction</button>
      <button id="toggleSidebarAction">toggleSidebarAction</button>
      <script src="tab.js"></script>
      </body></html>
      `,
      "panel.html": `
      <!DOCTYPE html>
      <html><head><meta charset="utf-8"></head><body>
      <script src="panel.js"></script>
      </body></html>
      `,
      "tab.js": function () {
        document.getElementById("openPageAction").addEventListener(
          "click",
          () => {
            browser.pageAction.openPopup();
          },
          { once: true }
        );
        document.getElementById("openSidebarAction").addEventListener(
          "click",
          () => {
            browser.sidebarAction.open();
          },
          { once: true }
        );
        document.getElementById("closeSidebarAction").addEventListener(
          "click",
          () => {
            browser.sidebarAction.close();
          },
          { once: true }
        );
        /* eslint-disable mozilla/balanced-listeners */
        document
          .getElementById("toggleSidebarAction")
          .addEventListener("click", () => {
            browser.sidebarAction.toggle();
          });
        /* eslint-enable mozilla/balanced-listeners */
      },
      "panel.js": function () {
        browser.runtime.sendMessage("from-panel");
      },
    },
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  async function click(id) {
    let open = extension.awaitMessage("panel-opened");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      id,
      {},
      gBrowser.selectedBrowser
    );
    return open;
  }

  await extension.startup();
  await extension.awaitMessage("ready");

  await click("#openPageAction");
  closePageAction(extension);
  await new Promise(resolve => setTimeout(resolve, 0));

  await click("#openSidebarAction");

  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#closeSidebarAction",
    {},
    gBrowser.selectedBrowser
  );
  await TestUtils.waitForCondition(() => !SidebarUI.isOpen);

  await click("#toggleSidebarAction");
  await TestUtils.waitForCondition(() => SidebarUI.isOpen);
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#toggleSidebarAction",
    {},
    gBrowser.selectedBrowser
  );
  await TestUtils.waitForCondition(() => !SidebarUI.isOpen);

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
  await extension.unload();
});