summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_WebrtcGlobalInformation.js
blob: b7986a906efc9aefa0bc7d88a5cc7e85a025fb59 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const ProcessTools = Cc["@mozilla.org/processtools-service;1"].getService(
  Ci.nsIProcessToolsService
);

let getStatsReports = async (filter = "") => {
  let { reports } = await new Promise(r =>
    WebrtcGlobalInformation.getAllStats(r, filter)
  );

  ok(Array.isArray(reports), "|reports| is an array");

  let sanityCheckReport = report => {
    isnot(report.pcid, "", "pcid is non-empty");
    if (filter.length) {
      is(report.pcid, filter, "pcid matches filter");
    }

    // Check for duplicates
    const checkForDuplicateId = statsArray => {
      ok(Array.isArray(statsArray), "|statsArray| is an array");
      const ids = new Set();
      statsArray.forEach(stat => {
        is(typeof stat.id, "string", "|stat.id| is a string");
        ok(
          !ids.has(stat.id),
          `Id ${stat.id} should appear only once. Stat was ${JSON.stringify(
            stat
          )}`
        );
        ids.add(stat.id);
      });
    };

    checkForDuplicateId(report.inboundRtpStreamStats);
    checkForDuplicateId(report.outboundRtpStreamStats);
    checkForDuplicateId(report.remoteInboundRtpStreamStats);
    checkForDuplicateId(report.remoteOutboundRtpStreamStats);
    checkForDuplicateId(report.rtpContributingSourceStats);
    checkForDuplicateId(report.iceCandidatePairStats);
    checkForDuplicateId(report.iceCandidateStats);
    checkForDuplicateId(report.trickledIceCandidateStats);
    checkForDuplicateId(report.dataChannelStats);
    checkForDuplicateId(report.codecStats);
  };

  reports.forEach(sanityCheckReport);
  return reports;
};

let getLogging = async () => {
  let logs = await new Promise(r => WebrtcGlobalInformation.getLogging("", r));
  ok(Array.isArray(logs), "|logs| is an array");
  return logs;
};

let checkStatsReportCount = async (count, filter = "") => {
  let reports = await getStatsReports(filter);
  is(reports.length, count, `|reports| should have length ${count}`);
  if (reports.length != count) {
    info(`reports = ${JSON.stringify(reports)}`);
  }
  return reports;
};

let checkLoggingEmpty = async () => {
  let logs = await getLogging();
  is(logs.length, 0, "Logging is empty");
  if (logs.length) {
    info(`logs = ${JSON.stringify(logs)}`);
  }
  return logs;
};

let checkLoggingNonEmpty = async () => {
  let logs = await getLogging();
  isnot(logs.length, 0, "Logging is not empty");
  return logs;
};

let clearAndCheck = async () => {
  WebrtcGlobalInformation.clearAllStats();
  WebrtcGlobalInformation.clearLogging();
  await checkStatsReportCount(0);
  await checkLoggingEmpty();
};

let openTabInNewProcess = async file => {
  let rootDir = getRootDirectory(gTestPath);
  rootDir = rootDir.replace(
    "chrome://mochitests/content/",
    "https://example.com/"
  );
  let absoluteURI = rootDir + file;

  return BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: absoluteURI,
    forceNewProcess: true,
  });
};

let killTabProcess = async tab => {
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    ChromeUtils.privateNoteIntentionalCrash();
  });
  ProcessTools.kill(tab.linkedBrowser.frameLoader.remoteTab.osPid);
};

add_task(async () => {
  info("Test that clearAllStats is callable");
  WebrtcGlobalInformation.clearAllStats();
  ok(true, "clearAllStats returns");
});

add_task(async () => {
  info("Test that clearLogging is callable");
  WebrtcGlobalInformation.clearLogging();
  ok(true, "clearLogging returns");
});

add_task(async () => {
  info(
    "Test that getAllStats is callable, and returns 0 results when no RTCPeerConnections have existed"
  );
  await checkStatsReportCount(0);
});

add_task(async () => {
  info(
    "Test that getLogging is callable, and returns 0 results when no RTCPeerConnections have existed"
  );
  await checkLoggingEmpty();
});

