summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/bugs/test_bug61098.html
blob: 9502a28749951927aa4d5e9dd9a2c9091c532d06 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=61098
-->
<head>
  <title>Test for Bug 61098</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="/tests/SimpleTest/MockObjects.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="runtests();">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=61098">Mozilla Bug 61098</a>
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<script class="testbody" type="text/javascript">
/** Test for Bug 61098 **/

SimpleTest.waitForExplicitFinish();

var mockPromptServiceRegisterer;

var promptState;

function registerMockPromptService()
{
  var { XPCOMUtils } = SpecialPowers.ChromeUtils.importESModule(
    "resource://gre/modules/XPCOMUtils.sys.mjs"
  );
  var Ci = SpecialPowers.Ci;

  function MockPrompt(aDOMWindow) {
    this.domWindow = SpecialPowers.unwrap(aDOMWindow);
  }

  MockPrompt.prototype = {
    QueryInterface(iid) {
      if (iid.equals(Ci.nsIPrompt)) {
        return this;
      }
      throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    },

    domWindow : null,

    _toggleModalState()
    {
      // The real prompt service puts the window into a modal state
      // immediately before showing a modal prompt, and leaves the modal state
      // when the prompt is dismissed by the user. This mock prompt doesn't
      // show anything to the user, so we only need to enter and immediately
      // leave the modal state -- this is done to trigger the necessary
      // accounting for triggering the "stop showing more prompts" code for
      // abusive pages.
      var winUtils = SpecialPowers.getDOMWindowUtils(this.domWindow);
      winUtils.enterModalState();
      winUtils.leaveModalState();
    },

    alert(aDialogTitle, aText)
    {
      this._toggleModalState();
      promptState = {method: "alert",
                     parent: SpecialPowers.unwrap(this.domWindow),
                     title: aDialogTitle,
                     msg: aText
      };
    },

    alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState)
    {
      this._toggleModalState();
      promptState = {method: "alertCheck",
                     parent: SpecialPowers.unwrap(this.domWindow),
                     title: aDialogTitle,
                     msg: aText,
                     checkMsg: aCheckMsg,
                     checkState: aCheckState
      };

      SpecialPowers.wrap(aCheckState).value = true;
    },

    confirm(aDialogTitle, aText)
    {
      this._toggleModalState();
      promptState = {method: "confirm",
                     parent: SpecialPowers.unwrap(this.domWindow),
                     title: aDialogTitle,
                     msg: aText
      };

      return true;
    },

    confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState)
    {
      this._toggleModalState();
      promptState = {method: "confirmCheck",
                     parent: SpecialPowers.unwrap(this.domWindow),
                     title: aDialogTitle,
                     msg: aText,
                     checkMsg: aCheckMsg,
                     checkState: aCheckState
      };

      SpecialPowers.wrap(aCheckState).value = true;

      return true;
    },

    confirmEx(aDialogTitle, aText, aButtonFlags,
                        aButton0Title, aButton1Title, aButton2Title,
                        aCheckMsg, aCheckState)
    {
      this._toggleModalState();
      promptState = {method: "confirmCheck",
                     parent: SpecialPowers.unwrap(this.domWindow),
                     title: aDialogTitle,
                     msg: aText,
                     checkMsg: aCheckMsg,
                     checkState: aCheckState
      };

      if (aCheckMsg != null)
        SpecialPowers.wrap(aCheckState).value = true;

      return 0;
    },

    prompt(aDialogTitle, aText, aValue, aCheckMsg,
                     aCheckState)
    {
      this._toggleModalState();
      promptState = {method: "prompt",
                     parent: SpecialPowers.unwrap(this.domWindow),
                     title: aDialogTitle,
                     msg: aText,
                     checkMsg: aCheckMsg,
                     checkState: aCheckState
      };

      if (aCheckMsg != null)
        SpecialPowers.wrap(aCheckState).value = true;

      return true;
    },
  };


  // Override the prompt service with our own so that we can test
  // modal dialogs

  function MockPromptService()
  {
  }

  MockPromptService.prototype = {
    QueryInterface(iid) {
      if (iid.equals(Ci.nsIPromptFactory) || iid.equals(Ci.nsIPromptService)) {
        return this;
      }
      throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    },

    getPrompt(aDOMWindow, aIID)
    {
        return SpecialPowers.wrapCallbackObject(new MockPrompt(aDOMWindow));
    },

    alert(aParent, aDialogTitle, aText)
    {
      var prompt = new MockPrompt(aParent);
      return prompt.alert(aDialogTitle, aText);
    },

    alertCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState)
    {
      var prompt = new MockPrompt(aParent);
      return prompt.alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState);
    },

    confirm(aParent, aDialogTitle, aText)
    {
      var prompt = new MockPrompt(aParent);
      return prompt.confirm(aDialogTitle, aText);
    },

    confirmCheck(aParent, aDialogTitle, aText, aCheckMsg,
                           aCheckState)
    {
      var prompt = new MockPrompt(aParent);
      return prompt.confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState);
    },

    confirmEx(aParent, aDialogTitle, aText, aButtonFlags,
                        aButton0Title, aButton1Title, aButton2Title,
                        aCheckMsg, aCheckState)
    {
      var prompt = new MockPrompt(aParent);
      return prompt.confirmEx(aDialogTitle, aText, aButtonFlags,
                        aButton0Title, aButton1Title, aButton2Title,
                        aCheckMsg, aCheckState);
    },

    prompt(aParent, aDialogTitle, aText, aValue, aCheckMsg,
                     aCheckState)
    {
      var prompt = new MockPrompt(aParent);
      return prompt.prompt(aDialogTitle, aText, aValue, aCheckMsg, aCheckState);
    },

  };

  mockPromptServiceRegisterer =
    new MockObjectRegisterer("@mozilla.org/prompter;1",
                             MockPromptService);
  mockPromptServiceRegisterer.register();
};

