summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_manage_shortcuts_hidden.js
blob: 327a99af9ee83f4535d212f819aa802ce13e2feb (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
/* eslint max-len: ["error", 80] */
"use strict";

async function loadShortcutsView() {
  let managerWin = await open_manager(null);
  managerWin.gViewController.loadView("addons://shortcuts/shortcuts");
  await wait_for_view_load(managerWin);
  return managerWin.document;
}

async function closeShortcutsView(doc) {
  let managerWin = doc.defaultView.parent;
  await close_manager(managerWin);
}

async function registerAndStartExtension(mockProvider, ext) {
  // Shortcuts are registered when an extension is started, so we need to load
  // and start an extension.
  let extension = ExtensionTestUtils.loadExtension(ext);
  await extension.startup();

  // Extensions only appear in the add-on manager when they are registered with
  // the add-on manager, e.g. by passing "useAddonManager" to `loadExtension`.
  // "useAddonManager" can however not be used, because the resulting add-ons
  // are unsigned, and only add-ons with privileged signatures can be hidden.
  mockProvider.createAddons([
    {
      id: extension.id,
      name: ext.manifest.name,
      type: "extension",
      version: "1",
      // We use MockProvider because the "hidden" property cannot
      // be set when "useAddonManager" is passed to loadExtension.
      hidden: ext.manifest.hidden,
      isSystem: ext.isSystem,
    },
  ]);
  return extension;
}

function getShortcutCard(doc, extension) {
  return doc.querySelector(`.shortcut[addon-id="${extension.id}"]`);
}

function getShortcutByName(doc, extension, name) {
  let card = getShortcutCard(doc, extension);
  return card && card.querySelector(`.shortcut-input[name="${name}"]`);
}

function getNoShortcutListItem(doc, extension) {
  let { id } = extension;
  let li = doc.querySelector(`.shortcuts-no-commands-list [addon-id="${id}"]`);
  return li && li.textContent;
}

add_task(async function extension_with_shortcuts() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "shortcut addon",
      commands: {
        theShortcut: {},
      },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();
  let doc = await loadShortcutsView();

  ok(
    getShortcutByName(doc, extension, "theShortcut"),
    "Extension with shortcuts should have a card"
  );
  is(
    getNoShortcutListItem(doc, extension),
    null,
    "Extension with shortcuts should not be listed"
  );

  await closeShortcutsView(doc);
  await extension.unload();
});

add_task(async function extension_without_shortcuts() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "no shortcut addon",
    },
    useAddonManager: "temporary",
  });
  await extension.startup();
  let doc = await loadShortcutsView();

  is(
    getShortcutCard(doc, extension),
    null,
    "Extension without shortcuts should not have a card"
  );
  is(
    getNoShortcutListItem(doc, extension),
    "no shortcut addon",
    "The add-on's name is set in the list"
  );

  await closeShortcutsView(doc);
  await extension.unload();
});

// Hidden add-ons without shortcuts should be hidden,
// but their card should be shown if there is a shortcut.
add_task(async function hidden_extension() {
  let mockProvider = new MockProvider();
  let hiddenExt1 = await registerAndStartExtension(mockProvider, {
    manifest: {
      name: "hidden with shortcuts",
      hidden: true,
      commands: {
        hiddenShortcut: {},
      },
    },
  });
  let hiddenExt2 = await registerAndStartExtension(mockProvider, {
    manifest: {
      name: "hidden without shortcuts",
      hidden: true,
    },
  });

  let doc = await loadShortcutsView();

  ok(
    getShortcutByName(doc, hiddenExt1, "hiddenShortcut"),
    "Hidden extension with shortcuts should have a card"
  );

  is(
    getShortcutCard(doc, hiddenExt2),
    null,
    "Hidden extension without shortcuts should not have a card"
  );
  is(
    getNoShortcutListItem(doc, hiddenExt2),
    null,
    "Hidden extension without shortcuts should not be listed"
  );

  await closeShortcutsView(doc);
  await hiddenExt1.unload();
  await hiddenExt2.unload();

  mockProvider.unregister();
});

add_task(async function system_addons_and_shortcuts() {
  let mockProvider = new MockProvider();
  let systemExt1 = await registerAndStartExtension(mockProvider, {
    isSystem: true,
    manifest: {
      name: "system with shortcuts",
      // In practice, all XPIStateLocations with isSystem=true also have
      // isBuiltin=true, which implies that hidden=true as well.
      hidden: true,
      commands: {
        systemShortcut: {},
      },
    },
  });
  let systemExt2 = await registerAndStartExtension(mockProvider, {
    isSystem: true,
    manifest: {
      name: "system without shortcuts",
      hidden: true,
    },
  });

  let doc = await loadShortcutsView();

  ok(
    getShortcutByName(doc, systemExt1, "systemShortcut"),
    "System add-on with shortcut should have a card"
  );

  is(
    getShortcutCard(doc, systemExt2),
    null,
    "System add-on without shortcut should not have a card"
  );
  is(
    getNoShortcutListItem(doc, systemExt2),
    null,
    "System add-on without shortcuts should not be listed"
  );

  await closeShortcutsView(doc);
  await systemExt1.unload();
  await systemExt2.unload();

  mockProvider.unregister();
});