summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_basic_form_autocomplete.html
blob: 0eee8e696b1c7c7918953660300c76f2ee6f9914 (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
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test basic login autocomplete</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="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();

var setupScript = runInParent(function setup() {
  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  Cu.import("resource://gre/modules/Services.jsm");

  // Create some logins just for this form, since we'll be deleting them.
  var nsLoginInfo = Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
                                           Ci.nsILoginInfo, "init");
  assert.ok(nsLoginInfo != null, "nsLoginInfo constructor");

  // login0 has no username, so should be filtered out from the autocomplete list.
  var login0 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete:8888", null,
                               "", "user0pass", "", "pword");

  var login1 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete:8888", null,
                               "tempuser1", "temppass1", "uname", "pword");

  var login2 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete:8888", null,
                               "testuser2", "testpass2", "uname", "pword");

  var login3 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete:8888", null,
                               "testuser3", "testpass3", "uname", "pword");

  var login4 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete:8888", null,
                               "zzzuser4", "zzzpass4", "uname", "pword");

  // login 5 only used in the single-user forms
  var login5 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete2", null,
                               "singleuser5", "singlepass5", "uname", "pword");

  var login6A = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete3", null,
                                "form7user1", "form7pass1", "uname", "pword");
  var login6B = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete3", null,
                                "form7user2", "form7pass2", "uname", "pword");

  var login7  = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete4", null,
                                "form8user", "form8pass", "uname", "pword");

  var login8A = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete5", null,
                                "form9userAB", "form9pass", "uname", "pword");
  var login8B = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete5", null,
                                "form9userAAB", "form9pass", "uname", "pword");
  var login8C = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete5", null,
                                "form9userAABzz", "form9pass", "uname", "pword");

  var login10 = new nsLoginInfo("http://mochi.test:8888", "http://autocomplete7", null,
                                "testuser10", "testpass10", "uname", "pword");


  // try/catch in case someone runs the tests manually, twice.
  try {
    Services.logins.addLogin(login0);
    Services.logins.addLogin(login1);
    Services.logins.addLogin(login2);
    Services.logins.addLogin(login3);
    Services.logins.addLogin(login4);
    Services.logins.addLogin(login5);
    Services.logins.addLogin(login6A);
    Services.logins.addLogin(login6B);
    Services.logins.addLogin(login7);
    Services.logins.addLogin(login8A);
    Services.logins.addLogin(login8B);
    // login8C is added later
    Services.logins.addLogin(login10);
  } catch (e) {
    assert.ok(false, "addLogin threw: " + e);
  }

  addMessageListener("addLogin", loginVariableName => {
    let login = eval(loginVariableName);
    assert.ok(!!login, "Login to add is defined: " + loginVariableName);
    Services.logins.addLogin(login);
  });
  addMessageListener("removeLogin", loginVariableName => {
    let login = eval(loginVariableName);
    assert.ok(!!login, "Login to delete is defined: " + loginVariableName);
    Services.logins.removeLogin(login);
  });
});
</script>
<p id="display"></p>

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

  <!-- form1 tests multiple matching logins -->
  <form id="form1" action="http://autocomplete:8888/formtest.js" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

  <!-- other forms test single logins, with autocomplete=off set -->
  <form id="form2" action="http://autocomplete2" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword" autocomplete="off">
    <button type="submit">Submit</button>
  </form>

  <form id="form3" action="http://autocomplete2" onsubmit="return false;">
    <input  type="text"       name="uname" autocomplete="off">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

  <form id="form4" action="http://autocomplete2" onsubmit="return false;" autocomplete="off">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

  <form id="form5" action="http://autocomplete2" onsubmit="return false;">
    <input  type="text"       name="uname" autocomplete="off">
    <input  type="password"   name="pword" autocomplete="off">
    <button type="submit">Submit</button>
  </form>

  <!-- control -->
  <form id="form6" action="http://autocomplete2" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

  <!-- This form will be manipulated to insert a different username field. -->
  <form id="form7" action="http://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="http://autocomplete4" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

  <!-- test autocomplete dropdown -->
  <form id="form9" action="http://autocomplete5" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>

  <!-- test for onUsernameInput recipe testing -->
  <form id="form11" action="http://autocomplete7" onsubmit="return false;">
    <input  type="text"   name="1">
    <input  type="text"   name="2">
    <button type="submit">Submit</button>
  </form>

  <!-- tests <form>-less autocomplete -->
  <div id="form12">
     <input  type="text"       name="uname" id="uname">
     <input  type="password"   name="pword" id="pword">
     <button type="submit">Submit</button>
   </div>
