Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / tools / perf / metrics / webrtc_stats_unittest.py
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import unittest
6
7 from metrics import webrtc_stats
8 from telemetry.unittest import simple_mock
9
10
11 SAMPLE_JSON = '''
12 [[
13    [
14       {
15          "googFrameHeightInput":"480",
16          "googFrameWidthInput":"640",
17          "googFrameRateSent": "23",
18          "packetsLost":"-1",
19          "googRtt":"-1",
20          "packetsSent":"1",
21          "bytesSent":"0"
22       },
23       {
24          "audioInputLevel":"2048",
25          "googRtt":"-1",
26          "googCodecName":"opus",
27          "packetsSent":"4",
28          "bytesSent":"0"
29       }
30    ],
31    [
32       {
33          "googFrameHeightInput":"480",
34          "googFrameWidthInput":"640",
35          "googFrameRateSent": "21",
36          "packetsLost":"-1",
37          "googRtt":"-1",
38          "packetsSent":"8",
39          "bytesSent":"6291"
40       },
41       {
42          "audioInputLevel":"1878",
43          "googRtt":"-1",
44          "googCodecName":"opus",
45          "packetsSent":"16",
46          "bytesSent":"634"
47       }
48    ]
49 ],
50 [
51    [
52       {
53          "googFrameRateReceived": "23",
54          "googDecodeMs":"0",
55          "packetsReceived":"8",
56          "googRenderDelayMs":"10",
57          "googMaxDecodeMs":"0"
58       }
59    ],
60    [
61       {
62          "googFrameRateReceived": "23",
63          "googDecodeMs":"14",
64          "packetsReceived":"1234",
65          "googRenderDelayMs":"102",
66          "googMaxDecodeMs":"150"
67       }
68    ]
69 ]]
70 '''
71
72
73 class FakeResults:
74   def __init__(self, current_page):
75     self._received_values = []
76     self._current_page = current_page
77
78   @property
79   def received_values(self):
80     return self._received_values
81
82   @property
83   def current_page(self):
84     return self._current_page
85
86   def AddValue(self, value):
87     self._received_values.append(value)
88
89
90 class WebRtcStatsUnittest(unittest.TestCase):
91
92   def _RunMetricOnJson(self, json_to_return):
93     stats_metric = webrtc_stats.WebRtcStatisticsMetric()
94
95     tab = simple_mock.MockObject()
96     page = simple_mock.MockObject()
97
98     stats_metric.Start(page, tab)
99
100     tab.ExpectCall('EvaluateJavaScript',
101                    simple_mock.DONT_CARE).WillReturn(json_to_return)
102     stats_metric.Stop(page, tab)
103
104     page.url = simple_mock.MockObject()
105     results = FakeResults(page)
106     stats_metric.AddResults(tab, results)
107     return results
108
109   def testExtractsValuesAsTimeSeries(self):
110     results = self._RunMetricOnJson(SAMPLE_JSON)
111
112     self.assertTrue(results.received_values,
113                     'Expected values for googDecodeMs and others, got none.')
114
115     # This also ensures we're clever enough to tell video packetsSent from audio
116     # packetsSent.
117     self.assertEqual(results.received_values[0].values,
118                      [4.0, 16.0])
119     self.assertEqual(results.received_values[1].values,
120                      [1.0, 8.0])
121
122   def testExtractsInterestingMetricsOnly(self):
123     results = self._RunMetricOnJson(SAMPLE_JSON)
124
125     self.assertEqual(len(results.received_values), 5)
126     self.assertEqual(results.received_values[0].name,
127                      'peer_connection_0_audio_packets_sent',
128                      'The result should be a ListOfScalarValues instance with '
129                      'a name <peer connection id>_<statistic>.')
130     self.assertEqual(results.received_values[1].name,
131                      'peer_connection_0_video_packets_sent')
132     self.assertEqual(results.received_values[2].name,
133                      'peer_connection_1_video_goog_max_decode_ms')
134     self.assertEqual(results.received_values[3].name,
135                      'peer_connection_1_video_packets_received')
136     self.assertEqual(results.received_values[4].name,
137                      'peer_connection_1_video_goog_decode_ms')
138
139   def testReturnsIfJsonIsEmpty(self):
140     results = self._RunMetricOnJson('[]')
141     self.assertFalse(results.received_values)
142