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.
6 from telemetry.page.actions.gesture_action import GestureAction
7 from telemetry.page.actions import page_action
9 class ScrollBounceAction(GestureAction):
10 def __init__(self, attributes=None):
11 super(ScrollBounceAction, self).__init__(attributes)
13 def WillRunAction(self, page, tab):
14 for js_file in ['gesture_common.js', 'scroll_bounce.js']:
15 with open(os.path.join(os.path.dirname(__file__), js_file)) as f:
17 tab.ExecuteJavaScript(js)
19 # Fail if browser doesn't support synthetic scroll bounce gestures.
20 if not tab.EvaluateJavaScript(
21 'window.__ScrollBounceAction_SupportedByBrowser()'):
22 raise page_action.PageActionNotSupported(
23 'Synthetic scroll bounce not supported for this browser')
25 # Fail if we can't send touch events (bouncing is really only
26 # interesting for touch)
27 if not GestureAction.IsGestureSourceTypeSupported(tab, 'touch'):
28 raise page_action.PageActionNotSupported(
29 'Touch scroll not supported for this browser')
31 if (GestureAction.GetGestureSourceTypeFromOptions(tab) ==
32 'chrome.gpuBenchmarking.MOUSE_INPUT'):
33 raise page_action.PageActionNotSupported(
34 'ScrollBounce page action does not support mouse input')
36 done_callback = 'function() { window.__scrollBounceActionDone = true; }'
37 tab.ExecuteJavaScript("""
38 window.__scrollBounceActionDone = false;
39 window.__scrollBounceAction = new __ScrollBounceAction(%s);"""
42 def RunGesture(self, page, tab):
43 left_start_percentage = 0.5
44 top_start_percentage = 0.5
46 # Should be big enough to do more than just hide the URL bar.
48 # This needs to be < height / repeat_count so we don't walk off the screen.
49 # We also probably don't want to spend more than a couple frames in
50 # overscroll since it may mask any synthetic delays.
52 # It's the transitions we really want to stress, make this big.
54 # 7 pixels per frame should be plenty of frames.
56 if hasattr(self, 'left_start_percentage'):
57 left_start_percentage = self.left_start_percentage
58 if hasattr(self, 'top_start_percentage'):
59 top_start_percentage = self.top_start_percentage
60 if hasattr(self, 'direction'):
61 direction = self.direction
62 if direction not in ['down', 'up', 'left', 'right']:
63 raise page_action.PageActionNotSupported(
64 'Invalid scroll bounce direction: %s' % direction)
65 if hasattr(self, 'distance'):
66 distance = self.distance
67 if hasattr(self, 'overscroll'):
68 overscroll = self.overscroll
69 if hasattr(self, 'repeat_count'):
70 repeat_count = self.repeat_count
71 if hasattr(self, 'speed'):
73 if hasattr(self, 'element_function'):
74 tab.ExecuteJavaScript("""
75 (%s)(function(element) { window.__scrollBounceAction.start(
77 left_start_percentage: %s,
78 top_start_percentage: %s,
84 });""" % (self.element_function,
85 left_start_percentage,
93 tab.ExecuteJavaScript("""
94 window.__scrollBounceAction.start(
95 { element: document.body,
96 left_start_percentage: %s,
97 top_start_percentage: %s,
103 % (left_start_percentage,
104 top_start_percentage,
111 tab.WaitForJavaScriptExpression('window.__scrollBounceActionDone', 60)
113 def CanBeBound(self):
116 def BindMeasurementJavaScript(self, tab, start_js, stop_js):
117 # Make the scroll bounce action start and stop measurement automatically.
118 tab.ExecuteJavaScript("""
119 window.__scrollBounceAction.beginMeasuringHook = function() { %s };
120 window.__scrollBounceAction.endMeasuringHook = function() { %s };
121 """ % (start_js, stop_js))