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

const PAGE =
  "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html";

add_task(async function accesskeys() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
  gBrowser.selectedTab = tab;

  async function background() {
    // description is informative.
    // title is passed to menus.create.
    // label and key are compared with the actual values.
    const TESTCASES = [
      {
        description: "Amp at start",
        title: "&accesskey",
        label: "accesskey",
        key: "a",
      },
      {
        description: "amp in between",
        title: "A& b",
        label: "A b",
        key: " ",
      },
      {
        description: "lonely amp",
        title: "&",
        label: "",
        key: "",
      },
      {
        description: "amp at end",
        title: "End &",
        label: "End ",
        key: "",
      },
      {
        description: "escaped amp",
        title: "A && B",
        label: "A & B",
        key: "",
      },
      {
        description: "amp before escaped amp",
        title: "A &T&& before",
        label: "A T& before",
        key: "T",
      },
      {
        description: "amp after escaped amp",
        title: "A &&&T after",
        label: "A &T after",
        key: "T",
      },
      {
        // Only the first amp should be used as the access key.
        description: "amp, escaped amp, amp to ignore",
        title: "First &1 comes && first &2 serves",
        label: "First 1 comes & first 2 serves",
        key: "1",
      },
      {
        description: "created with amp, updated without amp",
        title: "temp with &X", // will be updated below.
        label: "remove amp",
        key: "",
      },
      {
        description: "created without amp, update with amp",
        title: "temp without access key", // will be updated below.
        label: "add ampY",
        key: "Y",
      },
    ];

    let menuIds = TESTCASES.map(({ title }) => browser.menus.create({ title }));

    // Should clear the access key:
    await browser.menus.update(menuIds[menuIds.length - 2], {
      title: "remove amp",
    });

    // Should add an access key:
    await browser.menus.update(menuIds[menuIds.length - 1], {
      title: "add amp&Y",
    });
    // Should not clear the access key because title is not set:
    await browser.menus.update(menuIds[menuIds.length - 1], { enabled: true });

    browser.test.sendMessage("testCases", TESTCASES);
  }
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    background,
  });

  await extension.startup();

  const TESTCASES = await extension.awaitMessage("testCases");
  let menu = await openExtensionContextMenu();
  let items = menu.getElementsByTagName("menuitem");
  is(items.length, TESTCASES.length, "Expected menu items for page");
  TESTCASES.forEach(({ description, label, key }, i) => {
    is(items[i].label, label, `Label for item ${i} (${description})`);
    is(items[i].accessKey, key, `Accesskey for item ${i} (${description})`);
  });

  await closeExtensionContextMenu();
  await extension.unload();
  BrowserTestUtils.removeTab(tab);
});

add_task(async function accesskeys_selection() {
  const PAGE_WITH_AMPS = "data:text/plain;charset=utf-8,PageSelection&Amp";
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    PAGE_WITH_AMPS
  );
  gBrowser.selectedTab = tab;

  async function background() {
    const TESTCASES = [
      {
        description: "Selection without amp",
        title: "percent-s: %s.",
        label: "percent-s: PageSelection&Amp.",
        key: "",
      },
      {
        description: "Selection with amp after %s",
        title: "percent-s: %s &A.",
        label: "percent-s: PageSelection&Amp A.",
        key: "A",
      },
      {
        description: "Selection with amp before %s",
        title: "percent-s: &B %s.",
        label: "percent-s: B PageSelection&Amp.",
        key: "B",
      },
      {
        description: "Amp-percent",
        title: "Amp-percent: &%.",
        label: "Amp-percent: %.",
        key: "%",
      },
      {
        // "&%s" should be treated as "%s", and "ignore this" with amps should be ignored.
        description: "Selection with amp-percent-s",
        title: "Amp-percent-s: &%s.&i&g&n&o&r&e& &t&h&i&s",
        label: "Amp-percent-s: PageSelection&Amp.ignore this",
        // Chrome uses the first character of the selection as access key.
        // Let's not copy that behavior...
        key: "",
      },
      {
        description: "Selection with amp before amp-percent-s",
        title: "Amp-percent-s: &_ &%s.",
        label: "Amp-percent-s: _ PageSelection&Amp.",
        key: "_",
      },
    ];

    let lastMenuId;
    for (let { title } of TESTCASES) {
      lastMenuId = browser.menus.create({ contexts: ["selection"], title });
    }
    // Round-trip to ensure that the menus have been registered.
    await browser.menus.update(lastMenuId, {});
    browser.test.sendMessage("testCases", TESTCASES);
  }
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    background,
  });

  await extension.startup();

  // Select all
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function (arg) {
    let doc = content.document;
    let range = doc.createRange();
    let selection = content.getSelection();
    selection.removeAllRanges();
    range.selectNodeContents(doc.body);
    selection.addRange(range);
  });

  const TESTCASES = await extension.awaitMessage("testCases");
  let menu = await openExtensionContextMenu();
  let items = menu.getElementsByTagName("menuitem");
  is(items.length, TESTCASES.length, "Expected menu items for page");
  TESTCASES.forEach(({ description, label, key }, i) => {
    is(items[i].label, label, `Label for item ${i} (${description})`);
    is(items[i].accessKey, key, `Accesskey for item ${i} (${description})`);
  });

  await closeExtensionContextMenu();
  await extension.unload();
  BrowserTestUtils.removeTab(tab);
});