Bug 1333459 - part3: Add automated tests into browser_accesskeys.js r?smaug draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 07 Jul 2017 20:25:14 +0900
changeset 613984 f2e79675ac28c353b0933b8df5c6c45bc6173968
parent 613983 9ece1905194d14105847453cb589abf9105bc939
child 613985 3807492e4156dfea9c42795709975df6bcdc38c7
push id69882
push usermasayuki@d-toybox.com
push dateSun, 23 Jul 2017 15:20:52 +0000
reviewerssmaug
bugs1333459
milestone56.0a1
Bug 1333459 - part3: Add automated tests into browser_accesskeys.js r?smaug MozReview-Commit-ID: 4zH3kgnX6bM
browser/base/content/test/general/browser_accesskeys.js
--- a/browser/base/content/test/general/browser_accesskeys.js
+++ b/browser/base/content/test/general/browser_accesskeys.js
@@ -42,28 +42,98 @@ add_task(async function() {
 
   focusedId = await performAccessKey("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");
 
-  newButton.remove();
-
   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);
+  tab3.linkedBrowser.messageManager.loadFrameScript("data:,(" + childHandleFocus.toString() + ")();", false);
+
+  Services.focus.clearFocus(window);
+
+  focusedId = await performAccessKey("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("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);
+  tab4.linkedBrowser.messageManager.loadFrameScript("data:,(" + childHandleFocus.toString() + ")();", false);
+
+  Services.focus.clearFocus(window);
+
+  focusedId = await performAccessKey("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("z");
+  is(focusedId, "tab4body", "button accesskey in tab4 should keep having focus");
+
+  newButton.onfocus = null;
+
+  gBrowser.removeTab(tab4);
+
+  newButton.remove();
 });
 
 function childHandleFocus() {
+  var sent = false;
   content.document.body.firstChild.addEventListener("focus", function focused(event) {
+    sent = true;
     let focusedElement = content.document.activeElement;
     focusedElement.blur();
     sendAsyncMessage("Test:FocusFromAccessKey", { focus: focusedElement.id })
   }, true);
+  content.document.body.addEventListener("keydown", function keydown(event) {
+    sent = false;
+  }, true);
+  content.document.body.addEventListener("keyup", function keyup(event) {
+    if (!sent) {
+      sent = true;
+      let focusedElement = content.document.activeElement;
+      sendAsyncMessage("Test:FocusFromAccessKey", { focus: focusedElement.id });
+    }
+  });
 }
 
 function performAccessKey(key) {
   return new Promise(resolve => {
     let mm = gBrowser.selectedBrowser.messageManager;
     mm.addMessageListener("Test:FocusFromAccessKey", function listenForFocus(msg) {
       mm.removeMessageListener("Test:FocusFromAccessKey", listenForFocus);
       resolve(msg.data.focus);