File: test_autocomplete_basic_form.html

package info (click to toggle)
thunderbird 1%3A91.13.0-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,953,400 kB
  • sloc: cpp: 6,084,049; javascript: 4,790,441; ansic: 3,341,496; python: 862,958; asm: 366,542; xml: 204,277; java: 152,477; sh: 111,436; makefile: 21,388; perl: 15,312; yacc: 4,583; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; awk: 564; sql: 453; php: 436; lisp: 432; ruby: 99; sed: 69; csh: 45
file content (1057 lines) | stat: -rw-r--r-- 37,790 bytes parent folder | download | duplicates (4)
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test basic login autocomplete</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="../../../satchel/test/satchel_common.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: multiple login autocomplete

<script>
var chromeScript = runChecksAfterCommonInit();

// Temporarily allow use of eval() in this test.
SpecialPowers.pushPrefEnv({"set": [["security.allow_eval_with_system_principal", true]]});

function addLoginsInParent(...aLogins) {
  let script = runInParent(function addLoginsInParentInner() {
    let loginsMap = {};

    let initialCount = 5;

    addMessageListener("initLogins", logins => {
      // eslint-disable-next-line no-shadow
      const { Services } = ChromeUtils.import(
        "resource://gre/modules/Services.jsm"
      );

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

      for (let login of logins) {
        let loginInfo = new nsLoginInfo(...login[1]);

        // Only add the first 'initialCount' number of logins.
        if (initialCount-- > 0) {
          try {
            Services.logins.addLogin(loginInfo);
          } catch (e) {
            assert.ok(false, "addLogin threw: " + e);
          }
        }
        loginsMap[login[0]] = loginInfo;
      }
    });

    addMessageListener("addLogin", loginVariableName => {
      let login = loginsMap[loginVariableName];
      assert.ok(!!login, "Login to add is defined: " + loginVariableName);
      Services.logins.addLogin(login);
    });
    addMessageListener("removeLogin", loginVariableName => {
      let login = loginsMap[loginVariableName];
      assert.ok(!!login, "Login to delete is defined: " + loginVariableName);
      Services.logins.removeLogin(login);
    });
  });
  script.sendQuery("initLogins", aLogins);
  return script;
}


let origin = window.location.origin;
let setupScript = addLoginsInParent(
  // login0 has no username, so should be filtered out from the autocomplete list.
  ["login0", [origin, "https://autocomplete:8888", null,
              "", "user0pass", "", "pword"]],

  ["login1", [origin, "https://autocomplete:8888", null,
              "tempuser1", "temppass1", "uname", "pword"]],

  ["login2", [origin, "https://autocomplete:8888", null,
              "testuser2", "testpass2", "uname", "pword"]],

  ["login3", [origin, "https://autocomplete:8888", null,
              "testuser3", "testpass3", "uname", "pword"]],

  ["login4", [origin, "https://autocomplete:8888", null,
              "zzzuser4", "zzzpass4", "uname", "pword"]],

  ["login5", [origin, "https://autocomplete2", null,
              "singleuser5", "singlepass5", "uname", "pword"]],

  ["login6A", [origin, "https://autocomplete3", null,
               "form7user1", "form7pass1", "uname", "pword"]],
  ["login6B", [origin, "https://autocomplete3", null,
               "form7user2", "form7pass2", "uname", "pword"]],

  ["login7", [origin, "https://autocomplete4", null,
              "form8user", "form8pass", "uname", "pword"]],

  ["login8A", [origin, "https://autocomplete5", null,
               "form9userAB", "form9pass", "uname", "pword"]],
  ["login8B", [origin, "https://autocomplete5", null,
               "form9userAAB", "form9pass", "uname", "pword"]],
  ["login8C", [origin, "https://autocomplete5", null,
               "form9userAABzz", "form9pass", "uname", "pword"]],

  ["login10", [origin, "https://autocomplete7", null,
               "testuser10", "testpass10", "uname", "pword"]],

  ["login11", [origin, origin, null, "testuser11", "testpass11", "uname", "pword"]],

  ["login12", [origin, "https://autocomplete8", null,
               "testuser12", "testpass12", "uname", "pword"]]);
