summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_extension_content_telemetry.js
blob: ee5eb84907250c7f1302a6fd2bf49a518e45761c (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const HISTOGRAM = "WEBEXT_CONTENT_SCRIPT_INJECTION_MS";
const HISTOGRAM_KEYED = "WEBEXT_CONTENT_SCRIPT_INJECTION_MS_BY_ADDONID";

const server = createHttpServer();
server.registerDirectory("/data/", do_get_file("data"));

const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;

add_task(async function test_telemetry() {
  function contentScript() {
    browser.test.sendMessage("content-script-run");
  }

  let extension1 = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          matches: ["http://*/*/file_sample.html"],
          js: ["content_script.js"],
          run_at: "document_end",
        },
      ],
    },

    files: {
      "content_script.js": contentScript,
    },
  });
  let extension2 = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          matches: ["http://*/*/file_sample.html"],
          js: ["content_script.js"],
          run_at: "document_end",
        },
      ],
    },

    files: {
      "content_script.js": contentScript,
    },
  });

  clearHistograms();

  let process = IS_OOP ? "content" : "parent";
  ok(
    !(HISTOGRAM in getSnapshots(process)),
    `No data recorded for histogram: ${HISTOGRAM}.`
  );
  ok(
    !(HISTOGRAM_KEYED in getKeyedSnapshots(process)),
    `No data recorded for keyed histogram: ${HISTOGRAM_KEYED}.`
  );

  await extension1.startup();
  let extensionId = extension1.extension.id;

  info(`Started extension with id ${extensionId}`);

  ok(
    !(HISTOGRAM in getSnapshots(process)),
    `No data recorded for histogram after startup: ${HISTOGRAM}.`
  );
  ok(
    !(HISTOGRAM_KEYED in getKeyedSnapshots(process)),
    `No data recorded for keyed histogram: ${HISTOGRAM_KEYED}.`
  );

  let contentPage = await ExtensionTestUtils.loadContentPage(
    `${BASE_URL}/file_sample.html`
  );
  await extension1.awaitMessage("content-script-run");
  await promiseTelemetryRecorded(HISTOGRAM, process, 1);
  await promiseKeyedTelemetryRecorded(HISTOGRAM_KEYED, process, extensionId, 1);

  equal(
    valueSum(getSnapshots(process)[HISTOGRAM].values),
    1,
    `Data recorded for histogram: ${HISTOGRAM}.`
  );
  equal(
    valueSum(getKeyedSnapshots(process)[HISTOGRAM_KEYED][extensionId].values),
    1,
    `Data recorded for histogram: ${HISTOGRAM_KEYED} with key ${extensionId}.`
  );

  await contentPage.close();
  await extension1.unload();

  await extension2.startup();
  let extensionId2 = extension2.extension.id;

  info(`Started extension with id ${extensionId2}`);

  equal(
    valueSum(getSnapshots(process)[HISTOGRAM].values),
    1,
    `No new data recorded for histogram after extension2 startup: ${HISTOGRAM}.`
  );
  equal(
    valueSum(getKeyedSnapshots(process)[HISTOGRAM_KEYED][extensionId].values),
    1,
    `No new data recorded for histogram after extension2 startup: ${HISTOGRAM_KEYED} with key ${extensionId}.`
  );
  ok(
    !(extensionId2 in getKeyedSnapshots(process)[HISTOGRAM_KEYED]),
    `No data recorded for histogram after startup: ${HISTOGRAM_KEYED} with key ${extensionId2}.`
  );

  contentPage = await ExtensionTestUtils.loadContentPage(
    `${BASE_URL}/file_sample.html`
  );
  await extension2.awaitMessage("content-script-run");
  await promiseTelemetryRecorded(HISTOGRAM, process, 2);
  await promiseKeyedTelemetryRecorded(
    HISTOGRAM_KEYED,
    process,
    extensionId2,
    1
  );

  equal(
    valueSum(getSnapshots(process)[HISTOGRAM].values),
    2,
    `Data recorded for histogram: ${HISTOGRAM}.`
  );
  equal(
    valueSum(getKeyedSnapshots(process)[HISTOGRAM_KEYED][extensionId].values),
    1,
    `No new data recorded for histogram: ${HISTOGRAM_KEYED} with key ${extensionId}.`
  );
  equal(
    valueSum(getKeyedSnapshots(process)[HISTOGRAM_KEYED][extensionId2].values),
    1,
    `Data recorded for histogram: ${HISTOGRAM_KEYED} with key ${extensionId2}.`
  );

  await contentPage.close();
  await extension2.unload();
});