summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabPrompts/browser_promptFocus.js
blob: 89ca064c10f1ca3b90de56cd2d645dedeedfb004 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PromptTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromptTestUtils.sys.mjs"
);

// MacOS has different default focus behavior for prompts.
const isMacOS = Services.appinfo.OS === "Darwin";

/**
 * Tests that prompts are focused when switching tabs.
 */
add_task(async function test_tabdialogbox_tab_switch_focus() {
  // Open 3 tabs
  let tabPromises = [];
  for (let i = 0; i < 3; i += 1) {
    tabPromises.push(
      BrowserTestUtils.openNewForegroundTab(
        gBrowser,
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://example.com",
        true
      )
    );
  }
  // Wait for tabs to be ready
  let tabs = await Promise.all(tabPromises);
  let [tabA, tabB, tabC] = tabs;

  // Spawn two prompts, which have different default focus as determined by
  // CommonDialog#setDefaultFocus.
  let openPromise = PromptTestUtils.waitForPrompt(tabA.linkedBrowser, {
    modalType: Services.prompt.MODAL_TYPE_TAB,
    promptType: "confirm",
  });
  Services.prompt.asyncConfirm(
    tabA.linkedBrowser.browsingContext,
    Services.prompt.MODAL_TYPE_TAB,
    null,
    "prompt A"
  );
  let promptA = await openPromise;

  openPromise = PromptTestUtils.waitForPrompt(tabB.linkedBrowser, {
    modalType: Services.prompt.MODAL_TYPE_TAB,
    promptType: "promptPassword",
  });
  Services.prompt.asyncPromptPassword(
    tabB.linkedBrowser.browsingContext,
    Services.prompt.MODAL_TYPE_TAB,
    null,
    "prompt B",
    "",
    null,
    false
  );
  let promptB = await openPromise;

  // Switch tabs and check if the correct element was focused.

  // Switch back to the third tab which doesn't have a prompt.
  await BrowserTestUtils.switchTab(gBrowser, tabC);
  is(
    Services.focus.focusedElement,
    tabC.linkedBrowser,
    "Tab without prompt should have focus on browser."
  );

  // Switch to first tab which has prompt
  await BrowserTestUtils.switchTab(gBrowser, tabA);

  if (isMacOS) {
    is(
      Services.focus.focusedElement,
      promptA.ui.infoBody,
      "Tab with prompt should have focus on body."
    );
  } else {
    is(
      Services.focus.focusedElement,
      promptA.ui.button0,
      "Tab with prompt should have focus on default button."
    );
  }

  await PromptTestUtils.handlePrompt(promptA);

  // Switch to second tab which has prompt
  await BrowserTestUtils.switchTab(gBrowser, tabB);
  is(
    Services.focus.focusedElement,
    promptB.ui.password1Textbox,
    "Tab with password prompt should have focus on password field."
  );
  await PromptTestUtils.handlePrompt(promptB);

  // Cleanup
  tabs.forEach(tab => {
    BrowserTestUtils.removeTab(tab);
  });
});

/**
 * Tests that an alert prompt has focus on the default element.
 * @param {CommonDialog} prompt - Prompt to test focus for.
 * @param {number} index - Index of the prompt to log.
 */
function testAlertPromptFocus(prompt, index) {
  if (isMacOS) {
    is(
      Services.focus.focusedElement,
      prompt.ui.infoBody,
      `Prompt #${index} should have focus on body.`
    );
  } else {
    is(
      Services.focus.focusedElement,
      prompt.ui.button0,
      `Prompt #${index} should have focus on default button.`
    );
  }
}

/**
 * Test that we set the correct focus when queuing multiple prompts.
 */
add_task(async function test_tabdialogbox_prompt_queue_focus() {
  await BrowserTestUtils.withNewTab(gBrowser, async browser => {
    const PROMPT_COUNT = 10;

    let firstPromptPromise = PromptTestUtils.waitForPrompt(browser, {
      modalType: Services.prompt.MODAL_TYPE_TAB,
      promptType: "alert",
    });

    for (let i = 0; i < PROMPT_COUNT; i += 1) {
      Services.prompt.asyncAlert(
        browser.browsingContext,
        Services.prompt.MODAL_TYPE_TAB,
        null,
        "prompt " + i
      );
    }

    // Close prompts one by one and check focus.
    let nextPromptPromise = firstPromptPromise;
    for (let i = 0; i < PROMPT_COUNT; i += 1) {
      let p = await nextPromptPromise;
      testAlertPromptFocus(p, i);

      if (i < PROMPT_COUNT - 1) {
        nextPromptPromise = PromptTestUtils.waitForPrompt(browser, {
          modalType: Services.prompt.MODAL_TYPE_TAB,
          promptType: "alert",
        });
      }
      await PromptTestUtils.handlePrompt(p);
    }

    // All prompts are closed, focus should be back on the browser.
    is(
      Services.focus.focusedElement,
      browser,
      "Tab without prompts should have focus on browser."
    );
  });
});