</script>
<p id="display"></p>

<!-- we presumably can't hide the content for this test. -->
<div id="content">

  <!-- form1 tests multiple matching logins -->
  <!-- other forms test single logins, with autocomplete=off set -->
  <form id="form1" action="https://autocomplete:8888/formtest.js" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

</div>

<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Login Manager: multiple login autocomplete. **/

var uname = $_(1, "uname");
var pword = $_(1, "pword");

// Restore the form to the default state.
function restoreForm() {
  uname.value = "";
  pword.value = "";
  uname.focus();
}

function sendFakeAutocompleteEvent(element) {
  var acEvent = document.createEvent("HTMLEvents");
  acEvent.initEvent("DOMAutoComplete", true, false);
  element.dispatchEvent(acEvent);
}

function spinEventLoop() {
  return Promise.resolve();
}

async function promiseACPopupClosed() {
  return SimpleTest.promiseWaitForCondition(async () => {
    let popupState = await getPopupState();
    return !popupState.open;
  }, "Wait for AC popup to be closed");
}

add_task(async function setup() {
  listenForUnexpectedPopupShown();
});

add_task(async function test_form1_initial_empty() {
  await SimpleTest.promiseFocus(window);

  // Make sure initial form is empty.
  checkLoginForm(uname, "", pword, "");
  let popupState = await getPopupState();
  is(popupState.open, false, "Check popup is initially closed");
});

add_task(async function test_form1_menuitems() {
  await SimpleTest.promiseFocus(window);
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  let results = await shownPromise;

  let popupState = await getPopupState();
  is(popupState.selectedIndex, -1, "Check no entries are selected upon opening");

  let expectedMenuItems = ["tempuser1",
                           "testuser2",
                           "testuser3",
                           "zzzuser4"];
  checkAutoCompleteResults(results, expectedMenuItems,
                           window.location.host, "Check all menuitems are displayed correctly.");

  let acEvents = await getTelemetryEvents({ process: "parent", filterProps: TelemetryFilterPropsAC, clear: true });
  is(acEvents.length, 1, "One autocomplete event");
  checkACTelemetryEvent(acEvents[0], uname, {
    "hadPrevious": "0",
    "login": expectedMenuItems.length + "",
    "loginsFooter": "1"
  });

  checkLoginForm(uname, "", pword, ""); // value shouldn't update just by opening
  synthesizeKey("KEY_Escape");
  await promiseACPopupClosed();
  checkLoginForm(uname, "", pword, "");
});

add_task(async function test_form1_first_entry() {
  await SimpleTest.promiseFocus(window);
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  let popupState = await getPopupState();
  is(popupState.selectedIndex, -1, "Check no entries are selected upon opening");

  synthesizeKey("KEY_ArrowDown"); // first
  checkLoginForm(uname, "", pword, ""); // value shouldn't update just by selecting
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "tempuser1", pword, "temppass1");
});

add_task(async function test_form1_second_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowDown"); // second
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser2", pword, "testpass2");
});

add_task(async function test_form1_third_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowDown"); // second
  synthesizeKey("KEY_ArrowDown"); // third
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser3", pword, "testpass3");
});

add_task(async function test_form1_fourth_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowDown"); // second
  synthesizeKey("KEY_ArrowDown"); // third
  synthesizeKey("KEY_ArrowDown"); // fourth
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "zzzuser4", pword, "zzzpass4");
});

add_task(async function test_form1_wraparound_first_entry() {
  // Trigger autocomplete popup
  restoreForm();
  await spinEventLoop(); // let focus happen
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowDown"); // second
  synthesizeKey("KEY_ArrowDown"); // third
  synthesizeKey("KEY_ArrowDown"); // fourth
  synthesizeKey("KEY_ArrowDown"); // footer
  synthesizeKey("KEY_ArrowDown"); // deselects
  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "tempuser1", pword, "temppass1");
});

