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 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
):
28 def GetVideoFrameIter(self
):
29 for frame
in self
._frames
:
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
)
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
, _
):
68 class SpeedIndexImplTest(unittest
.TestCase
):
70 def testVideoCompleteness(self
):
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])),
82 impl
= speedindex
.VideoSpeedIndexImpl(FakeImageUtil())
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
):
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())
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__":