| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame^] | 1 | <!doctype html> |
| 2 | <!-- |
| 3 | This 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 | |
| 54 | // This function starts the test. |
| 55 | test.step(function() { |
| 56 | gFirstConnection = new RTCPeerConnection(null); |
| 57 | gFirstConnection.onicecandidate = onIceCandidateToFirst; |
| 58 | |
| 59 | gSecondConnection = new RTCPeerConnection(null); |
| 60 | gSecondConnection.onicecandidate = onIceCandidateToSecond; |
| 61 | gSecondConnection.onaddstream = onRemoteStream; |
| 62 | |
| 63 | // The createDataChannel is necessary and sufficient to make |
| 64 | // sure the ICE connection be attempted. |
| 65 | gFirstConnection.createDataChannel('channel'); |
| 66 | |
| 67 | var atStep = 'Create offer'; |
| 68 | |
| 69 | gFirstConnection.createOffer() |
| 70 | .then(function(offer) { |
| 71 | atStep = 'Set local description at first'; |
| 72 | return gFirstConnection.setLocalDescription(offer); |
| 73 | }) |
| 74 | .then(function() { |
| 75 | atStep = 'Set remote description at second'; |
| 76 | return gSecondConnection.setRemoteDescription( |
| 77 | gFirstConnection.localDescription); |
| 78 | }) |
| 79 | .then(function() { |
| 80 | atStep = 'Create answer'; |
| 81 | return gSecondConnection.createAnswer(); |
| 82 | }) |
| 83 | .then(function(answer) { |
| 84 | atStep = 'Set local description at second'; |
| 85 | return gSecondConnection.setLocalDescription(answer); |
| 86 | }) |
| 87 | .then(function() { |
| 88 | atStep = 'Set remote description at first'; |
| 89 | return gFirstConnection.setRemoteDescription( |
| 90 | gSecondConnection.localDescription); |
| 91 | }) |
| 92 | .then(function() { |
| 93 | atStep = 'Calling getStats'; |
| 94 | return gFirstConnection.getStats(); |
| 95 | }) |
| 96 | .then(function(report) { |
| 97 | atStep = 'Analyzing stats'; |
| 98 | // Show the retrieved stats info |
| 99 | var showStats = document.getElementById('stats'); |
| 100 | let reportDictionary = {}; |
| 101 | for (let stats of report.values()) { |
| 102 | reportDictionary[stats.id] = stats; |
| 103 | } |
| 104 | showStats.innerHTML = JSON.stringify(reportDictionary, null, 2); |
| 105 | // Check the stats properties. |
| 106 | assert_not_equals(report, null); |
| 107 | let sessionStat = getStatsRecordByType(report, 'peer-connection'); |
| 108 | assert_not_equals(sessionStat, null, 'Did not find peerconnection stats'); |
| 109 | assert_exists(sessionStat, 'dataChannelsOpened'); |
| 110 | test.done(); |
| 111 | }) |
| 112 | .catch(test.step_func(function(e) { |
| 113 | assert_unreached('Error ' + e.name + ': ' + e.message + |
| 114 | ' happened at step ' + atStep); |
| 115 | })); |
| 116 | }); |
| 117 | </script> |
| 118 | |
| 119 | </body> |
| 120 | </html> |