summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_commands_update.js
blob: 659a371a28967409fa945768c714cbe2eec5f4dd (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
  ExtensionSettingsStore:
    "resource://gre/modules/ExtensionSettingsStore.sys.mjs",
});

function enableAddon(addon) {
  return new Promise(resolve => {
    AddonManager.addAddonListener({
      onEnabled(enabledAddon) {
        if (enabledAddon.id == addon.id) {
          resolve();
          AddonManager.removeAddonListener(this);
        }
      },
    });
    addon.enable();
  });
}

function disableAddon(addon) {
  return new Promise(resolve => {
    AddonManager.addAddonListener({
      onDisabled(disabledAddon) {
        if (disabledAddon.id == addon.id) {
          resolve();
          AddonManager.removeAddonListener(this);
        }
      },
    });
    addon.disable();
  });
}

add_task(async function test_update_defined_command() {
  let extension;
  let updatedExtension;

  registerCleanupFunction(async () => {
    await extension.unload();

    // updatedExtension might not have started up if we didn't make it that far.
    if (updatedExtension) {
      await updatedExtension.unload();
    }

    // Check that ESS is cleaned up on uninstall.
    let storedCommands = ExtensionSettingsStore.getAllForExtension(
      extension.id,
      "commands"
    );
    is(storedCommands.length, 0, "There are no stored commands after unload");
  });

  extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",
    manifest: {
      version: "1.0",
      browser_specific_settings: { gecko: { id: "commands@mochi.test" } },
      commands: {
        foo: {
          suggested_key: {
            default: "Ctrl+Shift+I",
          },
          description: "The foo command",
        },
      },
    },
    background() {
      browser.test.onMessage.addListener(async (msg, data) => {
        if (msg == "update") {
          await browser.commands.update(data);
          return browser.test.sendMessage("updateDone");
        } else if (msg == "reset") {
          await browser.commands.reset(data);
          return browser.test.sendMessage("resetDone");
        } else if (msg != "run") {
          return;
        }
        // Test initial manifest command.
        let commands = await browser.commands.getAll();
        browser.test.assertEq(1, commands.length, "There is 1 command");
        let command = commands[0];
        browser.test.assertEq("foo", command.name, "The name is right");
        browser.test.assertEq(
          "The foo command",
          command.description,
          "The description is right"
        );
        browser.test.assertEq(
          "Ctrl+Shift+I",
          command.shortcut,
          "The shortcut is right"
        );

        // Update the shortcut.
        await browser.commands.update({
          name: "foo",
          shortcut: "Ctrl+Shift+L",
        });

        // Test the updated shortcut.
        commands = await browser.commands.getAll();
        browser.test.assertEq(1, commands.length, "There is still 1 command");
        command = commands[0];
        browser.test.assertEq("foo", command.name, "The name is unchanged");
        browser.test.assertEq(
          "The foo command",
          command.description,
          "The description is unchanged"
        );
        browser.test.assertEq(
          "Ctrl+Shift+L",
          command.shortcut,
          "The shortcut is updated"
        );

        // Update the description.
        await browser.commands.update({
          name: "foo",
          description: "The only command",
        });

        // Test the updated shortcut.
        commands = await browser.commands.getAll();
        browser.test.assertEq(1, commands.length, "There is still 1 command");
        command = commands[0];
        browser.test.assertEq("foo", command.name, "The name is unchanged");
        browser.test.assertEq(
          "The only command",
          command.description,
          "The description is updated"
        );
        browser.test.assertEq(
          "Ctrl+Shift+L",
          command.shortcut,
          "The shortcut is unchanged"
        );

        // Clear the shortcut.
        await browser.commands.update({
          name: "foo",
          shortcut: "",
        });
        commands = await browser.commands.getAll();
        browser.test.assertEq(1, commands.length, "There is still 1 command");
        command = commands[0];
        browser.test.assertEq("foo", command.name, "The name is unchanged");
        browser.test.assertEq(
          "The only command",
          command.description,
          "The description is unchanged"
        );
        browser.test.assertEq("", command.shortcut, "The shortcut is empty");

        // Update the description and shortcut.
        await browser.commands.update({
          name: "foo",
          description: "The new command",
          shortcut: "   Alt+  Shift +9",
        });

        // Test the updated shortcut.
        commands = await browser.commands.getAll();
        browser.test.assertEq(1, commands.length, "There is still 1 command");
        command = commands[0];
        browser.test.assertEq("foo", command.name, "The name is unchanged");
        browser.test.assertEq(
          "The new command",
          command.description,
          "The description is updated"
        );
        browser.test.assertEq(
          "Alt+Shift+9",
          command.shortcut,
          "The shortcut is updated"
        );

        // Test a bad shortcut update.
        browser.test.assertThrows(
          () =>
            browser.commands.update({ name: "foo", shortcut: "Ctl+Shift+L" }),
          /Type error for parameter detail .+ primary modifier and a key/,
          "It rejects for a bad shortcut"
        );

        // Try to update a command that doesn't exist.
        await browser.test.assertRejects(
          browser.commands.update({ name: "bar", shortcut: "Ctrl+Shift+L" }),
          'Unknown command "bar"',
          "It rejects for an unknown command"
        );

        browser.test.notifyPass("commands");
      });
      browser.test.sendMessage("ready");
    },
  });

  await extension.startup();

  function extensionKeyset(extensionId) {
    return document.getElementById(
      makeWidgetId(`ext-keyset-id-${extensionId}`)
    );
  }

  function checkKey(extensionId, shortcutKey, modifiers) {
    let keyset = extensionKeyset(extensionId);
    is(keyset.children.length, 1, "There is 1 key in the keyset");
    let key = keyset.children[0];
    is(key.getAttribute("key"), shortcutKey, "The key is correct");
    is(key.getAttribute("modifiers"), modifiers, "The modifiers are correct");
  }

  function checkNumericKey(extensionId, key, modifiers) {
    let keyset = extensionKeyset(extensionId);
    is(
      keyset.children.length,
      2,
      "There are 2 keys in the keyset now, 1 of which contains a keycode."
    );
    let numpadKey = keyset.children[0];
    is(
      numpadKey.getAttribute("keycode"),
      `VK_NUMPAD${key}`,
      "The numpad keycode is correct."
    );
    is(
      numpadKey.getAttribute("modifiers"),
      modifiers,
      "The modifiers are correct"
    );

    let originalNumericKey = keyset.children[1];
    is(
      originalNumericKey.getAttribute("keycode"),
      `VK_${key}`,
      "The original key is correct."
    );
    is(
      originalNumericKey.getAttribute("modifiers"),
      modifiers,
      "The modifiers are correct"
    );
  }

  // Check that the <key> is set for the original shortcut.
  checkKey(extension.id, "I", "accel,shift");

  await extension.awaitMessage("ready");
  extension.sendMessage("run");
  await extension.awaitFinish("commands");

  // Check that the <keycode> has been updated.
  checkNumericKey(extension.id, "9", "alt,shift");

  // Check that the updated command is stored in ExtensionSettingsStore.
  let storedCommands = ExtensionSettingsStore.getAllForExtension(
    extension.id,
    "commands"
  );
  is(storedCommands.length, 1, "There is only one stored command");
  let command = ExtensionSettingsStore.getSetting(
    "commands",
    "foo",
    extension.id
  ).value;
  is(command.description, "The new command", "The description is stored");
  is(command.shortcut, "Alt+Shift+9", "The shortcut is stored");

  // Check that the key is updated immediately.
  extension.sendMessage("update", { name: "foo", shortcut: "Ctrl+Shift+M" });
  await extension.awaitMessage("updateDone");
  checkKey(extension.id, "M", "accel,shift");

  // Ensure all successive updates are stored.
  // Force the command to only have a description saved.
  await ExtensionSettingsStore.addSetting(extension.id, "commands", "foo", {
    description: "description only",
  });
  // This command now only has a description set in storage, also update the shortcut.
  extension.sendMessage("update", { name: "foo", shortcut: "Alt+Shift+9" });
  await extension.awaitMessage("updateDone");
  let storedCommand = await ExtensionSettingsStore.getSetting(
    "commands",
    "foo",
    extension.id
  );
  is(
    storedCommand.value.shortcut,
    "Alt+Shift+9",
    "The shortcut is saved correctly"
  );
  is(
    storedCommand.value.description,
    "description only",
    "The description is saved correctly"
  );

  // Calling browser.commands.reset("foo") should reset to manifest version.
  extension.sendMessage("reset", "foo");
  await extension.awaitMessage("resetDone");

  checkKey(extension.id, "I", "accel,shift");

  // Check that enable/disable removes the keyset and reloads the saved command.
  let addon = await AddonManager.getAddonByID(extension.id);
  await disableAddon(addon);
  let keyset = extensionKeyset(extension.id);
  is(keyset, null, "The extension keyset is removed when disabled");
  // Add some commands to storage, only "foo" should get loaded.
  await ExtensionSettingsStore.addSetting(extension.id, "commands", "foo", {
    shortcut: "Alt+Shift+9",
  });
  await ExtensionSettingsStore.addSetting(extension.id, "commands", "unknown", {
    shortcut: "Ctrl+Shift+P",
  });
  storedCommands = ExtensionSettingsStore.getAllForExtension(
    extension.id,
    "commands"
  );
  is(storedCommands.length, 2, "There are now 2 commands stored");
  await enableAddon(addon);
  // Wait for the keyset to appear (it's async on enable).
  await TestUtils.waitForCondition(() => extensionKeyset(extension.id));
  // The keyset is back with the value from ExtensionSettingsStore.
  checkNumericKey(extension.id, "9", "alt,shift");

  // Check that an update to a shortcut in the manifest is mapped correctly.
  updatedExtension = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",
    manifest: {
      version: "1.0",
      browser_specific_settings: { gecko: { id: "commands@mochi.test" } },
      commands: {
        foo: {
          suggested_key: {
            default: "Ctrl+Shift+L",
          },
          description: "The foo command",
        },
      },
    },
  });
  await updatedExtension.startup();

  await TestUtils.waitForCondition(() => extensionKeyset(extension.id));
  // Shortcut is unchanged since it was previously updated.
  checkNumericKey(extension.id, "9", "alt,shift");
});

