summaryrefslogtreecommitdiffstats
path: root/browser/components/sidebar/tests/browser/browser_extensions_sidebar.js
blob: 0413849fd25e00b474fd3c9b3fd37602e81bf8bf (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Any copyright is dedicated to the Public Domain.
   https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_setup(() => SpecialPowers.pushPrefEnv({ set: [["sidebar.revamp", true]] }));
registerCleanupFunction(() => SpecialPowers.popPrefEnv());

const imageBuffer = imageBufferFromDataURI(
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg=="
);

function imageBufferFromDataURI(encodedImageData) {
  const decodedImageData = atob(encodedImageData);
  return Uint8Array.from(decodedImageData, byte => byte.charCodeAt(0)).buffer;
}

/* global browser */
const extData = {
  manifest: {
    sidebar_action: {
      default_icon: "default.png",
      default_panel: "default.html",
      default_title: "Default Title",
    },
  },
  useAddonManager: "temporary",

  files: {
    "default.html": `
      <!DOCTYPE html>
      <html>
      <head><meta charset="utf-8"/>
      <script src="sidebar.js"></script>
      </head>
      <body>
      A Test Sidebar
      </body></html>
    `,
    "sidebar.js": function () {
      window.onload = () => {
        browser.test.sendMessage("sidebar");
      };
    },
    "1.html": `
      <!DOCTYPE html>
      <html>
      <head><meta charset="utf-8"/></head>
      <body>
      A Test Sidebar
      </body></html>
    `,
    "default.png": imageBuffer,
    "1.png": imageBuffer,
  },

  background() {
    browser.test.onMessage.addListener(async ({ msg, data }) => {
      switch (msg) {
        case "set-icon":
          await browser.sidebarAction.setIcon({ path: data });
          break;
        case "set-panel":
          await browser.sidebarAction.setPanel({ panel: data });
          break;
        case "set-title":
          await browser.sidebarAction.setTitle({ title: data });
          break;
      }
      browser.test.sendMessage("done");
    });
  },
};

async function sendMessage(extension, msg, data) {
  extension.sendMessage({ msg, data });
  await extension.awaitMessage("done");
}

add_task(async function test_extension_sidebar_actions() {
  // TODO: Once `sidebar.revamp` is either enabled by default, or removed
  // entirely, this test should run in the current window, and it should only
  // await one "sidebar" message.
  const win = await BrowserTestUtils.openNewBrowserWindow();
  const { document } = win;
  const sidebar = document.getElementById("sidebar-main");
  ok(sidebar, "Sidebar is shown.");

  const extension = ExtensionTestUtils.loadExtension({ ...extData });
  await extension.startup();
  await extension.awaitMessage("sidebar");
  await extension.awaitMessage("sidebar");
  is(sidebar.extensionButtons.length, 1, "Extension is shown in the sidebar.");

  // Default icon and title matches.
  const button = sidebar.extensionButtons[0];
  let iconUrl = `moz-extension://${extension.uuid}/default.png`;
  is(
    button.style.getPropertyValue("--action-icon"),
    `image-set(url("${iconUrl}"), url("${iconUrl}") 2x)`,
    "Extension has the correct icon."
  );
  is(button.title, "Default Title", "Extension has the correct title.");

  // Icon can be updated.
  await sendMessage(extension, "set-icon", "1.png");
  await sidebar.updateComplete;
  iconUrl = `moz-extension://${extension.uuid}/1.png`;
  is(
    button.style.getPropertyValue("--action-icon"),
    `image-set(url("${iconUrl}"), url("${iconUrl}") 2x)`,
    "Extension has updated icon."
  );

  // Title can be updated.
  await sendMessage(extension, "set-title", "Updated Title");
  await sidebar.updateComplete;
  is(button.title, "Updated Title", "Extension has updated title.");

  // Panel can be updated.
  await sendMessage(extension, "set-panel", "1.html");
  const panelUrl = `moz-extension://${extension.uuid}/1.html`;
  await TestUtils.waitForCondition(() => {
    const browser = SidebarController.browser.contentDocument.getElementById(
      "webext-panels-browser"
    );
    return browser.currentURI.spec === panelUrl;
  }, "The new panel is visible.");

  await extension.unload();
  await sidebar.updateComplete;
  is(
    sidebar.extensionButtons.length,
    0,
    "Extension is removed from the sidebar."
  );
  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_open_new_window_after_install() {
  const extension = ExtensionTestUtils.loadExtension({ ...extData });
  await extension.startup();
  await extension.awaitMessage("sidebar");

  const win = await BrowserTestUtils.openNewBrowserWindow();
  const { document } = win;
  const sidebar = document.getElementById("sidebar-main");
  ok(sidebar, "Sidebar is shown.");
  await extension.awaitMessage("sidebar");
  is(
    sidebar.extensionButtons.length,
    1,
    "Extension is shown in new browser window."
  );

  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url: "about:addons" },
    async browser => {
      await BrowserTestUtils.synthesizeMouseAtCenter(
        "categories-box button[name=extension]",
        {},
        browser
      );
      const extensionToggle = await TestUtils.waitForCondition(
        () =>
          browser.contentDocument.querySelector(
            `addon-card[addon-id="${extension.id}"] moz-toggle`
          ),
        "Toggle button for extension is shown."
      );

      let promiseEvent = BrowserTestUtils.waitForEvent(
        win,
        "SidebarItemRemoved"
      );
      extensionToggle.click();
      await promiseEvent;
      await sidebar.updateComplete;
      is(sidebar.extensionButtons.length, 0, "The extension is disabled.");

      promiseEvent = BrowserTestUtils.waitForEvent(win, "SidebarItemAdded");
      extensionToggle.click();
      await promiseEvent;
      await sidebar.updateComplete;
      is(sidebar.extensionButtons.length, 1, "The extension is enabled.");
    }
  );

  await extension.unload();
  await sidebar.updateComplete;
  is(
    sidebar.extensionButtons.length,
    0,
    "Extension is removed from the sidebar."
  );
  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_open_new_private_window_after_install() {
  const extension = ExtensionTestUtils.loadExtension({ ...extData });
  await extension.startup();
  await extension.awaitMessage("sidebar");

  const privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  const { document } = privateWin;
  const sidebar = document.getElementById("sidebar-main");
  ok(sidebar, "Sidebar is shown.");
  await TestUtils.waitForCondition(
    () => sidebar.extensionButtons,
    "Extensions container is shown."
  );
  is(
    sidebar.extensionButtons.length,
    0,
    "Extension is hidden in private browser window."
  );

  await extension.unload();
  await BrowserTestUtils.closeWindow(privateWin);
});