tor-browser

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

browser_searchsuggestions.js (7096B)


      1 const { CustomizableUITestUtils } = ChromeUtils.importESModule(
      2  "resource://testing-common/CustomizableUITestUtils.sys.mjs"
      3 );
      4 let gCUITestUtils = new CustomizableUITestUtils(window);
      5 
      6 const SUGGEST_PREF_NAME = "browser.search.suggest.enabled";
      7 const URLBAR_SUGGEST_PREF_NAME = "browser.urlbar.suggest.searches";
      8 const PRIVATE_PREF_NAME = "browser.search.suggest.enabled.private";
      9 
     10 let initialUrlbarSuggestValue;
     11 let initialSuggestionsInPrivateValue;
     12 
     13 add_setup(async function () {
     14  const originalSuggest = Services.prefs.getBoolPref(SUGGEST_PREF_NAME);
     15  initialUrlbarSuggestValue = Services.prefs.getBoolPref(
     16    URLBAR_SUGGEST_PREF_NAME
     17  );
     18  initialSuggestionsInPrivateValue =
     19    Services.prefs.getBoolPref(PRIVATE_PREF_NAME);
     20 
     21  registerCleanupFunction(() => {
     22    Services.prefs.setBoolPref(SUGGEST_PREF_NAME, originalSuggest);
     23    Services.prefs.setBoolPref(
     24      URLBAR_SUGGEST_PREF_NAME,
     25      initialUrlbarSuggestValue
     26    );
     27    Services.prefs.setBoolPref(
     28      PRIVATE_PREF_NAME,
     29      initialSuggestionsInPrivateValue
     30    );
     31  });
     32 });
     33 
     34 async function toggleElement(
     35  id,
     36  prefName,
     37  element,
     38  initialCheckboxValue,
     39  initialPrefValue,
     40  descCheckbox,
     41  descPref,
     42  shouldUpdatePref = false
     43 ) {
     44  element.click();
     45 
     46  is(
     47    element.checked,
     48    !initialCheckboxValue,
     49    `Should have flipped the ${descCheckbox} checkbox`
     50  );
     51  let expectedPrefValue = shouldUpdatePref
     52    ? !initialPrefValue
     53    : initialPrefValue;
     54  let prefValue = Services.prefs.getBoolPref(prefName);
     55 
     56  is(
     57    prefValue,
     58    expectedPrefValue,
     59    `Should ${
     60      shouldUpdatePref ? "" : "not"
     61    } have updated the ${descPref} preference value`
     62  );
     63 
     64  element.click();
     65 
     66  is(
     67    element.checked,
     68    initialCheckboxValue,
     69    `Should have flipped the ${descCheckbox} checkbox back to the original value`
     70  );
     71  prefValue = Services.prefs.getBoolPref(prefName);
     72  expectedPrefValue = shouldUpdatePref ? !expectedPrefValue : initialPrefValue;
     73  is(
     74    prefValue,
     75    expectedPrefValue,
     76    `Should ${
     77      shouldUpdatePref ? "" : "not"
     78    } have updated the ${descPref} preference value`
     79  );
     80 }
     81 
     82 // Open with suggestions enabled
     83 add_task(async function test_suggestions_start_enabled() {
     84  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, true);
     85  await gCUITestUtils.addSearchBar();
     86 
     87  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     88 
     89  let doc = gBrowser.selectedBrowser.contentDocument;
     90  let urlbarBox = doc.getElementById("urlBarSuggestionCheckbox");
     91  let privateBox = doc.getElementById(
     92    "showSearchSuggestionsPrivateWindowsCheckbox"
     93  );
     94  ok(!urlbarBox.disabled, "Should have enabled the urlbar checkbox");
     95  ok(
     96    !privateBox.disabled,
     97    "Should have enabled the private mode suggestions checkbox"
     98  );
     99  is(
    100    urlbarBox.checked,
    101    initialUrlbarSuggestValue,
    102    "Should have the correct value for the urlbar checkbox"
    103  );
    104  is(
    105    privateBox.checked,
    106    initialSuggestionsInPrivateValue,
    107    "Should have the correct value for the private mode suggestions checkbox"
    108  );
    109 
    110  await toggleElement(
    111    "urlBarSuggestion",
    112    URLBAR_SUGGEST_PREF_NAME,
    113    urlbarBox,
    114    initialUrlbarSuggestValue,
    115    initialUrlbarSuggestValue,
    116    "urlbar",
    117    "urlbar",
    118    true
    119  );
    120 
    121  await toggleElement(
    122    "showSearchSuggestionsPrivateWindows",
    123    PRIVATE_PREF_NAME,
    124    privateBox,
    125    initialSuggestionsInPrivateValue,
    126    initialSuggestionsInPrivateValue,
    127    "private suggestion",
    128    "private suggestion",
    129    true
    130  );
    131 
    132  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, false);
    133  await urlbarBox.updateComplete;
    134  ok(!urlbarBox.checked, "Should have unchecked the urlbar box");
    135  ok(urlbarBox.disabled, "Should have disabled the urlbar box");
    136  gCUITestUtils.removeSearchBar();
    137  ok(!urlbarBox.visible, "Should have hidden the urlbar box");
    138  ok(!privateBox.checked, "Should have unchecked the private suggestions box");
    139  ok(privateBox.disabled, "Should have disabled the private suggestions box");
    140 
    141  gBrowser.removeCurrentTab();
    142 });
    143 
    144 // Open with suggestions disabled
    145 add_task(async function test_suggestions_start_disabled() {
    146  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, false);
    147 
    148  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    149 
    150  let doc = gBrowser.selectedBrowser.contentDocument;
    151  let urlbarBox = doc.getElementById("urlBarSuggestionCheckbox");
    152  ok(urlbarBox.disabled, "Should have the urlbar box disabled");
    153  let privateBox = doc.getElementById(
    154    "showSearchSuggestionsPrivateWindowsCheckbox"
    155  );
    156  ok(privateBox.disabled, "Should have the private suggestions box disabled");
    157 
    158  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, true);
    159  await urlbarBox.updateComplete;
    160 
    161  ok(!urlbarBox.disabled, "Should have enabled the urlbar box");
    162  ok(!privateBox.disabled, "Should have enabled the private suggestions box");
    163 
    164  gBrowser.removeCurrentTab();
    165 });
    166 
    167 add_task(async function test_sync_search_suggestions_prefs() {
    168  info("Adding the search bar to the toolbar");
    169  await gCUITestUtils.addSearchBar();
    170  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, true);
    171  Services.prefs.setBoolPref(URLBAR_SUGGEST_PREF_NAME, false);
    172  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    173 
    174  let doc = gBrowser.selectedBrowser.contentDocument;
    175  let suggestionsInSearchFieldsCheckbox = doc.getElementById(
    176    "suggestionsInSearchFieldsCheckbox"
    177  );
    178 
    179  is(
    180    suggestionsInSearchFieldsCheckbox.checked,
    181    true,
    182    "Should have the correct value for the search suggestions checkbox"
    183  );
    184 
    185  is(
    186    Services.prefs.getBoolPref(SUGGEST_PREF_NAME),
    187    true,
    188    `${SUGGEST_PREF_NAME} should be enabled`
    189  );
    190  is(
    191    Services.prefs.getBoolPref(URLBAR_SUGGEST_PREF_NAME),
    192    false,
    193    `${URLBAR_SUGGEST_PREF_NAME} should be disabled`
    194  );
    195 
    196  await toggleElement(
    197    "suggestionsInSearchFieldsCheckbox",
    198    URLBAR_SUGGEST_PREF_NAME,
    199    suggestionsInSearchFieldsCheckbox,
    200    suggestionsInSearchFieldsCheckbox.checked,
    201    false,
    202    "search suggestion",
    203    "urlbar suggest"
    204  );
    205 
    206  info("Removing the search bar from the toolbar");
    207  gCUITestUtils.removeSearchBar();
    208 
    209  const suggestsPref = [true, false];
    210  const urlbarSuggestsPref = [true, false];
    211 
    212  for (let suggestState of suggestsPref) {
    213    for (let urlbarSuggestsState of urlbarSuggestsPref) {
    214      Services.prefs.setBoolPref(SUGGEST_PREF_NAME, suggestState);
    215      Services.prefs.setBoolPref(URLBAR_SUGGEST_PREF_NAME, urlbarSuggestsState);
    216      await suggestionsInSearchFieldsCheckbox.updateComplete;
    217 
    218      if (suggestState && urlbarSuggestsState) {
    219        ok(
    220          suggestionsInSearchFieldsCheckbox.checked,
    221          "Should have checked the suggestions checkbox"
    222        );
    223      } else {
    224        ok(
    225          !suggestionsInSearchFieldsCheckbox.checked,
    226          "Should have unchecked the suggestions checkbox"
    227        );
    228      }
    229      ok(
    230        !suggestionsInSearchFieldsCheckbox.disabled,
    231        "Should have the suggestions checkbox enabled"
    232      );
    233    }
    234  }
    235 
    236  gBrowser.removeCurrentTab();
    237  Services.prefs.clearUserPref(URLBAR_SUGGEST_PREF_NAME);
    238  Services.prefs.clearUserPref(SUGGEST_PREF_NAME);
    239 });