add_task(async () => {
  info("Test that we can get stats/logging for a PC on the parent process");
  await clearAndCheck();
  let pc = new RTCPeerConnection();
  await pc.setLocalDescription(
    await pc.createOffer({ offerToReceiveAudio: true })
  );
  // Let ICE stack go quiescent
  await new Promise(r => {
    pc.onicegatheringstatechange = () => {
      if (pc.iceGatheringState == "complete") {
        r();
      }
    };
  });
  await checkStatsReportCount(1);
  await checkLoggingNonEmpty();
  pc.close();
  pc = null;
  // Closing a PC should not do anything to the ICE logging
  await checkLoggingNonEmpty();
  // There's just no way to get a signal that the ICE stack has stopped logging
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 2000));
  await clearAndCheck();
});

add_task(async () => {
  info("Test that we can get stats/logging for a PC on a content process");
  await clearAndCheck();
  let tab = await openTabInNewProcess("single_peerconnection.html");
  await checkStatsReportCount(1);
  await checkLoggingNonEmpty();
  await killTabProcess(tab);
  BrowserTestUtils.removeTab(tab);
  await clearAndCheck();
});

add_task(async () => {
  info(
    "Test that we can get stats/logging for two connected PCs on a content process"
  );
  await clearAndCheck();
  let tab = await openTabInNewProcess("peerconnection_connect.html");
  await checkStatsReportCount(2);
  await checkLoggingNonEmpty();
  await killTabProcess(tab);
  BrowserTestUtils.removeTab(tab);
  await clearAndCheck();
});

add_task(async () => {
  info("Test filtering for stats reports (parent process)");
  await clearAndCheck();
  let pc1 = new RTCPeerConnection();
  let pc2 = new RTCPeerConnection();
  let allReports = await checkStatsReportCount(2);
  await checkStatsReportCount(1, allReports[0].pcid);
  pc1.close();
  pc2.close();
  pc1 = null;
  pc2 = null;
  await checkStatsReportCount(1, allReports[0].pcid);
  await clearAndCheck();
});

add_task(async () => {
  info("Test filtering for stats reports (content process)");
  await clearAndCheck();
  let tab1 = await openTabInNewProcess("single_peerconnection.html");
  let tab2 = await openTabInNewProcess("single_peerconnection.html");
  let allReports = await checkStatsReportCount(2);
  await checkStatsReportCount(1, allReports[0].pcid);
  await killTabProcess(tab1);
  BrowserTestUtils.removeTab(tab1);
  await killTabProcess(tab2);
  BrowserTestUtils.removeTab(tab2);
  await checkStatsReportCount(1, allReports[0].pcid);
  await clearAndCheck();
});

add_task(async () => {
  info("Test that stats/logging persists when PC is closed (parent process)");
  await clearAndCheck();
  let pc = new RTCPeerConnection();
  // This stuff will generate logging
  await pc.setLocalDescription(
    await pc.createOffer({ offerToReceiveAudio: true })
  );
  // Once gathering is done, the ICE stack should go quiescent
  await new Promise(r => {
    pc.onicegatheringstatechange = () => {
      if (pc.iceGatheringState == "complete") {
        r();
      }
    };
  });
  let reports = await checkStatsReportCount(1);
  isnot(
    window.browsingContext.browserId,
    undefined,
    "browserId is defined for parent process"
  );
  is(
    reports[0].browserId,
    window.browsingContext.browserId,
    "browserId for stats report matches parent process"
  );
  await checkLoggingNonEmpty();
  pc.close();
  pc = null;
  await checkStatsReportCount(1);
  await checkLoggingNonEmpty();
  await clearAndCheck();
});

add_task(async () => {
  info("Test that stats/logging persists when PC is closed (content process)");
  await clearAndCheck();
  let tab = await openTabInNewProcess("single_peerconnection.html");
  let { browserId } = tab.linkedBrowser;
  let reports = await checkStatsReportCount(1);
  is(reports[0].browserId, browserId, "browserId for stats report matches tab");
  isnot(
    browserId,
    window.browsingContext.browserId,
    "tab browser id is not the same as parent process browser id"
  );
  await checkLoggingNonEmpty();
  await killTabProcess(tab);
  BrowserTestUtils.removeTab(tab);
  await checkStatsReportCount(1);
  await checkLoggingNonEmpty();
  await clearAndCheck();
});