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"""Unittests for SurfaceStatsCollector."""
6# pylint: disable=W0212
7
8import unittest
9
10from pylib.perf.surface_stats_collector import SurfaceStatsCollector
11
12
13class TestSurfaceStatsCollector(unittest.TestCase):
14  @staticmethod
15  def _CreateUniformTimestamps(base, num, delta):
16    return [base + i * delta for i in range(1, num + 1)]
17
18  @staticmethod
19  def _CreateDictionaryFromResults(results):
20    dictionary = {}
21    for result in results:
22      dictionary[result.name] = result
23    return dictionary
24
25  def setUp(self):
26    self.refresh_period = 0.1
27
28  def testOneFrameDelta(self):
29    timestamps = self._CreateUniformTimestamps(0, 10, self.refresh_period)
30    results = self._CreateDictionaryFromResults(
31                  SurfaceStatsCollector._CalculateResults(
32                      self.refresh_period, timestamps, ''))
33
34    self.assertEquals(results['avg_surface_fps'].value,
35                      int(round(1 / self.refresh_period)))
36    self.assertEquals(results['jank_count'].value, 0)
37    self.assertEquals(results['max_frame_delay'].value, 1)
38    self.assertEquals(len(results['frame_lengths'].value), len(timestamps) - 1)
39
40  def testAllFramesTooShort(self):
41    timestamps = self._CreateUniformTimestamps(0, 10, self.refresh_period / 100)
42    self.assertRaises(Exception,
43                      SurfaceStatsCollector._CalculateResults,
44                      [self.refresh_period, timestamps, ''])
45
46  def testSomeFramesTooShort(self):
47    timestamps = self._CreateUniformTimestamps(0, 5, self.refresh_period)
48    # The following timestamps should be skipped.
49    timestamps += self._CreateUniformTimestamps(timestamps[4],
50                                                5,
51                                                self.refresh_period / 100)
52    timestamps += self._CreateUniformTimestamps(timestamps[4],
53                                                5,
54                                                self.refresh_period)
55
56    results = self._CreateDictionaryFromResults(
57                  SurfaceStatsCollector._CalculateResults(
58                      self.refresh_period, timestamps, ''))
59
60    self.assertEquals(len(results['frame_lengths'].value), 9)
61
62
63if __name__ == '__main__':
64  unittest.main()
65