-
Notifications
You must be signed in to change notification settings - Fork 54
/
autofill_tasks.js
879 lines (811 loc) · 23.6 KB
/
autofill_tasks.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* import-globals-from ../head_common.js */
/* import-globals-from ./head_autocomplete.js */
function addAutofillTasks(origins) {
// Helpful reminder of the `autofilled` and `completed` properties in the
// object passed to check_autocomplete:
//
// autofilled: expected input.value after autofill
// completed: expected input.value after autofill and enter is pressed
//
// `completed` is the URL that the controller sets to input.value, and the URL
// that will ultimately be loaded when you press enter. When you press enter,
// the code path is:
//
// (1) urlbar.handleEnter
// (2) nsAutoCompleteController::HandleEnter
// (3) nsAutoCompleteController::EnterMatch (sets input.value)
// (4) input.onTextEntered
// (5) urlbar.handleCommand (loads input.value)
let path;
let search;
let searchCase;
let comment;
if (origins) {
path = "/";
search = "ex";
searchCase = "EX";
comment = "example.com";
} else {
path = "/foo";
search = "example.com/f";
searchCase = "EXAMPLE.COM/f";
comment = "example.com/foo";
}
let host = "example.com";
let url = host + path;
add_task(async function init() {
await cleanup();
});
// "ex" should match http://example.com/.
add_task(async function basic() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [{
value: url,
comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "EX" should match http://example.com/.
add_task(async function basicCase() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: searchCase,
autofilled: searchCase + url.substr(searchCase.length),
completed: "http://" + url,
matches: [{
value: url,
comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "ex" should match http://www.example.com/.
add_task(async function noWWWShouldMatchWWW() {
await PlacesTestUtils.addVisits([{
uri: "http://www." + url,
}]);
await check_autocomplete({
search,
autofilled: url,
completed: "http://www." + url,
matches: [{
value: url,
comment: "www." + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "EX" should match http://www.example.com/.
add_task(async function noWWWShouldMatchWWWCase() {
await PlacesTestUtils.addVisits([{
uri: "http://www." + url,
}]);
await check_autocomplete({
search: searchCase,
autofilled: searchCase + url.substr(searchCase.length),
completed: "http://www." + url,
matches: [{
value: url,
comment: "www." + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "www.ex" should *not* match http://example.com/.
add_task(async function wwwShouldNotMatchNoWWW() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: "www." + search,
matches: [],
});
await cleanup();
});
// "http://ex" should match http://example.com/.
add_task(async function prefix() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: "http://" + search,
autofilled: "http://" + url,
completed: "http://" + url,
matches: [{
value: "http://" + url,
comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "HTTP://EX" should match http://example.com/.
add_task(async function prefixCase() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: "HTTP://" + searchCase,
autofilled: "HTTP://" + searchCase + url.substr(searchCase.length),
completed: "http://" + url,
matches: [{
value: "http://" + url,
comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "http://ex" should match http://www.example.com/.
add_task(async function prefixNoWWWShouldMatchWWW() {
await PlacesTestUtils.addVisits([{
uri: "http://www." + url,
}]);
await check_autocomplete({
search: "http://" + search,
autofilled: "http://" + url,
completed: "http://www." + url,
matches: [{
value: "http://" + url,
comment: "www." + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "HTTP://EX" should match http://www.example.com/.
add_task(async function prefixNoWWWShouldMatchWWWCase() {
await PlacesTestUtils.addVisits([{
uri: "http://www." + url,
}]);
await check_autocomplete({
search: "HTTP://" + searchCase,
autofilled: "HTTP://" + searchCase + url.substr(searchCase.length),
completed: "http://www." + url,
matches: [{
value: "http://" + url,
comment: "www." + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "http://www.ex" should *not* match http://example.com/.
add_task(async function prefixWWWShouldNotMatchNoWWW() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: "http://www." + search,
matches: [],
});
await cleanup();
});
// "http://ex" should *not* match https://example.com/.
add_task(async function httpPrefixShouldNotMatchHTTPS() {
await PlacesTestUtils.addVisits([{
uri: "https://" + url,
}]);
await check_autocomplete({
search: "http://" + search,
matches: [{
value: "https://" + url,
comment: "test visit for https://" + url,
style: ["favicon"],
}],
});
await cleanup();
});
// "ex" should match https://example.com/.
add_task(async function httpsBasic() {
await PlacesTestUtils.addVisits([{
uri: "https://" + url,
}]);
await check_autocomplete({
search,
autofilled: url,
completed: "https://" + url,
matches: [{
value: url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "ex" should match https://www.example.com/.
add_task(async function httpsNoWWWShouldMatchWWW() {
await PlacesTestUtils.addVisits([{
uri: "https://www." + url,
}]);
await check_autocomplete({
search,
autofilled: url,
completed: "https://www." + url,
matches: [{
value: url,
comment: "https://www." + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "www.ex" should *not* match https://example.com/.
add_task(async function httpsWWWShouldNotMatchNoWWW() {
await PlacesTestUtils.addVisits([{
uri: "https://" + url,
}]);
await check_autocomplete({
search: "www." + search,
matches: [],
});
await cleanup();
});
// "https://ex" should match https://example.com/.
add_task(async function httpsPrefix() {
await PlacesTestUtils.addVisits([{
uri: "https://" + url,
}]);
await check_autocomplete({
search: "https://" + search,
autofilled: "https://" + url,
completed: "https://" + url,
matches: [{
value: "https://" + url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "https://ex" should match https://www.example.com/.
add_task(async function httpsPrefixNoWWWShouldMatchWWW() {
await PlacesTestUtils.addVisits([{
uri: "https://www." + url,
}]);
await check_autocomplete({
search: "https://" + search,
autofilled: "https://" + url,
completed: "https://www." + url,
matches: [{
value: "https://" + url,
comment: "https://www." + comment,
style: ["autofill", "heuristic"],
}],
});
await cleanup();
});
// "https://www.ex" should *not* match https://example.com/.
add_task(async function httpsPrefixWWWShouldNotMatchNoWWW() {
await PlacesTestUtils.addVisits([{
uri: "https://" + url,
}]);
await check_autocomplete({
search: "https://www." + search,
matches: [],
});
await cleanup();
});
// "https://ex" should *not* match http://example.com/.
add_task(async function httpsPrefixShouldNotMatchHTTP() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: "https://" + search,
matches: [{
value: "http://" + url,
comment: "test visit for http://" + url,
style: ["favicon"],
}],
});
await cleanup();
});
// "https://ex" should *not* match http://example.com/, even if the latter is
// more frecent and both could be autofilled.
add_task(async function httpsPrefixShouldNotMatchMoreFrecentHTTP() {
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
transition: PlacesUtils.history.TRANSITIONS.TYPED,
}, {
uri: "http://" + url,
}, {
uri: "https://" + url,
transition: PlacesUtils.history.TRANSITIONS.TYPED,
}, {
uri: "http://otherpage",
}]);
await check_autocomplete({
search: "https://" + search,
autofilled: "https://" + url,
completed: "https://" + url,
matches: [{
value: "https://" + url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
},
{
value: "http://" + url,
comment: "test visit for http://" + url,
style: ["favicon"],
}],
});
await cleanup();
});
// Autofill should respond to frecency changes.
add_task(async function frecency() {
// Start with an http visit. It should be completed.
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [{
value: url,
comment,
style: ["autofill", "heuristic"],
}],
});
// Add two https visits. https should now be completed.
for (let i = 0; i < 2; i++) {
await PlacesTestUtils.addVisits([
{ uri: "https://" + url },
]);
}
await check_autocomplete({
search,
autofilled: url,
completed: "https://" + url,
matches: [
{
value: url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
},
{
value: "http://" + url,
comment: "test visit for http://" + url,
style: ["favicon"],
},
],
});
// Add two more http visits, three total. http should now be completed
// again.
for (let i = 0; i < 2; i++) {
await PlacesTestUtils.addVisits([
{ uri: "http://" + url },
]);
}
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [
{
value: url,
comment,
style: ["autofill", "heuristic"],
},
{
value: "https://" + url,
comment: "test visit for https://" + url,
style: ["favicon"],
},
],
});
// Add four www https visits. www https should now be completed.
for (let i = 0; i < 4; i++) {
await PlacesTestUtils.addVisits([
{ uri: "https://www." + url },
]);
}
await check_autocomplete({
search,
autofilled: url,
completed: "https://www." + url,
matches: [
{
value: url,
comment: "https://www." + comment,
style: ["autofill", "heuristic"],
},
{
value: "http://" + url,
comment: "test visit for http://" + url,
style: ["favicon"],
},
{
value: "https://" + url,
comment: "test visit for https://" + url,
style: ["favicon"],
},
],
});
// Remove the www https page.
await PlacesUtils.history.remove([
"https://www." + url,
]);
// http should now be completed again.
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [
{
value: url,
comment,
style: ["autofill", "heuristic"],
},
{
value: "https://" + url,
comment: "test visit for https://" + url,
style: ["favicon"],
},
],
});
// Remove the http page.
await PlacesUtils.history.remove([
"http://" + url,
]);
// https should now be completed again.
await check_autocomplete({
search,
autofilled: url,
completed: "https://" + url,
matches: [
{
value: url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
},
],
});
// Add a visit with a different host so that "ex" doesn't autofill it.
// https://example.com/ should still have a higher frecency though, so it
// should still be autofilled.
await PlacesTestUtils.addVisits([
{ uri: "https://not-" + url },
]);
await check_autocomplete({
search,
autofilled: url,
completed: "https://" + url,
matches: [
{
value: url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
},
{
value: "https://not-" + url,
comment: "test visit for https://not-" + url,
style: ["favicon"],
},
],
});
// Now add 10 more visits to the different host so that the frecency of
// https://example.com/ falls below the autofill threshold. It should not
// be autofilled now.
for (let i = 0; i < 10; i++) {
await PlacesTestUtils.addVisits([
{ uri: "https://not-" + url },
]);
}
// Enable actions. In the `origins` case, the failure to make an autofill
// match should not interrupt creating another type of heuristic match, in
// this case a search (for "ex"). In the `!origins` case, autofill should
// still happen since there's no threshold comparison.
if (origins) {
await check_autocomplete({
search,
searchParam: "enable-actions",
matches: [
makeSearchMatch(search, { style: ["heuristic"] }),
{
value: "https://not-" + url,
comment: "test visit for https://not-" + url,
style: ["favicon"],
},
{
value: "https://" + url,
comment: "test visit for https://" + url,
style: ["favicon"],
},
],
});
} else {
await check_autocomplete({
search,
searchParam: "enable-actions",
autofilled: url,
completed: "https://" + url,
matches: [
{
value: url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
},
{
value: "https://not-" + url,
comment: "test visit for https://not-" + url,
style: ["favicon"],
},
],
});
}
// Remove the visits to the different host.
await PlacesUtils.history.remove([
"https://not-" + url,
]);
// https should be completed again.
await check_autocomplete({
search,
autofilled: url,
completed: "https://" + url,
matches: [
{
value: url,
comment: "https://" + comment,
style: ["autofill", "heuristic"],
},
],
});
// Remove the https visits.
await PlacesUtils.history.remove([
"https://" + url,
]);
// Now nothing should be completed.
await check_autocomplete({
search,
matches: [],
});
await cleanup();
});
// Bookmarked places should always be autofilled, even when they don't meet
// the threshold.
add_task(async function bookmarkBelowThreshold() {
// Add some visits to a URL so that the origin autofill threshold is large.
for (let i = 0; i < 3; i++) {
await PlacesTestUtils.addVisits([{
uri: "http://not-" + url,
}]);
}
// Now bookmark another URL.
await addBookmark({
uri: "http://" + url,
});
// Make sure the bookmarked origin and place frecencies are below the
// threshold so that the origin/URL otherwise would not be autofilled.
let placeFrecency =
await PlacesTestUtils.fieldInDB("http://" + url, "frecency");
let originFrecency = await getOriginFrecency("http://", host);
let threshold = await getOriginAutofillThreshold();
Assert.ok(placeFrecency < threshold,
`Place frecency should be below the threshold: ` +
`placeFrecency=${placeFrecency} threshold=${threshold}`);
Assert.ok(originFrecency < threshold,
`Origin frecency should be below the threshold: ` +
`originFrecency=${originFrecency} threshold=${threshold}`);
// The bookmark should be autofilled.
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [
{
value: url,
comment,
style: ["autofill", "heuristic"],
},
{
value: "http://not-" + url,
comment: "test visit for http://not-" + url,
style: ["favicon"],
},
],
});
await cleanup();
});
// Bookmarked places should be autofilled when they *do* meet the threshold.
add_task(async function bookmarkAboveThreshold() {
// Bookmark a URL.
await addBookmark({
uri: "http://" + url,
});
// The frecencies of the place and origin should be >= the threshold. In
// fact they should be the same as the threshold since the place is the only
// place in the database.
let placeFrecency =
await PlacesTestUtils.fieldInDB("http://" + url, "frecency");
let originFrecency = await getOriginFrecency("http://", host);
let threshold = await getOriginAutofillThreshold();
Assert.equal(placeFrecency, threshold);
Assert.equal(originFrecency, threshold);
// The bookmark should be autofilled.
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [
{
value: url,
comment,
style: ["autofill", "heuristic"],
},
],
});
await cleanup();
});
// Autofill should respect the browser.urlbar.suggest.history pref -- i.e., it
// should complete only bookmarked pages when that pref is false.
add_task(async function suggestHistoryFalse() {
// Force only bookmarked pages to be suggested and therefore only bookmarked
// pages to be completed.
Services.prefs.setBoolPref("browser.urlbar.suggest.history", false);
// Add a non-bookmarked page. It should not be suggested or completed.
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search,
matches: [],
});
// Bookmark the page. It should be suggested and completed.
await addBookmark({
uri: "http://" + url,
});
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [
{
value: url,
comment,
style: ["autofill", "heuristic"],
},
],
});
await cleanup();
});
// Same as previous but the search contains a prefix.
add_task(async function suggestHistoryFalsePrefix() {
// Force only bookmarked pages to be suggested and therefore only bookmarked
// pages to be completed.
Services.prefs.setBoolPref("browser.urlbar.suggest.history", false);
// Add a non-bookmarked page. It should not be suggested or completed.
await PlacesTestUtils.addVisits([{
uri: "http://" + url,
}]);
await check_autocomplete({
search: "http://" + search,
matches: [],
});
// Bookmark the page. It should be suggested and completed.
await addBookmark({
uri: "http://" + url,
});
await check_autocomplete({
search: "http://" + search,
autofilled: "http://" + url,
completed: "http://" + url,
matches: [
{
value: "http://" + url,
comment,
style: ["autofill", "heuristic"],
},
],
});
await cleanup();
});
// Bookmark a page and then clear history. The bookmarked origin/URL should
// be autofilled even though its frecency is <= 0 since the autofill threshold
// is 0.
add_task(async function zeroThreshold() {
await addBookmark({
uri: "http://" + url,
});
await PlacesUtils.history.clear();
// Make sure the place's frecency is <= 0. (It will be reset to -1 on the
// history.clear() above, and then on idle it will be reset to 0. xpcshell
// tests disable the idle service, so in practice it should always be -1,
// but in order to avoid possible intermittent failures in the future, don't
// assume that.)
let placeFrecency =
await PlacesTestUtils.fieldInDB("http://" + url, "frecency");
Assert.ok(placeFrecency <= 0);
// Make sure the origin's frecency is 0.
let originFrecency = await getOriginFrecency("http://", host);
Assert.equal(originFrecency, 0);
// Make sure the autofill threshold is 0.
let threshold = await getOriginAutofillThreshold();
Assert.equal(threshold, 0);
await check_autocomplete({
search,
autofilled: url,
completed: "http://" + url,
matches: [
{
value: url,
comment,
style: ["autofill", "heuristic"],
},
],
});
await cleanup();
});
}
/**
* Returns the frecency of an origin.
*
* @param {string} prefix
* The origin's prefix, e.g., "http://".
* @param {string} host
* The origin's host.
* @returns {number} The origin's frecency.
*/
async function getOriginFrecency(prefix, host) {
let db = await PlacesUtils.promiseDBConnection();
let rows = await db.execute(`
SELECT frecency
FROM moz_origins
WHERE prefix = :prefix AND host = :host
`, { prefix, host });
Assert.equal(rows.length, 1);
return rows[0].getResultByIndex(0);
}
/**
* Returns the origin frecency stats.
*
* @returns {object}
* An object { count, sum, squares }.
*/
async function getOriginFrecencyStats() {
let db = await PlacesUtils.promiseDBConnection();
let rows = await db.execute(`
SELECT
IFNULL((SELECT value FROM moz_meta WHERE key = "origin_frecency_count"), 0),
IFNULL((SELECT value FROM moz_meta WHERE key = "origin_frecency_sum"), 0),
IFNULL((SELECT value FROM moz_meta WHERE key = "origin_frecency_sum_of_squares"), 0)
`);
let count = rows[0].getResultByIndex(0);
let sum = rows[0].getResultByIndex(1);
let squares = rows[0].getResultByIndex(2);
return { count, sum, squares };
}
/**
* Returns the origin autofill frecency threshold.
*
* @returns {number}
* The threshold.
*/
async function getOriginAutofillThreshold() {
let { count, sum, squares } = await getOriginFrecencyStats();
if (!count) {
return 0;
}
if (count == 1) {
return sum;
}
let stddevMultiplier = UrlbarPrefs.get("autoFill.stddevMultiplier");
return (sum / count) + (stddevMultiplier * Math.sqrt((squares - ((sum * sum) / count)) / count));
}