add_task(async function test_form1_wraparound_up_last_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowUp"); // footer
  synthesizeKey("KEY_ArrowUp"); // last (fourth)
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "zzzuser4", pword, "zzzpass4");
});

add_task(async function test_form1_wraparound_down_up_up() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown"); // select first entry
  synthesizeKey("KEY_ArrowUp"); // selects nothing!
  synthesizeKey("KEY_ArrowUp"); // footer
  synthesizeKey("KEY_ArrowUp"); // select last entry
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "zzzuser4", pword, "zzzpass4");
});

add_task(async function test_form1_wraparound_up_last() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_ArrowUp"); // deselects
  synthesizeKey("KEY_ArrowUp"); // last entry
  synthesizeKey("KEY_ArrowUp");
  synthesizeKey("KEY_ArrowUp");
  synthesizeKey("KEY_ArrowUp");
  synthesizeKey("KEY_ArrowUp"); // first entry
  synthesizeKey("KEY_ArrowUp"); // deselects
  synthesizeKey("KEY_ArrowUp"); // footer
  synthesizeKey("KEY_ArrowUp"); // last entry
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "zzzuser4", pword, "zzzpass4");
});

add_task(async function test_form1_fill_username_without_autofill_right() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Set first entry w/o triggering autocomplete
  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowRight");
  await promiseACPopupClosed();
  checkLoginForm(uname, "tempuser1", pword, ""); // empty password
});

add_task(async function test_form1_fill_username_without_autofill_left() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Set first entry w/o triggering autocomplete
  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowLeft");
  await promiseACPopupClosed();
  checkLoginForm(uname, "tempuser1", pword, ""); // empty password
});

add_task(async function test_form1_pageup_first() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check first entry (page up)
  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_ArrowDown"); // second
  synthesizeKey("KEY_PageUp"); // first
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "tempuser1", pword, "temppass1");
});

add_task(async function test_form1_pagedown_last() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  /* test 13 */
  // Check last entry (page down)
  synthesizeKey("KEY_ArrowDown"); // first
  synthesizeKey("KEY_PageDown"); // footer
  synthesizeKey("KEY_ArrowUp"); // last
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "zzzuser4", pword, "zzzpass4");
});

add_task(async function test_form1_untrusted_event() {
  restoreForm();
  await spinEventLoop();

  // Send a fake (untrusted) event.
  checkLoginForm(uname, "", pword, "");
  uname.value = "zzzuser4";
  sendFakeAutocompleteEvent(uname);
  await spinEventLoop();
  checkLoginForm(uname, "zzzuser4", pword, "");
});

add_task(async function test_form1_delete() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // XXX tried sending character "t" before/during dropdown to test
  // filtering, but had no luck. Seemed like the character was getting lost.
  // Setting uname.value didn't seem to work either. This works with a human
  // driver, so I'm not sure what's up.

  // Delete the first entry (of 4), "tempuser1"
  synthesizeKey("KEY_ArrowDown");
  let numLogins = await LoginManager.countLogins(origin, "https://autocomplete:8888", null);
  is(numLogins, 5, "Correct number of logins before deleting one");

  let countChangedPromise = notifyMenuChanged(4);
  var deletionPromise = promiseStorageChanged(["removeLogin"]);
  // 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 deletionPromise;

  checkLoginForm(uname, "", pword, "");
  numLogins = await LoginManager.countLogins(origin, "https://autocomplete:8888", null);
  is(numLogins, 4, "Correct number of logins after deleting one");
  await countChangedPromise;
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser2", pword, "testpass2");
});

add_task(async function test_form1_first_after_deletion() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check the new first entry (of 3)
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser2", pword, "testpass2");
});

add_task(async function test_form1_delete_second() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Delete the second entry (of 3), "testuser3"
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Delete", {shiftKey: true});
  checkLoginForm(uname, "", pword, "");
  let numLogins = await LoginManager.countLogins(origin, "https://autocomplete:8888", null);
  is(numLogins, 3, "Correct number of logins after deleting one");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "zzzuser4", pword, "zzzpass4");
});

