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.
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
17 from measurements
import smooth_gesture_util
as sg_util
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'
27 # [ sub_async_slice_X ]
30 # [ record_2 ] [ record_3 ]
34 # Note: X and Y are async slice with name
35 # SyntheticGestureController::running
37 async_slice_X
= async_slice
.AsyncSlice(
38 'X', 'SyntheticGestureController::running', 10, duration
=20,
39 start_thread
=renderer_main
, end_thread
=renderer_main
)
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
)
47 async_slice_Y
= async_slice
.AsyncSlice(
48 'X', 'SyntheticGestureController::running', 60, duration
=20,
49 start_thread
=renderer_main
, end_thread
=renderer_main
)
51 renderer_main
.AddAsyncSlice(async_slice_X
)
52 renderer_main
.AddAsyncSlice(async_slice_Y
)
54 model
.FinalizeImport(shift_world_to_zero
=False)
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)
67 adjusted_record_1
= sg_util
.GetAdjustedInteractionIfContainGesture(
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
)
73 adjusted_record_2
= sg_util
.GetAdjustedInteractionIfContainGesture(
75 self
.assertEquals(adjusted_record_2
.start
, 10)
76 self
.assertEquals(adjusted_record_2
.end
, 30)
78 adjusted_record_3
= sg_util
.GetAdjustedInteractionIfContainGesture(
80 self
.assertEquals(adjusted_record_3
.start
, 10)
81 self
.assertEquals(adjusted_record_3
.end
, 30)
83 adjusted_record_4
= sg_util
.GetAdjustedInteractionIfContainGesture(
85 self
.assertEquals(adjusted_record_4
.start
, 10)
86 self
.assertEquals(adjusted_record_4
.end
, 30)
88 adjusted_record_5
= sg_util
.GetAdjustedInteractionIfContainGesture(
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
)
94 adjusted_record_6
= sg_util
.GetAdjustedInteractionIfContainGesture(
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
)
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
)
105 def RunPageInteractions(self
, action_runner
):
106 interaction
= action_runner
.BeginGestureInteraction(
108 # Add 0.5s gap between when Gesture records are issued to when we actually
111 action_runner
.ScrollPage()
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
))
124 class ScrollingGestureTestMeasurement(page_test
.PageTest
):
126 # pylint: disable=bad-super-call
127 super(ScrollingGestureTestMeasurement
, self
).__init
__()
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
):
143 self
.RunMeasurement(ScrollingGestureTestMeasurement(), ps
)
144 timeline_model
= models
[0]
145 renderer_thread
= timeline_model
.GetRendererThreadFromTabId(
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
)