tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_autofill_adaptiveHistory.js (43397B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 // Test for adaptive history autofill.
      8 
      9 testEngine_setup();
     10 
     11 registerCleanupFunction(async () => {
     12  Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
     13 });
     14 Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);
     15 
     16 const TEST_DATA = [
     17  {
     18    description: "Basic behavior for adaptive history autofill",
     19    pref: true,
     20    visitHistory: ["http://example.com/test"],
     21    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
     22    userInput: "exa",
     23    expected: {
     24      autofilled: "example.com/test",
     25      completed: "http://example.com/test",
     26      results: [
     27        context =>
     28          makeVisitResult(context, {
     29            uri: "http://example.com/test",
     30            title: "test visit for http://example.com/test",
     31            heuristic: true,
     32          }),
     33      ],
     34    },
     35  },
     36  {
     37    description: "URL that has www",
     38    pref: true,
     39    visitHistory: ["http://www.example.com/test"],
     40    inputHistory: [{ uri: "http://www.example.com/test", input: "exa" }],
     41    userInput: "exa",
     42    expected: {
     43      autofilled: "example.com/test",
     44      completed: "http://www.example.com/test",
     45      results: [
     46        context =>
     47          makeVisitResult(context, {
     48            uri: "http://www.example.com/test",
     49            title: "test visit for http://www.example.com/test",
     50            heuristic: true,
     51          }),
     52      ],
     53    },
     54  },
     55  {
     56    description: "User's input starts with www",
     57    pref: true,
     58    visitHistory: ["http://www.example.com/test"],
     59    inputHistory: [{ uri: "http://www.example.com/test", input: "www.exa" }],
     60    userInput: "www.exa",
     61    expected: {
     62      autofilled: "www.example.com/test",
     63      completed: "http://www.example.com/test",
     64      results: [
     65        context =>
     66          makeVisitResult(context, {
     67            uri: "http://www.example.com/test",
     68            title: "test visit for http://www.example.com/test",
     69            heuristic: true,
     70          }),
     71      ],
     72    },
     73  },
     74  {
     75    description: "Case differences for user's input are ignored",
     76    pref: true,
     77    visitHistory: ["http://example.com/test"],
     78    inputHistory: [{ uri: "http://example.com/test", input: "EXA" }],
     79    userInput: "eXA",
     80    expected: {
     81      autofilled: "eXAmple.com/test",
     82      completed: "http://example.com/test",
     83      results: [
     84        context =>
     85          makeVisitResult(context, {
     86            uri: "http://example.com/test",
     87            title: "test visit for http://example.com/test",
     88            heuristic: true,
     89          }),
     90      ],
     91    },
     92  },
     93  {
     94    description:
     95      "Case differences for user's input that starts with www are ignored",
     96    pref: true,
     97    visitHistory: ["http://www.example.com/test"],
     98    inputHistory: [{ uri: "http://www.example.com/test", input: "www.exa" }],
     99    userInput: "WWW.exa",
    100    expected: {
    101      autofilled: "WWW.example.com/test",
    102      completed: "http://www.example.com/test",
    103      results: [
    104        context =>
    105          makeVisitResult(context, {
    106            uri: "http://www.example.com/test",
    107            title: "test visit for http://www.example.com/test",
    108            heuristic: true,
    109          }),
    110      ],
    111    },
    112  },
    113  {
    114    description: "Mutiple case difference input history",
    115    pref: true,
    116    visitHistory: ["http://example.com/yes", "http://example.com/no"],
    117    inputHistory: [
    118      { uri: "http://example.com/yes", input: "exa" },
    119      { uri: "http://example.com/yes", input: "EXA" },
    120      { uri: "http://example.com/yes", input: "EXa" },
    121      { uri: "http://example.com/yes", input: "eXa" },
    122      { uri: "http://example.com/yes", input: "eXA" },
    123      { uri: "http://example.com/no", input: "exa" },
    124      { uri: "http://example.com/no", input: "exa" },
    125      { uri: "http://example.com/no", input: "exa" },
    126    ],
    127    userInput: "exa",
    128    expected: {
    129      autofilled: "example.com/yes",
    130      completed: "http://example.com/yes",
    131      results: [
    132        context =>
    133          makeVisitResult(context, {
    134            uri: "http://example.com/yes",
    135            title: "test visit for http://example.com/yes",
    136            heuristic: true,
    137          }),
    138        context =>
    139          makeVisitResult(context, {
    140            uri: "http://example.com/no",
    141            title: "test visit for http://example.com/no",
    142          }),
    143      ],
    144    },
    145  },
    146  {
    147    description: "Multiple input history count",
    148    pref: true,
    149    visitHistory: ["http://example.com/few", "http://example.com/many"],
    150    inputHistory: [
    151      { uri: "http://example.com/many", input: "exa" },
    152      { uri: "http://example.com/few", input: "exa" },
    153      { uri: "http://example.com/many", input: "examp" },
    154    ],
    155    userInput: "exa",
    156    expected: {
    157      autofilled: "example.com/many",
    158      completed: "http://example.com/many",
    159      results: [
    160        context =>
    161          makeVisitResult(context, {
    162            uri: "http://example.com/many",
    163            title: "test visit for http://example.com/many",
    164            heuristic: true,
    165          }),
    166        context =>
    167          makeVisitResult(context, {
    168            uri: "http://example.com/few",
    169            title: "test visit for http://example.com/few",
    170          }),
    171      ],
    172    },
    173  },
    174  {
    175    description: "Multiple input history count with same input",
    176    pref: true,
    177    visitHistory: ["http://example.com/few", "http://example.com/many"],
    178    inputHistory: [
    179      { uri: "http://example.com/many", input: "exa" },
    180      { uri: "http://example.com/few", input: "exa" },
    181      { uri: "http://example.com/many", input: "exa" },
    182    ],
    183    userInput: "exa",
    184    expected: {
    185      autofilled: "example.com/many",
    186      completed: "http://example.com/many",
    187      results: [
    188        context =>
    189          makeVisitResult(context, {
    190            uri: "http://example.com/many",
    191            title: "test visit for http://example.com/many",
    192            heuristic: true,
    193          }),
    194        context =>
    195          makeVisitResult(context, {
    196            uri: "http://example.com/few",
    197            title: "test visit for http://example.com/few",
    198          }),
    199      ],
    200    },
    201  },
    202  {
    203    description:
    204      "Multiple input history count with same input but different frecency",
    205    pref: true,
    206    visitHistory: [
    207      "http://example.com/few",
    208      "http://example.com/many",
    209      "http://example.com/many",
    210    ],
    211    inputHistory: [
    212      { uri: "http://example.com/many", input: "exa" },
    213      { uri: "http://example.com/few", input: "exa" },
    214    ],
    215    userInput: "exa",
    216    expected: {
    217      autofilled: "example.com/many",
    218      completed: "http://example.com/many",
    219      results: [
    220        context =>
    221          makeVisitResult(context, {
    222            uri: "http://example.com/many",
    223            title: "test visit for http://example.com/many",
    224            heuristic: true,
    225          }),
    226        context =>
    227          makeVisitResult(context, {
    228            uri: "http://example.com/few",
    229            title: "test visit for http://example.com/few",
    230          }),
    231      ],
    232    },
    233  },
    234  {
    235    description: "User input is shorter than the input history",
    236    pref: true,
    237    visitHistory: ["http://example.com/test"],
    238    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    239    userInput: "e",
    240    expected: {
    241      autofilled: "example.com/",
    242      completed: "http://example.com/",
    243      results: [
    244        context =>
    245          makeVisitResult(context, {
    246            uri: "http://example.com/",
    247            title: UrlbarTestUtils.trimURL("http://example.com/"),
    248            heuristic: true,
    249          }),
    250        context =>
    251          makeVisitResult(context, {
    252            uri: "http://example.com/test",
    253            title: "test visit for http://example.com/test",
    254          }),
    255      ],
    256    },
    257  },
    258  {
    259    description: "User input is longer than the input history",
    260    pref: true,
    261    visitHistory: ["http://example.com/test"],
    262    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    263    userInput: "example",
    264    expected: {
    265      autofilled: "example.com/test",
    266      completed: "http://example.com/test",
    267      results: [
    268        context =>
    269          makeVisitResult(context, {
    270            uri: "http://example.com/test",
    271            title: "test visit for http://example.com/test",
    272            heuristic: true,
    273          }),
    274      ],
    275    },
    276  },
    277  {
    278    description:
    279      "User input starts with input history and includes path of the url",
    280    pref: true,
    281    visitHistory: ["http://example.com/test"],
    282    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    283    userInput: "example.com/te",
    284    expected: {
    285      autofilled: "example.com/test",
    286      completed: "http://example.com/test",
    287      results: [
    288        context =>
    289          makeVisitResult(context, {
    290            uri: "http://example.com/test",
    291            title: "test visit for http://example.com/test",
    292            heuristic: true,
    293          }),
    294      ],
    295    },
    296  },
    297  {
    298    description: "User input starts with input history and but another url",
    299    pref: true,
    300    visitHistory: ["http://example.com/test", "http://example.org/test"],
    301    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    302    userInput: "example.o",
    303    expected: {
    304      autofilled: "example.org/",
    305      completed: "http://example.org/",
    306      results: [
    307        context =>
    308          makeVisitResult(context, {
    309            uri: "http://example.org/",
    310            title: UrlbarTestUtils.trimURL("http://example.org/"),
    311            heuristic: true,
    312          }),
    313        context =>
    314          makeVisitResult(context, {
    315            uri: "http://example.org/test",
    316            title: "test visit for http://example.org/test",
    317          }),
    318      ],
    319    },
    320  },
    321  {
    322    description: "User input does not start with input history",
    323    pref: true,
    324    visitHistory: ["http://example.com/test"],
    325    inputHistory: [{ uri: "http://example.com/test", input: "notmatch" }],
    326    userInput: "exa",
    327    expected: {
    328      autofilled: "example.com/",
    329      completed: "http://example.com/",
    330      results: [
    331        context =>
    332          makeVisitResult(context, {
    333            uri: "http://example.com/",
    334            title: UrlbarTestUtils.trimURL("http://example.com/"),
    335            heuristic: true,
    336          }),
    337        context =>
    338          makeVisitResult(context, {
    339            uri: "http://example.com/test",
    340            title: "test visit for http://example.com/test",
    341          }),
    342      ],
    343    },
    344  },
    345  {
    346    description:
    347      "User input does not start with input history, but it includes as part of URL",
    348    pref: true,
    349    visitHistory: ["http://example.com/test"],
    350    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    351    userInput: "test",
    352    expected: {
    353      results: [
    354        context =>
    355          makeSearchResult(context, {
    356            engineName: "Suggestions",
    357            heuristic: true,
    358          }),
    359        context =>
    360          makeVisitResult(context, {
    361            uri: "http://example.com/test",
    362            title: "test visit for http://example.com/test",
    363          }),
    364      ],
    365    },
    366  },
    367  {
    368    description: "User input does not start with visited URL",
    369    pref: true,
    370    visitHistory: ["http://mozilla.com/test"],
    371    inputHistory: [{ uri: "http://mozilla.com/test", input: "exa" }],
    372    userInput: "exa",
    373    expected: {
    374      results: [
    375        context =>
    376          makeSearchResult(context, {
    377            engineName: "Suggestions",
    378            heuristic: true,
    379          }),
    380        context =>
    381          makeVisitResult(context, {
    382            uri: "http://mozilla.com/test",
    383            title: "test visit for http://mozilla.com/test",
    384          }),
    385      ],
    386    },
    387  },
    388  {
    389    description: "Visited page is bookmarked",
    390    pref: true,
    391    visitHistory: ["http://example.com/test"],
    392    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    393    bookmarks: [{ uri: "http://example.com/test", title: "test bookmark" }],
    394    userInput: "exa",
    395    expected: {
    396      autofilled: "example.com/test",
    397      completed: "http://example.com/test",
    398      results: [
    399        context =>
    400          makeVisitResult(context, {
    401            uri: "http://example.com/test",
    402            title: "test bookmark",
    403            heuristic: true,
    404          }),
    405      ],
    406    },
    407  },
    408  {
    409    description: "Visit history and no bookamrk with HISTORY source",
    410    pref: true,
    411    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
    412    visitHistory: ["http://example.com/test"],
    413    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    414    userInput: "exa",
    415    expected: {
    416      autofilled: "example.com/test",
    417      completed: "http://example.com/test",
    418      results: [
    419        context =>
    420          makeVisitResult(context, {
    421            uri: "http://example.com/test",
    422            title: "test visit for http://example.com/test",
    423            heuristic: true,
    424          }),
    425      ],
    426    },
    427  },
    428  {
    429    description: "Visit history and no bookamrk with BOOKMARK source",
    430    pref: true,
    431    source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    432    visitHistory: ["http://example.com/test"],
    433    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    434    userInput: "exa",
    435    expected: {
    436      results: [
    437        context =>
    438          makeSearchResult(context, {
    439            engineName: "Suggestions",
    440            heuristic: true,
    441          }),
    442      ],
    443    },
    444  },
    445  {
    446    description: "Bookmarked visit history with HISTORY source",
    447    pref: true,
    448    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
    449    visitHistory: ["http://example.com/test", "http://example.com/bookmarked"],
    450    bookmarks: [
    451      { uri: "http://example.com/bookmarked", title: "test bookmark" },
    452    ],
    453    inputHistory: [
    454      {
    455        uri: "http://example.com/test",
    456        input: "exa",
    457      },
    458      {
    459        uri: "http://example.com/bookmarked",
    460        input: "exa",
    461      },
    462    ],
    463    userInput: "exa",
    464    expected: {
    465      autofilled: "example.com/bookmarked",
    466      completed: "http://example.com/bookmarked",
    467      results: [
    468        context =>
    469          makeVisitResult(context, {
    470            uri: "http://example.com/bookmarked",
    471            title: "test bookmark",
    472            heuristic: true,
    473          }),
    474        context =>
    475          makeVisitResult(context, {
    476            uri: "http://example.com/test",
    477            title: "test visit for http://example.com/test",
    478          }),
    479      ],
    480    },
    481  },
    482  {
    483    description: "Bookmarked visit history with BOOKMARK source",
    484    pref: true,
    485    source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    486    visitHistory: ["http://example.com/test", "http://example.com/bookmarked"],
    487    bookmarks: [
    488      { uri: "http://example.com/bookmarked", title: "test bookmark" },
    489    ],
    490    inputHistory: [
    491      {
    492        uri: "http://example.com/test",
    493        input: "exa",
    494      },
    495      {
    496        uri: "http://example.com/bookmarked",
    497        input: "exa",
    498      },
    499    ],
    500    userInput: "exa",
    501    expected: {
    502      autofilled: "example.com/bookmarked",
    503      completed: "http://example.com/bookmarked",
    504      results: [
    505        context =>
    506          makeVisitResult(context, {
    507            uri: "http://example.com/bookmarked",
    508            title: "test bookmark",
    509            heuristic: true,
    510          }),
    511      ],
    512    },
    513  },
    514  {
    515    description: "No visit history with HISTORY source",
    516    pref: true,
    517    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
    518    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    519    userInput: "exa",
    520    expected: {
    521      results: [
    522        context =>
    523          makeSearchResult(context, {
    524            engineName: "Suggestions",
    525            heuristic: true,
    526          }),
    527      ],
    528    },
    529  },
    530  {
    531    description: "No visit history with BOOKMARK source",
    532    pref: true,
    533    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
    534    bookmarks: [{ uri: "http://example.com/bookmarked", title: "test" }],
    535    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    536    userInput: "exa",
    537    expected: {
    538      results: [
    539        context =>
    540          makeSearchResult(context, {
    541            engineName: "Suggestions",
    542            heuristic: true,
    543          }),
    544      ],
    545    },
    546  },
    547  {
    548    description: "Match with path expression",
    549    pref: true,
    550    visitHistory: [
    551      "http://example.com/testMany",
    552      "http://example.com/testMany",
    553      "http://example.com/test",
    554    ],
    555    inputHistory: [{ uri: "http://example.com/test", input: "example.com/te" }],
    556    userInput: "example.com/te",
    557    expected: {
    558      autofilled: "example.com/test",
    559      completed: "http://example.com/test",
    560      results: [
    561        context =>
    562          makeVisitResult(context, {
    563            uri: "http://example.com/test",
    564            title: "test visit for http://example.com/test",
    565            heuristic: true,
    566          }),
    567        context =>
    568          makeVisitResult(context, {
    569            uri: "http://example.com/testMany",
    570            title: "test visit for http://example.com/testMany",
    571          }),
    572      ],
    573    },
    574  },
    575  {
    576    description:
    577      "Prefixed URL for input history and the same string for user input",
    578    pref: true,
    579    visitHistory: [
    580      "http://example.com/testMany",
    581      "http://example.com/testMany",
    582      "http://example.com/test",
    583    ],
    584    inputHistory: [
    585      { uri: "http://example.com/test", input: "http://example.com/test" },
    586    ],
    587    userInput: "http://example.com/test",
    588    expected: {
    589      autofilled: "http://example.com/test",
    590      completed: "http://example.com/test",
    591      results: [
    592        context =>
    593          makeVisitResult(context, {
    594            uri: "http://example.com/test",
    595            title: "test visit for http://example.com/test",
    596            heuristic: true,
    597          }),
    598        context =>
    599          makeVisitResult(context, {
    600            uri: "http://example.com/testMany",
    601            title: "test visit for http://example.com/testMany",
    602          }),
    603      ],
    604    },
    605  },
    606  {
    607    description:
    608      "Prefixed URL for input history and URL expression for user input",
    609    pref: true,
    610    visitHistory: [
    611      "http://example.com/testMany",
    612      "http://example.com/testMany",
    613      "http://example.com/test",
    614    ],
    615    inputHistory: [
    616      { uri: "http://example.com/test", input: "http://example.com/te" },
    617    ],
    618    userInput: "http://example.com/te",
    619    expected: {
    620      autofilled: "http://example.com/test",
    621      completed: "http://example.com/test",
    622      results: [
    623        context =>
    624          makeVisitResult(context, {
    625            uri: "http://example.com/test",
    626            title: "test visit for http://example.com/test",
    627            heuristic: true,
    628          }),
    629        context =>
    630          makeVisitResult(context, {
    631            uri: "http://example.com/testMany",
    632            title: "test visit for http://example.com/testMany",
    633          }),
    634      ],
    635    },
    636  },
    637  {
    638    description:
    639      "Prefixed URL for input history and path expression for user input",
    640    pref: true,
    641    visitHistory: [
    642      "http://example.com/testMany",
    643      "http://example.com/testMany",
    644      "http://example.com/test",
    645    ],
    646    inputHistory: [
    647      { uri: "http://example.com/test", input: "http://example.com/te" },
    648    ],
    649    userInput: "example.com/te",
    650    expected: {
    651      autofilled: "example.com/testMany",
    652      completed: "http://example.com/testMany",
    653      results: [
    654        context =>
    655          makeVisitResult(context, {
    656            uri: "http://example.com/testMany",
    657            title: "test visit for http://example.com/testMany",
    658            heuristic: true,
    659          }),
    660        context =>
    661          makeVisitResult(context, {
    662            uri: "http://example.com/test",
    663            title: "test visit for http://example.com/test",
    664          }),
    665      ],
    666    },
    667  },
    668  {
    669    description: "Prefixed URL for input history and 'http' for user input",
    670    pref: true,
    671    visitHistory: ["http://example.com/test"],
    672    inputHistory: [{ uri: "http://example.com/test", input: "http" }],
    673    userInput: "http",
    674    expected: {
    675      results: [
    676        context =>
    677          makeSearchResult(context, {
    678            engineName: "Suggestions",
    679            heuristic: true,
    680          }),
    681        context =>
    682          makeVisitResult(context, {
    683            uri: "http://example.com/test",
    684            title: "test visit for http://example.com/test",
    685          }),
    686      ],
    687    },
    688  },
    689  {
    690    description: "Prefixed URL for input history and 'http:' for user input",
    691    pref: true,
    692    visitHistory: ["http://example.com/test"],
    693    inputHistory: [{ uri: "http://example.com/test", input: "http:" }],
    694    userInput: "http:",
    695    expected: {
    696      results: [
    697        context =>
    698          makeSearchResult(context, {
    699            engineName: "Suggestions",
    700            heuristic: true,
    701          }),
    702        context =>
    703          makeVisitResult(context, {
    704            uri: "http://example.com/test",
    705            title: "test visit for http://example.com/test",
    706          }),
    707      ],
    708    },
    709  },
    710  {
    711    description: "Prefixed URL for input history and 'http:/' for user input",
    712    pref: true,
    713    visitHistory: ["http://example.com/test"],
    714    inputHistory: [{ uri: "http://example.com/test", input: "http:/" }],
    715    userInput: "http:/",
    716    expected: {
    717      results: [
    718        context =>
    719          makeSearchResult(context, {
    720            engineName: "Suggestions",
    721            heuristic: true,
    722          }),
    723        context =>
    724          makeVisitResult(context, {
    725            uri: "http://example.com/test",
    726            title: "test visit for http://example.com/test",
    727          }),
    728      ],
    729    },
    730  },
    731  {
    732    description: "Prefixed URL for input history and 'http://' for user input",
    733    pref: true,
    734    visitHistory: ["http://example.com/test"],
    735    inputHistory: [{ uri: "http://example.com/test", input: "http://" }],
    736    userInput: "http://",
    737    expected: {
    738      autofilled: "http://example.com/test",
    739      completed: "http://example.com/test",
    740      results: [
    741        context =>
    742          makeVisitResult(context, {
    743            uri: "http://example.com/test",
    744            title: "test visit for http://example.com/test",
    745            heuristic: true,
    746          }),
    747      ],
    748    },
    749  },
    750  {
    751    description: "Prefixed URL for input history and 'http://e' for user input",
    752    pref: true,
    753    visitHistory: ["http://example.com/test"],
    754    inputHistory: [{ uri: "http://example.com/test", input: "http://e" }],
    755    userInput: "http://e",
    756    expected: {
    757      autofilled: "http://example.com/test",
    758      completed: "http://example.com/test",
    759      results: [
    760        context =>
    761          makeVisitResult(context, {
    762            uri: "http://example.com/test",
    763            title: "test visit for http://example.com/test",
    764            heuristic: true,
    765          }),
    766      ],
    767    },
    768  },
    769  {
    770    description:
    771      "Prefixed URL with www omitted for input history and 'http://e' for user input",
    772    pref: true,
    773    visitHistory: ["http://www.example.com/test"],
    774    inputHistory: [{ uri: "http://www.example.com/test", input: "http://e" }],
    775    userInput: "http://e",
    776    expected: {
    777      autofilled: "http://example.com/test",
    778      completed: "http://www.example.com/test",
    779      results: [
    780        context =>
    781          makeVisitResult(context, {
    782            uri: "http://www.example.com/test",
    783            title: "test visit for http://www.example.com/test",
    784            heuristic: true,
    785          }),
    786      ],
    787    },
    788  },
    789  {
    790    description:
    791      "Those that match with fixed URL take precedence over those that match prefixed URL",
    792    pref: true,
    793    visitHistory: ["http://http.example.com/test", "http://example.com/test"],
    794    inputHistory: [
    795      { uri: "http://http.example.com/test", input: "http" },
    796      { uri: "http://example.com/test", input: "http://example.com/test" },
    797    ],
    798    userInput: "http",
    799    expected: {
    800      autofilled: "http.example.com/test",
    801      completed: "http://http.example.com/test",
    802      results: [
    803        context =>
    804          makeVisitResult(context, {
    805            uri: "http://http.example.com/test",
    806            title: "test visit for http://http.example.com/test",
    807            heuristic: true,
    808          }),
    809        context =>
    810          makeVisitResult(context, {
    811            uri: "http://example.com/test",
    812            title: "test visit for http://example.com/test",
    813          }),
    814      ],
    815    },
    816  },
    817  {
    818    description: "Input history is totally different string from the URL",
    819    pref: true,
    820    visitHistory: [
    821      "http://example.com/testMany",
    822      "http://example.com/testMany",
    823      "http://example.com/test",
    824    ],
    825    inputHistory: [
    826      { uri: "http://example.com/test", input: "totally-different-string" },
    827    ],
    828    userInput: "totally",
    829    expected: {
    830      results: [
    831        context =>
    832          makeSearchResult(context, {
    833            engineName: "Suggestions",
    834            heuristic: true,
    835          }),
    836        context =>
    837          makeVisitResult(context, {
    838            uri: "http://example.com/test",
    839            title: "test visit for http://example.com/test",
    840          }),
    841      ],
    842    },
    843  },
    844  {
    845    description:
    846      "Input history is totally different string from the URL and there is a visit history whose URL starts with the input",
    847    pref: true,
    848    visitHistory: ["http://example.com/test", "http://totally.example.com"],
    849    inputHistory: [
    850      { uri: "http://example.com/test", input: "totally-different-string" },
    851    ],
    852    userInput: "totally",
    853    expected: {
    854      autofilled: "totally.example.com/",
    855      completed: "http://totally.example.com/",
    856      results: [
    857        context =>
    858          makeVisitResult(context, {
    859            uri: "http://totally.example.com/",
    860            title: "test visit for http://totally.example.com/",
    861            heuristic: true,
    862          }),
    863        context =>
    864          makeVisitResult(context, {
    865            uri: "http://example.com/test",
    866            title: "test visit for http://example.com/test",
    867          }),
    868      ],
    869    },
    870  },
    871  {
    872    description: "Use count threshold is as same as use count of input history",
    873    pref: true,
    874    useCountThreshold: 1 * 0.9 + 1,
    875    visitHistory: ["http://example.com/test"],
    876    inputHistory: [
    877      { uri: "http://example.com/test", input: "exa" },
    878      { uri: "http://example.com/test", input: "exa" },
    879    ],
    880    userInput: "exa",
    881    expected: {
    882      autofilled: "example.com/test",
    883      completed: "http://example.com/test",
    884      results: [
    885        context =>
    886          makeVisitResult(context, {
    887            uri: "http://example.com/test",
    888            title: "test visit for http://example.com/test",
    889            heuristic: true,
    890          }),
    891      ],
    892    },
    893  },
    894  {
    895    description: "Use count threshold is less than use count of input history",
    896    pref: true,
    897    useCountThreshold: 3,
    898    visitHistory: ["http://example.com/test"],
    899    inputHistory: [
    900      { uri: "http://example.com/test", input: "exa" },
    901      { uri: "http://example.com/test", input: "exa" },
    902      { uri: "http://example.com/test", input: "exa" },
    903      { uri: "http://example.com/test", input: "exa" },
    904    ],
    905    userInput: "exa",
    906    expected: {
    907      autofilled: "example.com/test",
    908      completed: "http://example.com/test",
    909      results: [
    910        context =>
    911          makeVisitResult(context, {
    912            uri: "http://example.com/test",
    913            title: "test visit for http://example.com/test",
    914            heuristic: true,
    915          }),
    916      ],
    917    },
    918  },
    919  {
    920    description: "Use count threshold is more than use count of input history",
    921    pref: true,
    922    useCountThreshold: 10,
    923    visitHistory: ["http://example.com/test"],
    924    inputHistory: [
    925      { uri: "http://example.com/test", input: "exa" },
    926      { uri: "http://example.com/test", input: "exa" },
    927      { uri: "http://example.com/test", input: "exa" },
    928      { uri: "http://example.com/test", input: "exa" },
    929      { uri: "http://example.com/test", input: "exa" },
    930    ],
    931    userInput: "exa",
    932    expected: {
    933      autofilled: "example.com/",
    934      completed: "http://example.com/",
    935      results: [
    936        context =>
    937          makeVisitResult(context, {
    938            uri: "http://example.com/",
    939            title: UrlbarTestUtils.trimURL("http://example.com/"),
    940            heuristic: true,
    941          }),
    942        context =>
    943          makeVisitResult(context, {
    944            uri: "http://example.com/test",
    945            title: "test visit for http://example.com/test",
    946          }),
    947      ],
    948    },
    949  },
    950  {
    951    description: "minCharsThreshold pref equals to the user input length",
    952    pref: true,
    953    minCharsThreshold: 3,
    954    visitHistory: ["http://example.com/test"],
    955    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    956    userInput: "exa",
    957    expected: {
    958      autofilled: "example.com/test",
    959      completed: "http://example.com/test",
    960      results: [
    961        context =>
    962          makeVisitResult(context, {
    963            uri: "http://example.com/test",
    964            title: "test visit for http://example.com/test",
    965            heuristic: true,
    966          }),
    967      ],
    968    },
    969  },
    970  {
    971    description: "minCharsThreshold pref is smaller than the user input length",
    972    pref: true,
    973    minCharsThreshold: 2,
    974    visitHistory: ["http://example.com/test"],
    975    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    976    userInput: "exa",
    977    expected: {
    978      autofilled: "example.com/test",
    979      completed: "http://example.com/test",
    980      results: [
    981        context =>
    982          makeVisitResult(context, {
    983            uri: "http://example.com/test",
    984            title: "test visit for http://example.com/test",
    985            heuristic: true,
    986          }),
    987      ],
    988    },
    989  },
    990  {
    991    description: "minCharsThreshold pref is larger than the user input length",
    992    pref: true,
    993    minCharsThreshold: 4,
    994    visitHistory: ["http://example.com/test"],
    995    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
    996    userInput: "exa",
    997    expected: {
    998      autofilled: "example.com/",
    999      completed: "http://example.com/",
   1000      results: [
   1001        context =>
   1002          makeVisitResult(context, {
   1003            uri: "http://example.com/",
   1004            title: UrlbarTestUtils.trimURL("http://example.com/"),
   1005            heuristic: true,
   1006          }),
   1007        context =>
   1008          makeVisitResult(context, {
   1009            uri: "http://example.com/test",
   1010            title: "test visit for http://example.com/test",
   1011          }),
   1012      ],
   1013    },
   1014  },
   1015  {
   1016    description:
   1017      "Prioritize path component with case-sensitive and that is visited",
   1018    pref: true,
   1019    visitHistory: [
   1020      "http://example.com/TEST",
   1021      "http://example.com/TEST",
   1022      "http://example.com/test",
   1023    ],
   1024    inputHistory: [
   1025      { uri: "http://example.com/TEST", input: "example.com/test" },
   1026      { uri: "http://example.com/test", input: "example.com/test" },
   1027    ],
   1028    userInput: "example.com/test",
   1029    expected: {
   1030      autofilled: "example.com/test",
   1031      completed: "http://example.com/test",
   1032      results: [
   1033        context =>
   1034          makeVisitResult(context, {
   1035            uri: "http://example.com/test",
   1036            title: "test visit for http://example.com/test",
   1037            heuristic: true,
   1038          }),
   1039        context =>
   1040          makeVisitResult(context, {
   1041            uri: "http://example.com/TEST",
   1042            title: "test visit for http://example.com/TEST",
   1043          }),
   1044      ],
   1045    },
   1046  },
   1047  {
   1048    description:
   1049      "Prioritize path component with case-sensitive but no visited data",
   1050    pref: true,
   1051    visitHistory: ["http://example.com/TEST"],
   1052    inputHistory: [
   1053      { uri: "http://example.com/TEST", input: "example.com/test" },
   1054    ],
   1055    userInput: "example.com/test",
   1056    expected: {
   1057      completed: "http://example.com/test",
   1058      results: [
   1059        context =>
   1060          makeVisitResult(context, {
   1061            uri: "http://example.com/test",
   1062            title: UrlbarTestUtils.trimURL("http://example.com/test"),
   1063            heuristic: true,
   1064          }),
   1065        context =>
   1066          makeVisitResult(context, {
   1067            uri: "http://example.com/TEST",
   1068            title: "test visit for http://example.com/TEST",
   1069          }),
   1070      ],
   1071    },
   1072  },
   1073  {
   1074    description:
   1075      "With history and bookmarks sources, foreign_count == 0, frecency <= 0: No adaptive history autofill",
   1076    pref: true,
   1077    visitHistory: ["http://example.com/test"],
   1078    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
   1079    frecency: 0,
   1080    userInput: "exa",
   1081    expected: {
   1082      results: [
   1083        context =>
   1084          makeSearchResult(context, {
   1085            engineName: "Suggestions",
   1086            heuristic: true,
   1087          }),
   1088        context =>
   1089          makeVisitResult(context, {
   1090            uri: "http://example.com/test",
   1091            title: "test visit for http://example.com/test",
   1092          }),
   1093      ],
   1094    },
   1095  },
   1096  {
   1097    description:
   1098      "With history source, visit_count == 0, foreign_count != 0: No adaptive history autofill",
   1099    pref: true,
   1100    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
   1101    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
   1102    bookmarks: [{ uri: "http://example.com/test", title: "test bookmark" }],
   1103    userInput: "exa",
   1104    expected: {
   1105      results: [
   1106        context =>
   1107          makeSearchResult(context, {
   1108            engineName: "Suggestions",
   1109            heuristic: true,
   1110          }),
   1111      ],
   1112    },
   1113  },
   1114  {
   1115    description:
   1116      "With history source, visit_count > 0, foreign_count != 0, frecency <= 20: No adaptive history autofill",
   1117    pref: true,
   1118    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
   1119    visitHistory: ["http://example.com/test"],
   1120    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
   1121    bookmarks: [{ uri: "http://example.com/test", title: "test bookmark" }],
   1122    frecency: 5,
   1123    userInput: "exa",
   1124    expected: {
   1125      autofilled: "example.com/",
   1126      completed: "http://example.com/",
   1127      results: [
   1128        context =>
   1129          makeVisitResult(context, {
   1130            uri: "http://example.com/",
   1131            title: UrlbarTestUtils.trimURL("http://example.com/"),
   1132            heuristic: true,
   1133          }),
   1134        context =>
   1135          makeVisitResult(context, {
   1136            uri: "http://example.com/test",
   1137            title: "test bookmark",
   1138          }),
   1139      ],
   1140    },
   1141  },
   1142  {
   1143    description:
   1144      "With history source, visit_count > 0, foreign_count == 0, frecency <= 20: No adaptive history autofill",
   1145    pref: true,
   1146    source: UrlbarUtils.RESULT_SOURCE.HISTORY,
   1147    visitHistory: ["http://example.com/test"],
   1148    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
   1149    frecency: 5,
   1150    userInput: "exa",
   1151    expected: {
   1152      autofilled: "example.com/",
   1153      completed: "http://example.com/",
   1154      results: [
   1155        context =>
   1156          makeVisitResult(context, {
   1157            uri: "http://example.com/",
   1158            title: UrlbarTestUtils.trimURL("http://example.com/"),
   1159            heuristic: true,
   1160          }),
   1161        context =>
   1162          makeVisitResult(context, {
   1163            uri: "http://example.com/test",
   1164            title: "test visit for http://example.com/test",
   1165          }),
   1166      ],
   1167    },
   1168  },
   1169  {
   1170    description: "Empty input string",
   1171    pref: true,
   1172    visitHistory: ["http://example.com/test"],
   1173    inputHistory: [{ uri: "http://example.com/test", input: "" }],
   1174    userInput: "exa",
   1175    expected: {
   1176      autofilled: "example.com/",
   1177      completed: "http://example.com/",
   1178      results: [
   1179        context =>
   1180          makeVisitResult(context, {
   1181            uri: "http://example.com/",
   1182            title: UrlbarTestUtils.trimURL("http://example.com/"),
   1183            heuristic: true,
   1184          }),
   1185        context =>
   1186          makeVisitResult(context, {
   1187            uri: "http://example.com/test",
   1188            title: "test visit for http://example.com/test",
   1189          }),
   1190      ],
   1191    },
   1192  },
   1193  {
   1194    description: "Turn the pref off",
   1195    pref: false,
   1196    visitHistory: ["http://example.com/test"],
   1197    inputHistory: [{ uri: "http://example.com/test", input: "exa" }],
   1198    userInput: "exa",
   1199    expected: {
   1200      autofilled: "example.com/",
   1201      completed: "http://example.com/",
   1202      results: [
   1203        context =>
   1204          makeVisitResult(context, {
   1205            uri: "http://example.com/",
   1206            title: UrlbarTestUtils.trimURL("http://example.com/"),
   1207            heuristic: true,
   1208          }),
   1209        context =>
   1210          makeVisitResult(context, {
   1211            uri: "http://example.com/test",
   1212            title: "test visit for http://example.com/test",
   1213          }),
   1214      ],
   1215    },
   1216  },
   1217 ];
   1218 
   1219 add_task(async function inputTest() {
   1220  for (const {
   1221    description,
   1222    pref,
   1223    minCharsThreshold,
   1224    useCountThreshold,
   1225    source,
   1226    visitHistory,
   1227    inputHistory,
   1228    bookmarks,
   1229    frecency,
   1230    userInput,
   1231    expected,
   1232  } of TEST_DATA) {
   1233    info(description);
   1234 
   1235    UrlbarPrefs.set("autoFill.adaptiveHistory.enabled", pref);
   1236 
   1237    if (!isNaN(minCharsThreshold)) {
   1238      UrlbarPrefs.set(
   1239        "autoFill.adaptiveHistory.minCharsThreshold",
   1240        minCharsThreshold
   1241      );
   1242    }
   1243 
   1244    if (!isNaN(useCountThreshold)) {
   1245      UrlbarPrefs.set(
   1246        "autoFill.adaptiveHistory.useCountThreshold",
   1247        useCountThreshold
   1248      );
   1249    }
   1250 
   1251    if (visitHistory && visitHistory.length) {
   1252      await PlacesTestUtils.addVisits(visitHistory);
   1253    }
   1254    for (const { uri, input } of inputHistory) {
   1255      await UrlbarUtils.addToInputHistory(uri, input);
   1256    }
   1257    for (const bookmark of bookmarks || []) {
   1258      await PlacesTestUtils.addBookmarkWithDetails(bookmark);
   1259    }
   1260 
   1261    if (typeof frecency == "number") {
   1262      await PlacesUtils.withConnectionWrapper("test::setFrecency", db =>
   1263        db.execute(
   1264          `UPDATE moz_places SET frecency = :frecency WHERE url = :url`,
   1265          {
   1266            frecency,
   1267            url: visitHistory[0],
   1268          }
   1269        )
   1270      );
   1271    }
   1272 
   1273    const sources = source
   1274      ? [source]
   1275      : [
   1276          UrlbarUtils.RESULT_SOURCE.HISTORY,
   1277          UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
   1278        ];
   1279 
   1280    const context = createContext(userInput, {
   1281      sources,
   1282      isPrivate: false,
   1283    });
   1284 
   1285    await check_results({
   1286      context,
   1287      autofilled: expected.autofilled,
   1288      completed: expected.completed,
   1289      hasAutofillTitle: expected.hasAutofillTitle,
   1290      matches: expected.results.map(f => f(context)),
   1291    });
   1292 
   1293    await cleanupPlaces();
   1294    UrlbarPrefs.clear("autoFill.adaptiveHistory.enabled");
   1295    UrlbarPrefs.clear("autoFill.adaptiveHistory.minCharsThreshold");
   1296    UrlbarPrefs.clear("autoFill.adaptiveHistory.useCountThreshold");
   1297  }
   1298 });
   1299 
   1300 add_task(async function urlCase() {
   1301  UrlbarPrefs.set("autoFill.adaptiveHistory.enabled", true);
   1302 
   1303  const testVisitFixed = "example.com/ABC/DEF";
   1304  const testVisitURL = `http://${testVisitFixed}`;
   1305  const testInput = "example";
   1306  await PlacesTestUtils.addVisits([testVisitURL]);
   1307  await UrlbarUtils.addToInputHistory(testVisitURL, testInput);
   1308 
   1309  const userInput = "example.COM/abc/def";
   1310  for (let i = 1; i <= userInput.length; i++) {
   1311    const currentUserInput = userInput.substring(0, i);
   1312    const context = createContext(currentUserInput, { isPrivate: false });
   1313 
   1314    if (currentUserInput.length < testInput.length) {
   1315      // Autofill with host.
   1316      await check_results({
   1317        context,
   1318        autofilled: "example.com/",
   1319        completed: "http://example.com/",
   1320        matches: [
   1321          makeVisitResult(context, {
   1322            uri: "http://example.com/",
   1323            title: UrlbarTestUtils.trimURL("http://example.com/"),
   1324            heuristic: true,
   1325          }),
   1326          makeVisitResult(context, {
   1327            uri: "http://example.com/ABC/DEF",
   1328            title: "test visit for http://example.com/ABC/DEF",
   1329          }),
   1330        ],
   1331      });
   1332    } else if (currentUserInput.length !== testVisitFixed.length) {
   1333      // Autofill using input history.
   1334      const autofilled = currentUserInput + testVisitFixed.substring(i);
   1335      await check_results({
   1336        context,
   1337        autofilled,
   1338        completed: "http://example.com/ABC/DEF",
   1339        matches: [
   1340          makeVisitResult(context, {
   1341            uri: "http://example.com/ABC/DEF",
   1342            title: "test visit for http://example.com/ABC/DEF",
   1343            heuristic: true,
   1344          }),
   1345        ],
   1346      });
   1347    } else {
   1348      // Autofill using user's input.
   1349      await check_results({
   1350        context,
   1351        autofilled: "example.COM/abc/def",
   1352        completed: "http://example.com/abc/def",
   1353        matches: [
   1354          makeVisitResult(context, {
   1355            uri: "http://example.com/abc/def",
   1356            title: UrlbarTestUtils.trimURL("http://example.com/abc/def"),
   1357            heuristic: true,
   1358          }),
   1359          makeVisitResult(context, {
   1360            uri: "http://example.com/ABC/DEF",
   1361            title: "test visit for http://example.com/ABC/DEF",
   1362          }),
   1363        ],
   1364      });
   1365    }
   1366  }
   1367 
   1368  await cleanupPlaces();
   1369  UrlbarPrefs.clear("autoFill.adaptiveHistory.enabled");
   1370 });
   1371 
   1372 add_task(async function decayTest() {
   1373  UrlbarPrefs.set("autoFill.adaptiveHistory.enabled", true);
   1374 
   1375  await PlacesTestUtils.addVisits(["http://example.com/test"]);
   1376  await UrlbarUtils.addToInputHistory("http://example.com/test", "exa");
   1377 
   1378  const initContext = createContext("exa", { isPrivate: false });
   1379  await check_results({
   1380    context: initContext,
   1381    autofilled: "example.com/test",
   1382    completed: "http://example.com/test",
   1383    matches: [
   1384      makeVisitResult(initContext, {
   1385        uri: "http://example.com/test",
   1386        title: "test visit for http://example.com/test",
   1387        heuristic: true,
   1388      }),
   1389    ],
   1390  });
   1391 
   1392  // The decay rate for a day is 0.975, as defined in PlacesFrecencyRecalculator
   1393  // Therefore, after 30 days, as use_count will be 0.975^30 = 0.468, we set the
   1394  // useCountThreshold 0.47 to not take the input history passed 30 days.
   1395  UrlbarPrefs.set("autoFill.adaptiveHistory.useCountThreshold", 0.47);
   1396 
   1397  // Make 29 days later.
   1398  for (let i = 0; i < 29; i++) {
   1399    await Cc["@mozilla.org/places/frecency-recalculator;1"]
   1400      .getService(Ci.nsIObserver)
   1401      .wrappedJSObject.decay();
   1402  }
   1403  const midContext = createContext("exa", { isPrivate: false });
   1404  await check_results({
   1405    context: midContext,
   1406    autofilled: "example.com/test",
   1407    completed: "http://example.com/test",
   1408    matches: [
   1409      makeVisitResult(midContext, {
   1410        uri: "http://example.com/test",
   1411        title: "test visit for http://example.com/test",
   1412        heuristic: true,
   1413      }),
   1414    ],
   1415  });
   1416 
   1417  // Total 30 days later.
   1418  await Cc["@mozilla.org/places/frecency-recalculator;1"]
   1419    .getService(Ci.nsIObserver)
   1420    .wrappedJSObject.decay();
   1421  const context = createContext("exa", { isPrivate: false });
   1422  await check_results({
   1423    context,
   1424    autofilled: "example.com/",
   1425    completed: "http://example.com/",
   1426    matches: [
   1427      makeVisitResult(context, {
   1428        uri: "http://example.com/",
   1429        title: UrlbarTestUtils.trimURL("http://example.com"),
   1430        heuristic: true,
   1431      }),
   1432      makeVisitResult(context, {
   1433        uri: "http://example.com/test",
   1434        title: "test visit for http://example.com/test",
   1435      }),
   1436    ],
   1437  });
   1438 
   1439  await cleanupPlaces();
   1440  UrlbarPrefs.clear("autoFill.adaptiveHistory.enabled");
   1441  UrlbarPrefs.clear("autoFill.adaptiveHistory.useCountThreshold");
   1442 });