tor-browser

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

test_hideSponsoredHistory.js (2889B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // This tests the muxer functionality that hides URLs in history that were
      5 // originally sponsored.
      6 
      7 "use strict";
      8 
      9 add_task(async function test() {
     10  // Disable search suggestions to avoid hitting the network.
     11  UrlbarPrefs.set("suggest.searches", false);
     12  UrlbarPrefs.set("suggest.quickactions", false);
     13 
     14  let engine = await Services.search.getDefault();
     15  let pref = "browser.newtabpage.activity-stream.hideTopSitesWithSearchParam";
     16 
     17  // This maps URL search params to objects describing whether a URL with those
     18  // params is expected to appear in the search results. Each inner object maps
     19  // from a value of the pref to whether the URL is expected to appear given the
     20  // pref value.
     21  let tests = {
     22    "": {
     23      "": true,
     24      test: true,
     25      "test=": true,
     26      "test=hide": true,
     27      nomatch: true,
     28      "nomatch=": true,
     29      "nomatch=hide": true,
     30    },
     31    test: {
     32      "": true,
     33      test: false,
     34      "test=": false,
     35      "test=hide": true,
     36      nomatch: true,
     37      "nomatch=": true,
     38      "nomatch=hide": true,
     39    },
     40    "test=hide": {
     41      "": true,
     42      test: false,
     43      "test=": true,
     44      "test=hide": false,
     45      nomatch: true,
     46      "nomatch=": true,
     47      "nomatch=hide": true,
     48    },
     49    "test=foo&test=hide": {
     50      "": true,
     51      test: false,
     52      "test=": true,
     53      "test=hide": false,
     54      nomatch: true,
     55      "nomatch=": true,
     56      "nomatch=hide": true,
     57    },
     58  };
     59 
     60  for (let [urlParams, expected] of Object.entries(tests)) {
     61    for (let [prefValue, shouldAppear] of Object.entries(expected)) {
     62      info(
     63        "Running test: " +
     64          JSON.stringify({ urlParams, prefValue, shouldAppear })
     65      );
     66 
     67      // Add a visit to a URL with search params `urlParams`.
     68      let url = new URL("http://example.com/");
     69      url.search = urlParams;
     70      await PlacesTestUtils.addVisits(url);
     71 
     72      // Set the pref to `prefValue`.
     73      Services.prefs.setCharPref(pref, prefValue);
     74 
     75      // Set up the context and expected results. If `shouldAppear` is true, a
     76      // visit result for the URL should appear.
     77      let context = createContext("ample", { isPrivate: false });
     78      let expectedResults = [
     79        makeSearchResult(context, {
     80          heuristic: true,
     81          engineName: engine.name,
     82          engineIconUri: await engine.getIconURL(),
     83        }),
     84      ];
     85      if (shouldAppear) {
     86        expectedResults.push(
     87          makeVisitResult(context, {
     88            uri: url.toString(),
     89            title: "test visit for " + url,
     90          })
     91        );
     92      }
     93 
     94      // Do a search and check the results.
     95      await check_results({
     96        context,
     97        matches: expectedResults,
     98      });
     99 
    100      await PlacesUtils.history.clear();
    101    }
    102  }
    103 
    104  Services.prefs.clearUserPref(pref);
    105 });