summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_schemas_versioned.js
blob: 3dddbbc41be85dd1297764ee00677ce13abb137e (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
"use strict";

let json = [
  {
    namespace: "MV2",
    max_manifest_version: 2,

    properties: {
      PROP1: { value: 20 },
    },
  },
  {
    namespace: "MV3",
    min_manifest_version: 3,
    properties: {
      PROP1: { value: 20 },
    },
  },
  {
    namespace: "mixed",

    properties: {
      PROP_any: { value: 20 },
      PROP_mv3: {
        $ref: "submodule",
      },
    },
    types: [
      {
        id: "manifestTest",
        type: "object",
        properties: {
          // An example of extending the base type for permissions
          permissions: {
            type: "array",
            items: {
              $ref: "BaseType",
            },
            optional: true,
            default: [],
          },
          // An example of differentiating versions of a manifest entry
          multiple_choice: {
            optional: true,
            choices: [
              {
                max_manifest_version: 2,
                type: "array",
                items: {
                  type: "string",
                },
              },
              {
                min_manifest_version: 3,
                type: "array",
                items: {
                  type: "boolean",
                },
              },
              {
                type: "array",
                items: {
                  type: "object",
                  properties: {
                    value: { type: "boolean" },
                  },
                },
              },
            ],
          },
          accepting_unrecognized_props: {
            optional: true,
            type: "object",
            properties: {
              mv2_only_prop: {
                type: "string",
                optional: true,
                max_manifest_version: 2,
              },
              mv3_only_prop: {
                type: "string",
                optional: true,
                min_manifest_version: 3,
              },
              mv2_only_prop_with_default: {
                type: "string",
                optional: true,
                default: "only in MV2",
                max_manifest_version: 2,
              },
              mv3_only_prop_with_default: {
                type: "string",
                optional: true,
                default: "only in MV3",
                min_manifest_version: 3,
              },
            },
            additionalProperties: { $ref: "UnrecognizedProperty" },
          },
        },
      },
      {
        id: "submodule",
        type: "object",
        min_manifest_version: 3,
        functions: [
          {
            name: "sub_foo",
            type: "function",
            parameters: [],
            returns: { type: "integer" },
          },
          {
            name: "sub_no_match",
            type: "function",
            max_manifest_version: 2,
            parameters: [],
            returns: { type: "integer" },
          },
        ],
      },
      {
        id: "BaseType",
        choices: [
          {
            type: "string",
            enum: ["base"],
          },
        ],
      },
      {
        id: "type_any",
        type: "string",
        enum: ["value1", "value2", "value3"],
      },
      {
        id: "type_mv2",
        max_manifest_version: 2,
        type: "string",
        enum: ["value1", "value2", "value3"],
      },
      {
        id: "type_mv3",
        min_manifest_version: 3,
        type: "string",
        enum: ["value1", "value2", "value3"],
      },
      {
        id: "param_type_changed",
        type: "array",
        items: {
          choices: [
            { max_manifest_version: 2, type: "string" },
            {
              min_manifest_version: 3,
              type: "boolean",
            },
          ],
        },
      },
      {
        id: "object_type_changed",
        type: "object",
        properties: {
          prop_mv2: {
            type: "string",
            max_manifest_version: 2,
          },
          prop_mv3: {
            type: "string",
            min_manifest_version: 3,
          },
          prop_any: {
            type: "string",
          },
        },
      },
      {
        id: "no_valid_choices",
        type: "array",
        items: {
          choices: [
            { max_manifest_version: 1, type: "string" },
            {
              min_manifest_version: 4,
              type: "boolean",
            },
          ],
        },
      },
    ],

    functions: [
      {
        name: "fun_param_type_versioned",
        type: "function",
        parameters: [{ name: "arg1", $ref: "param_type_changed" }],
      },
      {
        name: "fun_mv2",
        max_manifest_version: 2,
        type: "function",
        parameters: [
          { name: "arg1", type: "integer", optional: true, default: 99 },
          { name: "arg2", type: "boolean", optional: true },
        ],
      },
      {
        name: "fun_mv3",
        min_manifest_version: 3,
        type: "function",
        parameters: [
          { name: "arg1", type: "integer", optional: true, default: 99 },
          { name: "arg2", type: "boolean", optional: true },
        ],
      },
      {
        name: "fun_param_change",
        type: "function",
        parameters: [{ name: "arg1", $ref: "object_type_changed" }],
      },
      {
        name: "fun_no_valid_param",
        type: "function",
        parameters: [{ name: "arg1", $ref: "no_valid_choices" }],
      },
    ],
    events: [
      {
        name: "onEvent_any",
        type: "function",
      },
      {
        name: "onEvent_mv2",
        max_manifest_version: 2,
        type: "function",
      },
      {
        name: "onEvent_mv3",
        min_manifest_version: 3,
        type: "function",
      },
    ],
  },
  {
    namespace: "mixed",
    types: [
      {
        $extend: "BaseType",
        choices: [
          {
            min_manifest_version: 3,
            type: "string",
            enum: ["extended"],
          },
        ],
      },
    ],
  },
  {
    namespace: "mixed",
    types: [
      {
        $extend: "manifestTest",
        properties: {
          versioned_extend: {
            optional: true,
            // just a simple type here
            type: "string",
            max_manifest_version: 2,
          },
        },
      },
    ],
  },
];

add_task(async function setup() {
  let url = "data:," + JSON.stringify(json);
  Schemas._rootSchema = null;
  await Schemas.load(url);

  // We want the actual errors thrown here, and not warnings recast as errors.
  ExtensionTestUtils.failOnSchemaWarnings(false);
});

add_task(async function test_inject_V2() {
  // Test injecting into a V2 context.
  let wrapper = getContextWrapper(2);

  let root = {};
  Schemas.inject(root, wrapper);

  // Test elements available to both
  Assert.equal(root.mixed.type_any.VALUE1, "value1", "type_any exists");
  Assert.equal(root.mixed.PROP_any, 20, "mixed value property");

  // Test elements available to MV2
  Assert.equal(root.MV2.PROP1, 20, "MV2 value property");
  Assert.equal(root.mixed.type_mv2.VALUE2, "value2", "type_mv2 exists");

  // Test MV3 elements not available
  Assert.equal(root.MV3, undefined, "MV3 not injected");
  Assert.ok(!("MV3" in root), "MV3 not enumerable");
  Assert.equal(
    root.mixed.PROP_mv3,
    undefined,
    "mixed submodule property does not exist"
  );
  Assert.ok(
    !("PROP_mv3" in root.mixed),
    "mixed submodule property not enumerable"
  );
  Assert.equal(root.mixed.type_mv3, undefined, "type_mv3 does not exist");

  // Function tests
  Assert.ok(
    "fun_param_type_versioned" in root.mixed,
    "fun_param_type_versioned exists"
  );
  Assert.ok(
    !!root.mixed.fun_param_type_versioned,
    "fun_param_type_versioned exists"
  );
  Assert.ok("fun_mv2" in root.mixed, "fun_mv2 exists");
  Assert.ok(!!root.mixed.fun_mv2, "fun_mv2 exists");
  Assert.ok(!("fun_mv3" in root.mixed), "fun_mv3 does not exist");
  Assert.ok(!root.mixed.fun_mv3, "fun_mv3 does not exist");

  // Event tests
  Assert.ok("onEvent_any" in root.mixed, "onEvent_any exists");
  Assert.ok(!!root.mixed.onEvent_any, "onEvent_any exists");
  Assert.ok("onEvent_mv2" in root.mixed, "onEvent_mv2 exists");
  Assert.ok(!!root.mixed.onEvent_mv2, "onEvent_mv2 exists");
  Assert.ok(!("onEvent_mv3" in root.mixed), "onEvent_mv3 does not exist");
  Assert.ok(!root.mixed.onEvent_mv3, "onEvent_mv3 does not exist");

  // Function call tests
  root.mixed.fun_param_type_versioned(["hello"]);
  wrapper.verify("call", "mixed", "fun_param_type_versioned", [["hello"]]);
  Assert.throws(
    () => root.mixed.fun_param_type_versioned([true]),
    /Expected string instead of true/,
    "fun_param_type_versioned should throw for invalid type"
  );

  let propObj = { prop_any: "prop_any", prop_mv2: "prop_mv2" };
  root.mixed.fun_param_change(propObj);
  wrapper.verify("call", "mixed", "fun_param_change", [propObj]);

  // Still throw same error as we did before we knew of the MV3 property.
  Assert.throws(
    () => root.mixed.fun_param_change({ prop_mv3: "prop_mv3", ...propObj }),
    /Type error for parameter arg1 \(Unexpected property "prop_mv3"\)/,
    "generic unexpected property message for MV3 property in MV2 extension"
  );

  // But print the more specific and descriptive warning message to console.
  wrapper.checkErrors([
    `Property "prop_mv3" is unsupported in Manifest Version 2`,
  ]);

  Assert.throws(
    () => root.mixed.fun_no_valid_param("anything"),
    /Incorrect argument types for mixed.fun_no_valid_param/,
    "fun_no_valid_param should throw for versioned type"
  );
});

function normalizeTest(manifest, test, wrapper) {
  let normalized = Schemas.normalize(manifest, "mixed.manifestTest", wrapper);
  test(normalized);
  // The test function should call wrapper.checkErrors if it expected errors.
  // Here we call checkErrors again to ensure that there are not any unexpected
  // errors left.
  wrapper.checkErrors([]);
}

add_task(async function test_normalize_V2() {
  let wrapper = getContextWrapper(2);

  // Test normalize additions to the manifest structure
  normalizeTest(
    {
      versioned_extend: "test",
    },
    normalized => {
      Assert.equal(
        normalized.value.versioned_extend,
        "test",
        "resources normalized"
      );
    },
    wrapper
  );

  // Test normalizing baseType
  normalizeTest(
    {
      permissions: ["base"],
    },
    normalized => {
      Assert.equal(
        normalized.value.permissions[0],
        "base",
        "resources normalized"
      );
    },
    wrapper
  );

  normalizeTest(
    {
      permissions: ["extended"],
    },
    normalized => {
      Assert.ok(
        normalized.error.startsWith("Error processing permissions.0"),
        `manifest normalized error ${normalized.error}`
      );
    },
    wrapper
  );

  // Test normalizing a value
  normalizeTest(
    {
      multiple_choice: ["foo.html"],
    },
    normalized => {
      Assert.equal(
        normalized.value.multiple_choice[0],
        "foo.html",
        "resources normalized"
      );
    },
    wrapper
  );

  normalizeTest(
    {
      multiple_choice: [true],
    },
    normalized => {
      Assert.ok(
        normalized.error.startsWith("Error processing multiple_choice"),
        "manifest error"
      );
    },
    wrapper
  );

  normalizeTest(
    {
      multiple_choice: [
        {
          value: true,
        },
      ],
    },
    normalized => {
      Assert.ok(
        normalized.value.multiple_choice[0].value,
        "resources normalized"
      );
    },
    wrapper
  );

  // Tests that object definitions including additionalProperties can
  // successfully accept objects from another manifest version, while ignoring
  // the actual value from the non-matching manifest value.
  normalizeTest(
    {
      accepting_unrecognized_props: {
        mv2_only_prop: "mv2 here",
        mv3_only_prop: "mv3 here",
      },
    },
    normalized => {
      equal(normalized.error, undefined, "no normalization error");
      Assert.deepEqual(
        normalized.value.accepting_unrecognized_props,
        {
          mv2_only_prop: "mv2 here",
          mv2_only_prop_with_default: "only in MV2",
        },
        "Normalized object for MV2, without MV3-specific props"
      );
      wrapper.checkErrors([
        `Property "mv3_only_prop" is unsupported in Manifest Version 2`,
      ]);
    },
    wrapper
  );
});

add_task(async function test_inject_V3() {
  // Test injecting into a V3 context.
  let wrapper = getContextWrapper(3);

  let root = {};
  Schemas.inject(root, wrapper);

  // Test elements available to both
  Assert.equal(root.mixed.type_any.VALUE1, "value1", "type_any exists");
  Assert.equal(root.mixed.PROP_any, 20, "mixed value property");

  // Test elements available to MV2
  Assert.equal(root.MV2, undefined, "MV2 value property");
  Assert.ok(!("MV2" in root), "MV2 not enumerable");
  Assert.equal(root.mixed.type_mv2, undefined, "type_mv2 does not exist");
  Assert.ok(!("type_mv2" in root.mixed), "type_mv2 not enumerable");

  // Test MV3 elements not available
  Assert.equal(root.MV3.PROP1, 20, "MV3 injected");
  Assert.ok(!!root.mixed.PROP_mv3, "mixed submodule property exists");
  Assert.equal(root.mixed.type_mv3.VALUE3, "value3", "type_mv3 exists");

  // Versioned submodule
  Assert.ok(!!root.mixed.PROP_mv3.sub_foo, "mixed submodule sub_foo exists");
  Assert.ok(
    !root.mixed.PROP_mv3.sub_no_match,
    "mixed submodule sub_no_match does not exist"
  );
  Assert.ok(
    !("sub_no_match" in root.mixed.PROP_mv3),
    "mixed submodule sub_no_match is not enumerable"
  );

  // Function tests
  Assert.ok(
    "fun_param_type_versioned" in root.mixed,
    "fun_param_type_versioned exists"
  );
  Assert.ok(
    !!root.mixed.fun_param_type_versioned,
    "fun_param_type_versioned exists"
  );
  Assert.ok(!("fun_mv2" in root.mixed), "fun_mv2 does not exist");
  Assert.ok(!root.mixed.fun_mv2, "fun_mv2 does not exist");
  Assert.ok("fun_mv3" in root.mixed, "fun_mv3 exists");
  Assert.ok(!!root.mixed.fun_mv3, "fun_mv3 exists");

  // Event tests
  Assert.ok("onEvent_any" in root.mixed, "onEvent_any exists");
  Assert.ok(!!root.mixed.onEvent_any, "onEvent_any exists");
  Assert.ok(!("onEvent_mv2" in root.mixed), "onEvent_mv2 not enumerable");
  Assert.ok(!root.mixed.onEvent_mv2, "onEvent_mv2 does not exist");
  Assert.ok("onEvent_mv3" in root.mixed, "onEvent_mv3 exists");
  Assert.ok(!!root.mixed.onEvent_mv3, "onEvent_mv3 exists");

  // Function call tests
  root.mixed.fun_param_type_versioned([true]);
  wrapper.verify("call", "mixed", "fun_param_type_versioned", [[true]]);
  Assert.throws(
    () => root.mixed.fun_param_type_versioned(["hello"]),
    /Expected boolean instead of "hello"/,
    "should throw for invalid type"
  );

  let propObj = { prop_any: "prop_any", prop_mv3: "prop_mv3" };
  root.mixed.fun_param_change(propObj);
  wrapper.verify("call", "mixed", "fun_param_change", [propObj]);
  Assert.throws(
    () => root.mixed.fun_param_change({ prop_mv2: "prop_mv2", ...propObj }),
    /Unexpected property "prop_mv2"/,
    "should throw for versioned type"
  );
  wrapper.checkErrors([
    `Property "prop_mv2" is unsupported in Manifest Version 3`,
  ]);

  root.mixed.PROP_mv3.sub_foo();
  wrapper.verify("call", "mixed.PROP_mv3", "sub_foo", []);
  Assert.throws(
    () => root.mixed.PROP_mv3.sub_no_match(),
    /TypeError: root.mixed.PROP_mv3.sub_no_match is not a function/,
    "sub_no_match should throw"
  );
});

add_task(async function test_normalize_V3() {
  let wrapper = getContextWrapper(3);

  // Test normalize additions to the manifest structure
  normalizeTest(
    {
      versioned_extend: "test",
    },
    normalized => {
      Assert.equal(
        normalized.error,
        `Unexpected property "versioned_extend"`,
        "expected manifest error"
      );
      wrapper.checkErrors([
        `Property "versioned_extend" is unsupported in Manifest Version 3`,
      ]);
    },
    wrapper
  );

  // Test normalizing baseType
  normalizeTest(
    {
      permissions: ["base"],
    },
    normalized => {
      Assert.equal(
        normalized.value.permissions[0],
        "base",
        "resources normalized"
      );
    },
    wrapper
  );

  normalizeTest(
    {
      permissions: ["extended"],
    },
    normalized => {
      Assert.equal(
        normalized.value.permissions[0],
        "extended",
        "resources normalized"
      );
    },
    wrapper
  );

  // Test normalizing a value
  normalizeTest(
    {
      multiple_choice: ["foo.html"],
    },
    normalized => {
      Assert.ok(
        normalized.error.startsWith("Error processing multiple_choice"),
        "manifest error"
      );
    },
    wrapper
  );

  normalizeTest(
    {
      multiple_choice: [true],
    },
    normalized => {
      Assert.equal(
        normalized.value.multiple_choice[0],
        true,
        "resources normalized"
      );
    },
    wrapper
  );

  normalizeTest(
    {
      multiple_choice: [
        {
          value: true,
        },
      ],
    },
    normalized => {
      Assert.ok(
        normalized.value.multiple_choice[0].value,
        "resources normalized"
      );
    },
    wrapper
  );

  wrapper.tallied = null;

  normalizeTest(
    {},
    normalized => {
      ok(!normalized.error, "manifest normalized");
    },
    wrapper
  );

  // Tests that object definitions including additionalProperties can
  // successfully accept objects from another manifest version, while ignoring
  // the actual value from the non-matching manifest value.
  normalizeTest(
    {
      accepting_unrecognized_props: {
        mv2_only_prop: "mv2 here",
        mv3_only_prop: "mv3 here",
      },
    },
    normalized => {
      equal(normalized.error, undefined, "no normalization error");
      Assert.deepEqual(
        normalized.value.accepting_unrecognized_props,
        {
          mv3_only_prop: "mv3 here",
          mv3_only_prop_with_default: "only in MV3",
        },
        "Normalized object for MV3, without MV2-specific props"
      );
      wrapper.checkErrors([
        `Property "mv2_only_prop" is unsupported in Manifest Version 3`,
      ]);
    },
    wrapper
  );
});