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
12 from metrics
import speedindex
13 from telemetry
.image_processing
import histogram
14 from telemetry
.image_processing
import rgba_color
17 class FakeImageUtil(object):
18 # pylint: disable=W0613
19 def GetColorHistogram(self
, image
, ignore_color
=None, tolerance
=None):
20 return image
.ColorHistogram()
23 class FakeVideo(object):
24 def __init__(self
, frames
):
27 def GetVideoFrameIter(self
):
28 for frame
in self
._frames
:
31 class FakeBitmap(object):
32 def __init__(self
, r
, g
, b
):
33 self
._histogram
= histogram
.ColorHistogram(r
, g
, b
, rgba_color
.WHITE
)
35 # pylint: disable=W0613
36 def ColorHistogram(self
, ignore_color
=None, tolerance
=None):
37 return self
._histogram
40 class FakeTab(object):
41 def __init__(self
, video_capture_result
=None):
42 self
._javascript
_result
= None
43 self
._video
_capture
_result
= FakeVideo(video_capture_result
)
46 def video_capture_supported(self
):
47 return self
._video
_capture
_result
is not None
49 def SetEvaluateJavaScriptResult(self
, result
):
50 self
._javascript
_result
= result
52 def EvaluateJavaScript(self
, _
):
53 return self
._javascript
_result
55 def StartVideoCapture(self
, min_bitrate_mbps
=1):
56 assert self
.video_capture_supported
57 assert min_bitrate_mbps
> 0
59 def StopVideoCapture(self
):
60 assert self
.video_capture_supported
61 return self
._video
_capture
_result
63 def Highlight(self
, _
):
67 class SpeedIndexImplTest(unittest
.TestCase
):
69 def testVideoCompleteness(self
):
71 (0.0, FakeBitmap([ 0, 0, 0,10], [ 0, 0, 0,10], [ 0, 0, 0,10])),
72 (0.1, FakeBitmap([10, 0, 0, 0], [10, 0, 0, 0], [10, 0, 0, 0])),
73 (0.2, FakeBitmap([ 0, 0, 2, 8], [ 0, 0, 4, 6], [ 0, 0, 1, 9])),
74 (0.3, FakeBitmap([ 0, 3, 2, 5], [ 2, 1, 0, 7], [ 0, 3, 0, 7])),
75 (0.4, FakeBitmap([ 0, 0, 1, 0], [ 0, 0, 1, 0], [ 0, 0, 1, 0])),
76 (0.5, FakeBitmap([ 0, 4, 6, 0], [ 0, 4, 6, 0], [ 0, 4, 6, 0])),
81 impl
= speedindex
.VideoSpeedIndexImpl(FakeImageUtil())
84 time_completeness
= impl
.GetTimeCompletenessList(tab
)
85 self
.assertEqual(len(time_completeness
), 6)
86 self
.assertEqual(time_completeness
[0], (0.0, 0))
87 self
.assertTimeCompleteness(
88 time_completeness
[1], 0.1, 1 - (16 + 16 + 16) / max_distance
)
89 self
.assertTimeCompleteness(
90 time_completeness
[2], 0.2, 1 - (12 + 10 + 13) / max_distance
)
91 self
.assertTimeCompleteness(
92 time_completeness
[3], 0.3, 1 - (6 + 10 + 8) / max_distance
)
93 self
.assertTimeCompleteness(
94 time_completeness
[4], 0.4, 1 - (4 + 4 + 4) / max_distance
)
95 self
.assertEqual(time_completeness
[5], (0.5, 1))
97 def testBlankPage(self
):
99 (0.0, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
100 (0.1, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
101 (0.2, FakeBitmap([1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1])),
102 (0.3, FakeBitmap([0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1])),
104 tab
= FakeTab(frames
)
105 impl
= speedindex
.VideoSpeedIndexImpl(FakeImageUtil())
108 time_completeness
= impl
.GetTimeCompletenessList(tab
)
109 self
.assertEqual(len(time_completeness
), 4)
110 self
.assertEqual(time_completeness
[0], (0.0, 1.0))
111 self
.assertEqual(time_completeness
[1], (0.1, 1.0))
112 self
.assertEqual(time_completeness
[2], (0.2, 0.0))
113 self
.assertEqual(time_completeness
[3], (0.3, 1.0))
115 def assertTimeCompleteness(self
, time_completeness
, time
, completeness
):
116 self
.assertEqual(time_completeness
[0], time
)
117 self
.assertAlmostEqual(time_completeness
[1], completeness
)
120 if __name__
== "__main__":