[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / perf / metrics / speedindex_unittest.py
blob760ff73f1b4a0c6718baf94a73f50bfffb7ff1d6
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.
5 # These tests access private methods in the speedindex module.
6 # pylint: disable=W0212
8 import json
9 import os
10 import unittest
12 from telemetry.image_processing import histogram
13 from telemetry.image_processing import rgba_color
15 from metrics import speedindex
18 class FakeImageUtil(object):
19 # pylint: disable=W0613
20 def GetColorHistogram(self, image, ignore_color=None, tolerance=None):
21 return image.ColorHistogram()
24 class FakeVideo(object):
25 def __init__(self, frames):
26 self._frames = frames
28 def GetVideoFrameIter(self):
29 for frame in self._frames:
30 yield frame
32 class FakeBitmap(object):
33 def __init__(self, r, g, b):
34 self._histogram = histogram.ColorHistogram(r, g, b, rgba_color.WHITE)
36 # pylint: disable=W0613
37 def ColorHistogram(self, ignore_color=None, tolerance=None):
38 return self._histogram
41 class FakeTab(object):
42 def __init__(self, video_capture_result=None):
43 self._javascript_result = None
44 self._video_capture_result = FakeVideo(video_capture_result)
46 @property
47 def video_capture_supported(self):
48 return self._video_capture_result is not None
50 def SetEvaluateJavaScriptResult(self, result):
51 self._javascript_result = result
53 def EvaluateJavaScript(self, _):
54 return self._javascript_result
56 def StartVideoCapture(self, min_bitrate_mbps=1):
57 assert self.video_capture_supported
58 assert min_bitrate_mbps > 0
60 def StopVideoCapture(self):
61 assert self.video_capture_supported
62 return self._video_capture_result
64 def Highlight(self, _):
65 pass
68 class SpeedIndexImplTest(unittest.TestCase):
70 def testVideoCompleteness(self):
71 frames = [
72 (0.0, FakeBitmap([ 0, 0, 0,10], [ 0, 0, 0,10], [ 0, 0, 0,10])),
73 (0.1, FakeBitmap([10, 0, 0, 0], [10, 0, 0, 0], [10, 0, 0, 0])),
74 (0.2, FakeBitmap([ 0, 0, 2, 8], [ 0, 0, 4, 6], [ 0, 0, 1, 9])),
75 (0.3, FakeBitmap([ 0, 3, 2, 5], [ 2, 1, 0, 7], [ 0, 3, 0, 7])),
76 (0.4, FakeBitmap([ 0, 0, 1, 0], [ 0, 0, 1, 0], [ 0, 0, 1, 0])),
77 (0.5, FakeBitmap([ 0, 4, 6, 0], [ 0, 4, 6, 0], [ 0, 4, 6, 0])),
79 max_distance = 42.
81 tab = FakeTab(frames)
82 impl = speedindex.VideoSpeedIndexImpl(FakeImageUtil())
83 impl.Start(tab)
84 impl.Stop(tab)
85 time_completeness = impl.GetTimeCompletenessList(tab)
86 self.assertEqual(len(time_completeness), 6)
87 self.assertEqual(time_completeness[0], (0.0, 0))
88 self.assertTimeCompleteness(
89 time_completeness[1], 0.1, 1 - (16 + 16 + 16) / max_distance)
90 self.assertTimeCompleteness(
91 time_completeness[2], 0.2, 1 - (12 + 10 + 13) / max_distance)
92 self.assertTimeCompleteness(
93 time_completeness[3], 0.3, 1 - (6 + 10 + 8) / max_distance)
94 self.assertTimeCompleteness(
95 time_completeness[4], 0.4, 1 - (4 + 4 + 4) / max_distance)
96 self.assertEqual(time_completeness[5], (0.5, 1))
98 def testBlankPage(self):
99 frames = [
100 (0.0, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
101 (0.1, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
102 (0.2, FakeBitmap([1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1])),
103 (0.3, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
105 tab = FakeTab(frames)
106 impl = speedindex.VideoSpeedIndexImpl(FakeImageUtil())
107 impl.Start(tab)
108 impl.Stop(tab)
109 time_completeness = impl.GetTimeCompletenessList(tab)
110 self.assertEqual(len(time_completeness), 4)
111 self.assertEqual(time_completeness[0], (0.0, 1.0))
112 self.assertEqual(time_completeness[1], (0.1, 1.0))
113 self.assertEqual(time_completeness[2], (0.2, 0.0))
114 self.assertEqual(time_completeness[3], (0.3, 1.0))
116 def assertTimeCompleteness(self, time_completeness, time, completeness):
117 self.assertEqual(time_completeness[0], time)
118 self.assertAlmostEqual(time_completeness[1], completeness)
121 if __name__ == "__main__":
122 unittest.main()