Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / actions / scroll_bounce.py
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 os
5
6 from telemetry.page.actions.gesture_action import GestureAction
7 from telemetry.page.actions import page_action
8
9 class ScrollBounceAction(GestureAction):
10   def __init__(self, attributes=None):
11     super(ScrollBounceAction, self).__init__(attributes)
12
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:
16         js = f.read()
17         tab.ExecuteJavaScript(js)
18
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')
24
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')
30
31     if (GestureAction.GetGestureSourceTypeFromOptions(tab) ==
32         'chrome.gpuBenchmarking.MOUSE_INPUT'):
33       raise page_action.PageActionNotSupported(
34           'ScrollBounce page action does not support mouse input')
35
36     done_callback = 'function() { window.__scrollBounceActionDone = true; }'
37     tab.ExecuteJavaScript("""
38         window.__scrollBounceActionDone = false;
39         window.__scrollBounceAction = new __ScrollBounceAction(%s);"""
40         % (done_callback))
41
42   def RunGesture(self, page, tab):
43     left_start_percentage = 0.5
44     top_start_percentage = 0.5
45     direction = 'down'
46     # Should be big enough to do more than just hide the URL bar.
47     distance = 100
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.
51     overscroll = 10
52     # It's the transitions we really want to stress, make this big.
53     repeat_count = 10
54     # 7 pixels per frame should be plenty of frames.
55     speed = 400
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'):
72       speed = self.speed
73     if hasattr(self, 'element_function'):
74       tab.ExecuteJavaScript("""
75           (%s)(function(element) { window.__scrollBounceAction.start(
76              { element: element,
77                left_start_percentage: %s,
78                top_start_percentage: %s,
79                direction: '%s',
80                distance: %s,
81                overscroll: %s,
82                repeat_count: %s,
83                speed: %s })
84              });""" % (self.element_function,
85                        left_start_percentage,
86                        top_start_percentage,
87                        direction,
88                        distance,
89                        overscroll,
90                        repeat_count,
91                        speed))
92     else:
93       tab.ExecuteJavaScript("""
94           window.__scrollBounceAction.start(
95           { element: document.body,
96             left_start_percentage: %s,
97             top_start_percentage: %s,
98             direction: '%s',
99             distance: %s,
100             overscroll: %s,
101             repeat_count: %s,
102             speed: %s });"""
103         % (left_start_percentage,
104            top_start_percentage,
105            direction,
106            distance,
107            overscroll,
108            repeat_count,
109            speed))
110
111     tab.WaitForJavaScriptExpression('window.__scrollBounceActionDone', 60)
112
113   def CanBeBound(self):
114     return True
115
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))