</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");
const shiftModifier = SpecialPowers.Ci.nsIDOMEvent.SHIFT_MASK;

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

// Check for expected username/password in form.
function checkACForm(expectedUsername, expectedPassword) {
  var formID = uname.parentNode.id;
  is(uname.value, expectedUsername, "Checking " + formID + " username is: " + expectedUsername);
  is(pword.value, expectedPassword, "Checking " + formID + " password is: " + expectedPassword);
}

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

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

add_task(function* setup() {
  yield SpecialPowers.pushPrefEnv({"set": [["security.insecure_field_warning.contextual.enabled", false],
                                           ["signon.autofillForms.http", true]]});
  listenForUnexpectedPopupShown();
});

add_task(function* test_form1_initial_empty() {
  yield SimpleTest.promiseFocus(window);

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

add_task(function* test_form1_menuitems() {
  yield SimpleTest.promiseFocus(window);
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  let results = yield shownPromise;

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

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

  checkACForm("", ""); // value shouldn't update just by selecting
  doKey("return"); // not "enter"!
  yield spinEventLoop(); // let focus happen
  checkACForm("", "");
});

add_task(function* test_form1_first_entry() {
  yield SimpleTest.promiseFocus(window);
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

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

  doKey("down"); // first
  checkACForm("", ""); // value shouldn't update just by selecting
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("tempuser1", "temppass1");
});

add_task(function* test_form1_second_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down"); // first
  doKey("down"); // second
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("testuser2", "testpass2");
});

add_task(function* test_form1_third_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down"); // first
  doKey("down"); // second
  doKey("down"); // third
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("testuser3", "testpass3");
});

add_task(function* test_form1_fourth_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down"); // first
  doKey("down"); // second
  doKey("down"); // third
  doKey("down"); // fourth
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("zzzuser4", "zzzpass4");
});

add_task(function* test_form1_wraparound_first_entry() {
  // Trigger autocomplete popup
  restoreForm();
  yield spinEventLoop(); // let focus happen
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down"); // first
  doKey("down"); // second
  doKey("down"); // third
  doKey("down"); // fourth
  doKey("down"); // deselects
  doKey("down"); // first
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("tempuser1", "temppass1");
});

add_task(function* test_form1_wraparound_up_last_entry() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("up"); // last (fourth)
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("zzzuser4", "zzzpass4");
});

add_task(function* test_form1_wraparound_down_up_up() {
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down"); // select first entry
  doKey("up");   // selects nothing!
  doKey("up");   // select last entry
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("zzzuser4", "zzzpass4");
});

add_task(function* test_form1_wraparound_up_last() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down");
  doKey("up"); // deselects
  doKey("up"); // last entry
  doKey("up");
  doKey("up");
  doKey("up"); // first entry
  doKey("up"); // deselects
  doKey("up"); // last entry
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("zzzuser4", "zzzpass4");
});

add_task(function* test_form1_fill_username_without_autofill_right() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Set first entry w/o triggering autocomplete
  doKey("down"); // first
  doKey("right");
  yield spinEventLoop();
  checkACForm("tempuser1", ""); // empty password
});

add_task(function* test_form1_fill_username_without_autofill_left() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Set first entry w/o triggering autocomplete
  doKey("down"); // first
  doKey("left");
  checkACForm("tempuser1", ""); // empty password
});

add_task(function* test_form1_pageup_first() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check first entry (page up)
  doKey("down"); // first
  doKey("down"); // second
  doKey("page_up"); // first
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("tempuser1", "temppass1");
});

add_task(function* test_form1_pagedown_last() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  /* test 13 */
  // Check last entry (page down)
  doKey("down"); // first
  doKey("page_down"); // last
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("zzzuser4", "zzzpass4");
});

add_task(function* test_form1_untrusted_event() {
  restoreForm();
  yield spinEventLoop();

  // Send a fake (untrusted) event.
  checkACForm("", "");
  uname.value = "zzzuser4";
  sendFakeAutocompleteEvent(uname);
  yield spinEventLoop();
  checkACForm("zzzuser4", "");
});

