summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_startup_scan.js
blob: 67c75cbd176323afe7d9100becb58c4bccfdfe02 (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
"use strict";

// Turn off startup scanning.
Services.prefs.setIntPref("extensions.startupScanScopes", 0);

createAppInfo("xpcshell@tessts.mozilla.org", "XPCShell", "42", "42");
// Prevent XPIStates.scanForChanges from seeing this as an update and forcing a
// full scan.
Services.prefs.setCharPref(
  "extensions.lastAppBuildId",
  Services.appinfo.appBuildID
);

// A small bootstrap calls monitor targeting a single extension (created to avoid introducing a workaround
// in BootstrapMonitor to be able to test Bug 1664144 fix).
let Monitor = {
  extensionId: undefined,
  collected: [],
  init() {
    const bootstrapCallListener = (_evtName, data) => {
      if (data.params.id == this.extensionId) {
        this.collected.push(data);
      }
    };
    AddonTestUtils.on("bootstrap-method", bootstrapCallListener);
    registerCleanupFunction(() => {
      AddonTestUtils.off("bootstrap-method", bootstrapCallListener);
    });
  },
  startCollecting(extensionId) {
    this.extensionId = extensionId;
  },
  stopCollecting() {
    this.extensionId = undefined;
  },
  getCollected() {
    const collected = this.collected;
    this.collected = [];
    return collected;
  },
};

Monitor.init();

// Bug 1664144: Test that during startup scans, updating an addon
// that has already started is restarted.
add_task(async function test_startup_sideload_updated() {
  const ID = "sideload@tests.mozilla.org";

  await createWebExtension(ID, initialVersion("1"), profileDir);
  await promiseStartupManager();

  // Ensure the sideload is enabled and running.
  let addon = await promiseAddonByID(ID);

  Monitor.startCollecting(ID);
  await addon.enable();
  Monitor.stopCollecting();

  let events = Monitor.getCollected();
  ok(events.length, "bootstrap methods called");
  equal(
    events[0].reason,
    BOOTSTRAP_REASONS.ADDON_ENABLE,
    "Startup reason is ADDON_ENABLE at install"
  );

  await promiseShutdownManager();
  // Touch the addon on disk before startup.
  await createWebExtension(ID, initialVersion("1.1"), profileDir);
  Monitor.startCollecting(ID);
  await promiseStartupManager();
  await AddonManagerPrivate.getNewSideloads();
  Monitor.stopCollecting();

  events = Monitor.getCollected().map(({ method, reason, params }) => {
    const { version } = params;
    return { method, reason, version };
  });

  const updatedVersion = "1.1.0";
  const expectedUpgradeParams = {
    reason: BOOTSTRAP_REASONS.ADDON_UPGRADE,
    version: updatedVersion,
  };

  const expectedCalls = [
    {
      method: "startup",
      reason: BOOTSTRAP_REASONS.APP_STARTUP,
      version: "1.0",
    },
    // Shutdown call has version 1.1 because the file was already
    // updated on disk and got the new version as part of the startup.
    { method: "shutdown", ...expectedUpgradeParams },
    { method: "update", ...expectedUpgradeParams },
    { method: "startup", ...expectedUpgradeParams },
  ];

  for (let i = 0; i < expectedCalls.length; i++) {
    Assert.deepEqual(
      events[i],
      expectedCalls[i],
      "Got the expected sequence of bootstrap method calls"
    );
  }

  equal(
    events.length,
    expectedCalls.length,
    "Got the expected number of bootstrap method calls"
  );

  // flush addonStartup.json
  await AddonTestUtils.loadAddonsList(true);
  // verify startupData is correct
  let startupData = aomStartup.readStartupData();
  Assert.equal(
    startupData["app-profile"].addons[ID].version,
    updatedVersion,
    "startup data is correct in cache"
  );

  await promiseShutdownManager();
});