summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_autocomplete_password_generation.html
blob: 3fd8b1e90b61f0491b42ea5838ae4bb460ad823f (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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autofill and autocomplete on autocomplete=new-password fields</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="pwmgr_common.js"></script>
  <script src="../../../satchel/test/satchel_common.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content"></div>
<pre id="test">
Login Manager test: autofill with password generation on `autocomplete=new-password` fields.

<template id="form1-template">
  <form id="form1" action="https://autofill">
    <input type="text" name="uname">
    <input type="password" name="p">
    <button type="submit" name="submit">Submit</button>
  </form>
</template>

<template id="form2-template">
  <form id="form2" action="https://autofill">
    <input type="text" name="uname">
    <input type="password" name="password" autocomplete="new-password">
    <button type="submit" name="submit">Submit</button>
  </form>
</template>

<template id="form3-template">
  <form id="form3" action="https://autofill">
    <input type="text" name="username">
    <label>New password<input type="password" name="password"></label>
    <button type="submit" name="submit">Submit</button>
  </form>
</template>

<script class="testbody" type="text/javascript">
  const { TestUtils } = SpecialPowers.ChromeUtils.importESModule(
    "resource://testing-common/TestUtils.sys.mjs"
  );

  const formTemplates = {
    form1: document.getElementById("form1-template"),
    form2: document.getElementById("form2-template"),
    form3: document.getElementById("form3-template"),
  };

  const dateAndTimeFormatter = new SpecialPowers.Services.intl.DateTimeFormat(undefined, {
    dateStyle: "medium",
  });

  async function showACPopup(formNumber, expectedACLabels) {
    const autocompleteItems = await popupByArrowDown();
    checkAutoCompleteResults(autocompleteItems, expectedACLabels,
                             window.location.host, "Check all rows are correct");
  }

  function clearGeneratedPasswords() {
    const { LoginManagerParent } = ChromeUtils.importESModule(
      "resource://gre/modules/LoginManagerParent.sys.mjs"
    );
    if (LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin()) {
      LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().clear();
    }
  }

  add_named_task("autofill autocomplete username no generation", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", false],
      ["signon.generation.enabled", false],
    );
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form1 = setContentForTask(formTemplates.form1);

    const autofillResult1 = await formAutofillResult(form1.id);
    is(autofillResult1, "filled", "form has not been filled due to multiple logins");

    // reference form was filled as expected?
    is(form1.uname.value, "user1", "username is filled");
    is(form1.p.value, "pass1", "password is filled");

    const form2 = setContentForTask(formTemplates.form2);
    const autofillResult2 = await formAutofillResult(form2.id);
    is(autofillResult2, "password_autocomplete_new_password", "form has not been filled due to password_autocomplete_new_password");

    // 2nd form should not be filled
    is(form2.uname.value, "", "username is empty");
    is(form2.password.value, "", "password is empty");

    form2.uname.focus();
    await showACPopup(2, ["user1"]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");

    const autofillResult3 = await formAutofillResult(form2.id);
    is(autofillResult3, "filled", "form has been filled");
    is(form2.uname.value, "user1", "username is filled");
    is(form2.password.value, "pass1", "password is filled");
  });

  add_named_task("autofill autocomplete password no generation", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", false],
      ["signon.generation.enabled", false],
    );
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form = setContentForTask(formTemplates.form2);
    const autofillResult = await formAutofillResult(form.id);
    is(autofillResult, "password_autocomplete_new_password", "form has not been filled due to password_autocomplete_new_password");

    // form should not be filled
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "", "password is empty");

    form.password.focus();
    // No generation option on username fields.
    await showACPopup(2, ["user1"]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");

    await SimpleTest.promiseWaitForCondition(() => form.password.value == "pass1", "Check pw filled");
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "pass1", "password is filled");

    // No autocomplete results should appear for non-empty pw fields.
    await noPopupByArrowDown();
  });

  add_named_task("autofill autocomplete username no generation #2", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", false],
      ["signon.generation.enabled", false],
    );
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form = setContentForTask(formTemplates.form2);
    const autofillResult1 = await formAutofillResult(form.id);
    is(autofillResult1, "password_autocomplete_new_password", "form has not been filled due to password_autocomplete_new_password");

    // form should not be filled
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "", "password is empty");

    form.uname.focus();
    // No generation option on username fields.
    await showACPopup(2, ["user1"]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");

    const autofillResult2 = await formAutofillResult(form.id);
    is(autofillResult2, "filled", "form has been filled");
    is(form.uname.value, "user1", "username is filled");
    is(form.password.value, "pass1", "password is filled");
  });

  add_named_task("autofill autocomplete passsword with generation", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", true],
      ["signon.generation.enabled", true],
    );

    runInParent(clearGeneratedPasswords);
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form = setContentForTask(formTemplates.form2);
    const formNumber = 2;

    const autofillResult = await formAutofillResult(form.id);
    is(autofillResult, "password_autocomplete_new_password", "form has not been filled due to password_autocomplete_new_password");

    form.reset();

    // form should not be filled
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "", "password is empty");

    form.password.focus();

    await showACPopup(formNumber, [
      "user1",
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");
    await SimpleTest.promiseWaitForCondition(() => form.password.value == "pass1", "Check pw filled");
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "pass1", "password is filled");

    // No autocomplete results should appear for non-empty pw fields.
    await noPopupByArrowDown();

    info("Removing all logins to test auto-saving of generated passwords");
    await LoginManager.removeAllUserFacingLogins();

    while (form.password.value) {
      synthesizeKey("KEY_Backspace");
    }
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blanked field");

    info("This time select the generated password");
    await showACPopup(formNumber, [
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    const storageAddPromise = promiseStorageChanged(["addLogin"]);
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Before first fill of generated pw");
    synthesizeKey("KEY_Enter");

    info("waiting for the password field to be filled with the generated password");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check generated pw filled");
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "After first fill of generated pw");
    info("Wait for generated password to be added to storage");
    await storageAddPromise;

    let logins = await LoginManager.getAllLogins();
    const timePasswordChanged = logins[logins.length - 1].timePasswordChanged;
    const time = dateAndTimeFormatter.format(new Date(timePasswordChanged));
    const LABEL_NO_USERNAME = "No username (" + time + ")";

    const generatedPW = form.password.value;
    is(generatedPW.length, GENERATED_PASSWORD_LENGTH, "Check generated password length");
    ok(generatedPW.match(GENERATED_PASSWORD_REGEX), "Check generated password format");
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "After fill");

    info("Check field is masked upon blurring");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "After blur");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "After shift-tab to focus again");
    // Remove selection for OS where the whole value is selected upon focus.
    synthesizeKey("KEY_ArrowRight");

    while (form.password.value) {
      LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, form.password.value);
      synthesizeKey("KEY_Backspace");
    }
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blanked field");

    info("Blur the empty field to trigger a 'change' event");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blur after blanking");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Focus again after blanking");

    info("Type a single character after blanking");
    synthesizeKey("@");

    info("Blur the single-character field to trigger a 'change' event");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blur after backspacing");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Focus again after backspacing");
    synthesizeKey("KEY_Backspace"); // Blank the field again

    await showACPopup(formNumber, [
      LABEL_NO_USERNAME,
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check generated pw filled");
    // Same generated password should be used, even despite the 'change' to @ earlier.
    is(form.uname.value, "", "username is empty");
    is(form.password.value, generatedPW, "password is filled with generated password");
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "Second fill of the generated pw");

    logins = await LoginManager.getAllLogins();
    is(logins.length, 1, "Still 1 login after filling the generated password a 2nd time");
    is(logins[0].timePasswordChanged, timePasswordChanged, "Saved login wasn't changed");
    is(logins[0].password, generatedPW, "Password is the same");

    info("filling the saved login to ensure the field is masked again");

    while (form.password.value) {
      LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, form.password.value);
      synthesizeKey("KEY_Backspace");
    }
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blanked field again");

    info("Blur the field to trigger a 'change' event again");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blur after blanking again");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Focus again after blanking again");
    // Remove selection for OS where the whole value is selected upon focus.
    synthesizeKey("KEY_ArrowRight");

    await showACPopup(formNumber, [
      LABEL_NO_USERNAME,
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check saved generated pw filled");
    // Same generated password should be used but from storage
    is(form.uname.value, "", "username is empty");
    is(form.password.value, generatedPW, "password is filled with generated password");
    // Passwords from storage should always be masked.
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "after fill from storage");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "after blur");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "after shift-tab to focus again");
  });

  add_named_task("autofill autocomplete passsword with generation #2", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", true],
      ["signon.generation.enabled", true],
    );

    runInParent(clearGeneratedPasswords);
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form = setContentForTask(formTemplates.form3);
    const formNumber = 3;

    const autofillResult = await formAutofillResult(form.id);
    is(autofillResult, "filled", "form has been filled");

    form.reset();

    is(form.username.value, "", "username is empty");
    is(form.password.value, "", "password is empty");

    form.password.focus();

    await showACPopup(formNumber, [
      "user1",
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");
    await SimpleTest.promiseWaitForCondition(() => form.password.value == "pass1", "Check pw filled");
    is(form.username.value, "", "username is empty");
    is(form.password.value, "pass1", "password is filled");

    // No autocomplete results should appear for non-empty pw fields.
    await noPopupByArrowDown();

    info("Removing all logins to test auto-saving of generated passwords");
    await LoginManager.removeAllUserFacingLogins();

    while (form.password.value) {
      synthesizeKey("KEY_Backspace");
    }
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blanked field");

    info("This time select the generated password");
    await showACPopup(formNumber, [
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    const storageAddPromise = promiseStorageChanged(["addLogin"]);
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Before first fill of generated pw");
    synthesizeKey("KEY_Enter");

    info("waiting for the password field to be filled with the generated password");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check generated pw filled");
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "After first fill of generated pw");
    info("Wait for generated password to be added to storage");
    await storageAddPromise;

    let logins = await LoginManager.getAllLogins();
    const timePasswordChanged = logins[logins.length - 1].timePasswordChanged;
    const time = dateAndTimeFormatter.format(new Date(timePasswordChanged));
    const LABEL_NO_USERNAME = "No username (" + time + ")";

    const generatedPW = form.password.value;
    is(generatedPW.length, GENERATED_PASSWORD_LENGTH, "Check generated password length");
    ok(generatedPW.match(GENERATED_PASSWORD_REGEX), "Check generated password format");
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "After fill");

    info("Check field is masked upon blurring");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "After blur");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "After shift-tab to focus again");
    // Remove selection for OS where the whole value is selected upon focus.
    synthesizeKey("KEY_ArrowRight");

    while (form.password.value) {
      LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, form.password.value);
      synthesizeKey("KEY_Backspace");
    }
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blanked field");

    info("Blur the empty field to trigger a 'change' event");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blur after blanking");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Focus again after blanking");

    info("Type a single character after blanking");
    synthesizeKey("@");

    info("Blur the single-character field to trigger a 'change' event");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blur after backspacing");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Focus again after backspacing");
    synthesizeKey("KEY_Backspace"); // Blank the field again

    await showACPopup(formNumber, [
      LABEL_NO_USERNAME,
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check generated pw filled");
    // Same generated password should be used, even despite the 'change' to @ earlier.
    is(form.username.value, "", "username is empty");
    is(form.password.value, generatedPW, "password is filled with generated password");
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, "Second fill of the generated pw");

    logins = await LoginManager.getAllLogins();
    is(logins.length, 1, "Still 1 login after filling the generated password a 2nd time");
    is(logins[0].timePasswordChanged, timePasswordChanged, "Saved login wasn't changed");
    is(logins[0].password, generatedPW, "Password is the same");

    info("filling the saved login to ensure the field is masked again");

    while (form.password.value) {
      LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, false, form.password.value);
      synthesizeKey("KEY_Backspace");
    }
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blanked field again");

    info("Blur the field to trigger a 'change' event again");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Blur after blanking again");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "Focus again after blanking again");
    // Remove selection for OS where the whole value is selected upon focus.
    synthesizeKey("KEY_ArrowRight");

    await showACPopup(formNumber, [
      LABEL_NO_USERNAME,
      "Use a Securely Generated Password",
    ]);

    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Enter");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check saved generated pw filled");
    // Same generated password should be used but from storage
    is(form.username.value, "", "username is empty");
    is(form.password.value, generatedPW, "password is filled with generated password");
    // Passwords from storage should always be masked.
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "after fill from storage");
    synthesizeKey("KEY_Tab"); // blur
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "after blur");
    synthesizeKey("KEY_Tab", { shiftKey: true }); // focus
    LOGIN_FIELD_UTILS.checkPasswordMasked(form.password, true, "after shift-tab to focus again");
  });

  add_named_task("autofill autocomplete password save login disabled", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", true],
      ["signon.generation.enabled", true],
    );
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form = setContentForTask(formTemplates.form2);
    const autofillResult = await formAutofillResult(form.id);
    is(autofillResult, "password_autocomplete_new_password", "form has not been filled due to password_autocomplete_new_password");

    // form should not be filled
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "", "password is empty");

    const formOrigin = new URL(document.documentURI).origin;
    is(formOrigin, location.origin, "Expected form origin");

    await LoginManager.setLoginSavingEnabled(location.origin, false);

    form.password.focus();
    // when login-saving is disabled for an origin, we expect no generated password row here
    await showACPopup(2, ["user1"]);

    // close any open menu
    synthesizeKey("KEY_Escape");
    await untilAutocompletePopupClosed();

    await LoginManager.setLoginSavingEnabled(location.origin, true);
  });

  add_named_task("delete and reselect generated password", async () => {
    await setPreferencesForTask(
      ["signon.generation.available", true],
      ["signon.generation.enabled", true],
    );
    await setStoredLoginsDuringTask([location.origin, "https://autofill", null, "user1", "pass1"]);

    const form = setContentForTask(formTemplates.form2);
    const autofillResult = await formAutofillResult(form.id);
    is(autofillResult, "password_autocomplete_new_password", "form has not been filled due to password_autocomplete_new_password");

    info("Removing all logins to test auto-saving of generated passwords");
    await LoginManager.removeAllUserFacingLogins();

    // form should not be filled
    is(form.uname.value, "", "username is empty");
    is(form.password.value, "", "password is empty");

    async function showAndSelectACPopupItem(index) {
      form.password.focus();
      if (form.password.value) {
        form.password.select();
        synthesizeKey("KEY_Backspace");
      }
      const autocompleteItems = await popupByArrowDown();
      if (index < 0) {
        index = autocompleteItems.length + index;
      }
      for (let i = 0; i <= index; i++) {
        synthesizeKey("KEY_ArrowDown");
      }
      await TestUtils.waitForTick();
      return autocompleteItems[index];
    }

    let menuLabel, itemIndex, savedLogins;

    // fill the password field with the generated password via auto-complete menu
    const addLoginPromise = promiseStorageChanged(["addLogin"]);
    // select last-but-2 item - the one before the footer
    menuLabel = await showAndSelectACPopupItem(-2);
    is(menuLabel, "Use a Securely Generated Password", "Check item label");
    synthesizeKey("KEY_Enter");
    info("waiting for the password field to be filled with the generated password");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check generated pw filled");
    info("Wait for generated password to be added to storage");
    await addLoginPromise;

    savedLogins = await LoginManager.getAllLogins();
    is(savedLogins.length, 1, "Check saved logins count");

    form.uname.focus();
    await TestUtils.waitForTick();

    is(form.password.value.length, LoginTestUtils.generation.LENGTH, "Check password looks generated");
    const GENERATED_PASSWORD = form.password.value;

    info("clear the password field and delete the saved login using the AC menu")
    const removeLoginPromise = promiseStorageChanged(["removeLogin"]);

    itemIndex = 0;
    menuLabel = await showAndSelectACPopupItem(itemIndex);
    ok(menuLabel.includes("No username"), "Check first item is the auto-saved login");
    // Send delete to remove the auto-saved login from storage
    // On OS X, shift-backspace and shift-delete work, just delete does not.
    // On Win/Linux, shift-backspace does not work, delete and shift-delete do.
    synthesizeKey("KEY_Delete", {shiftKey: true});
    await removeLoginPromise;

    form.uname.focus();
    await TestUtils.waitForTick();

    savedLogins = await LoginManager.getAllLogins();
    is(savedLogins.length, 0, "Check saved logins count");

    info("Re-fill with the generated password");
    // select last-but-2 item - the one before the footer
    menuLabel = await showAndSelectACPopupItem(-2);
    is(menuLabel, "Use a Securely Generated Password", "Check item label");
    synthesizeKey("KEY_Enter");
    info("waiting for the password field to be filled with the generated password");
    await SimpleTest.promiseWaitForCondition(() => !!form.password.value, "Check generated pw filled");

    form.uname.focus();
    await TestUtils.waitForTick();
    is(form.password.value, GENERATED_PASSWORD, "Generated password has not changed");
  });
</script>
</pre>
</body>
</html>