summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabPrompts/browser_windowPrompt.js
blob: 535142f485ec42f96ca1f648df8427974cd68781 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Check that the in-window modal dialogs work correctly.
 */
add_task(async function test_check_window_modal_prompt_service() {
  await SpecialPowers.pushPrefEnv({
    set: [["prompts.windowPromptSubDialog", true]],
  });
  let dialogPromise = BrowserTestUtils.promiseAlertDialogOpen();
  // Avoid blocking the test on the (sync) alert by sticking it in a timeout:
  setTimeout(
    () => Services.prompt.alert(window, "Some title", "some message"),
    0
  );
  let dialogWin = await dialogPromise;

  // Check dialog content:
  is(
    dialogWin.document.getElementById("infoTitle").textContent,
    "Some title",
    "Title should be correct."
  );
  ok(
    !dialogWin.document.getElementById("infoTitle").hidden,
    "Title should be shown."
  );
  is(
    dialogWin.document.getElementById("infoBody").textContent,
    "some message",
    "Body text should be correct."
  );

  // Check circumstances of opening.
  ok(
    dialogWin?.docShell?.chromeEventHandler,
    "Should have embedded the dialog."
  );
  for (let menu of document.querySelectorAll("menubar > menu")) {
    ok(menu.disabled, `Menu ${menu.id} should be disabled.`);
  }

  let container = dialogWin.docShell.chromeEventHandler.closest("dialog");
  let closedPromise = BrowserTestUtils.waitForMutationCondition(
    container,
    { childList: true, attributes: true },
    () => !container.hasChildNodes() && !container.open
  );

  EventUtils.sendKey("ESCAPE");
  await closedPromise;

  await BrowserTestUtils.waitForMutationCondition(
    document.querySelector("menubar > menu"),
    { attributes: true },
    () => !document.querySelector("menubar > menu").disabled
  );

  // Check we cleaned up:
  for (let menu of document.querySelectorAll("menubar > menu")) {
    ok(!menu.disabled, `Menu ${menu.id} should not be disabled anymore.`);
  }
});

/**
 * Check that the dialog's own closing methods being invoked don't break things.
 */
add_task(async function test_check_window_modal_prompt_service() {
  await SpecialPowers.pushPrefEnv({
    set: [["prompts.windowPromptSubDialog", true]],
  });
  let dialogPromise = BrowserTestUtils.promiseAlertDialogOpen();
  // Avoid blocking the test on the (sync) alert by sticking it in a timeout:
  setTimeout(
    () => Services.prompt.alert(window, "Some title", "some message"),
    0
  );
  let dialogWin = await dialogPromise;

  // Check circumstances of opening.
  ok(
    dialogWin?.docShell?.chromeEventHandler,
    "Should have embedded the dialog."
  );

  let container = dialogWin.docShell.chromeEventHandler.closest("dialog");
  let closedPromise = BrowserTestUtils.waitForMutationCondition(
    container,
    { childList: true, attributes: true },
    () => !container.hasChildNodes() && !container.open
  );

  // This can also be invoked by the user if the escape key is handled
  // outside of our embedded dialog.
  container.close();
  await closedPromise;

  // Check we cleaned up:
  for (let menu of document.querySelectorAll("menubar > menu")) {
    ok(!menu.disabled, `Menu ${menu.id} should not be disabled anymore.`);
  }
});

