summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_schemas_roots.js
blob: 86f52bde7b3716ad527b5d19b0b074204485e29d (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
234
235
236
237
238
239
240
241
242
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const { SchemaRoot } = ChromeUtils.importESModule(
  "resource://gre/modules/Schemas.sys.mjs"
);

let { SchemaAPIInterface } = ExtensionCommon;

const global = this;

let baseSchemaJSON = [
  {
    namespace: "base",

    properties: {
      PROP1: { value: 42 },
    },

    types: [
      {
        id: "type1",
        type: "string",
        enum: ["value1", "value2", "value3"],
      },
    ],

    functions: [
      {
        name: "foo",
        type: "function",
        parameters: [{ name: "arg1", $ref: "type1" }],
      },
    ],
  },
];

let experimentFooJSON = [
  {
    namespace: "experiments.foo",
    types: [
      {
        id: "typeFoo",
        type: "string",
        enum: ["foo1", "foo2", "foo3"],
      },
    ],

    functions: [
      {
        name: "foo",
        type: "function",
        parameters: [
          { name: "arg1", $ref: "typeFoo" },
          { name: "arg2", $ref: "base.type1" },
        ],
      },
    ],
  },
];

let experimentBarJSON = [
  {
    namespace: "experiments.bar",
    types: [
      {
        id: "typeBar",
        type: "string",
        enum: ["bar1", "bar2", "bar3"],
      },
    ],

    functions: [
      {
        name: "bar",
        type: "function",
        parameters: [
          { name: "arg1", $ref: "typeBar" },
          { name: "arg2", $ref: "base.type1" },
        ],
      },
    ],
  },
];

let tallied = null;

function tally(kind, ns, name, args) {
  tallied = [kind, ns, name, args];
}

function verify(...args) {
  equal(JSON.stringify(tallied), JSON.stringify(args));
  tallied = null;
}

let talliedErrors = [];

let permissions = new Set();

class TallyingAPIImplementation extends SchemaAPIInterface {
  constructor(namespace, name) {
    super();
    this.namespace = namespace;
    this.name = name;
  }

  callFunction(args) {
    tally("call", this.namespace, this.name, args);
    if (this.name === "sub_foo") {
      return 13;
    }
  }

  callFunctionNoReturn(args) {
    tally("call", this.namespace, this.name, args);
  }

  getProperty() {
    tally("get", this.namespace, this.name);
  }

  setProperty(value) {
    tally("set", this.namespace, this.name, value);
  }

  addListener(listener, args) {
    tally("addListener", this.namespace, this.name, [listener, args]);
  }

  removeListener(listener) {
    tally("removeListener", this.namespace, this.name, [listener]);
  }

  hasListener(listener) {
    tally("hasListener", this.namespace, this.name, [listener]);
  }
}

let wrapper = {
  url: "moz-extension://b66e3509-cdb3-44f6-8eb8-c8b39b3a1d27/",
  manifestVersion: 2,

  cloneScope: global,

  checkLoadURL(url) {
    return !url.startsWith("chrome:");
  },

  preprocessors: {
    localize(value) {
      return value.replace(/__MSG_(.*?)__/g, (m0, m1) => `${m1.toUpperCase()}`);
    },
  },

  logError(message) {
    talliedErrors.push(message);
  },

  hasPermission(permission) {
    return permissions.has(permission);
  },

  shouldInject(ns, name) {
    return name != "do-not-inject";
  },

  getImplementation(namespace, name) {
    return new TallyingAPIImplementation(namespace, name);
  },
};

add_task(async function () {
  let baseSchemas = new Map([["resource://schemas/base.json", baseSchemaJSON]]);
  let experimentSchemas = new Map([
    ["resource://experiment-foo/schema.json", experimentFooJSON],
    ["resource://experiment-bar/schema.json", experimentBarJSON],
  ]);

  let baseSchema = new SchemaRoot(null, baseSchemas);
  let schema = new SchemaRoot(baseSchema, experimentSchemas);

  baseSchema.parseSchemas();
  schema.parseSchemas();

  let root = {};
  let base = {};

  tallied = null;

  baseSchema.inject(base, wrapper);
  schema.inject(root, wrapper);

  equal(typeof base.base, "object", "base.base exists");
  equal(typeof root.base, "object", "root.base exists");
  equal(typeof base.experiments, "undefined", "base.experiments exists not");
  equal(typeof root.experiments, "object", "root.experiments exists");
  equal(typeof root.experiments.foo, "object", "root.experiments.foo exists");
  equal(typeof root.experiments.bar, "object", "root.experiments.bar exists");

  equal(tallied, null);

  equal(root.base.PROP1, 42, "root.base.PROP1");
  equal(base.base.PROP1, 42, "root.base.PROP1");

  root.base.foo("value2");
  verify("call", "base", "foo", ["value2"]);

  base.base.foo("value3");
  verify("call", "base", "foo", ["value3"]);

  root.experiments.foo.foo("foo2", "value1");
  verify("call", "experiments.foo", "foo", ["foo2", "value1"]);

  root.experiments.bar.bar("bar2", "value1");
  verify("call", "experiments.bar", "bar", ["bar2", "value1"]);

  Assert.throws(
    () => root.base.foo("Meh."),
    /Type error for parameter arg1/,
    "root.base.foo()"
  );

  Assert.throws(
    () => base.base.foo("Meh."),
    /Type error for parameter arg1/,
    "base.base.foo()"
  );

  Assert.throws(
    () => root.experiments.foo.foo("Meh."),
    /Incorrect argument types/,
    "root.experiments.foo.foo()"
  );

  Assert.throws(
    () => root.experiments.bar.bar("Meh."),
    /Incorrect argument types/,
    "root.experiments.bar.bar()"
  );
});