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

const { ExtensionShortcutKeyMap } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionShortcuts.sys.mjs"
);

add_task(function test_ExtensionShortcutKeymap() {
  const shortcutsMap = new ExtensionShortcutKeyMap();

  shortcutsMap.recordShortcut("Ctrl+Shift+1", "Addon1", "Command1");
  shortcutsMap.recordShortcut("Ctrl+Shift+1", "Addon2", "Command2");
  shortcutsMap.recordShortcut("Ctrl+Alt+2", "Addon2", "Command3");
  // Empty shortcut not expected to be recorded, just ignored.
  shortcutsMap.recordShortcut("", "Addon3", "Command4");

  Assert.equal(
    shortcutsMap.size,
    2,
    "Got the expected number of shortcut entries"
  );
  Assert.deepEqual(
    {
      shortcutWithTwoExtensions: shortcutsMap.getFirstAddonName("Ctrl+Shift+1"),
      shortcutWithOnlyOneExtension:
        shortcutsMap.getFirstAddonName("Ctrl+Alt+2"),
      shortcutWithNoExtension: shortcutsMap.getFirstAddonName(""),
    },
    {
      shortcutWithTwoExtensions: "Addon1",
      shortcutWithOnlyOneExtension: "Addon2",
      shortcutWithNoExtension: null,
    },
    "Got the expected results from getFirstAddonName calls"
  );

  Assert.deepEqual(
    {
      shortcutWithTwoExtensions: shortcutsMap.has("Ctrl+Shift+1"),
      shortcutWithOnlyOneExtension: shortcutsMap.has("Ctrl+Alt+2"),
      shortcutWithNoExtension: shortcutsMap.has(""),
    },
    {
      shortcutWithTwoExtensions: true,
      shortcutWithOnlyOneExtension: true,
      shortcutWithNoExtension: false,
    },
    "Got the expected results from `has` calls"
  );

  shortcutsMap.removeShortcut("Ctrl+Shift+1", "Addon1", "Command1");
  Assert.equal(
    shortcutsMap.has("Ctrl+Shift+1"),
    true,
    "Expect shortcut to already exist after removing one duplicate"
  );
  Assert.equal(
    shortcutsMap.getFirstAddonName("Ctrl+Shift+1"),
    "Addon2",
    "Expect getFirstAddonName to return the remaining addon name"
  );

  shortcutsMap.removeShortcut("Ctrl+Shift+1", "Addon2", "Command2");
  Assert.equal(
    shortcutsMap.has("Ctrl+Shift+1"),
    false,
    "Expect shortcut to not exist anymore after removing last entry"
  );
  Assert.equal(shortcutsMap.size, 1, "Got only one shortcut as expected");

  shortcutsMap.clear();
  Assert.equal(
    shortcutsMap.size,
    0,
    "Got no shortcut as expected after clearing the map"
  );
});

// This test verify that ExtensionShortcutKeyMap does catch duplicated
// shortcut when the two modifiers strings are associated to the same
// key (in particular on macOS where Ctrl and Command keys are both translated
// in the same modifier in the keyboard shortcuts).
add_task(function test_PlatformShortcutString() {
  const shortcutsMap = new ExtensionShortcutKeyMap();

  // Make the class instance behave like it would while running on macOS.
  // (this is just for unit testing purpose, there is a separate integration
  // test exercising this behavior in a real "Manage Extension Shortcut"
  // about:addons view and only running on macOS, skipped on other platforms).
  shortcutsMap._os = "mac";

  shortcutsMap.recordShortcut("Ctrl+Shift+1", "Addon1", "MacCommand1");

  Assert.deepEqual(
    {
      hasWithCtrl: shortcutsMap.has("Ctrl+Shift+1"),
      hasWithCommand: shortcutsMap.has("Command+Shift+1"),
    },
    {
      hasWithCtrl: true,
      hasWithCommand: true,
    },
    "Got the expected results from `has` calls"
  );

  Assert.deepEqual(
    {
      nameWithCtrl: shortcutsMap.getFirstAddonName("Ctrl+Shift+1"),
      nameWithCommand: shortcutsMap.getFirstAddonName("Command+Shift+1"),
    },
    {
      nameWithCtrl: "Addon1",
      nameWithCommand: "Addon1",
    },
    "Got the expected results from `getFirstAddonName` calls"
  );

  // Add a duplicate shortcut using Command instead of Ctrl and
  // verify the expected behaviors.
  shortcutsMap.recordShortcut("Command+Shift+1", "Addon2", "MacCommand2");
  Assert.equal(shortcutsMap.size, 1, "Got still one shortcut as expected");
  shortcutsMap.removeShortcut("Ctrl+Shift+1", "Addon1", "MacCommand1");
  Assert.equal(shortcutsMap.size, 1, "Got still one shortcut as expected");
  Assert.deepEqual(
    {
      nameWithCtrl: shortcutsMap.getFirstAddonName("Ctrl+Shift+1"),
      nameWithCommand: shortcutsMap.getFirstAddonName("Command+Shift+1"),
    },
    {
      nameWithCtrl: "Addon2",
      nameWithCommand: "Addon2",
    },
    "Got the expected results from `getFirstAddonName` calls"
  );

  // Remove the entry added with a shortcut using "Command" by using the
  // equivalent shortcut using Ctrl.
  shortcutsMap.removeShortcut("Ctrl+Shift+1", "Addon2", "MacCommand2");
  Assert.equal(shortcutsMap.size, 0, "Got no shortcut as expected");
});