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.
6 from measurements
import smoothness
7 from metrics
import power
8 from telemetry
.core
import exceptions
9 from telemetry
.core
import wpr_modes
10 from telemetry
.page
import page
11 from telemetry
.page
import page_test
12 from telemetry
.unittest_util
import options_for_unittests
13 from telemetry
.unittest_util
import page_test_test_case
14 from telemetry
.unittest_util
import test
16 class FakeTracingController(object):
18 self
.category_filter
= None
19 def Start(self
, _options
, category_filter
, _timeout
):
20 self
.category_filter
= category_filter
23 class FakePlatform(object):
25 self
.tracing_controller
= FakeTracingController()
26 def IsRawDisplayFrameRateSupported(self
):
28 def CanMonitorPower(self
):
32 class FakeBrowser(object):
34 self
.platform
= FakePlatform()
37 class AnimatedPage(page
.Page
):
38 def __init__(self
, page_set
):
39 super(AnimatedPage
, self
).__init
__(
40 url
='file://animated_page.html',
41 page_set
=page_set
, base_dir
=page_set
.base_dir
)
43 def RunSmoothness(self
, action_runner
):
44 action_runner
.Wait(.2)
47 class FakeTab(object):
49 self
.browser
= FakeBrowser()
51 def ExecuteJavaScript(self
, js
):
54 class SmoothnessUnitTest(page_test_test_case
.PageTestTestCase
):
55 """Smoke test for smoothness measurement
57 Runs smoothness measurement on a simple page and verifies
58 that all metrics were added to the results. The test is purely functional,
59 i.e. it only checks if the metrics are present and non-zero.
61 def testSyntheticDelayConfiguration(self
):
62 test_page
= page
.Page('http://dummy', None)
63 test_page
.synthetic_delays
= {
64 'cc.BeginMainFrame': { 'target_duration': 0.012 },
65 'cc.DrawAndSwap': { 'target_duration': 0.012, 'mode': 'alternating' },
66 'gpu.PresentingFrame': { 'target_duration': 0.012 }
70 measurement
= smoothness
.Smoothness()
71 measurement
.WillStartBrowser(tab
.browser
.platform
)
72 measurement
.WillNavigateToPage(test_page
, tab
)
73 measurement
.WillRunActions(test_page
, tab
)
75 expected_category_filter
= set([
76 'DELAY(cc.BeginMainFrame;0.012000;static)',
77 'DELAY(cc.DrawAndSwap;0.012000;alternating)',
78 'DELAY(gpu.PresentingFrame;0.012000;static)',
81 tracing_controller
= tab
.browser
.platform
.tracing_controller
82 actual_category_filter
= (
83 tracing_controller
.category_filter
.included_categories
)
85 # FIXME: Put blink.console into the expected above and remove these two
86 # remove entries when the blink.console change has rolled into chromium.
87 actual_category_filter
.remove('webkit.console')
88 actual_category_filter
.remove('blink.console')
90 if expected_category_filter
!= actual_category_filter
:
91 sys
.stderr
.write("Expected category filter: %s\n" %
92 repr(expected_category_filter
))
93 sys
.stderr
.write("Actual category filter filter: %s\n" %
94 repr(actual_category_filter
))
95 self
.assertEquals(expected_category_filter
, actual_category_filter
)
98 self
._options
= options_for_unittests
.GetCopy()
99 self
._options
.browser_options
.wpr_mode
= wpr_modes
.WPR_OFF
101 def testSmoothness(self
):
102 ps
= self
.CreatePageSetFromFileInUnittestDataDir('scrollable_page.html')
103 measurement
= smoothness
.Smoothness()
104 results
= self
.RunMeasurement(measurement
, ps
, options
=self
._options
)
105 self
.assertEquals(0, len(results
.failures
))
107 frame_times
= results
.FindAllPageSpecificValuesNamed('frame_times')
108 self
.assertEquals(len(frame_times
), 1)
109 self
.assertGreater(frame_times
[0].GetRepresentativeNumber(), 0)
111 mean_frame_time
= results
.FindAllPageSpecificValuesNamed('mean_frame_time')
112 self
.assertEquals(len(mean_frame_time
), 1)
113 self
.assertGreater(mean_frame_time
[0].GetRepresentativeNumber(), 0)
115 frame_time_discrepancy
= results
.FindAllPageSpecificValuesNamed(
116 'frame_time_discrepancy')
117 self
.assertEquals(len(frame_time_discrepancy
), 1)
118 self
.assertGreater(frame_time_discrepancy
[0].GetRepresentativeNumber(), 0)
120 percentage_smooth
= results
.FindAllPageSpecificValuesNamed(
122 self
.assertEquals(len(percentage_smooth
), 1)
123 self
.assertGreaterEqual(percentage_smooth
[0].GetRepresentativeNumber(), 0)
125 mean_input_event_latency
= results
.FindAllPageSpecificValuesNamed(
126 'mean_input_event_latency')
127 if mean_input_event_latency
:
128 self
.assertEquals(len(mean_input_event_latency
), 1)
130 mean_input_event_latency
[0].GetRepresentativeNumber(), 0)
132 @test.Disabled('mac', 'chromeos') # http://crbug.com/403903
133 def testSmoothnessForPageWithNoGesture(self
):
134 ps
= self
.CreateEmptyPageSet()
135 ps
.AddPage(AnimatedPage(ps
))
137 measurement
= smoothness
.Smoothness()
138 results
= self
.RunMeasurement(measurement
, ps
, options
=self
._options
)
139 self
.assertEquals(0, len(results
.failures
))
141 percentage_smooth
= results
.FindAllPageSpecificValuesNamed(
143 self
.assertEquals(len(percentage_smooth
), 1)
144 self
.assertGreaterEqual(percentage_smooth
[0].GetRepresentativeNumber(), 0)
146 def testCleanUpTrace(self
):
147 self
.TestTracingCleanedUp(smoothness
.Smoothness
, self
._options
)
149 def testCleanUpPowerMetric(self
):
150 class FailPage(page
.Page
):
151 def __init__(self
, page_set
):
152 # pylint: disable=bad-super-call
153 super(FailPage
, self
).__init
__(
154 url
='file://blank.html',
155 page_set
=page_set
, base_dir
=page_set
.base_dir
)
156 def RunSmoothness(self
, _
):
157 raise exceptions
.IntentionalException
159 class FakePowerMetric(power
.PowerMetric
):
162 def Start(self
, _1
, _2
):
163 self
.start_called
= True
164 def Stop(self
, _1
, _2
):
165 self
.stop_called
= True
167 ps
= self
.CreateEmptyPageSet()
168 ps
.AddPage(FailPage(ps
))
170 class BuggyMeasurement(smoothness
.Smoothness
):
172 # Inject fake power metric.
173 def WillStartBrowser(self
, platform
):
174 self
.fake_power
= self
._power
_metric
= FakePowerMetric(platform
)
176 measurement
= BuggyMeasurement()
178 self
.RunMeasurement(measurement
, ps
)
179 except page_test
.TestNotSupportedOnPlatformFailure
:
182 self
.assertTrue(measurement
.fake_power
.start_called
)
183 self
.assertTrue(measurement
.fake_power
.stop_called
)