add_task(async function test_form1_first_after_deletion2() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check the new first entry (of 2)
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser2", pword, "testpass2");
});

add_task(async function test_form1_delete_last() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  /* test 54 */
  // Delete the last entry (of 2), "zzzuser4"
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Delete", {shiftKey: true});
  checkLoginForm(uname, "", pword, "");
  let numLogins = await LoginManager.countLogins(origin, "https://autocomplete:8888", null);
  is(numLogins, 2, "Correct number of logins after deleting one");
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser2", pword, "testpass2");
});

add_task(async function test_form1_first_after_3_deletions() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check the only remaining entry
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser2", pword, "testpass2");
});

add_task(async function test_form1_check_only_entry_remaining() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  /* test 56 */
  // Delete the only remaining entry, "testuser2"
  synthesizeKey("KEY_ArrowDown");
  let storageChanged = promiseStorageChanged(["removeLogin", "removeLogin", "addLogin"]);
  synthesizeKey("KEY_Delete", {shiftKey: true});
  checkLoginForm(uname, "", pword, "");
  let numLogins = await LoginManager.countLogins(origin, "https://autocomplete:8888", null);
  is(numLogins, 1, "Correct number of logins after deleting one");

  // remove the logins for the previous tests
  setupScript.sendAsyncMessage("removeLogin", "login0");
  setupScript.sendAsyncMessage("addLogin", "login5");
  await storageChanged;

  // ensure the popup is closed for the next test
  synthesizeKey("KEY_Escape");
  await promiseACPopupClosed();
});

/* Tests for single-user forms for ignoring autocomplete=off */
add_task(async function test_form2() {
  await setFormAndWaitForFieldFilled(`
    <form id="form2" action="https://autocomplete2" onsubmit="return false;">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword" autocomplete="off">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "singleuser5"});

  // Turn our attention to form2
  uname = $_(2, "uname");
  pword = $_(2, "pword");
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");

  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check first entry
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
});

add_task(async function test_form3() {
  await setFormAndWaitForFieldFilled(`
    <form id="form3" action="https://autocomplete2" onsubmit="return false;">
      <input  type="text"       name="uname" autocomplete="off">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "singleuser5"});

  uname = $_(3, "uname");
  pword = $_(3, "pword");
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check first entry
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
});

add_task(async function test_form4() {
  await setFormAndWaitForFieldFilled(`
    <form id="form4" action="https://autocomplete2" onsubmit="return false;" autocomplete="off">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "singleuser5"});

  uname = $_(4, "uname");
  pword = $_(4, "pword");
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check first entry
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
});

add_task(async function test_form5() {
  await setFormAndWaitForFieldFilled(`
    <form id="form5" action="https://autocomplete2" onsubmit="return false;">
      <input  type="text"       name="uname" autocomplete="off">
      <input  type="password"   name="pword" autocomplete="off">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "singleuser5"});

  uname = $_(5, "uname");
  pword = $_(5, "pword");
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check first entry
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
});

