summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_getSubmission_params_prefNimbus.js
blob: 3963a073689f27687cceff7dba5d7b396be7cc93 (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/ */

/* Test that MozParam condition="pref" values used in search URLs can be set
   by Nimbus, and that their special characters are URL encoded. */

"use strict";

const { NimbusFeatures } = ChromeUtils.importESModule(
  "resource://nimbus/ExperimentAPI.sys.mjs"
);

const baseURL = "https://www.google.com/search?q=foo";
const baseURLSearchConfigV2 = "https://www.google.com/search?";

let getVariableStub;
let updateStub;

add_setup(async function () {
  updateStub = sinon.stub(NimbusFeatures.search, "onUpdate");
  getVariableStub = sinon.stub(NimbusFeatures.search, "getVariable");
  sinon.stub(NimbusFeatures.search, "ready").resolves();

  // The test engines used in this test need to be recognized as 'default'
  // engines, or their MozParams will be ignored.
  await SearchTestUtils.useTestEngines();
});

add_task(async function test_pref_initial_value() {
  // These values should match the nimbusParams below and the data/test/manifest.json
  // search engine configuration
  getVariableStub.withArgs("extraParams").returns([
    {
      key: "code",
      // The & and = in this parameter are to check that they are correctly
      // encoded, and not treated as a separate parameter.
      value: "good&id=unique",
    },
  ]);

  await AddonTestUtils.promiseStartupManager();
  await Services.search.init();

  Assert.ok(
    updateStub.called,
    "Should have called onUpdate to listen for future updates"
  );

  const engine = Services.search.getEngineByName("engine-pref");
  Assert.equal(
    engine.getSubmission("foo").uri.spec,
    SearchUtils.newSearchConfigEnabled
      ? baseURLSearchConfigV2 + "code=good%26id%3Dunique&q=foo"
      : baseURL + "&code=good%26id%3Dunique",
    "Should have got the submission URL with the correct code"
  );
});

add_task(async function test_pref_updated() {
  getVariableStub.withArgs("extraParams").returns([
    {
      key: "code",
      // The & and = in this parameter are to check that they are correctly
      // encoded, and not treated as a separate parameter.
      value: "supergood&id=unique123456",
    },
  ]);
  // Update the pref without re-init nor restart.
  updateStub.firstCall.args[0]();

  const engine = Services.search.getEngineByName("engine-pref");
  Assert.equal(
    engine.getSubmission("foo").uri.spec,
    SearchUtils.newSearchConfigEnabled
      ? baseURLSearchConfigV2 + "code=supergood%26id%3Dunique123456&q=foo"
      : baseURL + "&code=supergood%26id%3Dunique123456",
    "Should have got the submission URL with the updated code"
  );
});

add_task(async function test_multiple_params() {
  getVariableStub.withArgs("extraParams").returns([
    {
      key: "code",
      value: "sng",
    },
    {
      key: "test",
      value: "sup",
    },
  ]);
  // Update the pref without re-init nor restart.
  updateStub.firstCall.args[0]();

  let engine = Services.search.getEngineByName("engine-pref");
  Assert.equal(
    engine.getSubmission("foo").uri.spec,
    SearchUtils.newSearchConfigEnabled
      ? baseURLSearchConfigV2 + "code=sng&test=sup&q=foo"
      : baseURL + "&code=sng&test=sup",
    "Should have got the submission URL with both parameters"
  );

  // Test removing just one of the parameters.
  getVariableStub.withArgs("extraParams").returns([
    {
      key: "code",
      value: "sng",
    },
  ]);
  // Update the pref without re-init nor restart.
  updateStub.firstCall.args[0]();

  engine = Services.search.getEngineByName("engine-pref");
  Assert.equal(
    engine.getSubmission("foo").uri.spec,
    SearchUtils.newSearchConfigEnabled
      ? baseURLSearchConfigV2 + "code=sng&q=foo"
      : baseURL + "&code=sng",
    "Should have got the submission URL with one parameter"
  );
});

add_task(async function test_pref_cleared() {
  // Update the pref without re-init nor restart.
  // Note you can't delete a preference from the default branch.
  getVariableStub.withArgs("extraParams").returns([]);
  updateStub.firstCall.args[0]();

  let engine = Services.search.getEngineByName("engine-pref");
  Assert.equal(
    engine.getSubmission("foo").uri.spec,
    baseURL,
    "Should have just the base URL after the pref was cleared"
  );
});