add_task(function* test_form1_delete() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield 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"
  doKey("down");
  var numLogins;
  numLogins = LoginManager.countLogins("http://mochi.test:8888", "http://autocomplete:8888", null);
  is(numLogins, 5, "Correct number of logins before deleting one");

  let countChangedPromise = notifyMenuChanged(3);
  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.
  doKey("delete", shiftModifier);
  yield deletionPromise;

  checkACForm("", "");
  numLogins = LoginManager.countLogins("http://mochi.test:8888", "http://autocomplete:8888", null);
  is(numLogins, 4, "Correct number of logins after deleting one");
  yield countChangedPromise;
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("testuser2", "testpass2");
});

add_task(function* test_form1_first_after_deletion() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check the new first entry (of 3)
  doKey("down");
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("testuser2", "testpass2");
});

add_task(function* test_form1_delete_second() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Delete the second entry (of 3), "testuser3"
  doKey("down");
  doKey("down");
  doKey("delete", shiftModifier);
  checkACForm("", "");
  numLogins = LoginManager.countLogins("http://mochi.test:8888", "http://autocomplete:8888", null);
  is(numLogins, 3, "Correct number of logins after deleting one");
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("zzzuser4", "zzzpass4");
});

add_task(function* test_form1_first_after_deletion2() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check the new first entry (of 2)
  doKey("down");
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("testuser2", "testpass2");
});

add_task(function* test_form1_delete_last() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  /* test 54 */
  // Delete the last entry (of 2), "zzzuser4"
  doKey("down");
  doKey("down");
  doKey("delete", shiftModifier);
  checkACForm("", "");
  numLogins = LoginManager.countLogins("http://mochi.test:8888", "http://autocomplete:8888", null);
  is(numLogins, 2, "Correct number of logins after deleting one");
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("testuser2", "testpass2");
});

add_task(function* test_form1_first_after_3_deletions() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check the only remaining entry
  doKey("down");
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("testuser2", "testpass2");
});

add_task(function* test_form1_check_only_entry_remaining() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  /* test 56 */
  // Delete the only remaining entry, "testuser2"
  doKey("down");
  doKey("delete", shiftModifier);
  checkACForm("", "");
  numLogins = LoginManager.countLogins("http://mochi.test:8888", "http://autocomplete:8888", null);
  is(numLogins, 1, "Correct number of logins after deleting one");

  // remove the login that's not shown in the list.
  setupScript.sendSyncMessage("removeLogin", "login0");
});

/* Tests for single-user forms for ignoring autocomplete=off */
add_task(function* test_form2() {
  // Turn our attention to form2
  uname = $_(2, "uname");
  pword = $_(2, "pword");
  checkACForm("singleuser5", "singlepass5");

  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check first entry
  doKey("down");
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("singleuser5", "singlepass5");
});

add_task(function* test_form3() {
  uname = $_(3, "uname");
  pword = $_(3, "pword");
  checkACForm("singleuser5", "singlepass5");
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check first entry
  doKey("down");
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("singleuser5", "singlepass5");
});

add_task(function* test_form4() {
  uname = $_(4, "uname");
  pword = $_(4, "pword");
  checkACForm("singleuser5", "singlepass5");
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check first entry
  doKey("down");
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("singleuser5", "singlepass5");
});

add_task(function* test_form5() {
  uname = $_(5, "uname");
  pword = $_(5, "pword");
  checkACForm("singleuser5", "singlepass5");
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check first entry
  doKey("down");
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("singleuser5", "singlepass5");
});

add_task(function* test_form6() {
  // (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");
  checkACForm("singleuser5", "singlepass5");
});

add_task(function* test_form6_changeUsername() {
  // Test that the password field remains filled in after changing
  // the username.
  uname.focus();
  doKey("right");
  sendChar("X");
  // Trigger the 'blur' event on uname
  pword.focus();
  yield spinEventLoop();
  checkACForm("singleuser5X", "singlepass5");

  setupScript.sendSyncMessage("removeLogin", "login5");
});

add_task(function* test_form7() {
  uname = $_(7, "uname");
  pword = $_(7, "pword");
  checkACForm("", "");

  // 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.
  setupScript.sendSyncMessage("removeLogin", "login6B");
});

