summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_engine_selector_order.js
blob: 1de4792af14897a794bc04812d17ff1ed76c2431 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Any copyright is dedicated to the Public Domain.
 *    http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  SearchEngineSelector: "resource://gre/modules/SearchEngineSelector.sys.mjs",
});

/**
 * This constant defines the tests for the order. The input is an array of
 * engines that will be constructed. The engine definitions are arrays with
 * fields in order:
 *   name, orderHint, default, defaultPrivate
 *
 * The expected is an array of engine names.
 */
const TESTS = [
  {
    // Basic tests to ensure correct order for default engine.
    input: [
      ["A", 750, "no", "no"],
      ["B", 3000, "no", "no"],
      ["C", 2000, "yes", "no"],
      ["D", 1000, "yes-if-no-other", "no"],
      ["E", 500, "no", "no"],
    ],
    expected: ["C", "B", "D", "A", "E"],
    expectedPrivate: undefined,
  },
  {
    input: [
      ["A", 750, "no", "no"],
      ["B", 3000, "no", "no"],
      ["C", 2000, "yes-if-no-other", "no"],
      ["D", 1000, "yes", "no"],
      ["E", 500, "no", "no"],
    ],
    expected: ["D", "B", "C", "A", "E"],
    expectedPrivate: undefined,
  },
  // Check that yes-if-no-other works correctly.
  {
    input: [
      ["A", 750, "no", "no"],
      ["B", 3000, "no", "no"],
      ["C", 2000, "no", "no"],
      ["D", 1000, "yes-if-no-other", "no"],
      ["E", 500, "no", "no"],
    ],
    expected: ["D", "B", "C", "A", "E"],
    expectedPrivate: undefined,
  },
  // Basic tests to ensure correct order with private engine.
  {
    input: [
      ["A", 750, "no", "no"],
      ["B", 3000, "yes-if-no-other", "no"],
      ["C", 2000, "no", "yes"],
      ["D", 1000, "yes", "yes-if-no-other"],
      ["E", 500, "no", "no"],
    ],
    expected: ["D", "C", "B", "A", "E"],
    expectedPrivate: "C",
  },
  {
    input: [
      ["A", 750, "no", "yes-if-no-other"],
      ["B", 3000, "yes-if-no-other", "no"],
      ["C", 2000, "no", "yes"],
      ["D", 1000, "yes", "no"],
      ["E", 500, "no", "no"],
    ],
    expected: ["D", "C", "B", "A", "E"],
    expectedPrivate: "C",
  },
  // Private engine test for yes-if-no-other.
  {
    input: [
      ["A", 750, "no", "yes-if-no-other"],
      ["B", 3000, "yes-if-no-other", "no"],
      ["C", 2000, "no", "no"],
      ["D", 1000, "yes", "no"],
      ["E", 500, "no", "no"],
    ],
    expected: ["D", "A", "B", "C", "E"],
    expectedPrivate: "A",
  },
];

function getConfigData(testInput) {
  return testInput.map(info => ({
    engineName: info[0],
    orderHint: info[1],
    default: info[2],
    defaultPrivate: info[3],
    appliesTo: [
      {
        included: { everywhere: true },
      },
    ],
  }));
}

const engineSelector = new SearchEngineSelector();

add_task(async function () {
  const settings = await RemoteSettings(SearchUtils.SETTINGS_KEY);
  const getStub = sinon.stub(settings, "get");

  let i = 0;
  for (const test of TESTS) {
    // Remove the existing configuration and update the stub to return the data
    // for this test.
    delete engineSelector._configuration;
    getStub.returns(getConfigData(test.input));

    const { engines, privateDefault } =
      await engineSelector.fetchEngineConfiguration({
        locale: "en-US",
        region: "us",
      });

    let names = engines.map(obj => obj.engineName);
    Assert.deepEqual(
      names,
      test.expected,
      `Should have the correct order for the engines: test ${i}`
    );
    Assert.equal(
      privateDefault && privateDefault.engineName,
      test.expectedPrivate,
      `Should have the correct selection for the private engine: test ${i++}`
    );
  }
});