summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/test/resources/alertTestUtils.js
blob: 2b757480e1d6b6983a1a4b574dbaaa6d4363b1a0 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file provides support for writing mailnews tests that require hooking
 * into the alerts system. Normally these tests would require a UI and fail in
 * debug mode, but with this method you can hook into the alerts system and
 * avoid the UI.
 *
 * This file registers prompts for nsIWindowWatcher::getNewPrompter and also
 * registers a nsIPromptService service. nsIWindowWatcher::getNewAuthPrompter
 * is also implemented but returns the nsILoginManagerPrompter as this would
 * be expected when running mailnews.
 *
 * To register the system:
 *
 * function run_test() {
 *   registerAlertTestUtils();
 *   // ...
 * }
 *
 * You can then hook into the alerts just by defining a function of the same
 * name as the interface function:
 *
 * function alert(aDialogTitle, aText) {
 *   // do my check
 * }
 *
 * Interface functions that do not have equivalent functions defined and get
 * called will be treated as unexpected, and therefore they will call
 * do_throw().
 */
/* globals alert, confirm, prompt */

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { MockRegistrar } = ChromeUtils.importESModule(
  "resource://testing-common/MockRegistrar.sys.mjs"
);

var LoginInfo = Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  "nsILoginInfo",
  "init"
);

// Wrapper to the nsIPrompt interface.
// This allows the send code to attempt to display errors to the user without
// failing.
var alertUtilsPrompts = {
  alert(aDialogTitle, aText) {
    if (typeof alert == "function") {
      alert(aDialogTitle, aText);
      return;
    }

    do_throw("alert unexpectedly called: " + aText + "\n");
  },

  alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState) {
    if (typeof alertCheck == "function") {
      // eslint-disable-next-line no-undef
      alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState);
      return;
    }

    do_throw("alertCheck unexpectedly called: " + aText + "\n");
  },

  confirm(aDialogTitle, aText) {
    if (typeof confirm == "function") {
      return confirm(aDialogTitle, aText);
    }

    do_throw("confirm unexpectedly called: " + aText + "\n");
    return false;
  },

  confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState) {
    if (typeof confirmCheck == "function") {
      // eslint-disable-next-line no-undef
      return confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState);
    }

    do_throw("confirmCheck unexpectedly called: " + aText + "\n");
    return false;
  },

  confirmEx(
    aDialogTitle,
    aText,
    aButtonFlags,
    aButton0Title,
    aButton1Title,
    aButton2Title,
    aCheckMsg,
    aCheckState
  ) {
    if (typeof confirmEx == "function") {
      // eslint-disable-next-line no-undef
      return confirmEx(
        aDialogTitle,
        aText,
        aButtonFlags,
        aButton0Title,
        aButton1Title,
        aButton2Title,
        aCheckMsg,
        aCheckState
      );
    }

    do_throw("confirmEx unexpectedly called: " + aText + "\n");
    return 0;
  },

  prompt(aDialogTitle, aText, aValue, aCheckMsg, aCheckState) {
    if (typeof prompt == "function") {
      return prompt(aDialogTitle, aText, aValue, aCheckMsg, aCheckState);
    }

    do_throw("prompt unexpectedly called: " + aText + "\n");
    return false;
  },

  promptUsernameAndPassword(
    aDialogTitle,
    aText,
    aUsername,
    aPassword,
    aCheckMsg,
    aCheckState
  ) {
    if (typeof promptUsernameAndPassword == "function") {
      // eslint-disable-next-line no-undef
      return promptUsernameAndPassword(
        aDialogTitle,
        aText,
        aUsername,
        aPassword,
        aCheckMsg,
        aCheckState
      );
    }

    do_throw("promptUsernameAndPassword unexpectedly called: " + aText + "\n");
    return false;
  },

  promptPassword(aDialogTitle, aText, aPassword, aCheckMsg, aCheckState) {
    if (typeof promptPassword == "function") {
      // eslint-disable-next-line no-undef
      return promptPassword(
        aDialogTitle,
        aText,
        aPassword,
        aCheckMsg,
        aCheckState
      );
    }

    do_throw("promptPassword unexpectedly called: " + aText + "\n");
    return false;
  },

  select(aDialogTitle, aText, aCount, aSelectList, aOutSelection) {
    if (typeof select == "function") {
      // eslint-disable-next-line no-undef
      return select(aDialogTitle, aText, aCount, aSelectList, aOutSelection);
    }

    do_throw("select unexpectedly called: " + aText + "\n");
    return false;
  },

  QueryInterface: ChromeUtils.generateQI(["nsIPrompt"]),
};

