summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_accesskeys.js
blob: c8b27d6307a30da2bee2e2b00ff347707dba1278 (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
add_task(async function () {
  await pushPrefs(["ui.key.contentAccess", 5], ["ui.key.chromeAccess", 5]);

  const gPageURL1 =
    "data:text/html,<body><p>" +
    "<button id='button' accesskey='y'>Button</button>" +
    "<input id='checkbox' type='checkbox' accesskey='z'>Checkbox" +
    "</p></body>";
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, gPageURL1);

  Services.focus.clearFocus(window);

  // Press an accesskey in the child document while the chrome is focused.
  let focusedId = await performAccessKey(tab1.linkedBrowser, "y");
  is(focusedId, "button", "button accesskey");

  // Press an accesskey in the child document while the content document is focused.
  focusedId = await performAccessKey(tab1.linkedBrowser, "z");
  is(focusedId, "checkbox", "checkbox accesskey");

  // Add an element with an accesskey to the chrome and press its accesskey while the chrome is focused.
  let newButton = document.createXULElement("button");
  newButton.id = "chromebutton";
  newButton.setAttribute("accesskey", "z");
  document.documentElement.appendChild(newButton);

  Services.focus.clearFocus(window);

  newButton.getBoundingClientRect(); // Accesskey registration happens during frame construction.

  focusedId = await performAccessKeyForChrome("z");
  is(focusedId, "chromebutton", "chromebutton accesskey");

  // Add a second tab and ensure that accesskey from the first tab is not used.
  const gPageURL2 =
    "data:text/html,<body>" +
    "<button id='tab2button' accesskey='y'>Button in Tab 2</button>" +
    "</body>";
  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, gPageURL2);

  Services.focus.clearFocus(window);

  focusedId = await performAccessKey(tab2.linkedBrowser, "y");
  is(focusedId, "tab2button", "button accesskey in tab2");

  // Press the accesskey for the chrome element while the content document is focused.
  focusedId = await performAccessKeyForChrome("z");
  is(focusedId, "chromebutton", "chromebutton accesskey");

  gBrowser.removeTab(tab1);
  gBrowser.removeTab(tab2);

  // Test whether access key for the newButton isn't available when content
  // consumes the key event.

  // When content in the tab3 consumes all keydown events.
  const gPageURL3 =
    "data:text/html,<body id='tab3body'>" +
    "<button id='tab3button' accesskey='y'>Button in Tab 3</button>" +
    "<script>" +
    "document.body.addEventListener('keydown', (event)=>{ event.preventDefault(); });" +
    "</script></body>";
  let tab3 = await BrowserTestUtils.openNewForegroundTab(gBrowser, gPageURL3);

  Services.focus.clearFocus(window);

  focusedId = await performAccessKey(tab3.linkedBrowser, "y");
  is(focusedId, "tab3button", "button accesskey in tab3 should be focused");

  newButton.onfocus = () => {
    ok(false, "chromebutton shouldn't get focus during testing with tab3");
  };

  // Press the accesskey for the chrome element while the content document is focused.
  focusedId = await performAccessKey(tab3.linkedBrowser, "z");
  is(
    focusedId,
    "tab3body",
    "button accesskey in tab3 should keep having focus"
  );

  newButton.onfocus = null;

  gBrowser.removeTab(tab3);

  // When content in the tab4 consumes all keypress events.
  const gPageURL4 =
    "data:text/html,<body id='tab4body'>" +
    "<button id='tab4button' accesskey='y'>Button in Tab 4</button>" +
    "<script>" +
    "document.body.addEventListener('keypress', (event)=>{ event.preventDefault(); });" +
    "</script></body>";
  let tab4 = await BrowserTestUtils.openNewForegroundTab(gBrowser, gPageURL4);

  Services.focus.clearFocus(window);

  focusedId = await performAccessKey(tab4.linkedBrowser, "y");
  is(focusedId, "tab4button", "button accesskey in tab4 should be focused");

  newButton.onfocus = () => {
    // EventStateManager handles accesskey before dispatching keypress event
    // into the DOM tree, therefore, chrome accesskey always wins focus from
    // content. However, this is different from shortcut keys.
    todo(false, "chromebutton shouldn't get focus during testing with tab4");
  };

  // Press the accesskey for the chrome element while the content document is focused.
  focusedId = await performAccessKey(tab4.linkedBrowser, "z");
  is(
    focusedId,
    "tab4body",
    "button accesskey in tab4 should keep having focus"
  );

  newButton.onfocus = null;

  gBrowser.removeTab(tab4);

  newButton.remove();
});

function performAccessKey(browser, key) {
  return new Promise(resolve => {
    let removeFocus, removeKeyDown, removeKeyUp;
    function callback(eventName, result) {
      removeFocus();
      removeKeyUp();
      removeKeyDown();

      SpecialPowers.spawn(browser, [], () => {
        let oldFocusedElement = content._oldFocusedElement;
        delete content._oldFocusedElement;
        return oldFocusedElement.id;
      }).then(oldFocus => resolve(oldFocus));
    }

    removeFocus = BrowserTestUtils.addContentEventListener(
      browser,
      "focus",
      callback,
      { capture: true },
      event => {
        if (!HTMLElement.isInstance(event.target)) {
          return false; // ignore window and document focus events
        }

        event.target.ownerGlobal._sent = true;
        let focusedElement = event.target.ownerGlobal.document.activeElement;
        event.target.ownerGlobal._oldFocusedElement = focusedElement;
        focusedElement.blur();
        return true;
      }
    );

    removeKeyDown = BrowserTestUtils.addContentEventListener(
      browser,
      "keydown",
      () => {},
      { capture: true },
      event => {
        event.target.ownerGlobal._sent = false;
        return true;
      }
    );

    removeKeyUp = BrowserTestUtils.addContentEventListener(
      browser,
      "keyup",
      callback,
      {},
      event => {
        if (!event.target.ownerGlobal._sent) {
          event.target.ownerGlobal._sent = true;
          let focusedElement = event.target.ownerGlobal.document.activeElement;
          event.target.ownerGlobal._oldFocusedElement = focusedElement;
          focusedElement.blur();
          return true;
        }

        return false;
      }
    );

    // Spawn an no-op content task to better ensure that the messages
    // for adding the event listeners above get handled.
    SpecialPowers.spawn(browser, [], () => {}).then(() => {
      EventUtils.synthesizeKey(key, { altKey: true, shiftKey: true });
    });
  });
}

// This version is used when a chrome element is expected to be found for an accesskey.
async function performAccessKeyForChrome(key, inChild) {
  let waitFocusChangePromise = BrowserTestUtils.waitForEvent(
    document,
    "focus",
    true
  );
  EventUtils.synthesizeKey(key, { altKey: true, shiftKey: true });
  await waitFocusChangePromise;
  return document.activeElement.id;
}