add_task(async function test_form6() {
  await setFormAndWaitForFieldFilled(`
    <!-- control -->
    <form id="form6" action="https://autocomplete2" onsubmit="return false;">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "singleuser5"});

  // (this is a control, w/o autocomplete=off, to ensure the login
  // that was being suppressed would have been filled in otherwise)
  uname = $_(6, "uname");
  pword = $_(6, "pword");
  checkLoginForm(uname, "singleuser5", pword, "singlepass5");
});

add_task(async function test_form6_changeUsername() {
  // Test that the password field remains filled in after changing
  // the username.
  uname.focus();
  synthesizeKey("KEY_ArrowRight");
  synthesizeKey("X", {shiftKey: true});
  // Trigger the 'blur' event on uname
  pword.focus();
  await spinEventLoop();
  checkLoginForm(uname, "singleuser5X", pword, "singlepass5");
  uname.focus();

  let storageChanged = promiseStorageChanged(["removeLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login5");
  await storageChanged;
});

add_task(async function test_form7() {
  let storageChanged = promiseStorageChanged(["addLogin", "addLogin"]);
  setupScript.sendAsyncMessage("addLogin", "login6A");
  setupScript.sendAsyncMessage("addLogin", "login6B");
  await storageChanged;
  await setFormAndWaitForFieldFilled(`
    <!-- This form will be manipulated to insert a different username field. -->
    <form id="form7" action="https://autocomplete3" onsubmit="return false;">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: ""});

  uname = $_(7, "uname");
  pword = $_(7, "pword");
  checkLoginForm(uname, "", pword, "");

  // Insert a new username field into the form. We'll then make sure
  // that invoking the autocomplete doesn't try to fill the form.
  var newField = document.createElement("input");
  newField.setAttribute("type", "text");
  newField.setAttribute("name", "uname2");
  pword.parentNode.insertBefore(newField, pword);
  is($_(7, "uname2").value, "", "Verifying empty uname2");

  // Delete login6B. It was created just to prevent filling in a login
  // automatically, removing it makes it more likely that we'll catch a
  // future regression with form filling here.
  storageChanged = promiseStorageChanged(["removeLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login6B");
  await storageChanged;
});

add_task(async function test_form7_2() {
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  let results = await shownPromise;
  checkAutoCompleteResults(results,
                           ["form7user1"],
                           window.location.host,
                           "Check dropdown is showing all logins while field is blank");


  // Check first entry
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  // The form changes, so we expect the old username field to get the
  // selected autocomplete value, but neither the new username field nor
  // the password field should have any values filled in.
  await SimpleTest.promiseWaitForCondition(() => uname.value == "form7user1",
                                           "Wait for username to get filled");
  await promiseACPopupClosed();
  checkLoginForm(uname, "form7user1", pword, "");
  is($_(7, "uname2").value, "", "Verifying empty uname2");
  restoreForm(); // clear field, so reloading test doesn't fail

  let storageChanged = promiseStorageChanged(["removeLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login6A");
  await storageChanged;
});

add_task(async function test_form8() {
  let storageChanged = promiseStorageChanged(["addLogin"]);
  setupScript.sendAsyncMessage("addLogin", "login7");
  await storageChanged;
  await setFormAndWaitForFieldFilled(`
    <!-- This form will be manipulated to insert a different username field. -->
    <form id="form7" action="https://autocomplete3" onsubmit="return false;">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>
    <!-- test for no autofill after onblur with blank username -->
    <form id="form8" action="https://autocomplete4" onsubmit="return false;">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "form8user", formId: "form8"});

  uname = $_(8, "uname");
  pword = $_(8, "pword");
  checkLoginForm(uname, "form8user", pword, "form8pass");
  restoreForm();
});

add_task(async function test_form8_blur() {
  checkLoginForm(uname, "", pword, "");
  // Focus the previous form to trigger a blur.
  $_(7, "uname").focus();
});

add_task(async function test_form8_2() {
  checkLoginForm(uname, "", pword, "");
  restoreForm();
});

add_task(async function test_form8_3() {
  checkLoginForm(uname, "", pword, "");
  let storageChanged = promiseStorageChanged(["removeLogin", "addLogin", "addLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login7");
  setupScript.sendAsyncMessage("addLogin", "login8A");
  setupScript.sendAsyncMessage("addLogin", "login8B");
  await storageChanged;
});

add_task(async function test_form9_filtering() {
  await setFormAndWaitForFieldFilled(`
    <!-- test autocomplete dropdown -->
    <form id="form9" action="https://autocomplete5" onsubmit="return false;">
      <input  type="text"       name="uname">
      <input  type="password"   name="pword">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: ""});

  // Turn our attention to form9 to test the dropdown - bug 497541
  let shownPromise = promiseACShown();
  uname = $_(9, "uname");
  pword = $_(9, "pword");
  uname.focus();
  let results = await shownPromise;
  checkAutoCompleteResults(results,
                           ["form9userAAB", "form9userAB"],
                           window.location.host,
                           "Check dropdown is showing all logins while field is blank");
  synthesizeKey("KEY_Escape"); // Need to close the popup so we can get another popupshown after sending the string below.

  shownPromise = promiseACShown();
  sendString("form9userAB");
  results = await shownPromise;
  checkAutoCompleteResults(results,
                           ["form9userAB"],
                           window.location.host,
                           "Check dropdown is showing login with only one 'A'");

  checkLoginForm(uname, "form9userAB", pword, "");
  uname.focus();
  synthesizeKey("KEY_ArrowLeft");
  shownPromise = promiseACShown();
  synthesizeKey("A", {shiftKey: true});
  results = await shownPromise;

  checkLoginForm(uname, "form9userAAB", pword, "");
  checkAutoCompleteResults(results, ["form9userAAB"],
                           window.location.host, "Check dropdown is updated after inserting 'A'");
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "form9userAAB", pword, "form9pass");
});

