summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_webextension_embedded.js
blob: 3bd8a2bd83a09b1e92879c72a85c3eb439d02219 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

BootstrapMonitor.init();

const profileDir = gProfD.clone();
profileDir.append("extensions");

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "49");
startupManager();

// NOTE: the following import needs to be called after the `createAppInfo`
// or it will fail Extension.jsm internally imports AddonManager.jsm and
// AddonManager will raise a ReferenceError exception because it tried to
// access an undefined `Services.appinfo` object.
const { Management } = Components.utils.import("resource://gre/modules/Extension.jsm", {});

const {
  EmbeddedExtensionManager,
  LegacyExtensionsUtils,
} = Components.utils.import("resource://gre/modules/LegacyExtensionsUtils.jsm");

// Wait the startup of the embedded webextension.
function promiseWebExtensionStartup() {
  return new Promise(resolve => {
    let listener = (event, extension) => {
      Management.off("startup", listener);
      resolve(extension);
    };

    Management.on("startup", listener);
  });
}

function promiseWebExtensionShutdown() {
  return new Promise(resolve => {
    let listener = (event, extension) => {
      Management.off("shutdown", listener);
      resolve(extension);
    };

    Management.on("shutdown", listener);
  });
}

const BOOTSTRAP = String.raw`
  Components.utils.import("resource://xpcshell-data/BootstrapMonitor.jsm").monitor(this);
`;

const EMBEDDED_WEBEXT_MANIFEST = JSON.stringify({
  name: "embedded webextension addon",
  manifest_version: 2,
  version: "1.0",
});

/**
 *  This test case checks that an hasEmbeddedWebExtension addon property
 *  is persisted and restored correctly across restarts.
 */
add_task(function* has_embedded_webextension_persisted() {
  const ID = "embedded-webextension-addon-persist@tests.mozilla.org";

  const xpiFile = createTempXPIFile({
    id: ID,
    name: "Test Add-on",
    version: "1.0",
    bootstrap: true,
    hasEmbeddedWebExtension: true,
    targetApplications: [{
      id: "xpcshell@tests.mozilla.org",
      minVersion: "1",
      maxVersion: "1.9.2"
    }]
  }, {
    "bootstrap.js": BOOTSTRAP,
    "webextension/manifest.json": EMBEDDED_WEBEXT_MANIFEST,
  });

  yield promiseInstallFile(xpiFile);

  let addon = yield promiseAddonByID(ID);

  notEqual(addon, null, "Got an addon object as expected");
  equal(addon.version, "1.0", "Got the expected version");
  equal(addon.hasEmbeddedWebExtension, true,
        "Got the expected hasEmbeddedWebExtension value");

  // Check that the addon has been installed and started.
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");

  let startupInfo = BootstrapMonitor.started.get(ID);
  ok(("webExtension" in startupInfo.data),
     "Got an webExtension property in the startup bootstrap method params");
  ok(("startup" in startupInfo.data.webExtension),
     "Got the expected 'startup' property in the webExtension object");

  // After restarting the manager, the add-on should still have the
  // hasEmbeddedWebExtension property as expected.
  yield promiseRestartManager();

  let persisted = JSON.parse(Services.prefs.getCharPref("extensions.bootstrappedAddons"));
  ok(ID in persisted, "Hybrid add-on persisted to bootstrappedAddons.");
  equal(persisted[ID].hasEmbeddedWebExtension, true,
        "hasEmbeddedWebExtension flag persisted to bootstrappedAddons.");

  // Check that the addon has been installed and started.
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");

  addon = yield promiseAddonByID(ID);
  notEqual(addon, null, "Got an addon object as expected");
  equal(addon.hasEmbeddedWebExtension, true, "Got the expected hasEmbeddedWebExtension value");

  // Check that the addon has been installed and started.
  let newStartupInfo = BootstrapMonitor.started.get(ID);
  ok(("webExtension" in newStartupInfo.data),
     "Got an webExtension property in the startup bootstrap method params");
  ok(("startup" in newStartupInfo.data.webExtension),
     "Got the expected 'startup' property in the webExtension object");

  let waitUninstall = promiseAddonEvent("onUninstalled");
  addon.uninstall();
  yield waitUninstall;
});

/**
 *  This test case checks that an addon with hasEmbeddedWebExtension set to true
 *  in its install.rdf gets the expected `embeddedWebExtension` object in the
 *  parameters of its bootstrap methods.
 */
