summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/searchconfigs/test_google.js
blob: c6b9d6a99102fffa7d766d358a93c7ee96ae47b3 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs",
});

const test = new SearchConfigTest({
  identifier: "google",
  aliases: ["@google"],
  default: {
    // Included everywhere apart from the exclusions below. These are basically
    // just excluding what Yandex and Baidu include.
    excluded: [
      {
        regions: ["cn"],
        locales: ["zh-CN"],
      },
    ],
  },
  available: {
    excluded: [
      // Should be available everywhere.
    ],
  },
  details: [
    {
      included: [{ regions: ["us"] }],
      domain: "google.com",
      telemetryId:
        SearchUtils.MODIFIED_APP_CHANNEL == "esr"
          ? "google-b-1-e"
          : "google-b-1-d",
      codes:
        SearchUtils.MODIFIED_APP_CHANNEL == "esr"
          ? "client=firefox-b-1-e"
          : "client=firefox-b-1-d",
    },
    {
      excluded: [{ regions: ["us", "by", "kz", "ru", "tr"] }],
      included: [{}],
      domain: "google.com",
      telemetryId:
        SearchUtils.MODIFIED_APP_CHANNEL == "esr" ? "google-b-e" : "google-b-d",
      codes:
        SearchUtils.MODIFIED_APP_CHANNEL == "esr"
          ? "client=firefox-b-e"
          : "client=firefox-b-d",
    },
    {
      included: [{ regions: ["by", "kz", "ru", "tr"] }],
      domain: "google.com",
      telemetryId: "google-com-nocodes",
    },
  ],
});

add_setup(async function () {
  sinon.spy(NimbusFeatures.search, "onUpdate");
  sinon.stub(NimbusFeatures.search, "ready").resolves();
  await test.setup();
});

add_task(async function test_searchConfig_google() {
  await test.run();
});

add_task(async function test_searchConfig_google_with_mozparam() {
  // Test a couple of configurations with a MozParam set up.
  const TEST_DATA = [
    {
      locale: "en-US",
      region: "US",
      pref: "google_channel_us",
      expected: "us_param",
    },
    {
      locale: "en-US",
      region: "GB",
      pref: "google_channel_row",
      expected: "row_param",
    },
  ];

  const defaultBranch = Services.prefs.getDefaultBranch(
    SearchUtils.BROWSER_SEARCH_PREF
  );
  for (const testData of TEST_DATA) {
    defaultBranch.setCharPref("param." + testData.pref, testData.expected);
  }

  for (const testData of TEST_DATA) {
    info(`Checking region ${testData.region}, locale ${testData.locale}`);
    const engines = await test._getEngines(testData.region, testData.locale);

    Assert.ok(
      engines[0].identifier.startsWith("google"),
      "Should have the correct engine"
    );
    console.log(engines[0]);

    const submission = engines[0].getSubmission("test", URLTYPE_SEARCH_HTML);
    Assert.ok(
      submission.uri.query.split("&").includes("channel=" + testData.expected),
      "Should be including the correct MozParam parameter for the engine"
    );
  }

  // Reset the pref values for next tests
  for (const testData of TEST_DATA) {
    defaultBranch.setCharPref("param." + testData.pref, "");
  }
});

add_task(async function test_searchConfig_google_with_nimbus() {
  let sandbox = sinon.createSandbox();
  // Test a couple of configurations with a MozParam set up.
  const TEST_DATA = [
    {
      locale: "en-US",
      region: "US",
      expected: "nimbus_us_param",
    },
    {
      locale: "en-US",
      region: "GB",
      expected: "nimbus_row_param",
    },
  ];

  Assert.ok(
    NimbusFeatures.search.onUpdate.called,
    "Should register an update listener for Nimbus experiments"
  );
  // Stub getVariable to populate the cache with our expected data
  sandbox.stub(NimbusFeatures.search, "getVariable").returns([
    { key: "google_channel_us", value: "nimbus_us_param" },
    { key: "google_channel_row", value: "nimbus_row_param" },
  ]);
  // Set the pref cache with Nimbus values
  NimbusFeatures.search.onUpdate.firstCall.args[0]();

  for (const testData of TEST_DATA) {
    info(`Checking region ${testData.region}, locale ${testData.locale}`);
    const engines = await test._getEngines(testData.region, testData.locale);

    Assert.ok(
      engines[0].identifier.startsWith("google"),
      "Should have the correct engine"
    );
    console.log(engines[0]);

    const submission = engines[0].getSubmission("test", URLTYPE_SEARCH_HTML);
    Assert.ok(
      NimbusFeatures.search.ready.called,
      "Should wait for Nimbus to get ready"
    );
    Assert.ok(
      NimbusFeatures.search.getVariable,
      "Should call NimbusFeatures.search.getVariable to populate the cache"
    );
    Assert.ok(
      submission.uri.query.split("&").includes("channel=" + testData.expected),
      "Should be including the correct MozParam parameter for the engine"
    );
  }

  sandbox.restore();
});