summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_schema_change.js
blob: 591bf6eb56711e699b8e50e9d5eb26bb2ba5ab32 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const PREF_DB_SCHEMA = "extensions.databaseSchema";
const PREF_IS_EMBEDDED = "extensions.isembedded";

registerCleanupFunction(() => {
  Services.prefs.clearUserPref(PREF_IS_EMBEDDED);
});

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

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

add_task(async function test_setup() {
  await promiseStartupManager();
});

add_task(async function run_tests() {
  // Fake installTelemetryInfo used in the addon installation,
  // to verify that they are preserved after the DB is updated
  // from the addon manifests.
  const fakeInstallTelemetryInfo = { source: "amo", method: "amWebAPI" };

  const ID = "schema-change@tests.mozilla.org";

  const xpi1 = createTempWebExtensionFile({
    manifest: {
      name: "Test Add-on",
      version: "1.0",
      browser_specific_settings: { gecko: { id: ID } },
    },
  });

  const xpi2 = createTempWebExtensionFile({
    manifest: {
      name: "Test Add-on 2",
      version: "2.0",
      browser_specific_settings: { gecko: { id: ID } },
    },
  });

  let xpiPath = PathUtils.join(profileDir.path, `${ID}.xpi`);

  const TESTS = [
    {
      what: "Schema change with no application update reloads metadata.",
      expectedVersion: "2.0",
      action() {
        Services.prefs.setIntPref(PREF_DB_SCHEMA, 0);
      },
    },
    {
      what: "Application update with no schema change does not reload metadata.",
      expectedVersion: "1.0",
      action() {
        gAppInfo.version = "2";
      },
    },
    {
      what: "App update and a schema change causes a reload of the manifest.",
      expectedVersion: "2.0",
      action() {
        gAppInfo.version = "3";
        Services.prefs.setIntPref(PREF_DB_SCHEMA, 0);
      },
    },
    {
      what: "No schema change, no manifest reload.",
      expectedVersion: "1.0",
      action() {},
    },
    {
      what: "Modified timestamp on the XPI causes a reload of the manifest.",
      expectedVersion: "2.0",
      async action() {
        let stat = await IOUtils.stat(xpiPath);
        let newLastModTime = stat.lastModified + 60 * 1000;
        await IOUtils.setModificationTime(xpiPath, newLastModTime);
      },
    },
  ];

  for (let test of TESTS) {
    info(test.what);
    await promiseInstallFile(xpi1, false, fakeInstallTelemetryInfo);

    let addon = await promiseAddonByID(ID);
    notEqual(addon, null, "Got an addon object as expected");
    equal(addon.version, "1.0", "Got the expected version");
    Assert.deepEqual(
      addon.installTelemetryInfo,
      fakeInstallTelemetryInfo,
      "Got the expected installTelemetryInfo after installing the addon"
    );

    await promiseShutdownManager();

    let fileInfo = await IOUtils.stat(xpiPath);

    xpi2.copyTo(profileDir, `${ID}.xpi`);

    // Make sure the timestamp of the extension is unchanged, so it is not
    // re-scanned for that reason.
    await IOUtils.setModificationTime(xpiPath, fileInfo.lastModified);

    await test.action();

    await promiseStartupManager();

    addon = await promiseAddonByID(ID);
    notEqual(addon, null, "Got an addon object as expected");
    equal(addon.version, test.expectedVersion, "Got the expected version");
    Assert.deepEqual(
      addon.installTelemetryInfo,
      fakeInstallTelemetryInfo,
      "Got the expected installTelemetryInfo after rebuilding the DB"
    );

    await addon.uninstall();
  }
});

add_task(async function embedder_disabled_stays_disabled() {
  Services.prefs.setBoolPref(PREF_IS_EMBEDDED, true);

  const ID = "embedder-disabled@tests.mozilla.org";

  await promiseInstallWebExtension({
    manifest: {
      name: "Test Add-on",
      version: "1.0",
      browser_specific_settings: { gecko: { id: ID } },
    },
  });

  let addon = await promiseAddonByID(ID);

  equal(addon.embedderDisabled, false);

  await addon.setEmbedderDisabled(true);
  equal(addon.embedderDisabled, true);

  await promiseShutdownManager();

  // Change db schema to force reload
  Services.prefs.setIntPref(PREF_DB_SCHEMA, 0);

  await promiseStartupManager();

  addon = await promiseAddonByID(ID);
  equal(addon.embedderDisabled, true);

  await addon.uninstall();
});