tor-browser

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

test_UrlbarQueryContext_restrictSource.js (3198B)


      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 restrictions set through UrlbarQueryContext.sources.
      7 */
      8 
      9 testEngine_setup();
     10 
     11 add_task(async function test_restrictions() {
     12  await PlacesTestUtils.addVisits([
     13    { uri: "http://history.com/", title: "match" },
     14  ]);
     15  await PlacesUtils.bookmarks.insert({
     16    url: "http://bookmark.com/",
     17    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
     18    title: "match",
     19  });
     20  await UrlbarProviderOpenTabs.registerOpenTab(
     21    "http://openpagematch.com/",
     22    0,
     23    null,
     24    false
     25  );
     26 
     27  info("Bookmark restrict");
     28  let results = await get_results({
     29    sources: [UrlbarUtils.RESULT_SOURCE.BOOKMARKS],
     30    searchString: "match",
     31  });
     32  // Skip the heuristic result.
     33  Assert.deepEqual(
     34    results.filter(r => !r.heuristic).map(r => r.payload.url),
     35    ["http://bookmark.com/"]
     36  );
     37 
     38  info("History restrict");
     39  results = await get_results({
     40    sources: [UrlbarUtils.RESULT_SOURCE.HISTORY],
     41    searchString: "match",
     42  });
     43  // Skip the heuristic result.
     44  Assert.deepEqual(
     45    results.filter(r => !r.heuristic).map(r => r.payload.url),
     46    ["http://history.com/"]
     47  );
     48 
     49  info("tabs restrict");
     50  results = await get_results({
     51    sources: [UrlbarUtils.RESULT_SOURCE.TABS],
     52    searchString: "match",
     53  });
     54  // Skip the heuristic result.
     55  Assert.deepEqual(
     56    results.filter(r => !r.heuristic).map(r => r.payload.url),
     57    ["http://openpagematch.com/"]
     58  );
     59 
     60  info("search restrict");
     61  results = await get_results({
     62    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
     63    searchString: "match",
     64  });
     65  Assert.ok(
     66    !results.some(r => r.payload.engine != SUGGESTIONS_ENGINE_NAME),
     67    "All the results should be search results"
     68  );
     69 
     70  info("search restrict should ignore restriction token");
     71  results = await get_results({
     72    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
     73    searchString: `${UrlbarTokenizer.RESTRICT.BOOKMARKS} match`,
     74  });
     75  Assert.ok(
     76    !results.some(r => r.payload.engine != SUGGESTIONS_ENGINE_NAME),
     77    "All the results should be search results"
     78  );
     79  Assert.equal(
     80    results[0].payload.query,
     81    `${UrlbarTokenizer.RESTRICT.BOOKMARKS} match`,
     82    "The restriction token should be ignored and not stripped"
     83  );
     84 
     85  info("search restrict with other engine");
     86  results = await get_results({
     87    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
     88    searchString: "match",
     89    engineName: "Test",
     90  });
     91  Assert.ok(
     92    !results.some(r => r.payload.engine != "Test"),
     93    "All the results should be search results from the Test engine"
     94  );
     95 });
     96 
     97 async function get_results(test) {
     98  let controller = UrlbarTestUtils.newMockController();
     99  let options = {
    100    allowAutofill: false,
    101    isPrivate: false,
    102    maxResults: 10,
    103    sources: test.sources,
    104  };
    105  if (test.engineName) {
    106    options.searchMode = {
    107      source: UrlbarUtils.RESULT_SOURCE.SEARCH,
    108      engineName: test.engineName,
    109    };
    110  }
    111  let queryContext = createContext(test.searchString, options);
    112  await controller.startQuery(queryContext);
    113  return queryContext.results;
    114 }