add_task(function* test_form7_2() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Check first entry
  doKey("down");
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "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.
  yield spinEventLoop();
  checkACForm("form7user1", "");
  is($_(7, "uname2").value, "", "Verifying empty uname2");
  restoreForm(); // clear field, so reloading test doesn't fail

  setupScript.sendSyncMessage("removeLogin", "login6A");
});

add_task(function* test_form8() {
  uname = $_(8, "uname");
  pword = $_(8, "pword");
  checkACForm("form8user", "form8pass");
  restoreForm();
});

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

add_task(function* test_form8_2() {
  checkACForm("", "");
  restoreForm();
});

add_task(function* test_form8_3() {
  checkACForm("", "");
  setupScript.sendSyncMessage("removeLogin", "login7");
});

add_task(function* test_form9_filtering() {
  // Turn our attention to form9 to test the dropdown - bug 497541
  uname = $_(9, "uname");
  pword = $_(9, "pword");
  uname.focus();
  let shownPromise = promiseACShown();
  sendString("form9userAB");
  yield shownPromise;

  checkACForm("form9userAB", "");
  uname.focus();
  doKey("left");
  shownPromise = promiseACShown();
  sendChar("A");
  let results = yield shownPromise;

  checkACForm("form9userAAB", "");
  checkArrayValues(results, ["form9userAAB"], "Check dropdown is updated after inserting 'A'");
  doKey("down");
  doKey("return");
  yield promiseFormsProcessed();
  checkACForm("form9userAAB", "form9pass");
});

add_task(function* test_form9_autocomplete_cache() {
  // Note that this addLogin call will only be seen by the autocomplete
  // attempt for the sendChar if we do not successfully cache the
  // autocomplete results.
  setupScript.sendSyncMessage("addLogin", "login8C");
  uname.focus();
  let promise0 = notifyMenuChanged(0);
  sendChar("z");
  yield promise0;
  let popupState = yield getPopupState();
  is(popupState.open, false, "Check popup shouldn't open");

  // check that empty results are cached - bug 496466
  promise0 = notifyMenuChanged(0);
  sendChar("z");
  yield promise0;
  popupState = yield getPopupState();
  is(popupState.open, false, "Check popup stays closed due to cached empty result");
});

add_task(function* test_form11_recipes() {
  yield loadRecipes({
    siteRecipes: [{
      "hosts": ["mochi.test:8888"],
      "usernameSelector": "input[name='1']",
      "passwordSelector": "input[name='2']"
    }],
  });
  uname = $_(11, "1");
  pword = $_(11, "2");

  // First test DOMAutocomplete
  // Switch the password field to type=password so _fillForm marks the username
  // field for autocomplete.
  pword.type = "password";
  yield promiseFormsProcessed();
  restoreForm();
  checkACForm("", "");
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  doKey("down");
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("testuser10", "testpass10");

  // Now test recipes with blur on the username field.
  restoreForm();
  checkACForm("", "");
  uname.value = "testuser10";
  checkACForm("testuser10", "");
  doKey("tab");
  yield promiseFormsProcessed();
  checkACForm("testuser10", "testpass10");
  yield resetRecipes();
});

add_task(function* test_form12_formless() {
  // Test form-less autocomplete
  uname = $_(12, "uname");
  pword = $_(12, "pword");
  restoreForm();
  checkACForm("", "");
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;

  // Trigger autocomplete
  doKey("down");
  checkACForm("", ""); // value shouldn't update
  let processedPromise = promiseFormsProcessed();
  doKey("return"); // not "enter"!
  yield processedPromise;
  checkACForm("testuser", "testpass");
});

add_task(function* test_form12_open_on_trusted_focus() {
  uname = $_(12, "uname");
  pword = $_(12, "pword");
  uname.value = "";
  pword.value = "";

  // Move focus to the password field so we can test the first click on the
  // username field.
  pword.focus();
  checkACForm("", "");
  const firePrivEventPromise = new Promise((resolve) => {
    uname.addEventListener("click", (e) => {
      ok(e.isTrusted, "Ensure event is trusted");
      resolve();
    });
  });
  const shownPromise = promiseACShown();
  synthesizeMouseAtCenter(uname, {});
  yield firePrivEventPromise;
  yield shownPromise;
  doKey("down");
  const processedPromise = promiseFormsProcessed();
  doKey("return"); // not "enter"!
  yield processedPromise;
  checkACForm("testuser", "testpass");
});
</script>
</pre>
</body>
</html>