summaryrefslogtreecommitdiffstats
path: root/ipc/glue/test/browser/head-telemetry.js
blob: 64a6aee1c4299943b44b6db163420fd4a0fafd15 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from head.js */

const Telemetry = Services.telemetry;

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

/* eslint-disable mozilla/no-redeclare-with-import-autofix */
const { ContentTaskUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ContentTaskUtils.sys.mjs"
);

const MEDIA_AUDIO_PROCESS = "media.audio_process_per_codec_name";

const utilityPerCodecs = {
  Linux: [
    {
      process: "utility+audioDecoder_Generic",
      codecs: ["vorbis", "mp3", "aac", "flac"],
    },
  ],
  WINNT: [
    {
      process: "utility+audioDecoder_Generic",
      codecs: ["vorbis", "mp3", "flac"],
    },
    {
      process: "utility+audioDecoder_WMF",
      codecs: ["aac"],
    },
  ],
  Darwin: [
    {
      process: "utility+audioDecoder_Generic",
      codecs: ["vorbis", "mp3", "flac"],
    },
    {
      process: "utility+audioDecoder_AppleMedia",
      codecs: ["aac"],
    },
  ],
};

const kInterval = 300; /* ms */
const kRetries = 5;

/**
 * This function waits until utility scalars are reported into the
 * scalar snapshot.
 */
async function waitForKeyedScalars(process) {
  await ContentTaskUtils.waitForCondition(
    () => {
      const scalars = Telemetry.getSnapshotForKeyedScalars("main", false);
      return Object.keys(scalars).includes("content");
    },
    `Waiting for ${process} scalars to have been set`,
    kInterval,
    kRetries
  );
}

async function waitForValue(process, codecNames, extra = "") {
  await ContentTaskUtils.waitForCondition(
    () => {
      const telemetry = Telemetry.getSnapshotForKeyedScalars(
        "main",
        false
      ).content;
      if (telemetry && MEDIA_AUDIO_PROCESS in telemetry) {
        const keyProcMimeTypes = Object.keys(telemetry[MEDIA_AUDIO_PROCESS]);
        const found = codecNames.every(item =>
          keyProcMimeTypes.includes(`${process},${item}${extra}`)
        );
        return found;
      }
      return false;
    },
    `Waiting for ${MEDIA_AUDIO_PROCESS}`,
    kInterval,
    kRetries
  );
}

async function runTest({ expectUtility = false, expectRDD = false }) {
  info(
    `Running tests with decoding from Utility or RDD: expectUtility=${expectUtility} expectRDD=${expectRDD}`
  );

  await SpecialPowers.pushPrefEnv({
    set: [
      ["media.utility-process.enabled", expectUtility],
      ["media.rdd-process.enabled", expectRDD],
      ["toolkit.telemetry.ipcBatchTimeout", 0],
    ],
  });

  const platform = Services.appinfo.OS;

  for (let { src, expectations } of audioTestData()) {
    if (!(platform in expectations)) {
      info(`Skipping ${src} for ${platform}`);
      continue;
    }

    const expectation = expectations[platform];

    info(`Add media tab: ${src}`);
    let tab = await addMediaTab(src);

    info("Play tab");
    await play(
      tab,
      expectUtility ? expectation.process : "RDD",
      expectation.decoder,
      !expectUtility && !expectRDD
    );

    info("Stop tab");
    await stop(tab);

    info("Remove tab");
    await BrowserTestUtils.removeTab(tab);
  }
}

function getTelemetry() {
  const telemetry = Telemetry.getSnapshotForKeyedScalars("main", false).content;
  return telemetry;
}

async function verifyTelemetryForProcess(process, codecNames, extraKey = "") {
  // Once scalars are set by the utility process, they don't immediately get
  // sent to the parent process. Wait for the Telemetry IPC Timer to trigger
  // and batch send the data back to the parent process.
  await waitForKeyedScalars(process);
  await waitForValue(process, codecNames, extraKey);

  const telemetry = getTelemetry();

  // The amount here depends on how many times RemoteAudioDecoderParent::RemoteAudioDecoderParent
  // gets called, which might be more than actual audio files being playback, e.g., we would get one for metadata loading, then one for playback etc.
  // But we dont care really we just want to ensure 0 on RDD, Content and others
  // in the wild.[${codecName}]
  codecNames.forEach(codecName => {
    Assert.equal(
      telemetry[MEDIA_AUDIO_PROCESS][`${process},${codecName}${extraKey}`],
      1,
      `${MEDIA_AUDIO_PROCESS} must have the correct value (${process}, ${codecName}).`
    );
  });
}

async function verifyNoTelemetryForProcess(process, codecNames, extraKey = "") {
  try {
    await waitForKeyedScalars(process);
    await waitForValue(process, codecNames, extraKey);
  } catch (ex) {
    if (ex.indexOf("timed out after") > 0) {
      Assert.ok(
        true,
        `Expected timeout ${process}[${MEDIA_AUDIO_PROCESS}] for ${codecNames}`
      );
    } else {
      Assert.ok(
        false,
        `Unexpected exception on ${process}[${MEDIA_AUDIO_PROCESS}] for ${codecNames}: ${ex}`
      );
    }
  }

  const telemetry = getTelemetry();

  // There could be races with telemetry for power usage coming up
  codecNames.forEach(codecName => {
    if (telemetry) {
      if (telemetry && MEDIA_AUDIO_PROCESS in telemetry) {
        Assert.ok(
          !(
            `${process},${codecName}${extraKey}` in
            telemetry[MEDIA_AUDIO_PROCESS]
          ),
          `Some telemetry but no ${process}[${MEDIA_AUDIO_PROCESS}][${codecName}]`
        );
      } else {
        Assert.ok(
          !(MEDIA_AUDIO_PROCESS in telemetry),
          `No telemetry for ${process}[${MEDIA_AUDIO_PROCESS}][${codecName}]`
        );
      }
    } else {
      Assert.equal(
        undefined,
        telemetry,
        `No telemetry for ${process}[${MEDIA_AUDIO_PROCESS}][${codecName}]`
      );
    }
  });
}