summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_user_events.js
blob: 4852ffd124daaa0c774ac0e402c6c16e6e011ce8 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"use strict";

// Test that different types of events are all considered
// "handling user input".
add_task(async function testSources() {
  let extension = ExtensionTestUtils.loadExtension({
    async background() {
      async function request(perm) {
        try {
          let result = await browser.permissions.request({
            permissions: [perm],
          });
          browser.test.sendMessage("request", { success: true, result, perm });
        } catch (err) {
          browser.test.sendMessage("request", {
            success: false,
            errmsg: err.message,
            perm,
          });
        }
      }

      let tabs = await browser.tabs.query({
        active: true,
        currentWindow: true,
      });
      await browser.pageAction.show(tabs[0].id);

      browser.pageAction.onClicked.addListener(() => request("bookmarks"));
      browser.browserAction.onClicked.addListener(() => request("tabs"));
      browser.commands.onCommand.addListener(() => request("downloads"));

      browser.test.onMessage.addListener(msg => {
        if (msg === "contextMenus.update") {
          browser.contextMenus.onClicked.addListener(() =>
            request("webNavigation")
          );
          browser.contextMenus.update(
            "menu",
            {
              title: "test user events in onClicked",
              onclick: null,
            },
            () => browser.test.sendMessage("contextMenus.update-done")
          );
        }
        if (msg === "openOptionsPage") {
          browser.runtime.openOptionsPage();
        }
      });

      browser.contextMenus.create(
        {
          id: "menu",
          title: "test user events in onclick",
          contexts: ["page"],
          onclick() {
            request("cookies");
          },
        },
        () => {
          browser.test.sendMessage("actions-ready");
        }
      );
    },

    files: {
      "options.html": `<!DOCTYPE html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <script src="options.js"></script>
          <script src="https://example.com/tests/SimpleTest/EventUtils.js"></script>
        </head>
        <body>
          <a id="link" href="#">Link</a>
        </body>
        </html>`,

      "options.js"() {
        addEventListener("load", async () => {
          let link = document.getElementById("link");
          link.onclick = async event => {
            link.onclick = null;
            event.preventDefault();

            browser.test.log("Calling permission.request from options page.");

            let perm = "history";
            try {
              let result = await browser.permissions.request({
                permissions: [perm],
              });
              browser.test.sendMessage("request", {
                success: true,
                result,
                perm,
              });
            } catch (err) {
              browser.test.sendMessage("request", {
                success: false,
                errmsg: err.message,
                perm,
              });
            }
          };

          // Make a few trips through the event loop to make sure the
          // options browser is fully visible. This is a bit dodgy, but
          // we don't really have a reliable way to detect this from the
          // options page side, and synthetic click events won't work
          // until it is.
          do {
            browser.test.log(
              "Waiting for the options browser to be visible..."
            );
            await new Promise(resolve => setTimeout(resolve, 0));
            synthesizeMouseAtCenter(link, {});
          } while (link.onclick !== null);
        });
      },
    },

    manifest: {
      browser_action: {
        default_title: "test",
        default_area: "navbar",
      },
      page_action: { default_title: "test" },
      permissions: ["contextMenus"],
      optional_permissions: [
        "bookmarks",
        "tabs",
        "webNavigation",
        "history",
        "cookies",
        "downloads",
      ],
      options_ui: { page: "options.html" },
      content_security_policy:
        "script-src 'self' https://example.com; object-src 'none';",
      commands: {
        command: {
          suggested_key: {
            default: "Alt+Shift+J",
          },
        },
      },
    },

    useAddonManager: "temporary",
  });

  async function testPermissionRequest(
    { requestPermission, expectPrompt, perm },
    what
  ) {
    info(`check request permission from '${what}'`);

    let promptPromise = null;
    if (expectPrompt) {
      promptPromise = promisePopupNotificationShown(
        "addon-webext-permissions"
      ).then(panel => {
        panel.button.click();
      });
    }

    await requestPermission();
    await promptPromise;

    let result = await extension.awaitMessage("request");
    ok(result.success, `request() did not throw when called from ${what}`);
    is(result.result, true, `request() succeeded when called from ${what}`);
    is(result.perm, perm, `requested permission ${what}`);
    await promptPromise;
  }

  // Remove Sidebar button to prevent pushing extension button to overflow menu
  CustomizableUI.removeWidgetFromArea("sidebar-button");

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

  await testPermissionRequest(
    {
      requestPermission: () => clickPageAction(extension),
      expectPrompt: true,
      perm: "bookmarks",
    },
    "page action click"
  );

  await testPermissionRequest(
    {
      requestPermission: () => clickBrowserAction(extension),
      expectPrompt: true,
      perm: "tabs",
    },
    "browser action click"
  );

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  gBrowser.selectedTab = tab;

  await testPermissionRequest(
    {
      requestPermission: async () => {
        let menu = await openContextMenu("body");
        let items = menu.getElementsByAttribute(
          "label",
          "test user events in onclick"
        );
        is(items.length, 1, "Found context menu item");
        menu.activateItem(items[0]);
      },
      expectPrompt: false, // cookies permission has no prompt.
      perm: "cookies",
    },
    "context menu in onclick"
  );

  extension.sendMessage("contextMenus.update");
  await extension.awaitMessage("contextMenus.update-done");

  await testPermissionRequest(
    {
      requestPermission: async () => {
        let menu = await openContextMenu("body");
        let items = menu.getElementsByAttribute(
          "label",
          "test user events in onClicked"
        );
        is(items.length, 1, "Found context menu item again");
        menu.activateItem(items[0]);
      },
      expectPrompt: true,
      perm: "webNavigation",
    },
    "context menu in onClicked"
  );

  await testPermissionRequest(
    {
      requestPermission: () => {
        EventUtils.synthesizeKey("j", { altKey: true, shiftKey: true });
      },
      expectPrompt: true,
      perm: "downloads",
    },
    "commands shortcut"
  );

  await testPermissionRequest(
    {
      requestPermission: () => {
        extension.sendMessage("openOptionsPage");
      },
      expectPrompt: true,
      perm: "history",
    },
    "options page link click"
  );

  await BrowserTestUtils.removeTab(gBrowser.selectedTab);
  await BrowserTestUtils.removeTab(tab);

  await extension.unload();

  registerCleanupFunction(() => CustomizableUI.reset());
});