tor-browser

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

test_frecency.js (11768B)


      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 /**
      6 * Test for bug 406358 to make sure frecency works for empty input/search, but
      7 * this also tests for non-empty inputs as well. Because the interactions among
      8 * DIFFERENT* visit counts and visit dates is not well defined, this test
      9 * holds one of the two values constant when modifying the other.
     10 *
     11 * Also test bug 419068 to make sure tagged pages don't necessarily have to be
     12 * first in the results.
     13 *
     14 * Also test bug 426166 to make sure that the results of autocomplete searches
     15 * are stable.  Note that failures of this test will be intermittent by nature
     16 * since we are testing to make sure that the unstable sort algorithm used
     17 * by SQLite is not changing the order of the results on us.
     18 */
     19 
     20 testEngine_setup();
     21 
     22 async function task_setCountDate(uri, count, date) {
     23  // We need visits so that frecency can be computed over multiple visits
     24  let visits = [];
     25  for (let i = 0; i < count; i++) {
     26    visits.push({
     27      uri,
     28      visitDate: date,
     29      transition: PlacesUtils.history.TRANSITION_TYPED,
     30    });
     31  }
     32  await PlacesTestUtils.addVisits(visits);
     33 }
     34 
     35 async function setBookmark(uri) {
     36  await PlacesUtils.bookmarks.insert({
     37    parentGuid: PlacesUtils.bookmarks.menuGuid,
     38    url: uri,
     39    title: "bleh",
     40  });
     41 }
     42 
     43 async function tagURI(uri, tags) {
     44  await PlacesUtils.bookmarks.insert({
     45    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
     46    url: uri,
     47    title: "bleh",
     48  });
     49  PlacesUtils.tagging.tagURI(uri, tags);
     50 }
     51 
     52 var uri1 = Services.io.newURI("http://site.tld/1");
     53 var uri2 = Services.io.newURI("http://site.tld/2");
     54 var uri3 = Services.io.newURI("http://aaaaaaaaaa/1");
     55 var uri4 = Services.io.newURI("http://aaaaaaaaaa/2");
     56 
     57 // d1 is younger (should show up higher) than d2 (PRTime is in usecs not msec)
     58 // Make sure the dates fall into different frecency groups
     59 var d1 = new Date(Date.now() - 1000 * 60 * 60) * 1000;
     60 var d2 = new Date(Date.now() - 1000 * 60 * 60 * 24 * 10) * 1000;
     61 // c1 is larger (should show up higher) than c2
     62 var c1 = 10;
     63 var c2 = 1;
     64 
     65 var tests = [
     66  // test things without a search term
     67  async function () {
     68    info("Test 0: same count, different date");
     69    await task_setCountDate(uri1, c1, d1);
     70    await task_setCountDate(uri2, c1, d2);
     71    await tagURI(uri1, ["site"]);
     72    let context = createContext(" ", { isPrivate: false });
     73    await check_results({
     74      context,
     75      matches: [
     76        makeSearchResult(context, {
     77          engineName: SUGGESTIONS_ENGINE_NAME,
     78          heuristic: true,
     79          query: " ",
     80        }),
     81        // uri1 is a visit result despite being a tagged bookmark because we
     82        // are searching for the empty string. By default, the empty string
     83        // filters to history. uri1 will be displayed as a bookmark later in the
     84        // test when we are searching with a non-empty string.
     85        makeVisitResult(context, {
     86          uri: uri1.spec,
     87          title: "bleh",
     88        }),
     89        makeVisitResult(context, {
     90          uri: uri2.spec,
     91          title: `test visit for ${uri2.spec}`,
     92        }),
     93      ],
     94    });
     95  },
     96  async function () {
     97    info("Test 1: same count, different date");
     98    await task_setCountDate(uri1, c1, d2);
     99    await task_setCountDate(uri2, c1, d1);
    100    await tagURI(uri1, ["site"]);
    101    let context = createContext(" ", { isPrivate: false });
    102    await check_results({
    103      context,
    104      matches: [
    105        makeSearchResult(context, {
    106          engineName: SUGGESTIONS_ENGINE_NAME,
    107          heuristic: true,
    108          query: " ",
    109        }),
    110        makeVisitResult(context, {
    111          uri: uri2.spec,
    112          title: `test visit for ${uri2.spec}`,
    113        }),
    114        makeVisitResult(context, {
    115          uri: uri1.spec,
    116          title: "bleh",
    117        }),
    118      ],
    119    });
    120  },
    121  async function () {
    122    info("Test 2: different count, same date");
    123    await task_setCountDate(uri1, c1, d1);
    124    await task_setCountDate(uri2, c2, d1);
    125    await tagURI(uri1, ["site"]);
    126    let context = createContext(" ", { isPrivate: false });
    127    await check_results({
    128      context,
    129      matches: [
    130        makeSearchResult(context, {
    131          engineName: SUGGESTIONS_ENGINE_NAME,
    132          heuristic: true,
    133          query: " ",
    134        }),
    135        makeVisitResult(context, {
    136          uri: uri1.spec,
    137          title: "bleh",
    138        }),
    139        makeVisitResult(context, {
    140          uri: uri2.spec,
    141          title: `test visit for ${uri2.spec}`,
    142        }),
    143      ],
    144    });
    145  },
    146  async function () {
    147    info("Test 3: different count, same date");
    148    await task_setCountDate(uri1, c2, d1);
    149    await task_setCountDate(uri2, c1, d1);
    150    await tagURI(uri1, ["site"]);
    151    let context = createContext(" ", { isPrivate: false });
    152    await check_results({
    153      context,
    154      matches: [
    155        makeSearchResult(context, {
    156          engineName: SUGGESTIONS_ENGINE_NAME,
    157          heuristic: true,
    158          query: " ",
    159        }),
    160        makeVisitResult(context, {
    161          uri: uri2.spec,
    162          title: `test visit for ${uri2.spec}`,
    163        }),
    164        makeVisitResult(context, {
    165          uri: uri1.spec,
    166          title: "bleh",
    167        }),
    168      ],
    169    });
    170  },
    171 
    172  // test things with a search term
    173  async function () {
    174    info("Test 4: same count, different date");
    175    await task_setCountDate(uri1, c1, d1);
    176    await task_setCountDate(uri2, c1, d2);
    177    await tagURI(uri1, ["site"]);
    178    let context = createContext("site", { isPrivate: false });
    179    await check_results({
    180      context,
    181      matches: [
    182        makeSearchResult(context, {
    183          engineName: SUGGESTIONS_ENGINE_NAME,
    184          heuristic: true,
    185        }),
    186        makeBookmarkResult(context, {
    187          uri: uri1.spec,
    188          title: "bleh",
    189          tags: ["site"],
    190        }),
    191        makeVisitResult(context, {
    192          uri: uri2.spec,
    193          title: `test visit for ${uri2.spec}`,
    194        }),
    195      ],
    196    });
    197  },
    198  async function () {
    199    info("Test 5: same count, different date");
    200    await task_setCountDate(uri1, c1, d2);
    201    await task_setCountDate(uri2, c1, d1);
    202    await tagURI(uri1, ["site"]);
    203    let context = createContext("site", { isPrivate: false });
    204    await check_results({
    205      context,
    206      matches: [
    207        makeSearchResult(context, {
    208          engineName: SUGGESTIONS_ENGINE_NAME,
    209          heuristic: true,
    210        }),
    211        makeVisitResult(context, {
    212          uri: uri2.spec,
    213          title: `test visit for ${uri2.spec}`,
    214        }),
    215        makeBookmarkResult(context, {
    216          uri: uri1.spec,
    217          title: "bleh",
    218          tags: ["site"],
    219        }),
    220      ],
    221    });
    222  },
    223  async function () {
    224    info("Test 6: different count, same date");
    225    await task_setCountDate(uri1, c1, d1);
    226    await task_setCountDate(uri2, c2, d1);
    227    await tagURI(uri1, ["site"]);
    228    let context = createContext("site", { isPrivate: false });
    229    await check_results({
    230      context,
    231      matches: [
    232        makeSearchResult(context, {
    233          engineName: SUGGESTIONS_ENGINE_NAME,
    234          heuristic: true,
    235        }),
    236        makeBookmarkResult(context, {
    237          uri: uri1.spec,
    238          title: "bleh",
    239          tags: ["site"],
    240        }),
    241        makeVisitResult(context, {
    242          uri: uri2.spec,
    243          title: `test visit for ${uri2.spec}`,
    244        }),
    245      ],
    246    });
    247  },
    248  async function () {
    249    info("Test 7: different count, same date");
    250    await task_setCountDate(uri1, c2, d1);
    251    await task_setCountDate(uri2, c1, d1);
    252    await tagURI(uri1, ["site"]);
    253    let context = createContext("site", { isPrivate: false });
    254    await check_results({
    255      context,
    256      matches: [
    257        makeSearchResult(context, {
    258          engineName: SUGGESTIONS_ENGINE_NAME,
    259          heuristic: true,
    260        }),
    261        makeVisitResult(context, {
    262          uri: uri2.spec,
    263          title: `test visit for ${uri2.spec}`,
    264        }),
    265        makeBookmarkResult(context, {
    266          uri: uri1.spec,
    267          title: "bleh",
    268          tags: ["site"],
    269        }),
    270      ],
    271    });
    272  },
    273  // There are multiple tests for 8, hence the multiple functions
    274  // Bug 426166 section
    275  async function () {
    276    info("Test 8.1a: same count, same date");
    277    await setBookmark(uri3);
    278    await setBookmark(uri4);
    279    let context = createContext("a", { isPrivate: false });
    280    let bookmarkResults = [
    281      makeBookmarkResult(context, {
    282        uri: uri4.spec,
    283        title: "bleh",
    284      }),
    285      makeBookmarkResult(context, {
    286        uri: uri3.spec,
    287        title: "bleh",
    288      }),
    289    ];
    290    await check_results({
    291      context,
    292      matches: [
    293        makeSearchResult(context, {
    294          engineName: SUGGESTIONS_ENGINE_NAME,
    295          heuristic: true,
    296        }),
    297        ...bookmarkResults,
    298      ],
    299    });
    300 
    301    context = createContext("aa", { isPrivate: false });
    302    await check_results({
    303      context,
    304      matches: [
    305        // We need to continuously redefine the heuristic search result because it
    306        // is the only one that changes with the search string.
    307        makeSearchResult(context, {
    308          engineName: SUGGESTIONS_ENGINE_NAME,
    309          heuristic: true,
    310        }),
    311        ...bookmarkResults,
    312      ],
    313    });
    314 
    315    context = createContext("aaa", { isPrivate: false });
    316    await check_results({
    317      context,
    318      matches: [
    319        makeSearchResult(context, {
    320          engineName: SUGGESTIONS_ENGINE_NAME,
    321          heuristic: true,
    322        }),
    323        ...bookmarkResults,
    324      ],
    325    });
    326 
    327    context = createContext("aaaa", { isPrivate: false });
    328    await check_results({
    329      context,
    330      matches: [
    331        makeSearchResult(context, {
    332          engineName: SUGGESTIONS_ENGINE_NAME,
    333          heuristic: true,
    334        }),
    335        ...bookmarkResults,
    336      ],
    337    });
    338 
    339    context = createContext("aaa", { isPrivate: false });
    340    await check_results({
    341      context,
    342      matches: [
    343        makeSearchResult(context, {
    344          engineName: SUGGESTIONS_ENGINE_NAME,
    345          heuristic: true,
    346        }),
    347        ...bookmarkResults,
    348      ],
    349    });
    350 
    351    context = createContext("aa", { isPrivate: false });
    352    await check_results({
    353      context,
    354      matches: [
    355        makeSearchResult(context, {
    356          engineName: SUGGESTIONS_ENGINE_NAME,
    357          heuristic: true,
    358        }),
    359        ...bookmarkResults,
    360      ],
    361    });
    362 
    363    context = createContext("a", { isPrivate: false });
    364    await check_results({
    365      context,
    366      matches: [
    367        makeSearchResult(context, {
    368          engineName: SUGGESTIONS_ENGINE_NAME,
    369          heuristic: true,
    370        }),
    371        ...bookmarkResults,
    372      ],
    373    });
    374  },
    375 ];
    376 
    377 add_task(async function test_frecency() {
    378  Services.prefs.setBoolPref("browser.urlbar.autoFill", false);
    379  // always search in history + bookmarks, no matter what the default is
    380  Services.prefs.setBoolPref("browser.urlbar.suggest.history", true);
    381  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", true);
    382  Services.prefs.setBoolPref("browser.urlbar.suggest.openpage", false);
    383  Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
    384  Services.prefs.setBoolPref("browser.urlbar.suggest.engines", false);
    385  Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);
    386  for (let test of tests) {
    387    await PlacesUtils.bookmarks.eraseEverything();
    388    await PlacesUtils.history.clear();
    389 
    390    await test();
    391  }
    392  for (let type of [
    393    "history",
    394    "bookmark",
    395    "openpage",
    396    "searches",
    397    "engines",
    398    "quickactions",
    399  ]) {
    400    Services.prefs.clearUserPref("browser.urlbar.suggest." + type);
    401    Services.prefs.clearUserPref("browser.urlbar.autoFill");
    402  }
    403 });