ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / tools / perf / measurements / smooth_gesture_util_unittest.py
blob42475b031006953a3c8ea5224b52f55a674f0b77
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
7 from measurements import smooth_gesture_util as sg_util
8 from telemetry import decorators
9 from telemetry.core.platform import tracing_category_filter
10 from telemetry.core.platform import tracing_options
11 from telemetry.page import page as page_module
12 from telemetry.page import page_test
13 from telemetry.timeline import async_slice
14 from telemetry.timeline import model as model_module
15 from telemetry.unittest_util import page_test_test_case
16 from telemetry.web_perf import timeline_interaction_record as tir_module
19 class SmoothGestureUtilTest(unittest.TestCase):
20 def testGetAdjustedInteractionIfContainGesture(self):
21 model = model_module.TimelineModel()
22 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
23 renderer_main.name = 'CrRendererMain'
25 # [ X ] [ Y ]
26 # [ sub_async_slice_X ]
27 # [ record_1]
28 # [ record_6]
29 # [ record_2 ] [ record_3 ]
30 # [ record_4 ]
31 # [ record_5 ]
33 # Note: X and Y are async slice with name
34 # SyntheticGestureController::running
36 async_slice_X = async_slice.AsyncSlice(
37 'X', 'SyntheticGestureController::running', 10, duration=20,
38 start_thread=renderer_main, end_thread=renderer_main)
40 sub_async_slice_X = async_slice.AsyncSlice(
41 'X', 'SyntheticGestureController::running', 10, duration=20,
42 start_thread=renderer_main, end_thread=renderer_main)
43 sub_async_slice_X.parent_slice = async_slice_X
44 async_slice_X.AddSubSlice(sub_async_slice_X)
46 async_slice_Y = async_slice.AsyncSlice(
47 'X', 'SyntheticGestureController::running', 60, duration=20,
48 start_thread=renderer_main, end_thread=renderer_main)
50 renderer_main.AddAsyncSlice(async_slice_X)
51 renderer_main.AddAsyncSlice(async_slice_Y)
53 model.FinalizeImport(shift_world_to_zero=False)
55 record_1 = tir_module.TimelineInteractionRecord('Gesture_included', 15, 25)
56 record_2 = tir_module.TimelineInteractionRecord(
57 'Gesture_overlapped_left', 5, 25)
58 record_3 = tir_module.TimelineInteractionRecord(
59 'Gesture_overlapped_right', 25, 35)
60 record_4 = tir_module.TimelineInteractionRecord(
61 'Gesture_containing', 5, 35)
62 record_5 = tir_module.TimelineInteractionRecord(
63 'Gesture_non_overlapped', 35, 45)
64 record_6 = tir_module.TimelineInteractionRecord('Action_included', 15, 25)
66 adjusted_record_1 = sg_util.GetAdjustedInteractionIfContainGesture(
67 model, record_1)
68 self.assertEquals(adjusted_record_1.start, 10)
69 self.assertEquals(adjusted_record_1.end, 30)
70 self.assertTrue(adjusted_record_1 is not record_1)
72 adjusted_record_2 = sg_util.GetAdjustedInteractionIfContainGesture(
73 model, record_2)
74 self.assertEquals(adjusted_record_2.start, 10)
75 self.assertEquals(adjusted_record_2.end, 30)
77 adjusted_record_3 = sg_util.GetAdjustedInteractionIfContainGesture(
78 model, record_3)
79 self.assertEquals(adjusted_record_3.start, 10)
80 self.assertEquals(adjusted_record_3.end, 30)
82 adjusted_record_4 = sg_util.GetAdjustedInteractionIfContainGesture(
83 model, record_4)
84 self.assertEquals(adjusted_record_4.start, 10)
85 self.assertEquals(adjusted_record_4.end, 30)
87 adjusted_record_5 = sg_util.GetAdjustedInteractionIfContainGesture(
88 model, record_5)
89 self.assertEquals(adjusted_record_5.start, 35)
90 self.assertEquals(adjusted_record_5.end, 45)
91 self.assertTrue(adjusted_record_5 is not record_5)
93 adjusted_record_6 = sg_util.GetAdjustedInteractionIfContainGesture(
94 model, record_6)
95 self.assertEquals(adjusted_record_6.start, 15)
96 self.assertEquals(adjusted_record_6.end, 25)
97 self.assertTrue(adjusted_record_6 is not record_6)
100 class ScrollingPage(page_module.Page):
101 def __init__(self, url, page_set, base_dir):
102 super(ScrollingPage, self).__init__(url, page_set, base_dir)
104 def RunPageInteractions(self, action_runner):
105 interaction = action_runner.BeginGestureInteraction(
106 'ScrollAction', is_smooth=True)
107 # Add 0.5s gap between when Gesture records are issued to when we actually
108 # scroll the page.
109 time.sleep(0.5)
110 action_runner.ScrollPage()
111 time.sleep(0.5)
112 interaction.End()
115 class SmoothGestureTest(page_test_test_case.PageTestTestCase):
116 @decorators.Disabled('mac') # crbug.com/450171.
117 def testSmoothGestureAdjusted(self):
118 ps = self.CreateEmptyPageSet()
119 ps.AddUserStory(ScrollingPage(
120 'file://scrollable_page.html', ps, base_dir=ps.base_dir))
121 models = []
122 tab_ids = []
123 class ScrollingGestureTestMeasurement(page_test.PageTest):
124 def __init__(self):
125 # pylint: disable=bad-super-call
126 super(ScrollingGestureTestMeasurement, self).__init__(
127 'RunPageInteractions', False)
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())
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)
140 def ValidateAndMeasurePage(self, _page, _tab, _results):
141 pass
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)