OLD | NEW |
| (Empty) |
1 # Copyright 2014 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. | |
4 import time | |
5 import unittest | |
6 | |
7 from telemetry.core.platform import tracing_category_filter | |
8 from telemetry.core.platform import tracing_options | |
9 from telemetry import decorators | |
10 from telemetry.page import page as page_module | |
11 from telemetry.page import page_test | |
12 from telemetry.timeline import async_slice | |
13 from telemetry.timeline import model as model_module | |
14 from telemetry.unittest_util import page_test_test_case | |
15 from telemetry.web_perf import timeline_interaction_record as tir_module | |
16 | |
17 from measurements import smooth_gesture_util as sg_util | |
18 | |
19 | |
20 class SmoothGestureUtilTest(unittest.TestCase): | |
21 def testGetAdjustedInteractionIfContainGesture(self): | |
22 model = model_module.TimelineModel() | |
23 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2) | |
24 renderer_main.name = 'CrRendererMain' | |
25 | |
26 # [ X ] [ Y ] | |
27 # [ sub_async_slice_X ] | |
28 # [ record_1] | |
29 # [ record_6] | |
30 # [ record_2 ] [ record_3 ] | |
31 # [ record_4 ] | |
32 # [ record_5 ] | |
33 # | |
34 # Note: X and Y are async slice with name | |
35 # SyntheticGestureController::running | |
36 | |
37 async_slice_X = async_slice.AsyncSlice( | |
38 'X', 'SyntheticGestureController::running', 10, duration=20, | |
39 start_thread=renderer_main, end_thread=renderer_main) | |
40 | |
41 sub_async_slice_X = async_slice.AsyncSlice( | |
42 'X', 'SyntheticGestureController::running', 10, duration=20, | |
43 start_thread=renderer_main, end_thread=renderer_main) | |
44 sub_async_slice_X.parent_slice = async_slice_X | |
45 async_slice_X.AddSubSlice(sub_async_slice_X) | |
46 | |
47 async_slice_Y = async_slice.AsyncSlice( | |
48 'X', 'SyntheticGestureController::running', 60, duration=20, | |
49 start_thread=renderer_main, end_thread=renderer_main) | |
50 | |
51 renderer_main.AddAsyncSlice(async_slice_X) | |
52 renderer_main.AddAsyncSlice(async_slice_Y) | |
53 | |
54 model.FinalizeImport(shift_world_to_zero=False) | |
55 | |
56 record_1 = tir_module.TimelineInteractionRecord('Gesture_included', 15, 25) | |
57 record_2 = tir_module.TimelineInteractionRecord( | |
58 'Gesture_overlapped_left', 5, 25) | |
59 record_3 = tir_module.TimelineInteractionRecord( | |
60 'Gesture_overlapped_right', 25, 35) | |
61 record_4 = tir_module.TimelineInteractionRecord( | |
62 'Gesture_containing', 5, 35) | |
63 record_5 = tir_module.TimelineInteractionRecord( | |
64 'Gesture_non_overlapped', 35, 45) | |
65 record_6 = tir_module.TimelineInteractionRecord('Action_included', 15, 25) | |
66 | |
67 adjusted_record_1 = sg_util.GetAdjustedInteractionIfContainGesture( | |
68 model, record_1) | |
69 self.assertEquals(adjusted_record_1.start, 10) | |
70 self.assertEquals(adjusted_record_1.end, 30) | |
71 self.assertTrue(adjusted_record_1 is not record_1) | |
72 | |
73 adjusted_record_2 = sg_util.GetAdjustedInteractionIfContainGesture( | |
74 model, record_2) | |
75 self.assertEquals(adjusted_record_2.start, 10) | |
76 self.assertEquals(adjusted_record_2.end, 30) | |
77 | |
78 adjusted_record_3 = sg_util.GetAdjustedInteractionIfContainGesture( | |
79 model, record_3) | |
80 self.assertEquals(adjusted_record_3.start, 10) | |
81 self.assertEquals(adjusted_record_3.end, 30) | |
82 | |
83 adjusted_record_4 = sg_util.GetAdjustedInteractionIfContainGesture( | |
84 model, record_4) | |
85 self.assertEquals(adjusted_record_4.start, 10) | |
86 self.assertEquals(adjusted_record_4.end, 30) | |
87 | |
88 adjusted_record_5 = sg_util.GetAdjustedInteractionIfContainGesture( | |
89 model, record_5) | |
90 self.assertEquals(adjusted_record_5.start, 35) | |
91 self.assertEquals(adjusted_record_5.end, 45) | |
92 self.assertTrue(adjusted_record_5 is not record_5) | |
93 | |
94 adjusted_record_6 = sg_util.GetAdjustedInteractionIfContainGesture( | |
95 model, record_6) | |
96 self.assertEquals(adjusted_record_6.start, 15) | |
97 self.assertEquals(adjusted_record_6.end, 25) | |
98 self.assertTrue(adjusted_record_6 is not record_6) | |
99 | |
100 | |
101 class ScrollingPage(page_module.Page): | |
102 def __init__(self, url, page_set, base_dir): | |
103 super(ScrollingPage, self).__init__(url, page_set, base_dir) | |
104 | |
105 def RunPageInteractions(self, action_runner): | |
106 interaction = action_runner.BeginGestureInteraction( | |
107 'ScrollAction') | |
108 # Add 0.5s gap between when Gesture records are issued to when we actually | |
109 # scroll the page. | |
110 time.sleep(0.5) | |
111 action_runner.ScrollPage() | |
112 time.sleep(0.5) | |
113 interaction.End() | |
114 | |
115 | |
116 class SmoothGestureTest(page_test_test_case.PageTestTestCase): | |
117 @decorators.Disabled('mac') # crbug.com/450171. | |
118 def testSmoothGestureAdjusted(self): | |
119 ps = self.CreateEmptyPageSet() | |
120 ps.AddUserStory(ScrollingPage( | |
121 'file://scrollable_page.html', ps, base_dir=ps.base_dir)) | |
122 models = [] | |
123 tab_ids = [] | |
124 class ScrollingGestureTestMeasurement(page_test.PageTest): | |
125 def __init__(self): | |
126 # pylint: disable=bad-super-call | |
127 super(ScrollingGestureTestMeasurement, self).__init__() | |
128 | |
129 def WillRunActions(self, _page, tab): | |
130 options = tracing_options.TracingOptions() | |
131 options.enable_chrome_trace = True | |
132 tab.browser.platform.tracing_controller.Start( | |
133 options, tracing_category_filter.TracingCategoryFilter()) | |
134 | |
135 def DidRunActions(self, _page, tab): | |
136 models.append(model_module.TimelineModel( | |
137 tab.browser.platform.tracing_controller.Stop())) | |
138 tab_ids.append(tab.id) | |
139 | |
140 def ValidateAndMeasurePage(self, _page, _tab, _results): | |
141 pass | |
142 | |
143 self.RunMeasurement(ScrollingGestureTestMeasurement(), ps) | |
144 timeline_model = models[0] | |
145 renderer_thread = timeline_model.GetRendererThreadFromTabId( | |
146 tab_ids[0]) | |
147 smooth_record = None | |
148 for e in renderer_thread.async_slices: | |
149 if tir_module.IsTimelineInteractionRecord(e.name): | |
150 smooth_record = tir_module.TimelineInteractionRecord.FromAsyncEvent(e) | |
151 self.assertIsNotNone(smooth_record) | |
152 adjusted_smooth_gesture = ( | |
153 sg_util.GetAdjustedInteractionIfContainGesture( | |
154 timeline_model, smooth_record)) | |
155 # Test that the scroll gesture starts at at least 500ms after the start of | |
156 # the interaction record and ends at at least 500ms before the end of | |
157 # interaction record. | |
158 self.assertLessEqual( | |
159 500, adjusted_smooth_gesture.start - smooth_record.start) | |
160 self.assertLessEqual( | |
161 500, smooth_record.end - adjusted_smooth_gesture.end) | |
OLD | NEW |