tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_identityBlock_focus.js (4879B)


      1 /* Tests that the identity block can be reached via keyboard
      2 * shortcuts and that it has the correct tab order.
      3 */
      4 
      5 const TEST_PATH = getRootDirectory(gTestPath).replace(
      6  "chrome://mochitests/content",
      7  "https://example.com"
      8 );
      9 const PERMISSIONS_PAGE = TEST_PATH + "permissions.html";
     10 
     11 // The DevEdition has the DevTools button in the toolbar by default. Remove it
     12 // to prevent branch-specific rules what button should be focused.
     13 CustomizableUI.removeWidgetFromArea("developer-button");
     14 
     15 registerCleanupFunction(async function resetToolbar() {
     16  await CustomizableUI.reset();
     17 });
     18 
     19 add_task(async function setupHomeButton() {
     20  await SpecialPowers.pushPrefEnv({
     21    set: [["browser.urlbar.scotchBonnet.enableOverride", false]],
     22  });
     23  // Put the home button in the pre-proton placement to test focus states.
     24  CustomizableUI.addWidgetToArea(
     25    "home-button",
     26    "nav-bar",
     27    CustomizableUI.getPlacementOfWidget("stop-reload-button").position + 1
     28  );
     29 });
     30 
     31 function synthesizeKeyAndWaitForFocus(element, keyCode, options) {
     32  let focused = BrowserTestUtils.waitForEvent(element, "focus");
     33  EventUtils.synthesizeKey(keyCode, options);
     34  return focused;
     35 }
     36 
     37 // Checks that the tracking protection icon container is the next element after
     38 // the urlbar to be focused if there are no active notification anchors.
     39 add_task(async function testWithoutNotifications() {
     40  await SpecialPowers.pushPrefEnv({ set: [["accessibility.tabfocus", 7]] });
     41  await BrowserTestUtils.withNewTab("https://example.com", async function () {
     42    await synthesizeKeyAndWaitForFocus(gURLBar.inputField, "l", {
     43      accelKey: true,
     44    });
     45    is(document.activeElement, gURLBar.inputField, "urlbar should be focused");
     46    await synthesizeKeyAndWaitForFocus(
     47      gProtectionsHandler._trackingProtectionIconContainer,
     48      "VK_TAB",
     49      { shiftKey: true }
     50    );
     51    is(
     52      document.activeElement,
     53      gProtectionsHandler._trackingProtectionIconContainer,
     54      "tracking protection icon container should be focused"
     55    );
     56  });
     57 });
     58 
     59 // Checks that when there is a notification anchor, it will receive
     60 // focus before the identity block.
     61 add_task(async function testWithNotifications() {
     62  await SpecialPowers.pushPrefEnv({ set: [["accessibility.tabfocus", 7]] });
     63  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function (browser) {
     64    let popupshown = BrowserTestUtils.waitForEvent(
     65      PopupNotifications.panel,
     66      "popupshown"
     67    );
     68    // Request a permission;
     69    BrowserTestUtils.synthesizeMouseAtCenter("#geo", {}, browser);
     70    await popupshown;
     71 
     72    await synthesizeKeyAndWaitForFocus(gURLBar.inputField, "l", {
     73      accelKey: true,
     74    });
     75    is(document.activeElement, gURLBar.inputField, "urlbar should be focused");
     76    await synthesizeKeyAndWaitForFocus(
     77      gProtectionsHandler._trackingProtectionIconContainer,
     78      "VK_TAB",
     79      { shiftKey: true }
     80    );
     81    is(
     82      document.activeElement,
     83      gProtectionsHandler._trackingProtectionIconContainer,
     84      "tracking protection icon container should be focused"
     85    );
     86    await synthesizeKeyAndWaitForFocus(
     87      gIdentityHandler._identityIconBox,
     88      "ArrowRight"
     89    );
     90    is(
     91      document.activeElement,
     92      gIdentityHandler._identityIconBox,
     93      "identity block should be focused"
     94    );
     95    let geoIcon = document.getElementById("geo-notification-icon");
     96    await synthesizeKeyAndWaitForFocus(geoIcon, "ArrowRight");
     97    is(
     98      document.activeElement,
     99      geoIcon,
    100      "notification anchor should be focused"
    101    );
    102  });
    103 });
    104 
    105 // Checks that with invalid pageproxystate the identity block is ignored.
    106 add_task(async function testInvalidPageProxyState() {
    107  await SpecialPowers.pushPrefEnv({ set: [["accessibility.tabfocus", 7]] });
    108  await BrowserTestUtils.withNewTab("about:blank", async function () {
    109    // Loading about:blank will automatically focus the urlbar, which, however, can
    110    // race with the test code. So we only send the shortcut if the urlbar isn't focused yet.
    111    if (document.activeElement != gURLBar.inputField) {
    112      await synthesizeKeyAndWaitForFocus(gURLBar.inputField, "l", {
    113        accelKey: true,
    114      });
    115    }
    116    is(document.activeElement, gURLBar.inputField, "urlbar should be focused");
    117    let nextElement = document.getElementById(
    118      Services.prefs.getBoolPref("sidebar.revamp", true)
    119        ? "sidebar-button"
    120        : "home-button"
    121    );
    122    await synthesizeKeyAndWaitForFocus(nextElement, "VK_TAB", {
    123      shiftKey: true,
    124    });
    125    await synthesizeKeyAndWaitForFocus(
    126      document.getElementById("tabs-newtab-button"),
    127      "VK_TAB",
    128      { shiftKey: true }
    129    );
    130    isnot(
    131      document.activeElement,
    132      gProtectionsHandler._trackingProtectionIconContainer,
    133      "tracking protection icon container should not be focused"
    134    );
    135    // Restore focus to the url bar.
    136    gURLBar.focus();
    137  });
    138 });