summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_dictionary_webextension.js
blob: 847d519036ac3937b4b0dcddbf4632fa6389bb93 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
"use strict";

XPCOMUtils.defineLazyServiceGetter(
  this,
  "spellCheck",
  "@mozilla.org/spellchecker/engine;1",
  "mozISpellCheckingEngine"
);

add_task(async function setup() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "61", "61");

  // Initialize the URLPreloader so that we can load the built-in
  // add-ons list, which contains the list of built-in dictionaries.
  AddonTestUtils.initializeURLPreloader();

  await promiseStartupManager();

  // Starts collecting the Addon Manager Telemetry events.
  AddonTestUtils.hookAMTelemetryEvents();
});

add_task(
  {
    // We need to enable this pref because some assertions verify that
    // `installOrigins` is collected in some Telemetry events.
    pref_set: [["extensions.install_origins.enabled", true]],
  },
  async function test_validation() {
    await Assert.rejects(
      promiseInstallWebExtension({
        manifest: {
          browser_specific_settings: {
            gecko: { id: "en-US-no-dic@dictionaries.mozilla.org" },
          },
          dictionaries: {
            "en-US": "en-US.dic",
          },
        },
      }),
      /Expected file to be downloaded for install/
    );

    await Assert.rejects(
      promiseInstallWebExtension({
        manifest: {
          browser_specific_settings: {
            gecko: { id: "en-US-no-aff@dictionaries.mozilla.org" },
          },
          dictionaries: {
            "en-US": "en-US.dic",
          },
        },

        files: {
          "en-US.dic": "",
        },
      }),
      /Expected file to be downloaded for install/
    );

    let addon = await promiseInstallWebExtension({
      manifest: {
        browser_specific_settings: {
          gecko: { id: "en-US-1@dictionaries.mozilla.org" },
        },
        dictionaries: {
          "en-US": "en-US.dic",
        },
      },

      files: {
        "en-US.dic": "",
        "en-US.aff": "",
      },
    });

    let addon2 = await promiseInstallWebExtension({
      manifest: {
        browser_specific_settings: {
          gecko: { id: "en-US-2@dictionaries.mozilla.org" },
        },
        dictionaries: {
          "en-US": "dictionaries/en-US.dic",
        },
      },

      files: {
        "dictionaries/en-US.dic": "",
        "dictionaries/en-US.aff": "",
      },
    });

    await addon.uninstall();
    await addon2.uninstall();

    let amEvents = AddonTestUtils.getAMTelemetryEvents();

    let amInstallEvents = amEvents
      .filter(evt => evt.method === "install")
      .map(evt => {
        const { object, extra } = evt;
        return { object, extra };
      });

    Assert.deepEqual(
      amInstallEvents.filter(evt => evt.object === "unknown"),
      [
        {
          object: "unknown",
          extra: {
            step: "started",
            error: "ERROR_CORRUPT_FILE",
            install_origins: "0",
          },
        },
        {
          object: "unknown",
          extra: {
            step: "started",
            error: "ERROR_CORRUPT_FILE",
            install_origins: "0",
          },
        },
      ],
      "Got the expected install telemetry events for the corrupted dictionaries"
    );

    Assert.deepEqual(
      amInstallEvents.filter(evt => evt.extra.addon_id === addon.id),
      [
        {
          object: "dictionary",
          extra: { step: "started", addon_id: addon.id, install_origins: "0" },
        },
        {
          object: "dictionary",
          extra: {
            step: "completed",
            addon_id: addon.id,
            install_origins: "0",
          },
        },
      ],
      "Got the expected install telemetry events for the first installed dictionary"
    );

    Assert.deepEqual(
      amInstallEvents.filter(evt => evt.extra.addon_id === addon2.id),
      [
        {
          object: "dictionary",
          extra: { step: "started", addon_id: addon2.id, install_origins: "0" },
        },
        {
          object: "dictionary",
          extra: {
            step: "completed",
            addon_id: addon2.id,
            install_origins: "0",
          },
        },
      ],
      "Got the expected install telemetry events for the second installed dictionary"
    );

    let amUninstallEvents = amEvents
      .filter(evt => evt.method === "uninstall")
      .map(evt => {
        const { object, value } = evt;
        return { object, value };
      });

    Assert.deepEqual(
      amUninstallEvents,
      [
        { object: "dictionary", value: addon.id },
        { object: "dictionary", value: addon2.id },
      ],
      "Got the expected uninstall telemetry events"
    );
  }
);

const WORD = "Flehgragh";

add_task(async function test_registration() {
  spellCheck.dictionaries = ["en-US"];

  ok(!spellCheck.check(WORD), "Word should not pass check before add-on loads");

  let addon = await promiseInstallWebExtension({
    manifest: {
      browser_specific_settings: {
        gecko: { id: "en-US@dictionaries.mozilla.org" },
      },
      dictionaries: {
        "en-US": "en-US.dic",
      },
    },

    files: {
      "en-US.dic": `2\n${WORD}\nnativ/A\n`,
      "en-US.aff": `
SFX A Y 1
SFX A   0       en         [^elr]
      `,
    },
  });

  ok(
    spellCheck.check(WORD),
    "Word should pass check while add-on load is loaded"
  );
  ok(spellCheck.check("nativen"), "Words should have correct affixes");

  await addon.uninstall();

  await new Promise(executeSoon);

  ok(
    !spellCheck.check(WORD),
    "Word should not pass check after add-on unloads"
  );
});

add_task(function teardown_telemetry_events() {
  // Ignore any additional telemetry events collected in this file.
  AddonTestUtils.getAMTelemetryEvents();
});