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
10 from telemetry
.util
import color_histogram
11 from telemetry
.util
import rgba_color
13 from metrics
import speedindex
16 class FakeImageUtil(object):
17 # pylint: disable=W0613
18 def GetColorHistogram(self
, image
, ignore_color
=None, tolerance
=None):
19 return image
.ColorHistogram()
22 class FakeVideo(object):
23 def __init__(self
, frames
):
26 def GetVideoFrameIter(self
):
27 for frame
in self
._frames
:
30 class FakeBitmap(object):
31 def __init__(self
, r
, g
, b
):
32 self
._histogram
= color_histogram
.ColorHistogram(r
, g
, b
, rgba_color
.WHITE
)
34 # pylint: disable=W0613
35 def ColorHistogram(self
, ignore_color
=None, tolerance
=None):
36 return self
._histogram
39 class FakeTab(object):
40 def __init__(self
, video_capture_result
=None):
41 self
._javascript
_result
= None
42 self
._video
_capture
_result
= FakeVideo(video_capture_result
)
45 def video_capture_supported(self
):
46 return self
._video
_capture
_result
is not None
48 def SetEvaluateJavaScriptResult(self
, result
):
49 self
._javascript
_result
= result
51 def EvaluateJavaScript(self
, _
):
52 return self
._javascript
_result
54 def StartVideoCapture(self
, min_bitrate_mbps
=1):
55 assert self
.video_capture_supported
56 assert min_bitrate_mbps
> 0
58 def StopVideoCapture(self
):
59 assert self
.video_capture_supported
60 return self
._video
_capture
_result
62 def Highlight(self
, _
):
66 class SpeedIndexImplTest(unittest
.TestCase
):
68 def testVideoCompleteness(self
):
70 (0.0, FakeBitmap([ 0, 0, 0,10], [ 0, 0, 0,10], [ 0, 0, 0,10])),
71 (0.1, FakeBitmap([10, 0, 0, 0], [10, 0, 0, 0], [10, 0, 0, 0])),
72 (0.2, FakeBitmap([ 0, 0, 2, 8], [ 0, 0, 4, 6], [ 0, 0, 1, 9])),
73 (0.3, FakeBitmap([ 0, 3, 2, 5], [ 2, 1, 0, 7], [ 0, 3, 0, 7])),
74 (0.4, FakeBitmap([ 0, 0, 1, 0], [ 0, 0, 1, 0], [ 0, 0, 1, 0])),
75 (0.5, FakeBitmap([ 0, 4, 6, 0], [ 0, 4, 6, 0], [ 0, 4, 6, 0])),
80 impl
= speedindex
.VideoSpeedIndexImpl(FakeImageUtil())
83 time_completeness
= impl
.GetTimeCompletenessList(tab
)
84 self
.assertEqual(len(time_completeness
), 6)
85 self
.assertEqual(time_completeness
[0], (0.0, 0))
86 self
.assertTimeCompleteness(
87 time_completeness
[1], 0.1, 1 - (16 + 16 + 16) / max_distance
)
88 self
.assertTimeCompleteness(
89 time_completeness
[2], 0.2, 1 - (12 + 10 + 13) / max_distance
)
90 self
.assertTimeCompleteness(
91 time_completeness
[3], 0.3, 1 - (6 + 10 + 8) / max_distance
)
92 self
.assertTimeCompleteness(
93 time_completeness
[4], 0.4, 1 - (4 + 4 + 4) / max_distance
)
94 self
.assertEqual(time_completeness
[5], (0.5, 1))
96 def testBlankPage(self
):
98 (0.0, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
99 (0.1, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
100 (0.2, FakeBitmap([1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1])),
101 (0.3, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
103 tab
= FakeTab(frames
)
104 impl
= speedindex
.VideoSpeedIndexImpl(FakeImageUtil())
107 time_completeness
= impl
.GetTimeCompletenessList(tab
)
108 self
.assertEqual(len(time_completeness
), 4)
109 self
.assertEqual(time_completeness
[0], (0.0, 1.0))
110 self
.assertEqual(time_completeness
[1], (0.1, 1.0))
111 self
.assertEqual(time_completeness
[2], (0.2, 0.0))
112 self
.assertEqual(time_completeness
[3], (0.3, 1.0))
114 def assertTimeCompleteness(self
, time_completeness
, time
, completeness
):
115 self
.assertEqual(time_completeness
[0], time
)
116 self
.assertAlmostEqual(time_completeness
[1], completeness
)
119 if __name__
== "__main__":