blob: 937f54b74ec8266f890d60a4c67e75c3d6714826 [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>
15 <div id="stats">
16 </div>
17 </pre>
18
19 <!-- These files are in place when executing on W3C. -->
20 <script src="/resources/testharness.js"></script>
21 <script src="/resources/testharnessreport.js"></script>
22 <script type="text/javascript">
23 var test = async_test('Can get stats from a basic WebRTC call.');
24
25 var gFirstConnection = null;
26 var gSecondConnection = null;
27
28 var onIceCandidateToFirst = test.step_func(function(event) {
29 // If event.candidate is null = no more candidates.
30 if (event.candidate) {
31 gSecondConnection.addIceCandidate(event.candidate);
32 }
33 });
34
35 var onIceCandidateToSecond = test.step_func(function(event) {
36 if (event.candidate) {
37 gFirstConnection.addIceCandidate(event.candidate);
38 }
39 });
40
41 var onRemoteStream = test.step_func(function(event) {
42 assert_unreached('WebRTC received a stream when there was none');
43 });
44
45 var getStatsRecordByType = function(stats, type) {
46 for (let stat of stats.values()) {
47 if (stat.type == type) {
48 return stat;
49 }
50 }
51 return null;
52 }
53
Harald Alvestrandef4de3d2017-01-04 09:49:2154 var onIceConnectionStateChange = test.step_func(function(event) {
55 // Wait until connection is established.
56 // Note - not all browsers reach 'completed' state, so we're
57 // checking for 'connected' state instead.
58 if (gFirstConnection.iceConnectionState != 'connected') {
59 return;
60 }
61 gFirstConnection.getStats()
62 .then(function(report) {
63 // Show the retrieved stats info
64 var showStats = document.getElementById('stats');
65 let reportDictionary = {};
66 for (let stats of report.values()) {
67 reportDictionary[stats.id] = stats;
68 }
69 showStats.innerHTML = JSON.stringify(reportDictionary, null, 2);
70 // Check the stats properties.
Harald Alvestrand7d504a22017-10-17 22:21:5571 assert_not_equals(report, null, 'No report');
Harald Alvestrandef4de3d2017-01-04 09:49:2172 let sessionStat = getStatsRecordByType(report, 'peer-connection');
73 assert_not_equals(sessionStat, null, 'Did not find peer-connection stats');
jugglinmikeefa1c1e2018-09-24 09:49:5874 assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat');
Harald Alvestrand7d504a22017-10-17 22:21:5575 // Once every 4000 or so tests, the datachannel won't be opened when the getStats
76 // function is done, so allow both 0 and 1 datachannels.
77 assert_true(sessionStat.dataChannelsOpened == 1 || sessionStat.dataChannelsOpened == 0,
78 'dataChannelsOpened count wrong');
Harald Alvestrandef4de3d2017-01-04 09:49:2179 test.done();
80 })
81 .catch(test.step_func(function(e) {
82 assert_unreached(e.name + ': ' + e.message + ': ');
83 }));
84 });
85
Harald Alvestrandfb648512017-01-03 16:49:1686 // This function starts the test.
87 test.step(function() {
88 gFirstConnection = new RTCPeerConnection(null);
89 gFirstConnection.onicecandidate = onIceCandidateToFirst;
Harald Alvestrandef4de3d2017-01-04 09:49:2190 gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange;
Harald Alvestrandfb648512017-01-03 16:49:1691
92 gSecondConnection = new RTCPeerConnection(null);
93 gSecondConnection.onicecandidate = onIceCandidateToSecond;
94 gSecondConnection.onaddstream = onRemoteStream;
95
96 // The createDataChannel is necessary and sufficient to make
97 // sure the ICE connection be attempted.
98 gFirstConnection.createDataChannel('channel');
Harald Alvestrandfb648512017-01-03 16:49:1699 var atStep = 'Create offer';
100
101 gFirstConnection.createOffer()
102 .then(function(offer) {
103 atStep = 'Set local description at first';
104 return gFirstConnection.setLocalDescription(offer);
105 })
106 .then(function() {
107 atStep = 'Set remote description at second';
108 return gSecondConnection.setRemoteDescription(
109 gFirstConnection.localDescription);
110 })
111 .then(function() {
112 atStep = 'Create answer';
113 return gSecondConnection.createAnswer();
114 })
115 .then(function(answer) {
116 atStep = 'Set local description at second';
117 return gSecondConnection.setLocalDescription(answer);
118 })
119 .then(function() {
120 atStep = 'Set remote description at first';
121 return gFirstConnection.setRemoteDescription(
122 gSecondConnection.localDescription);
123 })
Harald Alvestrandfb648512017-01-03 16:49:16124 .catch(test.step_func(function(e) {
125 assert_unreached('Error ' + e.name + ': ' + e.message +
126 ' happened at step ' + atStep);
127 }));
128 });
129</script>
130
131</body>
132</html>