tor-browser

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

browser_searchRestoreDefaults.js (8715B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { SearchTestUtils } = ChromeUtils.importESModule(
      5  "resource://testing-common/SearchTestUtils.sys.mjs"
      6 );
      7 const { SearchUtils } = ChromeUtils.importESModule(
      8  "moz-src:///toolkit/components/search/SearchUtils.sys.mjs"
      9 );
     10 
     11 add_setup(async function () {
     12  await SpecialPowers.pushPrefEnv({
     13    set: [["browser.urlbar.scotchBonnet.enableOverride", true]],
     14  });
     15 });
     16 
     17 add_task(async function test_restore_functionality() {
     18  // Ensure no engines are hidden to begin with.
     19  for (let engine of await Services.search.getAppProvidedEngines()) {
     20    if (engine.hidden) {
     21      engine.hidden = false;
     22    }
     23  }
     24 
     25  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     26 
     27  let doc = gBrowser.selectedBrowser.contentDocument;
     28  let restoreDefaultsButton = doc.getElementById("restoreDefaultSearchEngines");
     29 
     30  Assert.ok(
     31    restoreDefaultsButton.disabled,
     32    "Should have disabled the restore default search engines button on open"
     33  );
     34 
     35  let updatedPromise = SearchTestUtils.promiseSearchNotification(
     36    SearchUtils.MODIFIED_TYPE.CHANGED,
     37    SearchUtils.TOPIC_ENGINE_MODIFIED
     38  );
     39 
     40  let tree = doc.querySelector("#engineList");
     41  // Check for default search engines to be displayed in the engineList
     42  let defaultEngines = await Services.search.getAppProvidedEngines();
     43  for (let i = 0; i < defaultEngines.length; i++) {
     44    let cellName = tree.view.getCellText(
     45      i,
     46      tree.columns.getNamedColumn("engineName")
     47    );
     48    if (cellName == "DuckDuckGo") {
     49      tree.view.selection.select(i);
     50      break;
     51    }
     52  }
     53  doc.getElementById("removeEngineButton").click();
     54  await updatedPromise;
     55 
     56  let engine = await Services.search.getEngineByName("DuckDuckGo");
     57 
     58  Assert.ok(engine.hidden, "Should have hidden the engine");
     59  Assert.ok(
     60    !restoreDefaultsButton.disabled,
     61    "Should have enabled the restore default search engines button"
     62  );
     63 
     64  updatedPromise = SearchTestUtils.promiseSearchNotification(
     65    SearchUtils.MODIFIED_TYPE.CHANGED,
     66    SearchUtils.TOPIC_ENGINE_MODIFIED
     67  );
     68  restoreDefaultsButton.click();
     69  await updatedPromise;
     70  // Let the stack unwind so that the restore defaults button can update its
     71  // state.
     72  await new Promise(resolve => Services.tm.dispatchToMainThread(resolve));
     73 
     74  Assert.ok(!engine.hidden, "Should have re-enabled the disabled engine");
     75  Assert.ok(
     76    restoreDefaultsButton.disabled,
     77    "Should have disabled the restore default search engines button after use"
     78  );
     79 
     80  gBrowser.removeCurrentTab();
     81 });
     82 
     83 add_task(async function test_restoreEnabledOnOpenWithEngineHidden() {
     84  let engine = await Services.search.getEngineByName("DuckDuckGo");
     85  engine.hidden = true;
     86 
     87  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     88 
     89  let doc = gBrowser.selectedBrowser.contentDocument;
     90  let restoreDefaultsButton = doc.getElementById("restoreDefaultSearchEngines");
     91 
     92  Assert.ok(
     93    !restoreDefaultsButton.disabled,
     94    "Should have enabled the restore default search engines button on open"
     95  );
     96 
     97  let updatedPromise = SearchTestUtils.promiseSearchNotification(
     98    SearchUtils.MODIFIED_TYPE.CHANGED,
     99    SearchUtils.TOPIC_ENGINE_MODIFIED
    100  );
    101  restoreDefaultsButton.click();
    102  await updatedPromise;
    103  // Let the stack unwind so that the restore defaults button can update its
    104  // state.
    105  await new Promise(resolve => Services.tm.dispatchToMainThread(resolve));
    106 
    107  Assert.ok(!engine.hidden, "Should have re-enabled the disabled engine");
    108  Assert.ok(
    109    restoreDefaultsButton.disabled,
    110    "Should have disabled the restore default search engines button after use"
    111  );
    112 
    113  gBrowser.removeCurrentTab();
    114 });
    115 
    116 // This removes the last two engines and then the remaining engines from top to
    117 // bottom, and then it restores the default engines.  See bug 1681818.
    118 add_task(async function test_removeOutOfOrder() {
    119  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    120 
    121  let doc = gBrowser.selectedBrowser.contentDocument;
    122  let restoreDefaultsButton = doc.getElementById("restoreDefaultSearchEngines");
    123  Assert.ok(
    124    restoreDefaultsButton.disabled,
    125    "The restore-defaults button is disabled initially"
    126  );
    127 
    128  let tree = doc.querySelector("#engineList");
    129  let removeEngineButton = doc.getElementById("removeEngineButton");
    130  removeEngineButton.scrollIntoView();
    131 
    132  let defaultEngines = await Services.search.getAppProvidedEngines();
    133 
    134  // Remove the last two engines.  After each removal, the selection should move
    135  // to the first local shortcut.
    136  for (let i = 0; i < 2; i++) {
    137    tree.view.selection.select(defaultEngines.length - i - 1);
    138    let updatedPromise = SearchTestUtils.promiseSearchNotification(
    139      SearchUtils.MODIFIED_TYPE.CHANGED,
    140      SearchUtils.TOPIC_ENGINE_MODIFIED
    141    );
    142    removeEngineButton.click();
    143    await updatedPromise;
    144    Assert.ok(
    145      removeEngineButton.disabled,
    146      "The remove-engine button is disabled because a local shortcut is selected"
    147    );
    148    Assert.ok(
    149      !restoreDefaultsButton.disabled,
    150      "The restore-defaults button is enabled after removing an engine"
    151    );
    152  }
    153 
    154  // Remove the remaining engines from top to bottom except for the default engine
    155  // which can't be removed.
    156  for (let i = 0; i < defaultEngines.length - 3; i++) {
    157    tree.view.selection.select(0);
    158 
    159    if (defaultEngines[0].name == Services.search.defaultEngine.name) {
    160      tree.view.selection.select(1);
    161    }
    162 
    163    let updatedPromise = SearchTestUtils.promiseSearchNotification(
    164      SearchUtils.MODIFIED_TYPE.CHANGED,
    165      SearchUtils.TOPIC_ENGINE_MODIFIED
    166    );
    167    removeEngineButton.click();
    168    await updatedPromise;
    169    Assert.ok(
    170      !restoreDefaultsButton.disabled,
    171      "The restore-defaults button is enabled after removing an engine"
    172    );
    173  }
    174  Assert.ok(
    175    removeEngineButton.disabled,
    176    "The remove-engine button is disabled because only one engine remains"
    177  );
    178 
    179  // Click the restore-defaults button.
    180  let updatedPromise = SearchTestUtils.promiseSearchNotification(
    181    SearchUtils.MODIFIED_TYPE.CHANGED,
    182    SearchUtils.TOPIC_ENGINE_MODIFIED
    183  );
    184  restoreDefaultsButton.click();
    185  await updatedPromise;
    186 
    187  // Wait for the restore-defaults button to update its state.
    188  await TestUtils.waitForCondition(
    189    () => restoreDefaultsButton.disabled,
    190    "Waiting for the restore-defaults button to become disabled"
    191  );
    192 
    193  Assert.ok(
    194    restoreDefaultsButton.disabled,
    195    "The restore-defaults button is disabled after restoring defaults"
    196  );
    197  Assert.equal(
    198    tree.view.rowCount,
    199    defaultEngines.length + UrlbarUtils.LOCAL_SEARCH_MODES.length,
    200    "All engines are restored"
    201  );
    202 
    203  gBrowser.removeCurrentTab();
    204 });
    205 
    206 add_task(async function test_removeAndRestoreMultiple() {
    207  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    208 
    209  let doc = gBrowser.selectedBrowser.contentDocument;
    210  let restoreDefaultsButton = doc.getElementById("restoreDefaultSearchEngines");
    211  let tree = doc.querySelector("#engineList");
    212  let removeEngineButton = doc.getElementById("removeEngineButton");
    213  removeEngineButton.scrollIntoView();
    214 
    215  let defaultEngines = await Services.search.getAppProvidedEngines();
    216 
    217  // Remove the second and fourth engines.
    218  for (let i = 0; i < 2; i++) {
    219    tree.view.selection.select(i * 2 + 1);
    220    let updatedPromise = SearchTestUtils.promiseSearchNotification(
    221      SearchUtils.MODIFIED_TYPE.CHANGED,
    222      SearchUtils.TOPIC_ENGINE_MODIFIED
    223    );
    224    removeEngineButton.click();
    225    await updatedPromise;
    226  }
    227 
    228  // Click the restore-defaults button.
    229  let updatedPromise = SearchTestUtils.promiseSearchNotification(
    230    SearchUtils.MODIFIED_TYPE.CHANGED,
    231    SearchUtils.TOPIC_ENGINE_MODIFIED
    232  );
    233  restoreDefaultsButton.click();
    234  await updatedPromise;
    235 
    236  // Remove the third engine.
    237  tree.view.selection.select(3);
    238  updatedPromise = SearchTestUtils.promiseSearchNotification(
    239    SearchUtils.MODIFIED_TYPE.CHANGED,
    240    SearchUtils.TOPIC_ENGINE_MODIFIED
    241  );
    242  removeEngineButton.click();
    243  await updatedPromise;
    244 
    245  // Now restore again.
    246  updatedPromise = SearchTestUtils.promiseSearchNotification(
    247    SearchUtils.MODIFIED_TYPE.CHANGED,
    248    SearchUtils.TOPIC_ENGINE_MODIFIED
    249  );
    250  restoreDefaultsButton.click();
    251  await updatedPromise;
    252 
    253  // Wait for the restore-defaults button to update its state.
    254  await TestUtils.waitForCondition(
    255    () => restoreDefaultsButton.disabled,
    256    "Waiting for the restore-defaults button to become disabled"
    257  );
    258 
    259  Assert.equal(
    260    tree.view.rowCount,
    261    defaultEngines.length + UrlbarUtils.LOCAL_SEARCH_MODES.length,
    262    "Should have the correct amount of engines"
    263  );
    264 
    265  gBrowser.removeCurrentTab();
    266 });