var alertUtilsPromptService = {
  alert(aParent, aDialogTitle, aText) {
    if (typeof alertPS == "function") {
      // eslint-disable-next-line no-undef
      alertPS(aParent, aDialogTitle, aText);
      return;
    }

    do_throw("alertPS unexpectedly called: " + aText + "\n");
  },

  alertCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) {
    if (typeof alertCheckPS == "function") {
      // eslint-disable-next-line no-undef
      alertCheckPS(aParent, aDialogTitle, aText, aCheckMsg, aCheckState);
      return;
    }

    do_throw("alertCheckPS unexpectedly called: " + aText + "\n");
  },

  confirm(aParent, aDialogTitle, aText) {
    if (typeof confirmPS == "function") {
      // eslint-disable-next-line no-undef
      return confirmPS(aParent, aDialogTitle, aText);
    }

    do_throw("confirmPS unexpectedly called: " + aText + "\n");
    return false;
  },

  confirmCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) {
    if (typeof confirmCheckPS == "function") {
      // eslint-disable-next-line no-undef
      return confirmCheckPS(
        aParent,
        aDialogTitle,
        aText,
        aCheckMsg,
        aCheckState
      );
    }

    do_throw("confirmCheckPS unexpectedly called: " + aText + "\n");
    return false;
  },

  confirmEx(
    aParent,
    aDialogTitle,
    aText,
    aButtonFlags,
    aButton0Title,
    aButton1Title,
    aButton2Title,
    aCheckMsg,
    aCheckState
  ) {
    if (typeof confirmExPS == "function") {
      // eslint-disable-next-line no-undef
      return confirmExPS(
        aParent,
        aDialogTitle,
        aText,
        aButtonFlags,
        aButton0Title,
        aButton1Title,
        aButton2Title,
        aCheckMsg,
        aCheckState
      );
    }

    do_throw("confirmExPS unexpectedly called: " + aText + "\n");
    return 0;
  },

  prompt(aParent, aDialogTitle, aText, aValue) {
    if (typeof promptPS == "function") {
      // eslint-disable-next-line no-undef
      return promptPS(aParent, aDialogTitle, aText, aValue);
    }

    do_throw("promptPS unexpectedly called: " + aText + "\n");
    return false;
  },

  promptUsernameAndPassword(
    aParent,
    aDialogTitle,
    aText,
    aUsername,
    aPassword
  ) {
    if (typeof promptUsernameAndPasswordPS == "function") {
      // eslint-disable-next-line no-undef
      return promptUsernameAndPasswordPS(
        aParent,
        aDialogTitle,
        aText,
        aUsername,
        aPassword
      );
    }

    do_throw(
      "promptUsernameAndPasswordPS unexpectedly called: " + aText + "\n"
    );
    return false;
  },

  promptPassword(aParent, aDialogTitle, aText, aPassword) {
    if (typeof promptPasswordPS == "function") {
      // eslint-disable-next-line no-undef
      return promptPasswordPS(aParent, aDialogTitle, aText, aPassword);
    }

    do_throw("promptPasswordPS unexpectedly called: " + aText + "\n");
    return false;
  },

  select(aParent, aDialogTitle, aText, aCount, aSelectList, aOutSelection) {
    if (typeof selectPS == "function") {
      // eslint-disable-next-line no-undef
      return selectPS(
        aParent,
        aDialogTitle,
        aText,
        aCount,
        aSelectList,
        aOutSelection
      );
    }

    do_throw("selectPS unexpectedly called: " + aText + "\n");
    return false;
  },

  createInstance(iid) {
    return this.QueryInterface(iid);
  },

  QueryInterface: ChromeUtils.generateQI([
    "nsIPromptService",
    "nsIPromptService2",
  ]),
};

