summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/ext/browser/head.js
blob: 8d11a88066654fe68c05e8d030c37a377f4ca282 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * The files in this directory test the browser.urlbarExperiments WebExtension
 * Experiment APIs, which are the WebExtension APIs we ship in our urlbar
 * experiment extensions.  Unlike the WebExtension APIs we ship in mozilla-
 * central, which have continuous test coverage [1], our WebExtension Experiment
 * APIs would not have continuous test coverage were it not for the fact that we
 * copy and test them here.  This is especially useful for APIs that are used in
 * experiments that target multiple versions of Firefox, and for APIs that are
 * reused in multiple experiments.  See [2] and [3] for more info on
 * experiments.
 *
 * [1] See browser/components/extensions/test
 * [2] browser/components/urlbar/docs/experiments.rst
 * [3] https://firefox-source-docs.mozilla.org/toolkit/components/extensions/webextensions/basics.html#webextensions-experiments
 */

"use strict";

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/browser/components/urlbar/tests/browser/head-common.js",
  this
);

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

const SCHEMA_BASENAME = "schema.json";
const SCRIPT_BASENAME = "api.js";

const SCHEMA_PATH = getTestFilePath(SCHEMA_BASENAME);
const SCRIPT_PATH = getTestFilePath(SCRIPT_BASENAME);

let schemaSource;
let scriptSource;

add_setup(async function loadSource() {
  schemaSource = await (await fetch("file://" + SCHEMA_PATH)).text();
  scriptSource = await (await fetch("file://" + SCRIPT_PATH)).text();
});

/**
 * Loads a mock extension with our browser.experiments.urlbar API and a
 * background script.  Be sure to call `await ext.unload()` when you're done
 * with it.
 *
 * @param {object} options
 *   Options object
 * @param {Function} options.background
 *   This function is serialized and becomes the background script.
 * @param {object} [options.extraFiles]
 *   Extra files to load in the extension.
 * @returns {object}
 *   The extension.
 */
async function loadExtension({ background, extraFiles = {} }) {
  let ext = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["urlbar"],
      experiment_apis: {
        experiments_urlbar: {
          schema: SCHEMA_BASENAME,
          parent: {
            scopes: ["addon_parent"],
            paths: [["experiments", "urlbar"]],
            script: SCRIPT_BASENAME,
          },
        },
      },
    },
    files: {
      [SCHEMA_BASENAME]: schemaSource,
      [SCRIPT_BASENAME]: scriptSource,
      ...extraFiles,
    },
    isPrivileged: true,
    background,
  });
  await ext.startup();
  return ext;
}

/**
 * Tests toggling a preference value via an experiments.urlbar API.
 *
 * @param {string} prefName
 *   The name of the pref to be tested.
 * @param {string} type
 *   The type of the pref being set. One of "string", "boolean", or "number".
 * @param {Function} background
 *   Boilerplate function that returns the value from calling the
 *   browser.experiments.urlbar.prefName[method] APIs.
 */
function add_settings_tasks(prefName, type, background) {
  let defaultPreferences = new Preferences({ defaultBranch: true });

  let originalValue = defaultPreferences.get(prefName);
  registerCleanupFunction(() => {
    defaultPreferences.set(prefName, originalValue);
  });

  let firstValue, secondValue;
  switch (type) {
    case "string":
      firstValue = "test value 1";
      secondValue = "test value 2";
      break;
    case "number":
      firstValue = 10;
      secondValue = 100;
      break;
    case "boolean":
      firstValue = false;
      secondValue = true;
      break;
    default:
      Assert.ok(
        false,
        `"type" parameter must be one of "string", "number", or "boolean"`
      );
  }

  add_task(async function get() {
    let ext = await loadExtension({ background });

    defaultPreferences.set(prefName, firstValue);
    ext.sendMessage("get", {});
    let result = await ext.awaitMessage("done");
    Assert.strictEqual(result.value, firstValue);

    defaultPreferences.set(prefName, secondValue);
    ext.sendMessage("get", {});
    result = await ext.awaitMessage("done");
    Assert.strictEqual(result.value, secondValue);

    await ext.unload();
  });

  add_task(async function set() {
    let ext = await loadExtension({ background });

    defaultPreferences.set(prefName, firstValue);
    ext.sendMessage("set", { value: secondValue });
    let result = await ext.awaitMessage("done");
    Assert.strictEqual(result, true);
    Assert.strictEqual(defaultPreferences.get(prefName), secondValue);

    ext.sendMessage("set", { value: firstValue });
    result = await ext.awaitMessage("done");
    Assert.strictEqual(result, true);
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);

    await ext.unload();
  });

  add_task(async function clear() {
    // no set()
    defaultPreferences.set(prefName, firstValue);
    let ext = await loadExtension({ background });
    ext.sendMessage("clear", {});
    let result = await ext.awaitMessage("done");
    Assert.strictEqual(result, false);
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);
    await ext.unload();

    // firstValue -> secondValue
    defaultPreferences.set(prefName, firstValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: secondValue });
    await ext.awaitMessage("done");
    ext.sendMessage("clear", {});
    result = await ext.awaitMessage("done");
    Assert.strictEqual(result, true);
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);
    await ext.unload();

    // secondValue -> firstValue
    defaultPreferences.set(prefName, secondValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: firstValue });
    await ext.awaitMessage("done");
    ext.sendMessage("clear", {});
    result = await ext.awaitMessage("done");
    Assert.strictEqual(result, true);
    Assert.strictEqual(defaultPreferences.get(prefName), secondValue);
    await ext.unload();

    // firstValue -> firstValue
    defaultPreferences.set(prefName, firstValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: firstValue });
    await ext.awaitMessage("done");
    ext.sendMessage("clear", {});
    result = await ext.awaitMessage("done");
    Assert.strictEqual(result, true);
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);
    await ext.unload();

    // secondValue -> secondValue
    defaultPreferences.set(prefName, secondValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: secondValue });
    await ext.awaitMessage("done");
    ext.sendMessage("clear", {});
    result = await ext.awaitMessage("done");
    Assert.strictEqual(result, true);
    Assert.strictEqual(defaultPreferences.get(prefName), secondValue);
    await ext.unload();
  });

  add_task(async function shutdown() {
    // no set()
    defaultPreferences.set(prefName, firstValue);
    let ext = await loadExtension({ background });
    await ext.unload();
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);

    // firstValue -> secondValue
    defaultPreferences.set(prefName, firstValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: secondValue });
    await ext.awaitMessage("done");
    await ext.unload();
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);

    // secondValue -> firstValue
    defaultPreferences.set(prefName, secondValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: firstValue });
    await ext.awaitMessage("done");
    await ext.unload();
    Assert.strictEqual(defaultPreferences.get(prefName), secondValue);

    // firstValue -> firstValue
    defaultPreferences.set(prefName, firstValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: firstValue });
    await ext.awaitMessage("done");
    await ext.unload();
    Assert.strictEqual(defaultPreferences.get(prefName), firstValue);

    // secondValue -> secondValue
    defaultPreferences.set(prefName, secondValue);
    ext = await loadExtension({ background });
    ext.sendMessage("set", { value: secondValue });
    await ext.awaitMessage("done");
    await ext.unload();
    Assert.strictEqual(defaultPreferences.get(prefName), secondValue);
  });
}