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.
7 from telemetry
import decorators
8 from telemetry
.page
import page
9 from telemetry
.testing
import options_for_unittests
10 from telemetry
.testing
import page_test_test_case
11 from telemetry
.util
import wpr_modes
12 from telemetry
.value
import scalar
14 from measurements
import smoothness
19 class FakeTracingController(object):
21 self
.category_filter
= None
22 def Start(self
, options
, category_filter
):
24 self
.category_filter
= category_filter
26 def IsChromeTracingSupported(self
):
30 class FakePlatform(object):
32 self
.tracing_controller
= FakeTracingController()
35 class FakeBrowser(object):
37 self
.platform
= FakePlatform()
40 class FakeTab(object):
42 self
.browser
= FakeBrowser()
44 def CollectGarbage(self
):
47 def ExecuteJavaScript(self
, js
):
51 class CustomResultsWrapperUnitTest(unittest
.TestCase
):
52 def testOnlyOneInteractionRecordPerPage(self
):
53 test_page
= page
.Page('http://dummy', None)
55 #pylint: disable=protected-access
56 results_wrapper
= smoothness
._CustomResultsWrapper
()
57 results_wrapper
.SetResults(mock
.Mock())
59 results_wrapper
.SetTirLabel('foo')
60 results_wrapper
.AddValue(scalar
.ScalarValue(test_page
, 'num', 'ms', 44))
62 results_wrapper
.SetTirLabel('bar')
63 with self
.assertRaises(AssertionError):
64 results_wrapper
.AddValue(scalar
.ScalarValue(test_page
, 'num', 'ms', 42))
67 class SmoothnessUnitTest(page_test_test_case
.PageTestTestCase
):
68 """Smoke test for smoothness measurement
70 Runs smoothness measurement on a simple page and verifies
71 that all metrics were added to the results. The test is purely functional,
72 i.e. it only checks if the metrics are present and non-zero.
75 self
._options
= options_for_unittests
.GetCopy()
76 self
._options
.browser_options
.wpr_mode
= wpr_modes
.WPR_OFF
78 def testSyntheticDelayConfiguration(self
):
79 test_page
= page
.Page('http://dummy', None)
80 test_page
.synthetic_delays
= {
81 'cc.BeginMainFrame': { 'target_duration': 0.012 },
82 'cc.DrawAndSwap': { 'target_duration': 0.012, 'mode': 'alternating' },
83 'gpu.PresentingFrame': { 'target_duration': 0.012 }
87 measurement
= smoothness
.Smoothness()
88 measurement
.WillStartBrowser(tab
.browser
.platform
)
89 measurement
.WillNavigateToPage(test_page
, tab
)
91 expected_synthetic_delay
= set([
92 'DELAY(cc.BeginMainFrame;0.012000;static)',
93 'DELAY(cc.DrawAndSwap;0.012000;alternating)',
94 'DELAY(gpu.PresentingFrame;0.012000;static)',
96 tracing_controller
= tab
.browser
.platform
.tracing_controller
97 actual_synthetic_delay
= (
98 tracing_controller
.category_filter
.synthetic_delays
)
100 if expected_synthetic_delay
!= actual_synthetic_delay
:
101 sys
.stderr
.write("Expected category filter: %s\n" %
102 repr(expected_synthetic_delay
))
103 sys
.stderr
.write("Actual category filter filter: %s\n" %
104 repr(actual_synthetic_delay
))
105 self
.assertEquals(expected_synthetic_delay
, actual_synthetic_delay
)
107 @decorators.Disabled('chromeos') # crbug.com/483212
108 def testSmoothness(self
):
109 ps
= self
.CreateStorySetFromFileInUnittestDataDir('scrollable_page.html')
110 measurement
= smoothness
.Smoothness()
111 results
= self
.RunMeasurement(measurement
, ps
, options
=self
._options
)
112 self
.assertEquals(0, len(results
.failures
))
114 frame_times
= results
.FindAllPageSpecificValuesNamed('frame_times')
115 self
.assertEquals(len(frame_times
), 1)
116 self
.assertGreater(frame_times
[0].GetRepresentativeNumber(), 0)
118 mean_frame_time
= results
.FindAllPageSpecificValuesNamed('mean_frame_time')
119 self
.assertEquals(len(mean_frame_time
), 1)
120 self
.assertGreater(mean_frame_time
[0].GetRepresentativeNumber(), 0)
122 frame_time_discrepancy
= results
.FindAllPageSpecificValuesNamed(
123 'frame_time_discrepancy')
124 self
.assertEquals(len(frame_time_discrepancy
), 1)
125 self
.assertGreater(frame_time_discrepancy
[0].GetRepresentativeNumber(), 0)
127 percentage_smooth
= results
.FindAllPageSpecificValuesNamed(
129 self
.assertEquals(len(percentage_smooth
), 1)
130 self
.assertGreaterEqual(percentage_smooth
[0].GetRepresentativeNumber(), 0)
132 mean_input_event_latency
= results
.FindAllPageSpecificValuesNamed(
133 'mean_input_event_latency')
134 if mean_input_event_latency
:
135 self
.assertEquals(len(mean_input_event_latency
), 1)
137 mean_input_event_latency
[0].GetRepresentativeNumber(), 0)
139 @decorators.Enabled('android') # SurfaceFlinger is android-only
140 def testSmoothnessSurfaceFlingerMetricsCalculated(self
):
141 ps
= self
.CreateStorySetFromFileInUnittestDataDir('scrollable_page.html')
142 measurement
= smoothness
.Smoothness()
143 results
= self
.RunMeasurement(measurement
, ps
, options
=self
._options
)
144 self
.assertEquals(0, len(results
.failures
))
146 avg_surface_fps
= results
.FindAllPageSpecificValuesNamed('avg_surface_fps')
147 self
.assertEquals(1, len(avg_surface_fps
))
148 self
.assertGreater(avg_surface_fps
[0].GetRepresentativeNumber
, 0)
150 jank_count
= results
.FindAllPageSpecificValuesNamed('jank_count')
151 self
.assertEquals(1, len(jank_count
))
152 self
.assertGreater(jank_count
[0].GetRepresentativeNumber(), -1)
154 max_frame_delay
= results
.FindAllPageSpecificValuesNamed('max_frame_delay')
155 self
.assertEquals(1, len(max_frame_delay
))
156 self
.assertGreater(max_frame_delay
[0].GetRepresentativeNumber
, 0)
158 frame_lengths
= results
.FindAllPageSpecificValuesNamed('frame_lengths')
159 self
.assertEquals(1, len(frame_lengths
))
160 self
.assertGreater(frame_lengths
[0].GetRepresentativeNumber
, 0)
162 def testCleanUpTrace(self
):
163 self
.TestTracingCleanedUp(smoothness
.Smoothness
, self
._options
)