tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

getstats.html (4359B)


      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  <input type="button" onclick="showStats()" value="Show stats"></input>
     16  <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.');
     25  var statsToShow;
     26  var gFirstConnection = null;
     27  var gSecondConnection = null;
     28 
     29  var onIceCandidateToFirst = test.step_func(function(event) {
     30    gSecondConnection.addIceCandidate(event.candidate);
     31  });
     32 
     33  var onIceCandidateToSecond = test.step_func(function(event) {
     34    gFirstConnection.addIceCandidate(event.candidate);
     35  });
     36 
     37  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 
     46  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) {
     55      let reportDictionary = {};
     56      for (let stats of report.values()) {
     57        reportDictionary[stats.id] = stats;
     58      }
     59      statsToShow = JSON.stringify(reportDictionary, null, 2);
     60      // Check the stats properties.
     61      assert_not_equals(report, null, 'No report');
     62      let sessionStat = getStatsRecordByType(report, 'peer-connection');
     63      assert_not_equals(sessionStat, null, 'Did not find peer-connection stats');
     64      assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat');
     65      // 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');
     69      test.done();
     70    })
     71    .catch(test.step_func(function(e) {
     72      assert_unreached(e.name + ': ' + e.message + ': ');
     73    }));
     74  });
     75 
     76  // This function starts the test.
     77  test.step(function() {
     78    gFirstConnection = new RTCPeerConnection(null);
     79    test.add_cleanup(() => gFirstConnection.close());
     80    gFirstConnection.onicecandidate = onIceCandidateToFirst;
     81    gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange;
     82 
     83    gSecondConnection = new RTCPeerConnection(null);
     84    test.add_cleanup(() => gSecondConnection.close());
     85    gSecondConnection.onicecandidate = onIceCandidateToSecond;
     86 
     87    // The createDataChannel is necessary and sufficient to make
     88    // sure the ICE connection be attempted.
     89    gFirstConnection.createDataChannel('channel');
     90    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    })
    115    .catch(test.step_func(function(e) {
    116      assert_unreached('Error ' + e.name + ': ' + e.message +
    117                       ' happened at step ' + atStep);
    118    }));
    119  });
    120 
    121  function showStats() {
    122    // Show the retrieved stats info
    123    var showStats = document.getElementById('stats');
    124    showStats.innerHTML = statsToShow;
    125  }
    126 
    127 </script>
    128 
    129 </body>
    130 </html>