summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_trendingsuggestions.js
blob: 81285b7f6f7cf078b787251a981eb098581febd1 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const { SearchTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SearchTestUtils.sys.mjs"
);

const MAIN_PREF = "browser.search.suggest.enabled";
const URLBAR_PREF = "browser.urlbar.suggest.searches";
const TRENDING_PREF = "browser.urlbar.trending.featureGate";

const TRENDING_CHECKBOX_ID = "showTrendingSuggestions";
const SUGGESTIONED_CHECKBOX_ID = "suggestionsInSearchFieldsCheckbox";

SearchTestUtils.init(this);

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      [MAIN_PREF, true],
      [URLBAR_PREF, true],
      [TRENDING_PREF, true],
    ],
  });

  await SearchTestUtils.installSearchExtension({
    name: "engine1",
    search_url: "https://example.com/engine1",
    search_url_get_params: "search={searchTerms}",
  });
  const defaultEngine = await Services.search.getDefault();

  registerCleanupFunction(async () => {
    await Services.search.setDefault(
      defaultEngine,
      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
    );
  });
});

add_task(async function testSuggestionsDisabled() {
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;
  let trendingCheckbox = doc.getElementById(TRENDING_CHECKBOX_ID);
  let suggestionsCheckbox = doc.getElementById(SUGGESTIONED_CHECKBOX_ID);

  Assert.ok(trendingCheckbox.checked, "Checkbox should be visible and checked");
  Assert.ok(!trendingCheckbox.disabled, "Checkbox should not be disabled");

  // Disable search suggestions.
  suggestionsCheckbox.checked = false;
  suggestionsCheckbox.doCommand();

  await BrowserTestUtils.waitForCondition(
    () => trendingCheckbox.disabled,
    "Trending checkbox should be disabled when search suggestions are disabled"
  );

  // Clean up.
  Services.prefs.clearUserPref(MAIN_PREF);
  gBrowser.removeCurrentTab();
});

add_task(async function testNonTrendingEngine() {
  // Set engine that does not support trending suggestions as default.
  const engine1 = Services.search.getEngineByName("engine1");
  Services.search.setDefault(
    engine1,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;
  let trendingCheckbox = doc.getElementById(TRENDING_CHECKBOX_ID);

  Assert.ok(
    trendingCheckbox.disabled,
    "Checkbox should be disabled when an engine that doesnt support trending suggestions is default"
  );
  gBrowser.removeCurrentTab();
});

add_task(async function testEnabledTrendingEngine() {
  const engine1 = Services.search.getEngineByName("Google");
  Services.search.setDefault(
    engine1,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;
  let trendingCheckbox = doc.getElementById(TRENDING_CHECKBOX_ID);

  Assert.ok(
    !trendingCheckbox.disabled,
    "Checkbox should not be disabled when an engine that supports trending suggestions is default"
  );
  gBrowser.removeCurrentTab();
});