summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabdialogs/browser_tabdialogbox_focus.js
blob: 08c0e8828df9041a7ad36efb0eb1774af141458d (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
203
204
205
206
207
208
209
210
211
212
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_ROOT_CHROME = getRootDirectory(gTestPath);
const TEST_DIALOG_PATH = TEST_ROOT_CHROME + "subdialog.xhtml";

/**
 * Tests that tab dialogs 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);

  // Open subdialog in first two tabs
  let dialogs = [];
  for (let i = 0; i < 2; i += 1) {
    let dialogBox = gBrowser.getTabDialogBox(tabs[i].linkedBrowser);
    dialogBox.open(TEST_DIALOG_PATH);
    dialogs.push(dialogBox.getTabDialogManager()._topDialog);
  }

  // Wait for dialogs to be ready
  await Promise.all([dialogs[0]._dialogReady, dialogs[1]._dialogReady]);

  // Switch to first tab which has dialog
  await BrowserTestUtils.switchTab(gBrowser, tabs[0]);

  // The textbox in the dialogs content window should be focused
  let dialogTextbox =
    dialogs[0]._frame.contentDocument.querySelector("#textbox");
  is(Services.focus.focusedElement, dialogTextbox, "Dialog textbox is focused");

  // Switch to second tab which has dialog
  await BrowserTestUtils.switchTab(gBrowser, tabs[1]);

  // The textbox in the dialogs content window should be focused
  let dialogTextbox2 =
    dialogs[1]._frame.contentDocument.querySelector("#textbox");
  is(
    Services.focus.focusedElement,
    dialogTextbox2,
    "Dialog2 textbox is focused"
  );

  // Switch to third tab which does not have a dialog
  await BrowserTestUtils.switchTab(gBrowser, tabs[2]);

  // Test that content is focused
  is(
    Services.focus.focusedElement,
    tabs[2].linkedBrowser,
    "Top level browser is focused"
  );

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

/**
 * Tests that if we're showing multiple tab dialogs they are focused in the
 * correct order and custom focus handlers are called.
 */
add_task(async function test_tabdialogbox_multiple_focus() {
  await BrowserTestUtils.withNewTab(gBrowser, async browser => {
    let dialogBox = gBrowser.getTabDialogBox(browser);
    let dialogAClose = dialogBox.open(
      TEST_DIALOG_PATH,
      {},
      {
        testCustomFocusHandler: true,
      }
    ).closedPromise;
    let dialogBClose = dialogBox.open(TEST_DIALOG_PATH).closedPromise;
    let dialogCClose = dialogBox.open(
      TEST_DIALOG_PATH,
      {},
      {
        testCustomFocusHandler: true,
      }
    ).closedPromise;

    let dialogs = dialogBox._tabDialogManager._dialogs;
    let [dialogA, dialogB, dialogC] = dialogs;

    // Wait until all dialogs are ready
    await Promise.all(dialogs.map(dialog => dialog._dialogReady));

    // Dialog A's custom focus target should be focused
    let dialogElementA =
      dialogA._frame.contentDocument.querySelector("#custom-focus-el");
    is(
      Services.focus.focusedElement,
      dialogElementA,
      "Dialog A custom focus target is focused"
    );

    // Close top dialog
    dialogA.close();
    await dialogAClose;

    // Dialog B's first focus target should be focused
    let dialogElementB =
      dialogB._frame.contentDocument.querySelector("#textbox");
    is(
      Services.focus.focusedElement,
      dialogElementB,
      "Dialog B default focus target is focused"
    );

    // close top dialog
    dialogB.close();
    await dialogBClose;

    // Dialog C's custom focus target should be focused
    let dialogElementC =
      dialogC._frame.contentDocument.querySelector("#custom-focus-el");
    is(
      Services.focus.focusedElement,
      dialogElementC,
      "Dialog C custom focus target is focused"
    );

    // Close last dialog
    dialogC.close();
    await dialogCClose;

    is(
      dialogBox._tabDialogManager._dialogs.length,
      0,
      "All dialogs should be closed"
    );
    is(
      Services.focus.focusedElement,
      browser,
      "Focus should be back on the browser"
    );
  });
});

/**
 * Tests that other dialogs are still visible if one dialog is hidden.
 */
add_task(async function test_tabdialogbox_tab_switch_hidden() {
  // Open 2 tabs
  let tabPromises = [];
  for (let i = 0; i < 2; 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);

  // Open subdialog in tabs
  let dialogs = [];
  let dialogBox, dialogBoxManager, browser;
  for (let i = 0; i < 2; i += 1) {
    dialogBox = gBrowser.getTabDialogBox(tabs[i].linkedBrowser);
    browser = tabs[i].linkedBrowser;
    dialogBox.open(TEST_DIALOG_PATH);
    dialogBoxManager = dialogBox.getTabDialogManager();
    dialogs.push(dialogBoxManager._topDialog);
  }

  // Wait for dialogs to be ready
  await Promise.all([dialogs[0]._dialogReady, dialogs[1]._dialogReady]);

  // Hide the top dialog
  dialogBoxManager.hideDialog(browser);

  ok(
    BrowserTestUtils.is_hidden(dialogBoxManager._dialogStack),
    "Dialog stack is hidden"
  );

  // Switch to first tab
  await BrowserTestUtils.switchTab(gBrowser, tabs[0]);

  // Check the dialog stack is showing in first tab
  dialogBoxManager = gBrowser
    .getTabDialogBox(tabs[0].linkedBrowser)
    .getTabDialogManager();
  is(dialogBoxManager._dialogStack.hidden, false, "Dialog stack is showing");

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