summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_shortcuts_duplicate_check.js
blob: 17842ba848a756df6cd3c6bfb828b40dc1347004 (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
"use strict";

const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(
  /Message manager disconnected/
);

async function loadShortcutsView() {
  let win = await loadInitialView("extension");

  // There should be a manage shortcuts link.
  let shortcutsLink = win.document.querySelector('[action="manage-shortcuts"]');

  // Open the shortcuts view.
  let loaded = waitForViewLoad(win);
  shortcutsLink.click();
  await loaded;

  return win;
}

add_task(async function testDuplicateShortcutsWarnings() {
  let duplicateCommands = {
    commandOne: {
      suggested_key: { default: "Shift+Alt+1" },
    },
    commandTwo: {
      description: "Command Two!",
      suggested_key: { default: "Shift+Alt+2" },
    },
  };
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      commands: duplicateCommands,
      name: "Extension 1",
    },
    background() {
      browser.test.sendMessage("ready");
    },
    useAddonManager: "temporary",
  });

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

  let extension2 = ExtensionTestUtils.loadExtension({
    manifest: {
      commands: {
        ...duplicateCommands,
        commandThree: {
          description: "Command Three!",
          suggested_key: { default: "Shift+Alt+3" },
        },
      },
      name: "Extension 2",
    },
    background() {
      browser.test.sendMessage("ready");
    },
    useAddonManager: "temporary",
  });

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

  let win = await loadShortcutsView();
  let doc = win.document;

  let warningBars = doc.querySelectorAll("message-bar");
  // Ensure warning messages are shown for each duplicate shorctut.
  is(
    warningBars.length,
    Object.keys(duplicateCommands).length,
    "There is a warning message bar for each duplicate shortcut"
  );

  // Ensure warning messages are correct with correct shortcuts.
  let count = 1;
  for (let warning of warningBars) {
    let warningMsg = warning.querySelector("span");
    let l10nAttrs = doc.l10n.getAttributes(warningMsg);
    is(
      l10nAttrs.id,
      "shortcuts-duplicate-warning-message",
      "Warning message is shown"
    );
    Assert.deepEqual(
      l10nAttrs.args,
      { shortcut: `Shift+Alt+${count}` },
      "Warning message shortcut is correct"
    );
    count++;
  }

  ["Shift+Alt+1", "Shift+Alt+2"].forEach((shortcut, index) => {
    // Ensure warning messages are correct with correct shortcuts.
    let warning = warningBars[index];
    let warningMsg = warning.querySelector("span");
    let l10nAttrs = doc.l10n.getAttributes(warningMsg);
    is(
      l10nAttrs.id,
      "shortcuts-duplicate-warning-message",
      "Warning message is shown"
    );
    Assert.deepEqual(
      l10nAttrs.args,
      { shortcut },
      "Warning message shortcut is correct"
    );

    // Check if all inputs have warning style.
    let inputs = doc.querySelectorAll(`input[shortcut="${shortcut}"]`);
    for (let input of inputs) {
      // Check if warning error message is shown on focus.
      input.focus();
      let error = doc.querySelector(".error-message");
      let label = error.querySelector(".error-message-label");
      is(error.style.visibility, "visible", "The error element is shown");
      is(
        error.getAttribute("type"),
        "warning",
        "Duplicate shortcut has warning class"
      );
      is(
        label.dataset.l10nId,
        "shortcuts-duplicate",
        "Correct error message is shown"
      );

      // On keypress events wrning class should be removed.
      EventUtils.synthesizeKey("A");
      ok(
        !error.classList.contains("warning"),
        "Error element should not have warning class"
      );

      input.blur();
      is(
        error.style.visibility,
        "hidden",
        "The error element is hidden on blur"
      );
    }
  });

  await closeView(win);
  await extension.unload();
  await extension2.unload();
});

add_task(async function testDuplicateShortcutOnMacOSCtrlKey() {
  if (AppConstants.platform !== "macosx") {
    ok(
      true,
      `Skipping macos specific test on platform ${AppConstants.platform}`
    );
    return;
  }

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      name: "Extension 1",
      browser_specific_settings: {
        gecko: { id: "extension1@mochi.test" },
      },
      commands: {
        commandOne: {
          // Cover expected mac normalized shortcut on default shortcut.
          suggested_key: { default: "Ctrl+Shift+1" },
        },
        commandTwo: {
          suggested_key: {
            default: "Alt+Shift+2",
            // Cover expected mac normalized shortcut on mac-specific shortcut.
            mac: "Ctrl+Shift+2",
          },
        },
      },
    },
  });

  const extension2 = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      name: "Extension 2",
      browser_specific_settings: {
        gecko: { id: "extension2@mochi.test" },
      },
      commands: {
        anotherCommand: {},
      },
    },
  });

  await extension.startup();
  await extension2.startup();

  const win = await loadShortcutsView();
  const doc = win.document;
  const errorEl = doc.querySelector("addon-shortcuts .error-message");
  const errorLabel = errorEl.querySelector(".error-message-label");

  ok(
    BrowserTestUtils.is_hidden(errorEl),
    "Expect shortcut error element to be initially hidden"
  );

  const getShortcutInput = commandName =>
    doc.querySelector(`input.shortcut-input[name="${commandName}"]`);

  const assertDuplicateShortcutWarning = async msg => {
    await TestUtils.waitForCondition(
      () => BrowserTestUtils.is_visible(errorEl),
      `Wait for the shortcut-duplicate error to be visible on ${msg}`
    );
    Assert.deepEqual(
      document.l10n.getAttributes(errorLabel),
      {
        id: "shortcuts-exists",
        args: { addon: "Extension 1" },
      },
      `Got the expected warning message on duplicate shortcut on ${msg}`
    );
  };

  const clearWarning = async inputEl => {
    anotherCommandInput.blur();
    await TestUtils.waitForCondition(
      () => BrowserTestUtils.is_hidden(errorEl),
      "Wait for the shortcut-duplicate error to be hidden"
    );
  };

  const anotherCommandInput = getShortcutInput("anotherCommand");
  anotherCommandInput.focus();
  EventUtils.synthesizeKey("1", { metaKey: true, shiftKey: true });

  await assertDuplicateShortcutWarning("shortcut conflict with commandOne");
  await clearWarning(anotherCommandInput);

  anotherCommandInput.focus();
  EventUtils.synthesizeKey("2", { metaKey: true, shiftKey: true });

  await assertDuplicateShortcutWarning("shortcut conflict with commandTwo");
  await clearWarning(anotherCommandInput);

  await closeView(win);
  await extension.unload();
  await extension2.unload();
});