summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_sort_orders-no-hints.js
blob: cb7e314fd4ed42f61c4c0a750da8a772113001a9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Check the correct default engines are picked from the configuration list,
 * when we have some with the same orderHint, and some without any.
 */

"use strict";

add_setup(async function () {
  await AddonTestUtils.promiseStartupManager();

  await SearchTestUtils.useTestEngines(
    "data",
    null,
    (
      await readJSONFile(
        do_get_file(
          SearchUtils.newSearchConfigEnabled
            ? "data/search-config-v2-no-order-hint.json"
            : "data/engines-no-order-hint.json"
        )
      )
    ).data
  );

  Services.prefs.setBoolPref(
    SearchUtils.BROWSER_SEARCH_PREF + "separatePrivateDefault",
    true
  );
});

async function checkOrder(type, expectedOrder) {
  // Reset the sorted list.
  Services.search.wrappedJSObject._cachedSortedEngines = null;

  const sortedEngines = await Services.search[type]();
  Assert.deepEqual(
    sortedEngines.map(s => s.name),
    expectedOrder,
    `Should have the expected engine order from ${type}`
  );
}

add_task(async function test_engine_sort_with_non_builtins_sort() {
  await SearchTestUtils.installSearchExtension({ name: "nonbuiltin1" });

  // As we've added an engine, the pref will have been set to true, but
  // we do really want to test the default sort.
  Services.search.wrappedJSObject._settings.setMetaDataAttribute(
    "useSavedOrder",
    false
  );

  const EXPECTED_ORDER = [
    // Default engine.
    "Test search engine",
    // Alphabetical order for the two with orderHint = 1000.
    "engine-chromeicon",
    "engine-rel-searchform-purpose",
    // Alphabetical order for the remaining engines without orderHint.
    "engine-pref",
    "engine-resourceicon",
    "Test search engine (Reordered)",
  ];

  // We should still have the same built-in engines listed.
  await checkOrder("getAppProvidedEngines", EXPECTED_ORDER);

  const expected = [...EXPECTED_ORDER];
  // This is inserted in alphabetical order for the last three.
  expected.splice(expected.length - 1, 0, "nonbuiltin1");
  await checkOrder("getEngines", expected);
});