summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_hideSponsoredHistory.js
blob: d94a655b224725a23996e909796dfb6cdfb030d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// This tests the muxer functionality that hides URLs in history that were
// originally sponsored.

"use strict";

add_task(async function test() {
  // Disable search suggestions to avoid hitting the network.
  UrlbarPrefs.set("suggest.searches", false);

  let engine = await Services.search.getDefault();
  let pref = "browser.newtabpage.activity-stream.hideTopSitesWithSearchParam";

  // This maps URL search params to objects describing whether a URL with those
  // params is expected to appear in the search results. Each inner object maps
  // from a value of the pref to whether the URL is expected to appear given the
  // pref value.
  let tests = {
    "": {
      "": true,
      test: true,
      "test=": true,
      "test=hide": true,
      nomatch: true,
      "nomatch=": true,
      "nomatch=hide": true,
    },
    test: {
      "": true,
      test: false,
      "test=": false,
      "test=hide": true,
      nomatch: true,
      "nomatch=": true,
      "nomatch=hide": true,
    },
    "test=hide": {
      "": true,
      test: false,
      "test=": true,
      "test=hide": false,
      nomatch: true,
      "nomatch=": true,
      "nomatch=hide": true,
    },
    "test=foo&test=hide": {
      "": true,
      test: false,
      "test=": true,
      "test=hide": false,
      nomatch: true,
      "nomatch=": true,
      "nomatch=hide": true,
    },
  };

  for (let [urlParams, expected] of Object.entries(tests)) {
    for (let [prefValue, shouldAppear] of Object.entries(expected)) {
      info(
        "Running test: " +
          JSON.stringify({ urlParams, prefValue, shouldAppear })
      );

      // Add a visit to a URL with search params `urlParams`.
      let url = new URL("http://example.com/");
      url.search = urlParams;
      await PlacesTestUtils.addVisits(url);

      // Set the pref to `prefValue`.
      Services.prefs.setCharPref(pref, prefValue);

      // Set up the context and expected results. If `shouldAppear` is true, a
      // visit result for the URL should appear.
      let context = createContext("ample", { isPrivate: false });
      let expectedResults = [
        makeSearchResult(context, {
          heuristic: true,
          engineName: engine.name,
          engineIconUri: engine.iconURI?.spec,
        }),
      ];
      if (shouldAppear) {
        expectedResults.push(
          makeVisitResult(context, {
            uri: url.toString(),
            title: "test visit for " + url,
          })
        );
      }

      // Do a search and check the results.
      await check_results({
        context,
        matches: expectedResults,
      });

      await PlacesUtils.history.clear();
    }
  }

  Services.prefs.clearUserPref(pref);
});