summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/xpcshell/test_locale_data.js
blob: c3cd44e5779a8116a01bb61163d6a80c4af64a7b (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
"use strict";

Cu.import("resource://gre/modules/Extension.jsm");

/* globals ExtensionData */

const uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);

function* generateAddon(data) {
  let id = uuidGenerator.generateUUID().number;

  data = Object.assign({embedded: true}, data);
  data.manifest = Object.assign({applications: {gecko: {id}}}, data.manifest);

  let xpi = Extension.generateXPI(data);
  do_register_cleanup(() => {
    Services.obs.notifyObservers(xpi, "flush-cache-entry", null);
    xpi.remove(false);
  });

  let fileURI = Services.io.newFileURI(xpi);
  let jarURI = NetUtil.newURI(`jar:${fileURI.spec}!/webextension/`);

  let extension = new ExtensionData(jarURI);
  yield extension.readManifest();

  return extension;
}

add_task(function* testMissingDefaultLocale() {
  let extension = yield generateAddon({
    "files": {
      "_locales/en_US/messages.json": {},
    },
  });

  equal(extension.errors.length, 0, "No errors reported");

  yield extension.initAllLocales();

  equal(extension.errors.length, 1, "One error reported");

  do_print(`Got error: ${extension.errors[0]}`);

  ok(extension.errors[0].includes('"default_locale" property is required'),
     "Got missing default_locale error");
});


add_task(function* testInvalidDefaultLocale() {
  let extension = yield generateAddon({
    "manifest": {
      "default_locale": "en",
    },

    "files": {
      "_locales/en_US/messages.json": {},
    },
  });

  equal(extension.errors.length, 1, "One error reported");

  do_print(`Got error: ${extension.errors[0]}`);

  ok(extension.errors[0].includes("Loading locale file _locales/en/messages.json"),
     "Got invalid default_locale error");

  yield extension.initAllLocales();

  equal(extension.errors.length, 2, "Two errors reported");

  do_print(`Got error: ${extension.errors[1]}`);

  ok(extension.errors[1].includes('"default_locale" property must correspond'),
     "Got invalid default_locale error");
});


add_task(function* testUnexpectedDefaultLocale() {
  let extension = yield generateAddon({
    "manifest": {
      "default_locale": "en_US",
    },
  });

  equal(extension.errors.length, 1, "One error reported");

  do_print(`Got error: ${extension.errors[0]}`);

  ok(extension.errors[0].includes("Loading locale file _locales/en-US/messages.json"),
     "Got invalid default_locale error");

  yield extension.initAllLocales();

  equal(extension.errors.length, 2, "One error reported");

  do_print(`Got error: ${extension.errors[1]}`);

  ok(extension.errors[1].includes('"default_locale" property must correspond'),
     "Got unexpected default_locale error");
});


add_task(function* testInvalidSyntax() {
  let extension = yield generateAddon({
    "manifest": {
      "default_locale": "en_US",
    },

    "files": {
      "_locales/en_US/messages.json": '{foo: {message: "bar", description: "baz"}}',
    },
  });

  equal(extension.errors.length, 1, "No errors reported");

  do_print(`Got error: ${extension.errors[0]}`);

  ok(extension.errors[0].includes("Loading locale file _locales\/en_US\/messages\.json: SyntaxError"),
     "Got syntax error");

  yield extension.initAllLocales();

  equal(extension.errors.length, 2, "One error reported");

  do_print(`Got error: ${extension.errors[1]}`);

  ok(extension.errors[1].includes("Loading locale file _locales\/en_US\/messages\.json: SyntaxError"),
     "Got syntax error");
});