add_task(async function test_check_multiple_prompts() {
  await SpecialPowers.pushPrefEnv({
    set: [["prompts.windowPromptSubDialog", true]],
  });
  let container = document.getElementById("window-modal-dialog");
  let dialogPromise = BrowserTestUtils.promiseAlertDialogOpen();

  let firstDialogClosedPromise = new Promise(resolve => {
    // Avoid blocking the test on the (sync) alert by sticking it in a timeout:
    setTimeout(() => {
      Services.prompt.alert(window, "Some title", "some message");
      resolve();
    }, 0);
  });
  let dialogWin = await dialogPromise;

  // Check circumstances of opening.
  ok(
    dialogWin?.docShell?.chromeEventHandler,
    "Should have embedded the dialog."
  );
  is(container.childElementCount, 1, "Should only have 1 dialog in the DOM.");

  let secondDialogClosedPromise = new Promise(resolve => {
    // Avoid blocking the test on the (sync) alert by sticking it in a timeout:
    setTimeout(() => {
      Services.prompt.alert(window, "Another title", "another message");
      resolve();
    }, 0);
  });

  dialogPromise = BrowserTestUtils.promiseAlertDialogOpen();

  info("Accepting dialog");
  dialogWin.document.querySelector("dialog").acceptDialog();

  let oldWin = dialogWin;

  info("Second dialog should automatically come up.");
  dialogWin = await dialogPromise;

  isnot(oldWin, dialogWin, "Opened a new dialog.");
  ok(container.open, "Dialog should be open.");

  info("Now close the second dialog.");
  dialogWin.document.querySelector("dialog").acceptDialog();

  await firstDialogClosedPromise;
  await secondDialogClosedPromise;

  await BrowserTestUtils.waitForMutationCondition(
    container,
    { childList: true, attributes: true },
    () => !container.hasChildNodes() && !container.open
  );
  // Check we cleaned up:
  for (let menu of document.querySelectorAll("menubar > menu")) {
    ok(!menu.disabled, `Menu ${menu.id} should not be disabled anymore.`);
  }
});

/**
 * Check that the in-window modal dialogs un-minimizes windows when necessary.
 */
add_task(async function test_check_minimize_response() {
  // Window minimization doesn't necessarily work on Linux...
  if (AppConstants.platform == "linux") {
    return;
  }
  await SpecialPowers.pushPrefEnv({
    set: [["prompts.windowPromptSubDialog", true]],
  });

  let promiseSizeModeChange = BrowserTestUtils.waitForEvent(
    window,
    "sizemodechange"
  );
  window.minimize();
  await promiseSizeModeChange;
  is(window.windowState, window.STATE_MINIMIZED, "Should be minimized.");

  promiseSizeModeChange = BrowserTestUtils.waitForEvent(
    window,
    "sizemodechange"
  );
  let dialogPromise = BrowserTestUtils.promiseAlertDialogOpen();
  // Use an async alert to avoid blocking.
  Services.prompt.asyncAlert(
    window.browsingContext,
    Ci.nsIPrompt.MODAL_TYPE_INTERNAL_WINDOW,
    "Some title",
    "some message"
  );
  let dialogWin = await dialogPromise;
  await promiseSizeModeChange;

  isnot(
    window.windowState,
    window.STATE_MINIMIZED,
    "Should no longer be minimized."
  );

  // Check dialog content:
  is(
    dialogWin.document.getElementById("infoTitle").textContent,
    "Some title",
    "Title should be correct."
  );

  let container = dialogWin.docShell.chromeEventHandler.closest("dialog");
  let closedPromise = BrowserTestUtils.waitForMutationCondition(
    container,
    { childList: true, attributes: true },
    () => !container.hasChildNodes() && !container.open
  );

  EventUtils.sendKey("ESCAPE");
  await closedPromise;

  await BrowserTestUtils.waitForMutationCondition(
    document.querySelector("menubar > menu"),
    { attributes: true },
    () => !document.querySelector("menubar > menu").disabled
  );
});

/**
 * Tests that we get a closed callback even when closing the prompt before the
 * underlying SubDialog has fully opened.
 */
add_task(async function test_closed_callback() {
  await SpecialPowers.pushPrefEnv({
    set: [["prompts.windowPromptSubDialog", true]],
  });

  let promptClosedPromise = Services.prompt.asyncAlert(
    window.browsingContext,
    Services.prompt.MODAL_TYPE_INTERNAL_WINDOW,
    "Hello",
    "Hello, World!"
  );

  let dialog = gDialogBox._dialog;
  ok(dialog, "gDialogBox should have a dialog");

  // Directly close the dialog without waiting for it to initialize.
  dialog.close();

  info("Waiting for prompt close");
  await promptClosedPromise;

  ok(!gDialogBox._dialog, "gDialogBox should no longer have a dialog");
});