Closed Bug 1534969 Opened 2 years ago Closed 2 years ago

browser tests: is unset after extension.startup() when useAddonManager is set


(WebExtensions :: General, defect, P1)



(firefox-esr60 unaffected, firefox65 wontfix, firefox66 wontfix, firefox67 fixed)

Tracking Status
firefox-esr60 --- unaffected
firefox65 --- wontfix
firefox66 --- wontfix
firefox67 --- fixed


(Reporter: robwu, Assigned: robwu)




(1 file)

Both xpcshell and browser tests have an ExtensionTestUtils.loadExtension method to generate an extension for testing, with a common interface. is one of the commonly used properties, but it is not reliable in browser tests (i.e. unset).

Minimal test case:

// Let this be a browser test
add_task(async () => {
  let ext = ExtensionTestUtils.loadExtension({
    manifest: {},
    useAddonManager: "temporary", // or "permanent"
  await ext.startup();
  Assert.ok(, "Extension ID must be set");
  await ext.unnload();

The above assertion fails. This is because the following happens:

  1. ExtensionTestUtils.loadExtension forwards the extension creation request to the main process, where ExtensionTestCommon.generate in SpecialPowersObserverAPI.js is called.
  2. ExtensionTestCommon.generate returns a MockExtension (instead of an Extension) because the useAddonManager property is set.
  3. When the test starts the extension, a listener to the "startup" event is added, which is responsible for forwarding the extension ID (and UUID) to the test runner.

Since the extension is a MockExtension, the extension.on("startup", ...) call is handled by MockExtension. However, the event registration in MockExtension is deferred until the extension has started:,93,110-114,126,140
Consequently, the test will miss the startup event from Extension.jsm, and the extension ID is never forwarded to the test runner.

This issue is currently hidden in most tests, because ext-backgroundPage.js also triggers the "startup" event, and most tests have a background page.
I have however observed this issue multiple times during development, and after observing it once again while working on bug 1525729, I decided to investigate and fix the root cause.

I'm going to propose a patch with minimal changes (patching MockExtension), since we're close to the merge date.

The original implementation in SpecialPowersObserverAPI used the "startup" event on the Management object, but started using the "startup" event on extension instead in (to fix memory leaks).
These memory leaks happened because when extension is a MockExtension (i.e. in browser tests where useAddonManager is used), the ext == extension condition never passed (because ext is an actual Extension), and the"startup", startupListener); was never called (and the extension ID is not set either).

If we're able to identify the correct extension in the "startup" event of Management, then we can use that instead of the "startup" event of extension, and resolve bug 1522973 by removing the extension.emit("startup", extension); call.

See Also: → 1522973
Pushed by
Ensure that is set in browser tests r=aswan
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla67

Is QA validation needed for this fix? If yes, please provide some info on how to validate. Thanks!

Flags: needinfo?(rob)
Flags: needinfo?(rob) → qe-verify-
You need to log in before you can comment on or make changes to this bug.