tor-browser

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

browser_searchChangedEngine.js (2819B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { AddonTestUtils } = ChromeUtils.importESModule(
      5  "resource://testing-common/AddonTestUtils.sys.mjs"
      6 );
      7 const { SearchTestUtils } = ChromeUtils.importESModule(
      8  "resource://testing-common/SearchTestUtils.sys.mjs"
      9 );
     10 const { SearchUtils } = ChromeUtils.importESModule(
     11  "moz-src:///toolkit/components/search/SearchUtils.sys.mjs"
     12 );
     13 
     14 AddonTestUtils.initMochitest(this);
     15 SearchTestUtils.init(this);
     16 
     17 function findRow(tree, expectedName) {
     18  for (let i = 0; i < tree.view.rowCount; i++) {
     19    let name = tree.view.getCellText(
     20      i,
     21      tree.columns.getNamedColumn("engineName")
     22    );
     23 
     24    if (name == expectedName) {
     25      return i;
     26    }
     27  }
     28  return -1;
     29 }
     30 
     31 add_task(async function test_change_engine() {
     32  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     33 
     34  let doc = gBrowser.selectedBrowser.contentDocument;
     35 
     36  await SearchTestUtils.installSearchExtension({
     37    id: "example@tests.mozilla.org",
     38    name: "Example",
     39    version: "1.0",
     40    keyword: "foo",
     41    icons: {
     42      16: "img123.png",
     43    },
     44  });
     45 
     46  let tree = doc.querySelector("#engineList");
     47 
     48  let row = findRow(tree, "Example");
     49 
     50  Assert.notEqual(row, -1, "Should have found the entry");
     51  await TestUtils.waitForCondition(
     52    () => tree.view.getImageSrc(row, tree.columns.getNamedColumn("engineName")),
     53    "Should have go an image URL"
     54  );
     55  Assert.ok(
     56    tree.view
     57      .getImageSrc(row, tree.columns.getNamedColumn("engineName"))
     58      .includes("img123.png"),
     59    "Should have the correct image URL"
     60  );
     61  Assert.equal(
     62    tree.view.getCellText(row, tree.columns.getNamedColumn("engineKeyword")),
     63    "foo",
     64    "Should show the correct keyword"
     65  );
     66 
     67  let updatedPromise = SearchTestUtils.promiseSearchNotification(
     68    SearchUtils.MODIFIED_TYPE.CHANGED,
     69    SearchUtils.TOPIC_ENGINE_MODIFIED
     70  );
     71  await SearchTestUtils.installSearchExtension({
     72    id: "example@tests.mozilla.org",
     73    name: "Example 2",
     74    version: "2.0",
     75    keyword: "bar",
     76    icons: {
     77      16: "img456.png",
     78    },
     79  });
     80  await updatedPromise;
     81 
     82  row = findRow(tree, "Example 2");
     83 
     84  Assert.notEqual(row, -1, "Should have found the updated entry");
     85  await TestUtils.waitForCondition(
     86    () =>
     87      tree.view
     88        .getImageSrc(row, tree.columns.getNamedColumn("engineName"))
     89        ?.includes("img456.png"),
     90    "Should have updated the image URL"
     91  );
     92  Assert.ok(
     93    tree.view
     94      .getImageSrc(row, tree.columns.getNamedColumn("engineName"))
     95      .includes("img456.png"),
     96    "Should have the correct image URL"
     97  );
     98  Assert.equal(
     99    tree.view.getCellText(row, tree.columns.getNamedColumn("engineKeyword")),
    100    "bar",
    101    "Should show the correct keyword"
    102  );
    103 
    104  gBrowser.removeCurrentTab();
    105 });