add_task(function* run_embedded_webext_bootstrap() {
  const ID = "embedded-webextension-addon2@tests.mozilla.org";

  const xpiFile = createTempXPIFile({
    id: ID,
    name: "Test Add-on",
    version: "1.0",
    bootstrap: true,
    hasEmbeddedWebExtension: true,
    targetApplications: [{
      id: "xpcshell@tests.mozilla.org",
      minVersion: "1",
      maxVersion: "1.9.2"
    }]
  }, {
    "bootstrap.js": BOOTSTRAP,
    "webextension/manifest.json": EMBEDDED_WEBEXT_MANIFEST,
  });

  yield AddonManager.installTemporaryAddon(xpiFile);

  let addon = yield promiseAddonByID(ID);

  notEqual(addon, null, "Got an addon object as expected");
  equal(addon.version, "1.0", "Got the expected version");
  equal(addon.hasEmbeddedWebExtension, true,
        "Got the expected hasEmbeddedWebExtension value");

  // Check that the addon has been installed and started.
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");

  let installInfo = BootstrapMonitor.installed.get(ID);
  ok(!("webExtension" in installInfo.data),
     "No webExtension property is expected in the install bootstrap method params");

  BootstrapMonitor.checkAddonStarted(ID, "1.0");

  let startupInfo = BootstrapMonitor.started.get(ID);

  ok(("webExtension" in startupInfo.data),
     "Got an webExtension property in the startup bootstrap method params");

  ok(("startup" in startupInfo.data.webExtension),
     "Got the expected 'startup' property in the webExtension object");

  const waitForWebExtensionStartup = promiseWebExtensionStartup();

  const embeddedAPI = yield startupInfo.data.webExtension.startup();

  // WebExtension startup should have been fully resolved.
  yield waitForWebExtensionStartup;

  Assert.deepEqual(
    Object.keys(embeddedAPI.browser.runtime).sort(),
    ["onConnect", "onMessage"],
    `Got the expected 'runtime' in the 'browser' API object`
  );

  // Uninstall the addon and wait that the embedded webextension has been stopped and
  // test the params of the shutdown and uninstall bootstrap method.
  let waitForWebExtensionShutdown = promiseWebExtensionShutdown();
  let waitUninstall = promiseAddonEvent("onUninstalled");
  addon.uninstall();
  yield waitForWebExtensionShutdown;
  yield waitUninstall;

  BootstrapMonitor.checkAddonNotStarted(ID, "1.0");

  let shutdownInfo = BootstrapMonitor.stopped.get(ID);
  ok(!("webExtension" in shutdownInfo.data),
     "No webExtension property is expected in the shutdown bootstrap method params");

  let uninstallInfo = BootstrapMonitor.uninstalled.get(ID);
  ok(!("webExtension" in uninstallInfo.data),
     "No webExtension property is expected in the uninstall bootstrap method params");
});

/**
 *  This test case checks that an addon with hasEmbeddedWebExtension can be reloaded
 *  without raising unexpected exceptions due to race conditions.
 */
add_task(function* reload_embedded_webext_bootstrap() {
  const ID = "embedded-webextension-addon2@tests.mozilla.org";

  // No embedded webextension should be currently around.
  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 0,
        "No embedded extension instance should be tracked here");

  const xpiFile = createTempXPIFile({
    id: ID,
    name: "Test Add-on",
    version: "1.0",
    bootstrap: true,
    hasEmbeddedWebExtension: true,
    targetApplications: [{
      id: "xpcshell@tests.mozilla.org",
      minVersion: "1",
      maxVersion: "1.9.2"
    }]
  }, {
    "bootstrap.js": BOOTSTRAP,
    "webextension/manifest.json": EMBEDDED_WEBEXT_MANIFEST,
  });

  yield AddonManager.installTemporaryAddon(xpiFile);

  let addon = yield promiseAddonByID(ID);

  notEqual(addon, null, "Got an addon object as expected");
  equal(addon.version, "1.0", "Got the expected version");
  equal(addon.isActive, true, "The Addon is active");
  equal(addon.appDisabled, false, "The addon is not app disabled");
  equal(addon.userDisabled, false, "The addon is not user disabled");

  // Check that the addon has been installed and started.
  BootstrapMonitor.checkAddonInstalled(ID, "1.0");
  BootstrapMonitor.checkAddonStarted(ID, "1.0");

  // Only one embedded extension.
  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 1,
        "Got the expected number of tracked extension instances");

  const embeddedWebExtension = EmbeddedExtensionManager.embeddedExtensionsByAddonId.get(ID);

  let startupInfo = BootstrapMonitor.started.get(ID);
  yield startupInfo.data.webExtension.startup();

  const waitForAddonDisabled = promiseAddonEvent("onDisabled");
  addon.userDisabled = true;
  yield waitForAddonDisabled;

  // No embedded webextension should be currently around.
  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 0,
        "No embedded extension instance should be tracked here");

  const waitForAddonEnabled = promiseAddonEvent("onEnabled");
  addon.userDisabled = false;
  yield waitForAddonEnabled;

  // Only one embedded extension.
  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 1,
        "Got the expected number of tracked extension instances");

  const embeddedWebExtensionAfterEnabled = EmbeddedExtensionManager.embeddedExtensionsByAddonId.get(ID);
  notEqual(embeddedWebExtensionAfterEnabled, embeddedWebExtension,
           "Got a new EmbeddedExtension instance after the addon has been disabled and then enabled");

  startupInfo = BootstrapMonitor.started.get(ID);
  yield startupInfo.data.webExtension.startup();

  const waitForReinstalled = promiseAddonEvent("onInstalled");
  addon.reload();
  yield waitForReinstalled;

  // No leaked embedded extension after the previous reloads.
  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 1,
        "Got the expected number of tracked extension instances");

  const embeddedWebExtensionAfterReload = EmbeddedExtensionManager.embeddedExtensionsByAddonId.get(ID);
  notEqual(embeddedWebExtensionAfterReload, embeddedWebExtensionAfterEnabled,
           "Got a new EmbeddedExtension instance after the addon has been reloaded");

  startupInfo = BootstrapMonitor.started.get(ID);
  yield startupInfo.data.webExtension.startup();

  // Uninstall the test addon
  let waitUninstalled = promiseAddonEvent("onUninstalled");
  addon.uninstall();
  yield waitUninstalled;

  // No leaked embedded extension after uninstalling.
  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 0,
        "No embedded extension instance should be tracked after the addon uninstall");
});