blob: d6a692bb7860b9c6bd7699ba4e99ea8010922c08 [file] [log] [blame]
Harald Alvestrandfb648512017-01-03 16:49:161<!doctype html>
2<!--
3This test uses data only, and thus does not require fake media devices.
4-->
5
6<html>
7<head>
8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9 <title>RTCPeerConnection GetStats</title>
10</head>
11<body>
12 <div id="log"></div>
13 <h2>Retrieved stats info</h2>
14 <pre>
Youenn Fableta6be85c2018-11-13 22:25:1915 <input type="button" onclick="showStats()" value="Show stats"></input>
Harald Alvestrandfb648512017-01-03 16:49:1616 <div id="stats">
17 </div>
18 </pre>
19
20 <!-- These files are in place when executing on W3C. -->
21 <script src="/resources/testharness.js"></script>
22 <script src="/resources/testharnessreport.js"></script>
23 <script type="text/javascript">
24 var test = async_test('Can get stats from a basic WebRTC call.');
Youenn Fableta6be85c2018-11-13 22:25:1925 var statsToShow;
Harald Alvestrandfb648512017-01-03 16:49:1626 var gFirstConnection = null;
27 var gSecondConnection = null;
28
29 var onIceCandidateToFirst = test.step_func(function(event) {
Blink WPT Bot92b3b832021-02-24 12:27:2730 gSecondConnection.addIceCandidate(event.candidate);
Harald Alvestrandfb648512017-01-03 16:49:1631 });
32
33 var onIceCandidateToSecond = test.step_func(function(event) {
Blink WPT Bot92b3b832021-02-24 12:27:2734 gFirstConnection.addIceCandidate(event.candidate);
Harald Alvestrandfb648512017-01-03 16:49:1635 });
36
Harald Alvestrandfb648512017-01-03 16:49:1637 var getStatsRecordByType = function(stats, type) {
38 for (let stat of stats.values()) {
39 if (stat.type == type) {
40 return stat;
41 }
42 }
43 return null;
44 }
45
Harald Alvestrandef4de3d2017-01-04 09:49:2146 var onIceConnectionStateChange = test.step_func(function(event) {
47 // Wait until connection is established.
48 // Note - not all browsers reach 'completed' state, so we're
49 // checking for 'connected' state instead.
50 if (gFirstConnection.iceConnectionState != 'connected') {
51 return;
52 }
53 gFirstConnection.getStats()
54 .then(function(report) {
Harald Alvestrandef4de3d2017-01-04 09:49:2155 let reportDictionary = {};
56 for (let stats of report.values()) {
57 reportDictionary[stats.id] = stats;
58 }
Youenn Fableta6be85c2018-11-13 22:25:1959 statsToShow = JSON.stringify(reportDictionary, null, 2);
Harald Alvestrandef4de3d2017-01-04 09:49:2160 // Check the stats properties.
Harald Alvestrand7d504a22017-10-17 22:21:5561 assert_not_equals(report, null, 'No report');
Harald Alvestrandef4de3d2017-01-04 09:49:2162 let sessionStat = getStatsRecordByType(report, 'peer-connection');
63 assert_not_equals(sessionStat, null, 'Did not find peer-connection stats');
jugglinmikeefa1c1e2018-09-24 09:49:5864 assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat');
Harald Alvestrand7d504a22017-10-17 22:21:5565 // Once every 4000 or so tests, the datachannel won't be opened when the getStats
66 // function is done, so allow both 0 and 1 datachannels.
67 assert_true(sessionStat.dataChannelsOpened == 1 || sessionStat.dataChannelsOpened == 0,
68 'dataChannelsOpened count wrong');
Harald Alvestrandef4de3d2017-01-04 09:49:2169 test.done();
70 })
71 .catch(test.step_func(function(e) {
72 assert_unreached(e.name + ': ' + e.message + ': ');
73 }));
74 });
75
Harald Alvestrandfb648512017-01-03 16:49:1676 // This function starts the test.
77 test.step(function() {
78 gFirstConnection = new RTCPeerConnection(null);
Byron Campen [:bwc]d27f50c2020-06-25 14:38:0379 test.add_cleanup(() => gFirstConnection.close());
Harald Alvestrandfb648512017-01-03 16:49:1680 gFirstConnection.onicecandidate = onIceCandidateToFirst;
Harald Alvestrandef4de3d2017-01-04 09:49:2181 gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange;
Harald Alvestrandfb648512017-01-03 16:49:1682
83 gSecondConnection = new RTCPeerConnection(null);
Byron Campen [:bwc]d27f50c2020-06-25 14:38:0384 test.add_cleanup(() => gSecondConnection.close());
Harald Alvestrandfb648512017-01-03 16:49:1685 gSecondConnection.onicecandidate = onIceCandidateToSecond;
Harald Alvestrandfb648512017-01-03 16:49:1686
87 // The createDataChannel is necessary and sufficient to make
88 // sure the ICE connection be attempted.
89 gFirstConnection.createDataChannel('channel');
Harald Alvestrandfb648512017-01-03 16:49:1690 var atStep = 'Create offer';
91
92 gFirstConnection.createOffer()
93 .then(function(offer) {
94 atStep = 'Set local description at first';
95 return gFirstConnection.setLocalDescription(offer);
96 })
97 .then(function() {
98 atStep = 'Set remote description at second';
99 return gSecondConnection.setRemoteDescription(
100 gFirstConnection.localDescription);
101 })
102 .then(function() {
103 atStep = 'Create answer';
104 return gSecondConnection.createAnswer();
105 })
106 .then(function(answer) {
107 atStep = 'Set local description at second';
108 return gSecondConnection.setLocalDescription(answer);
109 })
110 .then(function() {
111 atStep = 'Set remote description at first';
112 return gFirstConnection.setRemoteDescription(
113 gSecondConnection.localDescription);
114 })
Harald Alvestrandfb648512017-01-03 16:49:16115 .catch(test.step_func(function(e) {
116 assert_unreached('Error ' + e.name + ': ' + e.message +
117 ' happened at step ' + atStep);
118 }));
119 });
Youenn Fableta6be85c2018-11-13 22:25:19120
121 function showStats() {
122 // Show the retrieved stats info
123 var showStats = document.getElementById('stats');
124 showStats.innerHTML = statsToShow;
125 }
126
Harald Alvestrandfb648512017-01-03 16:49:16127</script>
128
129</body>
130</html>