var alertUtilsWindowWatcher = {
  getNewPrompter(aParent) {
    return alertUtilsPrompts;
  },

  getNewAuthPrompter(aParent) {
    return Cc["@mozilla.org/login-manager/authprompter;1"].getService(
      Ci.nsIAuthPrompt
    );
  },

  QueryInterface: ChromeUtils.generateQI(["nsIWindowWatcher"]),
};

// Special prompt that ensures we get prompted for logins. Calls
// promptPasswordPS/promptUsernameAndPasswordPS directly, rather than through
// the prompt service, because the function signature changed and no longer
// allows a "save password" check box.
let alertUtilsMsgAuthPrompt = {
  QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt"]),

  _getFormattedOrigin(aURI) {
    let uri;
    if (aURI instanceof Ci.nsIURI) {
      uri = aURI;
    } else {
      uri = Services.io.newURI(aURI);
    }

    return uri.scheme + "://" + uri.displayHostPort;
  },

  _getRealmInfo(aRealmString) {
    var httpRealm = /^.+ \(.+\)$/;
    if (httpRealm.test(aRealmString)) {
      return [null, null, null];
    }

    var uri = Services.io.newURI(aRealmString);
    var pathname = "";

    if (uri.pathQueryRef != "/") {
      pathname = uri.pathQueryRef;
    }

    var formattedOrigin = this._getFormattedOrigin(uri);

    return [formattedOrigin, formattedOrigin + pathname, uri.username];
  },

  promptUsernameAndPassword(
    aDialogTitle,
    aText,
    aPasswordRealm,
    aSavePassword,
    aUsername,
    aPassword
  ) {
    var checkBox = { value: false };
    var checkBoxLabel = null;
    var [origin, realm] = this._getRealmInfo(aPasswordRealm);

    if (typeof promptUsernameAndPasswordPS != "function") {
      throw new Error(
        "promptUsernameAndPasswordPS unexpectedly called: " + aText + "\n"
      );
    }

    // eslint-disable-next-line no-undef
    var ok = promptUsernameAndPasswordPS(
      this._chromeWindow,
      aDialogTitle,
      aText,
      aUsername,
      aPassword,
      checkBoxLabel,
      checkBox
    );

    if (!ok || !checkBox.value || !origin) {
      return ok;
    }

    if (!aPassword.value) {
      return ok;
    }

    let newLogin = new LoginInfo(
      origin,
      null,
      realm,
      aUsername.value,
      aPassword.value
    );
    Services.logins.addLogin(newLogin);

    return ok;
  },

  promptPassword(
    aDialogTitle,
    aText,
    aPasswordRealm,
    aSavePassword,
    aPassword
  ) {
    var checkBox = { value: false };
    var checkBoxLabel = null;
    var [origin, realm, username] = this._getRealmInfo(aPasswordRealm);

    username = decodeURIComponent(username);

    if (typeof promptPasswordPS != "function") {
      throw new Error("promptPasswordPS unexpectedly called: " + aText + "\n");
    }

    // eslint-disable-next-line no-undef
    var ok = promptPasswordPS(
      this._chromeWindow,
      aDialogTitle,
      aText,
      aPassword,
      checkBoxLabel,
      checkBox
    );

    if (ok && checkBox.value && origin && aPassword.value) {
      let newLogin = new LoginInfo(
        origin,
        null,
        realm,
        username,
        aPassword.value
      );

      Services.logins.addLogin(newLogin);
    }

    return ok;
  },
};

function registerAlertTestUtils() {
  MockRegistrar.register(
    "@mozilla.org/embedcomp/window-watcher;1",
    alertUtilsWindowWatcher
  );
  MockRegistrar.register(
    "@mozilla.org/messenger/msgAuthPrompt;1",
    alertUtilsMsgAuthPrompt
  );
  MockRegistrar.register("@mozilla.org/prompter;1", alertUtilsPromptService);
  Services.prompt = alertUtilsPromptService;
}

var gDummyMsgWindow = Cc["@mozilla.org/messenger/msgwindow;1"].createInstance(
  Ci.nsIMsgWindow
);