summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_telemetry.js
blob: c651e4673249529eaf86bfa99f379699980fed20 (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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
"use strict";

const { TelemetryArchive } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryArchive.sys.mjs"
);
const { TelemetryUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryUtils.sys.mjs"
);
const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

const { TelemetryArchiveTesting } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryArchiveTesting.sys.mjs"
);

const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

// All tests run privileged unless otherwise specified not to.
function createExtension(
  backgroundScript,
  permissions,
  isPrivileged = true,
  telemetry
) {
  let extensionData = {
    background: backgroundScript,
    manifest: { permissions, telemetry },
    isPrivileged,
  };

  return ExtensionTestUtils.loadExtension(extensionData);
}

async function run(test) {
  let extension = createExtension(
    test.backgroundScript,
    test.permissions || ["telemetry"],
    test.isPrivileged,
    test.telemetry
  );
  await extension.startup();
  await extension.awaitFinish(test.doneSignal);
  await extension.unload();
}

// Currently unsupported on Android: blocked on 1220177.
// See 1280234 c67 for discussion.
if (AppConstants.MOZ_BUILD_APP === "browser") {
  add_task(async function test_telemetry_without_telemetry_permission() {
    await run({
      backgroundScript: () => {
        browser.test.assertTrue(
          !browser.telemetry,
          "'telemetry' permission is required"
        );
        browser.test.notifyPass("telemetry_permission");
      },
      permissions: [],
      doneSignal: "telemetry_permission",
      isPrivileged: false,
    });
  });

  add_task(
    async function test_telemetry_without_telemetry_permission_privileged() {
      await run({
        backgroundScript: () => {
          browser.test.assertTrue(
            !browser.telemetry,
            "'telemetry' permission is required"
          );
          browser.test.notifyPass("telemetry_permission");
        },
        permissions: [],
        doneSignal: "telemetry_permission",
      });
    }
  );

  add_task(async function test_telemetry_scalar_add() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.scalarAdd(
          "telemetry.test.unsigned_int_kind",
          1
        );
        browser.test.notifyPass("scalar_add");
      },
      doneSignal: "scalar_add",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("parent", false, true),
      "telemetry.test.unsigned_int_kind",
      1
    );
  });

  add_task(async function test_telemetry_scalar_add_unknown_name() {
    let { messages } = await promiseConsoleOutput(async () => {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.scalarAdd("telemetry.test.does_not_exist", 1);
          browser.test.notifyPass("scalar_add_unknown_name");
        },
        doneSignal: "scalar_add_unknown_name",
      });
    });
    Assert.ok(
      messages.find(({ message }) => message.includes("Unknown scalar")),
      "Telemetry should warn if an unknown scalar is incremented"
    );
  });

  add_task(async function test_telemetry_scalar_add_illegal_value() {
    await run({
      backgroundScript: () => {
        browser.test.assertThrows(
          () =>
            browser.telemetry.scalarAdd("telemetry.test.unsigned_int_kind", {}),
          /Incorrect argument types for telemetry.scalarAdd/,
          "The second 'value' argument to scalarAdd must be an integer, string, or boolean"
        );
        browser.test.notifyPass("scalar_add_illegal_value");
      },
      doneSignal: "scalar_add_illegal_value",
    });
  });

  add_task(async function test_telemetry_scalar_add_invalid_keyed_scalar() {
    let { messages } = await promiseConsoleOutput(async function () {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.scalarAdd(
            "telemetry.test.keyed_unsigned_int",
            1
          );
          browser.test.notifyPass("scalar_add_invalid_keyed_scalar");
        },
        doneSignal: "scalar_add_invalid_keyed_scalar",
      });
    });
    Assert.ok(
      messages.find(({ message }) =>
        message.includes("Attempting to manage a keyed scalar as a scalar")
      ),
      "Telemetry should warn if a scalarAdd is called for a keyed scalar"
    );
  });

  add_task(async function test_telemetry_scalar_set_bool_true() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.scalarSet("telemetry.test.boolean_kind", true);
        browser.test.notifyPass("scalar_set_bool_true");
      },
      doneSignal: "scalar_set_bool_true",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("parent", false, true),
      "telemetry.test.boolean_kind",
      true
    );
  });

  add_task(async function test_telemetry_scalar_set_bool_false() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.scalarSet("telemetry.test.boolean_kind", false);
        browser.test.notifyPass("scalar_set_bool_false");
      },
      doneSignal: "scalar_set_bool_false",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("parent", false, true),
      "telemetry.test.boolean_kind",
      false
    );
  });

  add_task(async function test_telemetry_scalar_unset_bool() {
    Services.telemetry.clearScalars();
    TelemetryTestUtils.assertScalarUnset(
      TelemetryTestUtils.getProcessScalars("parent", false, true),
      "telemetry.test.boolean_kind"
    );
  });

  add_task(async function test_telemetry_scalar_set_unknown_name() {
    let { messages } = await promiseConsoleOutput(async function () {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.scalarSet(
            "telemetry.test.does_not_exist",
            true
          );
          browser.test.notifyPass("scalar_set_unknown_name");
        },
        doneSignal: "scalar_set_unknown_name",
      });
    });
    Assert.ok(
      messages.find(({ message }) => message.includes("Unknown scalar")),
      "Telemetry should warn if an unknown scalar is set"
    );
  });

  add_task(async function test_telemetry_scalar_set_zero() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.scalarSet(
          "telemetry.test.unsigned_int_kind",
          0
        );
        browser.test.notifyPass("scalar_set_zero");
      },
      doneSignal: "scalar_set_zero",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("parent", false, true),
      "telemetry.test.unsigned_int_kind",
      0
    );
  });

  add_task(async function test_telemetry_scalar_set_maximum() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.scalarSetMaximum(
          "telemetry.test.unsigned_int_kind",
          123
        );
        browser.test.notifyPass("scalar_set_maximum");
      },
      doneSignal: "scalar_set_maximum",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("parent", false, true),
      "telemetry.test.unsigned_int_kind",
      123
    );
  });

  add_task(async function test_telemetry_scalar_set_maximum_unknown_name() {
    let { messages } = await promiseConsoleOutput(async function () {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.scalarSetMaximum(
            "telemetry.test.does_not_exist",
            1
          );
          browser.test.notifyPass("scalar_set_maximum_unknown_name");
        },
        doneSignal: "scalar_set_maximum_unknown_name",
      });
    });
    Assert.ok(
      messages.find(({ message }) => message.includes("Unknown scalar")),
      "Telemetry should warn if an unknown scalar is set"
    );
  });

  add_task(async function test_telemetry_scalar_set_maximum_illegal_value() {
    await run({
      backgroundScript: () => {
        browser.test.assertThrows(
          () =>
            browser.telemetry.scalarSetMaximum(
              "telemetry.test.unsigned_int_kind",
              "string"
            ),
          /Incorrect argument types for telemetry.scalarSetMaximum/,
          "The second 'value' argument to scalarSetMaximum must be a scalar"
        );
        browser.test.notifyPass("scalar_set_maximum_illegal_value");
      },
      doneSignal: "scalar_set_maximum_illegal_value",
    });
  });

  add_task(async function test_telemetry_keyed_scalar_add() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.keyedScalarAdd(
          "telemetry.test.keyed_unsigned_int",
          "foo",
          1
        );
        browser.test.notifyPass("keyed_scalar_add");
      },
      doneSignal: "keyed_scalar_add",
    });
    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "telemetry.test.keyed_unsigned_int",
      "foo",
      1
    );
  });

  add_task(async function test_telemetry_keyed_scalar_add_unknown_name() {
    let { messages } = await promiseConsoleOutput(async () => {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.keyedScalarAdd(
            "telemetry.test.does_not_exist",
            "foo",
            1
          );
          browser.test.notifyPass("keyed_scalar_add_unknown_name");
        },
        doneSignal: "keyed_scalar_add_unknown_name",
      });
    });
    Assert.ok(
      messages.find(({ message }) => message.includes("Unknown scalar")),
      "Telemetry should warn if an unknown keyed scalar is incremented"
    );
  });

  add_task(async function test_telemetry_keyed_scalar_add_illegal_value() {
    await run({
      backgroundScript: () => {
        browser.test.assertThrows(
          () =>
            browser.telemetry.keyedScalarAdd(
              "telemetry.test.keyed_unsigned_int",
              "foo",
              {}
            ),
          /Incorrect argument types for telemetry.keyedScalarAdd/,
          "The second 'value' argument to keyedScalarAdd must be an integer, string, or boolean"
        );
        browser.test.notifyPass("keyed_scalar_add_illegal_value");
      },
      doneSignal: "keyed_scalar_add_illegal_value",
    });
  });

  add_task(async function test_telemetry_keyed_scalar_add_invalid_scalar() {
    let { messages } = await promiseConsoleOutput(async function () {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.keyedScalarAdd(
            "telemetry.test.unsigned_int_kind",
            "foo",
            1
          );
          browser.test.notifyPass("keyed_scalar_add_invalid_scalar");
        },
        doneSignal: "keyed_scalar_add_invalid_scalar",
      });
    });
    Assert.ok(
      messages.find(({ message }) =>
        message.includes(
          "Attempting to manage a keyed scalar as a scalar (or vice-versa)"
        )
      ),
      "Telemetry should warn if a scalar is incremented as a keyed scalar"
    );
  });

  add_task(async function test_telemetry_keyed_scalar_add_long_key() {
    let { messages } = await promiseConsoleOutput(async () => {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.keyedScalarAdd(
            "telemetry.test.keyed_unsigned_int",
            "X".repeat(73),
            1
          );
          browser.test.notifyPass("keyed_scalar_add_long_key");
        },
        doneSignal: "keyed_scalar_add_long_key",
      });
    });
    Assert.ok(
      messages.find(({ message }) =>
        message.includes("The key length must be limited to 72 characters.")
      ),
      "Telemetry should warn if keyed scalar's key is too long"
    );
  });

  add_task(async function test_telemetry_keyed_scalar_set() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.keyedScalarSet(
          "telemetry.test.keyed_boolean_kind",
          "foo",
          true
        );
        browser.test.notifyPass("keyed_scalar_set");
      },
      doneSignal: "keyed_scalar_set",
    });
    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "telemetry.test.keyed_boolean_kind",
      "foo",
      true
    );
  });

  add_task(async function test_telemetry_keyed_scalar_set_unknown_name() {
    let { messages } = await promiseConsoleOutput(async function () {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.keyedScalarSet(
            "telemetry.test.does_not_exist",
            "foo",
            true
          );
          browser.test.notifyPass("keyed_scalar_set_unknown_name");
        },
        doneSignal: "keyed_scalar_set_unknown_name",
      });
    });
    Assert.ok(
      messages.find(({ message }) => message.includes("Unknown scalar")),
      "Telemetry should warn if an unknown keyed scalar is incremented"
    );
  });

  add_task(async function test_telemetry_keyed_scalar_set_long_key() {
    let { messages } = await promiseConsoleOutput(async () => {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.keyedScalarSet(
            "telemetry.test.keyed_unsigned_int",
            "X".repeat(73),
            1
          );
          browser.test.notifyPass("keyed_scalar_set_long_key");
        },
        doneSignal: "keyed_scalar_set_long_key",
      });
    });
    Assert.ok(
      messages.find(({ message }) =>
        message.includes("The key length must be limited to 72 characters")
      ),
      "Telemetry should warn if keyed scalar's key is too long"
    );
  });

  add_task(async function test_telemetry_keyed_scalar_set_maximum() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.keyedScalarSetMaximum(
          "telemetry.test.keyed_unsigned_int",
          "foo",
          123
        );
        browser.test.notifyPass("keyed_scalar_set_maximum");
      },
      doneSignal: "keyed_scalar_set_maximum",
    });
    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "telemetry.test.keyed_unsigned_int",
      "foo",
      123
    );
  });

  add_task(
    async function test_telemetry_keyed_scalar_set_maximum_unknown_name() {
      let { messages } = await promiseConsoleOutput(async function () {
        await run({
          backgroundScript: async () => {
            await browser.telemetry.keyedScalarSetMaximum(
              "telemetry.test.does_not_exist",
              "foo",
              1
            );
            browser.test.notifyPass("keyed_scalar_set_maximum_unknown_name");
          },
          doneSignal: "keyed_scalar_set_maximum_unknown_name",
        });
      });
      Assert.ok(
        messages.find(({ message }) => message.includes("Unknown scalar")),
        "Telemetry should warn if an unknown keyed scalar is set"
      );
    }
  );

  add_task(
    async function test_telemetry_keyed_scalar_set_maximum_illegal_value() {
      await run({
        backgroundScript: () => {
          browser.test.assertThrows(
            () =>
              browser.telemetry.keyedScalarSetMaximum(
                "telemetry.test.keyed_unsigned_int",
                "foo",
                "string"
              ),
            /Incorrect argument types for telemetry.keyedScalarSetMaximum/,
            "The third 'value' argument to keyedScalarSetMaximum must be a scalar"
          );
          browser.test.notifyPass("keyed_scalar_set_maximum_illegal_value");
        },
        doneSignal: "keyed_scalar_set_maximum_illegal_value",
      });
    }
  );

  add_task(async function test_telemetry_keyed_scalar_set_maximum_long_key() {
    let { messages } = await promiseConsoleOutput(async () => {
      await run({
        backgroundScript: async () => {
          await browser.telemetry.keyedScalarSetMaximum(
            "telemetry.test.keyed_unsigned_int",
            "X".repeat(73),
            1
          );
          browser.test.notifyPass("keyed_scalar_set_maximum_long_key");
        },
        doneSignal: "keyed_scalar_set_maximum_long_key",
      });
    });
    Assert.ok(
      messages.find(({ message }) =>
        message.includes("The key length must be limited to 72 characters")
      ),
      "Telemetry should warn if keyed scalar's key is too long"
    );
  });

  add_task(async function test_telemetry_record_event() {
    Services.telemetry.clearEvents();
    Services.telemetry.setEventRecordingEnabled("telemetry.test", true);

    await run({
      backgroundScript: async () => {
        await browser.telemetry.recordEvent(
          "telemetry.test",
          "test1",
          "object1"
        );
        browser.test.notifyPass("record_event_ok");
      },
      doneSignal: "record_event_ok",
    });

    TelemetryTestUtils.assertEvents(
      [
        {
          category: "telemetry.test",
          method: "test1",
          object: "object1",
        },
      ],
      { category: "telemetry.test" }
    );

    Services.telemetry.setEventRecordingEnabled("telemetry.test", false);
    Services.telemetry.clearEvents();
  });

  // Bug 1536877
  add_task(async function test_telemetry_record_event_value_must_be_string() {
    Services.telemetry.clearEvents();
    Services.telemetry.setEventRecordingEnabled("telemetry.test", true);

    await run({
      backgroundScript: async () => {
        try {
          await browser.telemetry.recordEvent(
            "telemetry.test",
            "test1",
            "object1",
            "value1"
          );
          browser.test.notifyPass("record_event_string_value");
        } catch (ex) {
          browser.test.fail(
            `Unexpected exception raised during record_event_value_must_be_string: ${ex}`
          );
          browser.test.notifyPass("record_event_string_value");
          throw ex;
        }
      },
      doneSignal: "record_event_string_value",
    });

    TelemetryTestUtils.assertEvents(
      [
        {
          category: "telemetry.test",
          method: "test1",
          object: "object1",
          value: "value1",
        },
      ],
      { category: "telemetry.test" }
    );

    Services.telemetry.setEventRecordingEnabled("telemetry.test", false);
    Services.telemetry.clearEvents();
  });

  add_task(async function test_telemetry_register_scalars_string() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.registerScalars("telemetry.test.dynamic", {
          webext_string: {
            kind: browser.telemetry.ScalarType.STRING,
            keyed: false,
            record_on_release: true,
          },
        });
        await browser.telemetry.scalarSet(
          "telemetry.test.dynamic.webext_string",
          "hello"
        );
        browser.test.notifyPass("register_scalars_string");
      },
      doneSignal: "register_scalars_string",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("dynamic", false, true),
      "telemetry.test.dynamic.webext_string",
      "hello"
    );
  });

  add_task(async function test_telemetry_register_scalars_multiple() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.registerScalars("telemetry.test.dynamic", {
          webext_string: {
            kind: browser.telemetry.ScalarType.STRING,
            keyed: false,
            record_on_release: true,
          },
          webext_string_too: {
            kind: browser.telemetry.ScalarType.STRING,
            keyed: false,
            record_on_release: true,
          },
        });
        await browser.telemetry.scalarSet(
          "telemetry.test.dynamic.webext_string",
          "hello"
        );
        await browser.telemetry.scalarSet(
          "telemetry.test.dynamic.webext_string_too",
          "world"
        );
        browser.test.notifyPass("register_scalars_multiple");
      },
      doneSignal: "register_scalars_multiple",
    });
    const scalars = TelemetryTestUtils.getProcessScalars(
      "dynamic",
      false,
      true
    );
    TelemetryTestUtils.assertScalar(
      scalars,
      "telemetry.test.dynamic.webext_string",
      "hello"
    );
    TelemetryTestUtils.assertScalar(
      scalars,
      "telemetry.test.dynamic.webext_string_too",
      "world"
    );
  });

  add_task(async function test_telemetry_register_scalars_boolean() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.registerScalars("telemetry.test.dynamic", {
          webext_boolean: {
            kind: browser.telemetry.ScalarType.BOOLEAN,
            keyed: false,
            record_on_release: true,
          },
        });
        await browser.telemetry.scalarSet(
          "telemetry.test.dynamic.webext_boolean",
          true
        );
        browser.test.notifyPass("register_scalars_boolean");
      },
      doneSignal: "register_scalars_boolean",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("dynamic", false, true),
      "telemetry.test.dynamic.webext_boolean",
      true
    );
  });

  add_task(async function test_telemetry_register_scalars_count() {
    Services.telemetry.clearScalars();
    await run({
      backgroundScript: async () => {
        await browser.telemetry.registerScalars("telemetry.test.dynamic", {
          webext_count: {
            kind: browser.telemetry.ScalarType.COUNT,
            keyed: false,
            record_on_release: true,
          },
        });
        await browser.telemetry.scalarSet(
          "telemetry.test.dynamic.webext_count",
          123
        );
        browser.test.notifyPass("register_scalars_count");
      },
      doneSignal: "register_scalars_count",
    });
    TelemetryTestUtils.assertScalar(
      TelemetryTestUtils.getProcessScalars("dynamic", false, true),
      "telemetry.test.dynamic.webext_count",
      123
    );
  });

  add_task(async function test_telemetry_register_events() {
    Services.telemetry.clearEvents();

    await run({
      backgroundScript: async () => {
        await browser.telemetry.registerEvents("telemetry.test.dynamic", {
          test1: {
            methods: ["test1"],
            objects: ["object1"],
            extra_keys: [],
          },
        });
        await browser.telemetry.recordEvent(
          "telemetry.test.dynamic",
          "test1",
          "object1"
        );
        browser.test.notifyPass("register_events");
      },
      doneSignal: "register_events",
    });

    TelemetryTestUtils.assertEvents(
      [
        {
          category: "telemetry.test.dynamic",
          method: "test1",
          object: "object1",
        },
      ],
      { category: "telemetry.test.dynamic" },
      { process: "dynamic" }
    );
  });

  add_task(async function test_telemetry_submit_ping() {
    let archiveTester = new TelemetryArchiveTesting.Checker();
    await archiveTester.promiseInit();

    await run({
      backgroundScript: async () => {
        await browser.telemetry.submitPing("webext-test", {}, {});
        browser.test.notifyPass("submit_ping");
      },
      doneSignal: "submit_ping",
    });

    await TestUtils.waitForCondition(
      () => archiveTester.promiseFindPing("webext-test", []),
      "Failed to find the webext-test ping"
    );
  });

  add_task(async function test_telemetry_submit_encrypted_ping() {
    await run({
      backgroundScript: async () => {
        try {
          await browser.telemetry.submitEncryptedPing(
            { payload: "encrypted-webext-test" },
            {
              schemaName: "schema-name",
              schemaVersion: 123,
            }
          );
          browser.test.fail(
            "Expected exception without required manifest entries set."
          );
        } catch (e) {
          browser.test.assertTrue(
            e,
            /Encrypted telemetry pings require ping_type and public_key to be set in manifest./
          );
          browser.test.notifyPass("submit_encrypted_ping_fail");
        }
      },
      doneSignal: "submit_encrypted_ping_fail",
    });

    const telemetryManifestEntries = {
      ping_type: "encrypted-webext-ping",
      schemaNamespace: "schema-namespace",
      public_key: {
        id: "pioneer-dev-20200423",
        key: {
          crv: "P-256",
          kty: "EC",
          x: "Qqihp7EryDN2-qQ-zuDPDpy5mJD5soFBDZmzPWTmjwk",
          y: "PiEQVUlywi2bEsA3_5D0VFrCHClCyUlLW52ajYs-5uc",
        },
      },
    };

    await run({
      backgroundScript: async () => {
        await browser.telemetry.submitEncryptedPing(
          {
            payload: "encrypted-webext-test",
          },
          {
            schemaName: "schema-name",
            schemaVersion: 123,
          }
        );
        browser.test.notifyPass("submit_encrypted_ping_pass");
      },
      permissions: ["telemetry"],
      doneSignal: "submit_encrypted_ping_pass",
      isPrivileged: true,
      telemetry: telemetryManifestEntries,
    });

    telemetryManifestEntries.pioneer_id = true;
    telemetryManifestEntries.study_name = "test123";
    Services.prefs.setStringPref("toolkit.telemetry.pioneerId", "test123");

    await run({
      backgroundScript: async () => {
        await browser.telemetry.submitEncryptedPing(
          { payload: "encrypted-webext-test" },
          {
            schemaName: "schema-name",
            schemaVersion: 123,
          }
        );
        browser.test.notifyPass("submit_encrypted_ping_pass");
      },
      permissions: ["telemetry"],
      doneSignal: "submit_encrypted_ping_pass",
      isPrivileged: true,
      telemetry: telemetryManifestEntries,
    });

    let pings;
    await TestUtils.waitForCondition(async function () {
      pings = await TelemetryArchive.promiseArchivedPingList();
      return pings.length >= 3;
    }, "Wait until we have at least 3 pings in the telemetry archive");

    equal(pings.length, 3);
    equal(pings[1].type, "encrypted-webext-ping");
    equal(pings[2].type, "encrypted-webext-ping");
  });

  add_task(async function test_telemetry_can_upload_enabled() {
    Services.prefs.setBoolPref(
      TelemetryUtils.Preferences.FhrUploadEnabled,
      true
    );

    await run({
      backgroundScript: async () => {
        const result = await browser.telemetry.canUpload();
        browser.test.assertTrue(result);
        browser.test.notifyPass("can_upload_enabled");
      },
      doneSignal: "can_upload_enabled",
    });

    Services.prefs.clearUserPref(TelemetryUtils.Preferences.FhrUploadEnabled);
  });

  add_task(async function test_telemetry_can_upload_disabled() {
    Services.prefs.setBoolPref(
      TelemetryUtils.Preferences.FhrUploadEnabled,
      false
    );

    await run({
      backgroundScript: async () => {
        const result = await browser.telemetry.canUpload();
        browser.test.assertFalse(result);
        browser.test.notifyPass("can_upload_disabled");
      },
      doneSignal: "can_upload_disabled",
    });

    Services.prefs.clearUserPref(TelemetryUtils.Preferences.FhrUploadEnabled);
  });
}