add_task(async function test_form9_autocomplete_cache() {
  // Note that this addLogin call will only be seen by the autocomplete
  // attempt for the synthesizeKey if we do not successfully cache the
  // autocomplete results.
  let storageChanged = promiseStorageChanged(["addLogin"]);
  setupScript.sendAsyncMessage("addLogin", "login8C");
  await storageChanged;
  uname.focus();
  let promise1 = notifyMenuChanged(1);
  sendString("z");
  let results = await promise1;
  checkAutoCompleteResults(results, [], window.location.host,
                           "Check popup does not have any login items");

  // check that empty results are cached - bug 496466
  promise1 = notifyMenuChanged(1);
  sendString("z");
  results = await promise1;
  checkAutoCompleteResults(results, [], window.location.host,
                           "Check popup only has the footer when it opens");
});

add_task(async function test_form11_formless() {
  let storageChanged = promiseStorageChanged(["removeLogin", "removeLogin", "removeLogin", "addLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login8A");
  setupScript.sendAsyncMessage("removeLogin", "login8B");
  setupScript.sendAsyncMessage("removeLogin", "login8C");
  setupScript.sendAsyncMessage("addLogin", "login11");
  await storageChanged;
  await setFormAndWaitForFieldFilled(`
    <!-- tests <form>-less autocomplete -->
    <div id="form11">
      <input  type="text"       name="uname" id="uname">
      <input  type="password"   name="pword" id="pword">
      <button type="submit">Submit</button>
    </div>`, {fieldSelector: `input[name="uname"]`, fieldValue: "testuser11"});

  // Test form-less autocomplete
  uname = $_(11, "uname");
  pword = $_(11, "pword");
  restoreForm();
  checkLoginForm(uname, "", pword, "");
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Trigger autocomplete
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  let processedPromise = promiseFormsProcessedInSameProcess();
  synthesizeKey("KEY_Enter");
  await processedPromise;
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser11", pword, "testpass11");
});

add_task(async function test_form11_open_on_trusted_focus() {
  uname = $_(11, "uname");
  pword = $_(11, "pword");
  uname.value = "";
  pword.value = "";

  // Move focus to the password field so we can test the first click on the
  // username field.
  pword.focus();
  checkLoginForm(uname, "", pword, "");
  const firePrivEventPromise = new Promise((resolve) => {
    uname.addEventListener("click", (e) => {
      ok(e.isTrusted, "Ensure event is trusted");
      resolve();
    });
  });
  const shownPromise = promiseACShown();
  synthesizeMouseAtCenter(uname, {});
  await firePrivEventPromise;
  await shownPromise;
  synthesizeKey("KEY_ArrowDown");
  const processedPromise = promiseFormsProcessedInSameProcess();
  synthesizeKey("KEY_Enter");
  await processedPromise;
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser11", pword, "testpass11");
  let storageChanged = promiseStorageChanged(["removeLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login11");
  await storageChanged;
});

add_task(async function test_form12_recipes() {
  let storageChanged = promiseStorageChanged(["addLogin"]);
  setupScript.sendAsyncMessage("addLogin", "login10");
  await storageChanged;
  await setFormAndWaitForFieldFilled(`
    <!-- test for onUsernameInput recipe testing -->
    <form id="form12" action="https://autocomplete7" onsubmit="return false;">
      <input  type="text"   name="1">
      <input  type="text"   name="2">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="1"]`, fieldValue: ""});

  await loadRecipes({
    siteRecipes: [{
      "hosts": [window.location.host],
      "usernameSelector": "input[name='1']",
      "passwordSelector": "input[name='2']",
    }],
  });
  uname = $_(12, "1");
  pword = $_(12, "2");

  // First test DOMAutocomplete
  // Switch the password field to type=password so _fillForm marks the username
  // field for autocomplete.
  pword.type = "password";
  await promiseFormsProcessedInSameProcess();
  restoreForm();
  checkLoginForm(uname, "", pword, "");
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser10", pword, "testpass10");

  // Now test recipes with blur on the username field.
  restoreForm();
  checkLoginForm(uname, "", pword, "");
  uname.value = "testuser10";
  checkLoginForm(uname, "testuser10", pword, "");
  synthesizeKey("KEY_Tab");
  await promiseFormsProcessedInSameProcess();
  checkLoginForm(uname, "testuser10", pword, "testpass10");
  await resetRecipes();
});

add_task(async function test_form13_stays_open_upon_empty_search() {
  await setFormAndWaitForFieldFilled(`
    <!-- test not closing when the search string (.value) becomes empty -->
    <form id="form13" action="http://autocomplete:8888/formtest.js" onsubmit="return false;">
      <input  type="text"       name="uname" value="prefilled">
      <input  type="password"   name="pword" value="prefilled">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "prefilled"});

  uname = $_(13, "uname");
  pword = $_(13, "pword");
  checkLoginForm(uname, "prefilled", pword, "prefilled");

  uname.scrollIntoView();
  let shownPromise = promiseACShown();
  synthesizeMouseAtCenter(uname, {});
  await shownPromise;
  uname.select();
  synthesizeKey("KEY_Delete");

  await spinEventLoop();
  let popupState = await getPopupState();
  is(popupState.open, true, "Check popup is still open");
  checkLoginForm(uname, "", pword, "prefilled");

  info("testing password field");
  synthesizeMouseAtCenter(pword, {});
  pword.select();
  popupState = await getPopupState();
  is(popupState.open, false, "Check popup closed since password field isn't empty");
  shownPromise = promiseACShown();
  synthesizeKey("KEY_Delete");
  await shownPromise;
  checkLoginForm(uname, "", pword, "");

  // ensure the popup is closed for the next test
  synthesizeKey("KEY_Escape");
  await promiseACPopupClosed();
});

add_task(async function test_form14_username_only() {
  await SimpleTest.promiseFocus(window);
  let storageChanged = promiseStorageChanged(["removeLogin", "addLogin"]);
  setupScript.sendAsyncMessage("removeLogin", "login10");
  setupScript.sendAsyncMessage("addLogin", "login12");
  await storageChanged;

  await setFormAndWaitForFieldFilled(`
    <form id="form14" action="https://autocomplete8" onsubmit="return false;" autocomplete="off">
      <input  type="email"       name="uname" autocomplete="username" value="prefilled">
      <button type="submit">Submit</button>
    </form>`, {fieldSelector: `input[name="uname"]`, fieldValue: "prefilled"});

  uname = $_(14, "uname");
  pword = {value: ""};
  checkLoginForm(uname, "prefilled", pword, "");
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  await shownPromise;

  // Check first entry
  synthesizeKey("KEY_ArrowDown");
  checkLoginForm(uname, "", pword, ""); // value shouldn't update
  synthesizeKey("KEY_Enter");
  await promiseACPopupClosed();
  checkLoginForm(uname, "testuser12", pword, "");
});
</script>
</pre>
</body>
</html>