var expectedState;

function runtests()
{
  SpecialPowers.pushPrefEnv({'set': [["dom.successive_dialog_time_limit", 3]]},
                            runtestsInner);
}

async function runtestsInner()
{
  registerMockPromptService();

  // Test that alert() works normally and then gets blocked on the
  // second call.
  w = window.open();
  w.alert("alert message 1");
  is (promptState.method, "alert", "Wrong prompt method called");
  is (promptState.parent, w, "Wrong alert parent");
  is (promptState.msg, "alert message 1", "Wrong alert message");
  promptState = void(0);

  w.alert("alert message 2");
  is (promptState.method, "alertCheck", "Wrong prompt method called");
  is (promptState.parent, w, "Wrong alert parent");
  is (promptState.msg, "alert message 2", "Wrong alert message");
  promptState = void(0);

  try {
    w.alert("alert message 3");
  } catch(e) {
    is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception");
  }

  is (promptState, void(0), "Wrong prompt state after blocked alert()");

  w.close();

  // Then check that alert() gets blocked with a new window, because
  // dialogs are disabled/enabled on basis of browsing context groups.
  w = window.open();

  try {
    w.alert("alert message 4");
  } catch(e) {
    is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception");
  }

  is (promptState, void(0), "Wrong prompt state after blocked alert()");

  // Reset the state and enable the dialogs again.
  SpecialPowers.DOMWindowUtils.enableDialogs();
  SpecialPowers.DOMWindowUtils.resetDialogAbuseState();

  // Test that confirm() works normally and then gets blocked on the
  // second call.
  w.confirm("confirm message 1");
  is (promptState.method, "confirm", "Wrong prompt method called");
  is (promptState.parent, w, "Wrong confirm parent");
  is (promptState.msg, "confirm message 1", "Wrong confirm message");
  promptState = void(0);

  w.confirm("confirm message 2");
  is (promptState.method, "confirmCheck", "Wrong prompt method called");
  is (promptState.parent, w, "Wrong confirm parent");
  is (promptState.msg, "confirm message 2", "Wrong confirm message");
  promptState = void(0);

  try {
    w.confirm("confirm message 3");
  } catch(e) {
    is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception");
  }

  is (promptState, void(0), "Wrong prompt state after blocked confirm()");

  w.close();

  // Then check that confirm() gets blocked with a new window, because
  // dialogs are disabled/enabled on basis of browsing context groups.
  w = window.open();

  try {
    w.confirm("confirm message 4");
  } catch(e) {
    is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception");
  }

  is (promptState, void(0), "Wrong prompt state after blocked prompt()");

  // Reset the state and enable the dialogs again.
  SpecialPowers.DOMWindowUtils.enableDialogs();
  SpecialPowers.DOMWindowUtils.resetDialogAbuseState();

  // Test that prompt() works normally and then gets blocked on the
  // second call.
  w.prompt("prompt message 1");
  is (promptState.method, "prompt", "Wrong prompt method called");
  is (promptState.parent, w, "Wrong prompt parent");
  is (promptState.msg, "prompt message 1", "Wrong prompt message");
  is (promptState.checkMsg, null, "Wrong dialog value");
  promptState = void(0);

  w.prompt("prompt message 2");
  is (promptState.method, "prompt", "Wrong prompt method called");
  is (promptState.parent, w, "Wrong prompt parent");
  is (promptState.msg, "prompt message 2", "Wrong prompt message");
  is (promptState.checkMsg, "Prevent this page from creating additional dialogs", "Wrong dialog value");
  promptState = void(0);

  try {
    w.prompt("prompt message 3");
  } catch(e) {
    is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception");
  }

  is (promptState, void(0), "Wrong prompt state after blocked prompt()");

  w.close();

  // Then check that prompt() gets blocked with a new window, because
  // dialogs are disabled/enabled on basis of browsing context groups.
  w = window.open();

  try {
    w.prompt("prompt message 4");
  } catch(e) {
    is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception");
  }

  is (promptState, void(0), "Wrong prompt state after blocked prompt()");

  w.close();

  // Reset the state and enable the dialogs again.
  SpecialPowers.DOMWindowUtils.enableDialogs();
  SpecialPowers.DOMWindowUtils.resetDialogAbuseState();

  mockPromptServiceRegisterer.unregister();

  SimpleTest.finish();
}

</script>
</body>
</html>