tor-browser

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

test_autofill_bookmarked.js (5759B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This is a specific autofill test to ensure we pick the correct bookmarked
      6 // state of an origin. Regardless of the order of origins, we should always pick
      7 // the correct bookmarked status.
      8 
      9 add_task(async function () {
     10  registerCleanupFunction(async () => {
     11    Services.prefs.clearUserPref("browser.urlbar.suggest.searches");
     12    Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
     13  });
     14  Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
     15  Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);
     16 
     17  let host = "example.com";
     18  // Add a bookmark to the http version, but ensure the https version has an
     19  // higher frecency.
     20  let bookmark = await PlacesUtils.bookmarks.insert({
     21    url: `http://${host}`,
     22    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
     23  });
     24  // Add one visit to http to give it a realistic origin frecency score instead
     25  // of the default value of 1. This ensures the threshold test doesn't use an
     26  // artificially low baseline.
     27  await PlacesTestUtils.addVisits({
     28    uri: `http://${host}`,
     29    visitDate: daysAgo(90),
     30  });
     31 
     32  await PlacesTestUtils.addVisits({
     33    uri: `https://${host}`,
     34    visitDate: daysAgo(30),
     35  });
     36 
     37  await PlacesTestUtils.addVisits({
     38    uri: `https://fakedomain1.com/`,
     39  });
     40  await PlacesTestUtils.addVisits({
     41    uri: `https://fakedomain2.com/`,
     42  });
     43  await PlacesTestUtils.addVisits({
     44    url: `https://not-${host}/`,
     45    transition: PlacesUtils.history.TRANSITION_TYPED,
     46  });
     47 
     48  async function check_autofill() {
     49    await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
     50    let threshold = await getOriginAutofillThreshold();
     51    let httpOriginFrecency = await getOriginFrecency("http://", host);
     52    Assert.less(
     53      httpOriginFrecency,
     54      threshold,
     55      "Http origin frecency should be below the threshold"
     56    );
     57    let httpsOriginFrecency = await getOriginFrecency("https://", host);
     58    Assert.less(
     59      httpsOriginFrecency,
     60      threshold,
     61      "Https origin frecency should be below the threshold"
     62    );
     63    Assert.less(
     64      httpOriginFrecency,
     65      httpsOriginFrecency,
     66      "Http origin frecency should be below the https origin frecency"
     67    );
     68    let not = await getOriginFrecency("https://", "not-example.com");
     69    Assert.less(
     70      httpsOriginFrecency,
     71      not,
     72      "Http origin frecency should be below not example.com"
     73    );
     74 
     75    // The http version should be filled because it's bookmarked, but with the
     76    // https prefix that is more frecent.
     77    let context = createContext("ex", { isPrivate: false });
     78    await check_results({
     79      context,
     80      autofilled: `${host}/`,
     81      completed: `https://${host}/`,
     82      matches: [
     83        makeVisitResult(context, {
     84          uri: `https://${host}/`,
     85          title: `test visit for https://${host}/`,
     86          heuristic: true,
     87        }),
     88        makeVisitResult(context, {
     89          uri: `https://not-${host}/`,
     90          title: `test visit for https://not-${host}/`,
     91        }),
     92      ],
     93    });
     94  }
     95 
     96  await check_autofill();
     97 
     98  // Now remove the bookmark, ensure to remove the orphans, then reinsert the
     99  // bookmark; thus we physically invert the order of the rows in the table.
    100  await checkOriginsOrder(host, ["http://", "https://"]);
    101  await PlacesUtils.bookmarks.remove(bookmark);
    102  await PlacesUtils.withConnectionWrapper("removeOrphans", async db => {
    103    // Delete the visit.
    104    await db.execute(
    105      `
    106      DELETE FROM moz_historyvisits
    107      WHERE place_id = (SELECT id FROM moz_places WHERE url = :url)`,
    108      { url: `http://${host}/` }
    109    );
    110 
    111    await db.execute(`DELETE FROM moz_places WHERE url = :url`, {
    112      url: `http://${host}/`,
    113    });
    114 
    115    await db.execute(
    116      `DELETE FROM moz_origins WHERE prefix = "http://" AND host = :host`,
    117      { host }
    118    );
    119  });
    120  bookmark = await PlacesUtils.bookmarks.insert({
    121    url: `http://${host}`,
    122    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    123  });
    124 
    125  await checkOriginsOrder(host, ["https://", "http://"]);
    126 
    127  await check_autofill();
    128  await PlacesUtils.history.clear();
    129  await PlacesUtils.bookmarks.remove(bookmark);
    130 });
    131 
    132 add_task(async function test_www() {
    133  // Add a bookmark to the www version
    134  let host = "example.com";
    135  await PlacesUtils.bookmarks.insert({
    136    url: `http://www.${host}`,
    137    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    138  });
    139 
    140  info("search for start of www.");
    141  let context = createContext("w", { isPrivate: false });
    142  await check_results({
    143    context,
    144    autofilled: `www.${host}/`,
    145    completed: `http://www.${host}/`,
    146    matches: [
    147      makeVisitResult(context, {
    148        uri: `http://www.${host}/`,
    149        title: UrlbarTestUtils.trimURL(`http://www.${host}`),
    150        heuristic: true,
    151      }),
    152    ],
    153  });
    154  info("search for full www.");
    155  context = createContext("www.", { isPrivate: false });
    156  await check_results({
    157    context,
    158    autofilled: `www.${host}/`,
    159    completed: `http://www.${host}/`,
    160    matches: [
    161      makeVisitResult(context, {
    162        uri: `http://www.${host}/`,
    163        title: UrlbarTestUtils.trimURL(`http://www.${host}`),
    164        heuristic: true,
    165      }),
    166    ],
    167  });
    168  info("search for host without www.");
    169  context = createContext("ex", { isPrivate: false });
    170  await check_results({
    171    context,
    172    autofilled: `${host}/`,
    173    completed: `http://www.${host}/`,
    174    matches: [
    175      makeVisitResult(context, {
    176        uri: `http://www.${host}/`,
    177        title: UrlbarTestUtils.trimURL(`http://www.${host}`),
    178        heuristic: true,
    179      }),
    180    ],
    181  });
    182 });