add_task(async function updateSidebarCommand() {
  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      commands: {
        _execute_sidebar_action: {
          suggested_key: {
            default: "Ctrl+Shift+E",
          },
        },
      },
      sidebar_action: {
        default_panel: "sidebar.html",
      },
    },
    background() {
      browser.test.onMessage.addListener(async (msg, data) => {
        if (msg == "updateShortcut") {
          await browser.commands.update(data);
          return browser.test.sendMessage("done");
        }
        throw new Error("Unknown message");
      });
    },
    files: {
      "sidebar.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");
        };
      },
    },
  });
  await extension.startup();
  await extension.awaitMessage("sidebar");

  // Show and hide the switcher panel to generate the initial shortcuts.
  let switcherShown = promisePopupShown(SidebarUI._switcherPanel);
  SidebarUI.showSwitcherPanel();
  await switcherShown;
  let switcherHidden = promisePopupHidden(SidebarUI._switcherPanel);
  SidebarUI.hideSwitcherPanel();
  await switcherHidden;

  let buttonId = `button_${makeWidgetId(extension.id)}-sidebar-action`;
  let button = document.getElementById(buttonId);
  let shortcut = button.getAttribute("shortcut");
  ok(shortcut.endsWith("E"), "The button has the shortcut set");

  extension.sendMessage("updateShortcut", {
    name: "_execute_sidebar_action",
    shortcut: "Ctrl+Shift+M",
  });
  await extension.awaitMessage("done");

  shortcut = button.getAttribute("shortcut");
  ok(shortcut.endsWith("M"), "The button shortcut has been updated");

  await extension.unload();
});