tor-browser

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

browser_promptFocus.js (4556B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { PromptTestUtils } = ChromeUtils.importESModule(
      7  "resource://testing-common/PromptTestUtils.sys.mjs"
      8 );
      9 
     10 // MacOS has different default focus behavior for prompts.
     11 const isMacOS = Services.appinfo.OS === "Darwin";
     12 
     13 /**
     14 * Tests that prompts are focused when switching tabs.
     15 */
     16 add_task(async function test_tabdialogbox_tab_switch_focus() {
     17  // Open 3 tabs
     18  let tabPromises = [];
     19  for (let i = 0; i < 3; i += 1) {
     20    tabPromises.push(
     21      BrowserTestUtils.openNewForegroundTab(
     22        gBrowser,
     23        "https://example.com",
     24        true
     25      )
     26    );
     27  }
     28  // Wait for tabs to be ready
     29  let tabs = await Promise.all(tabPromises);
     30  let [tabA, tabB, tabC] = tabs;
     31 
     32  // Spawn two prompts, which have different default focus as determined by
     33  // CommonDialog#setDefaultFocus.
     34  let openPromise = PromptTestUtils.waitForPrompt(tabA.linkedBrowser, {
     35    modalType: Services.prompt.MODAL_TYPE_TAB,
     36    promptType: "confirm",
     37  });
     38  Services.prompt.asyncConfirm(
     39    tabA.linkedBrowser.browsingContext,
     40    Services.prompt.MODAL_TYPE_TAB,
     41    null,
     42    "prompt A"
     43  );
     44  let promptA = await openPromise;
     45 
     46  openPromise = PromptTestUtils.waitForPrompt(tabB.linkedBrowser, {
     47    modalType: Services.prompt.MODAL_TYPE_TAB,
     48    promptType: "promptPassword",
     49  });
     50  Services.prompt.asyncPromptPassword(
     51    tabB.linkedBrowser.browsingContext,
     52    Services.prompt.MODAL_TYPE_TAB,
     53    null,
     54    "prompt B",
     55    "",
     56    null,
     57    false
     58  );
     59  let promptB = await openPromise;
     60 
     61  // Switch tabs and check if the correct element was focused.
     62 
     63  // Switch back to the third tab which doesn't have a prompt.
     64  await BrowserTestUtils.switchTab(gBrowser, tabC);
     65  is(
     66    Services.focus.focusedElement,
     67    tabC.linkedBrowser,
     68    "Tab without prompt should have focus on browser."
     69  );
     70 
     71  // Switch to first tab which has prompt
     72  await BrowserTestUtils.switchTab(gBrowser, tabA);
     73 
     74  if (isMacOS) {
     75    is(
     76      Services.focus.focusedElement,
     77      promptA.ui.infoBody,
     78      "Tab with prompt should have focus on body."
     79    );
     80  } else {
     81    is(
     82      Services.focus.focusedElement,
     83      promptA.ui.button0,
     84      "Tab with prompt should have focus on default button."
     85    );
     86  }
     87 
     88  await PromptTestUtils.handlePrompt(promptA);
     89 
     90  // Switch to second tab which has prompt
     91  await BrowserTestUtils.switchTab(gBrowser, tabB);
     92  is(
     93    Services.focus.focusedElement,
     94    promptB.ui.password1Textbox,
     95    "Tab with password prompt should have focus on password field."
     96  );
     97  await PromptTestUtils.handlePrompt(promptB);
     98 
     99  // Cleanup
    100  tabs.forEach(tab => {
    101    BrowserTestUtils.removeTab(tab);
    102  });
    103 });
    104 
    105 /**
    106 * Tests that an alert prompt has focus on the default element.
    107 *
    108 * @param {CommonDialog} prompt - Prompt to test focus for.
    109 * @param {number} index - Index of the prompt to log.
    110 */
    111 function testAlertPromptFocus(prompt, index) {
    112  if (isMacOS) {
    113    is(
    114      Services.focus.focusedElement,
    115      prompt.ui.infoBody,
    116      `Prompt #${index} should have focus on body.`
    117    );
    118  } else {
    119    is(
    120      Services.focus.focusedElement,
    121      prompt.ui.button0,
    122      `Prompt #${index} should have focus on default button.`
    123    );
    124  }
    125 }
    126 
    127 /**
    128 * Test that we set the correct focus when queuing multiple prompts.
    129 */
    130 add_task(async function test_tabdialogbox_prompt_queue_focus() {
    131  await BrowserTestUtils.withNewTab(gBrowser, async browser => {
    132    const PROMPT_COUNT = 10;
    133 
    134    let firstPromptPromise = PromptTestUtils.waitForPrompt(browser, {
    135      modalType: Services.prompt.MODAL_TYPE_TAB,
    136      promptType: "alert",
    137    });
    138 
    139    for (let i = 0; i < PROMPT_COUNT; i += 1) {
    140      Services.prompt.asyncAlert(
    141        browser.browsingContext,
    142        Services.prompt.MODAL_TYPE_TAB,
    143        null,
    144        "prompt " + i
    145      );
    146    }
    147 
    148    // Close prompts one by one and check focus.
    149    let nextPromptPromise = firstPromptPromise;
    150    for (let i = 0; i < PROMPT_COUNT; i += 1) {
    151      let p = await nextPromptPromise;
    152      testAlertPromptFocus(p, i);
    153 
    154      if (i < PROMPT_COUNT - 1) {
    155        nextPromptPromise = PromptTestUtils.waitForPrompt(browser, {
    156          modalType: Services.prompt.MODAL_TYPE_TAB,
    157          promptType: "alert",
    158        });
    159      }
    160      await PromptTestUtils.handlePrompt(p);
    161    }
    162 
    163    // All prompts are closed, focus should be back on the browser.
    164    is(
    165      Services.focus.focusedElement,
    166      browser,
    167      "Tab without prompts should have focus on browser."
    168    );
    169  });
    170 });