tor-browser

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

test_providerPlaces.js (8476B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // This is a simple test to check the Places provider works, it is not
      7 // intended to check all the edge cases, because that component is already
      8 // covered by a good amount of tests.
      9 
     10 const SUGGEST_PREF = "browser.urlbar.suggest.searches";
     11 const SUGGEST_ENABLED_PREF = "browser.search.suggest.enabled";
     12 const QUICKACTIONS_PREF = "browser.urlbar.suggest.quickactions";
     13 
     14 add_task(async function test_places() {
     15  Services.prefs.setBoolPref(SUGGEST_PREF, true);
     16  Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, true);
     17  Services.prefs.setBoolPref(QUICKACTIONS_PREF, false);
     18  let engine = await addTestSuggestionsEngine();
     19  await Services.search.setDefault(
     20    engine,
     21    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
     22  );
     23  let oldCurrentEngine = Services.search.defaultEngine;
     24  registerCleanupFunction(async () => {
     25    Services.prefs.clearUserPref(SUGGEST_PREF);
     26    Services.prefs.clearUserPref(SUGGEST_ENABLED_PREF);
     27    Services.prefs.clearUserPref(QUICKACTIONS_PREF);
     28    await Services.search.setDefault(
     29      oldCurrentEngine,
     30      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
     31    );
     32  });
     33 
     34  let controller = UrlbarTestUtils.newMockController();
     35  // Also check case insensitivity.
     36  let searchString = "MoZ oRg";
     37  let tabGroupId = "1234567890-1";
     38  let context = createContext(searchString, { isPrivate: false });
     39 
     40  // Add entries from multiple sources.
     41  await PlacesUtils.bookmarks.insert({
     42    url: "https://bookmark.mozilla.org/",
     43    title: "Test bookmark",
     44    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
     45  });
     46  PlacesUtils.tagging.tagURI(
     47    Services.io.newURI("https://bookmark.mozilla.org/"),
     48    ["mozilla", "org", "ham", "moz", "bacon"]
     49  );
     50  await PlacesTestUtils.addVisits([
     51    { uri: "https://history.mozilla.org/", title: "Test history" },
     52    { uri: "https://tab.mozilla.org/", title: "Test tab" },
     53    { uri: "https://tabingroup.mozilla.org/", title: "Test tab in group" },
     54  ]);
     55  UrlbarProviderOpenTabs.registerOpenTab(
     56    "https://tab.mozilla.org/",
     57    0,
     58    null,
     59    false
     60  );
     61  UrlbarProviderOpenTabs.registerOpenTab(
     62    "https://tabingroup.mozilla.org/",
     63    0,
     64    tabGroupId,
     65    false
     66  );
     67  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
     68 
     69  await controller.startQuery(context);
     70 
     71  info("Results:\n" + context.results.map(m => m.payload.url).join("\n"));
     72  Assert.equal(
     73    context.results.length,
     74    7,
     75    "Found the expected number of matches"
     76  );
     77 
     78  Assert.deepEqual(
     79    [
     80      UrlbarUtils.RESULT_TYPE.SEARCH,
     81      UrlbarUtils.RESULT_TYPE.SEARCH,
     82      UrlbarUtils.RESULT_TYPE.SEARCH,
     83      UrlbarUtils.RESULT_TYPE.URL,
     84      UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
     85      UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
     86      UrlbarUtils.RESULT_TYPE.URL,
     87    ],
     88    context.results.map(m => m.type),
     89    "Check result types"
     90  );
     91 
     92  Assert.deepEqual(
     93    [
     94      searchString,
     95      searchString + " foo",
     96      searchString + " bar",
     97      "Test bookmark",
     98      "Test tab in group",
     99      "Test tab",
    100      "Test history",
    101    ],
    102    context.results.map(m => m.getDisplayableValueAndHighlights("title").value),
    103    "Check match titles"
    104  );
    105 
    106  Assert.deepEqual(
    107    context.results[3].payload.tags,
    108    ["moz", "mozilla", "org"],
    109    "Check tags"
    110  );
    111 
    112  Assert.equal(
    113    context.results[4].payload.tabGroup,
    114    tabGroupId,
    115    "Check tab group result for tab in group"
    116  );
    117 
    118  Assert.equal(
    119    context.results[5].payload.tabGroup,
    120    null,
    121    "Check tab group result for tab not in group"
    122  );
    123 
    124  await PlacesUtils.history.clear();
    125  await PlacesUtils.bookmarks.eraseEverything();
    126  UrlbarProviderOpenTabs.unregisterOpenTab(
    127    "https://tab.mozilla.org/",
    128    0,
    129    null,
    130    false
    131  );
    132  UrlbarProviderOpenTabs.unregisterOpenTab(
    133    "https://tabingroup.mozilla.org/",
    134    0,
    135    tabGroupId,
    136    false
    137  );
    138 });
    139 
    140 add_task(async function test_bookmarkBehaviorDisabled_tagged() {
    141  Services.prefs.setBoolPref(SUGGEST_PREF, false);
    142  Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, false);
    143 
    144  // Disable the bookmark behavior.
    145  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", false);
    146 
    147  let controller = UrlbarTestUtils.newMockController();
    148  // Also check case insensitivity.
    149  let searchString = "MoZ oRg";
    150  let context = createContext(searchString, { isPrivate: false });
    151 
    152  // Add a tagged bookmark that's also visited.
    153  await PlacesUtils.bookmarks.insert({
    154    url: "https://bookmark.mozilla.org/",
    155    title: "Test bookmark",
    156    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    157  });
    158  PlacesUtils.tagging.tagURI(
    159    Services.io.newURI("https://bookmark.mozilla.org/"),
    160    ["mozilla", "org", "ham", "moz", "bacon"]
    161  );
    162  await PlacesTestUtils.addVisits("https://bookmark.mozilla.org/");
    163  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
    164 
    165  await controller.startQuery(context);
    166 
    167  info("Results:\n" + context.results.map(m => m.payload.url).join("\n"));
    168  Assert.equal(
    169    context.results.length,
    170    2,
    171    "Found the expected number of matches"
    172  );
    173 
    174  Assert.deepEqual(
    175    [UrlbarUtils.RESULT_TYPE.SEARCH, UrlbarUtils.RESULT_TYPE.URL],
    176    context.results.map(m => m.type),
    177    "Check result types"
    178  );
    179 
    180  Assert.deepEqual(
    181    [searchString, "Test bookmark"],
    182    context.results.map(m => m.getDisplayableValueAndHighlights("title").value),
    183    "Check match titles"
    184  );
    185 
    186  Assert.deepEqual(context.results[1].payload.tags, [], "Check tags");
    187 
    188  await PlacesUtils.history.clear();
    189  await PlacesUtils.bookmarks.eraseEverything();
    190 });
    191 
    192 add_task(async function test_bookmarkBehaviorDisabled_untagged() {
    193  Services.prefs.setBoolPref(SUGGEST_PREF, false);
    194  Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, false);
    195 
    196  // Disable the bookmark behavior.
    197  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", false);
    198 
    199  let controller = UrlbarTestUtils.newMockController();
    200  // Also check case insensitivity.
    201  let searchString = "MoZ oRg";
    202  let context = createContext(searchString, { isPrivate: false });
    203 
    204  // Add an *untagged* bookmark that's also visited.
    205  await PlacesUtils.bookmarks.insert({
    206    url: "https://bookmark.mozilla.org/",
    207    title: "Test bookmark",
    208    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    209  });
    210  await PlacesTestUtils.addVisits("https://bookmark.mozilla.org/");
    211  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
    212 
    213  await controller.startQuery(context);
    214 
    215  info("Results:\n" + context.results.map(m => m.payload.url).join("\n"));
    216  Assert.equal(
    217    context.results.length,
    218    2,
    219    "Found the expected number of matches"
    220  );
    221 
    222  Assert.deepEqual(
    223    [UrlbarUtils.RESULT_TYPE.SEARCH, UrlbarUtils.RESULT_TYPE.URL],
    224    context.results.map(m => m.type),
    225    "Check result types"
    226  );
    227 
    228  Assert.deepEqual(
    229    [searchString, "Test bookmark"],
    230    context.results.map(m => m.getDisplayableValueAndHighlights("title").value),
    231    "Check match titles"
    232  );
    233 
    234  Assert.deepEqual(context.results[1].payload.tags, [], "Check tags");
    235 
    236  await PlacesUtils.history.clear();
    237  await PlacesUtils.bookmarks.eraseEverything();
    238 });
    239 
    240 add_task(async function test_diacritics() {
    241  Services.prefs.setBoolPref(SUGGEST_PREF, false);
    242  Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, false);
    243 
    244  // Enable the bookmark behavior.
    245  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", true);
    246 
    247  let controller = UrlbarTestUtils.newMockController();
    248  let searchString = "agui";
    249  let context = createContext(searchString, { isPrivate: false });
    250 
    251  await PlacesUtils.bookmarks.insert({
    252    url: "https://bookmark.mozilla.org/%C3%A3g%CC%83u%C4%A9",
    253    title: "Test bookmark with accents in path",
    254    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    255  });
    256  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
    257 
    258  await controller.startQuery(context);
    259 
    260  info("Results:\n" + context.results.map(m => m.payload.url).join("\n"));
    261  Assert.equal(
    262    context.results.length,
    263    2,
    264    "Found the expected number of matches"
    265  );
    266 
    267  Assert.deepEqual(
    268    [UrlbarUtils.RESULT_TYPE.SEARCH, UrlbarUtils.RESULT_TYPE.URL],
    269    context.results.map(m => m.type),
    270    "Check result types"
    271  );
    272 
    273  Assert.deepEqual(
    274    [searchString, "Test bookmark with accents in path"],
    275    context.results.map(m => m.getDisplayableValueAndHighlights("title").value),
    276    "Check match titles"
    277  );
    278 
    279  await PlacesUtils.history.clear();
    280  await PlacesUtils.bookmarks.eraseEverything();
    281 });