| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 import os | 4 import os |
| 5 | 5 |
| 6 from telemetry.core import util | 6 from telemetry.core import util |
| 7 from telemetry.page import page_action | 7 from telemetry.page import page_action |
| 8 | 8 |
| 9 class ScrollingAction(page_action.PageAction): | 9 class ScrollingAction(page_action.PageAction): |
| 10 def __init__(self, attributes=None): | 10 def __init__(self, attributes=None): |
| 11 super(ScrollingAction, self).__init__(attributes) | 11 super(ScrollingAction, self).__init__(attributes) |
| 12 | 12 |
| 13 def WillRunAction(self, page, tab): | 13 def WillRunAction(self, page, tab): |
| 14 with open( | 14 with open( |
| 15 os.path.join(os.path.dirname(__file__), | 15 os.path.join(os.path.dirname(__file__), |
| 16 'scrolling_action.js')) as f: | 16 'scrolling_action.js')) as f: |
| 17 js = f.read() | 17 js = f.read() |
| 18 tab.ExecuteJavaScript(js) | 18 tab.ExecuteJavaScript(js) |
| 19 | 19 |
| 20 tab.ExecuteJavaScript(""" | 20 tab.ExecuteJavaScript(""" |
| 21 window.__scrollingActionDone = false; | 21 window.__scrollingActionDone = false; |
| 22 window.__scrollingAction = new __ScrollingAction(function() { | 22 window.__scrollingAction = new __ScrollingAction(function() { |
| 23 window.__scrollingActionDone = true; | 23 window.__scrollingActionDone = true; |
| 24 }); | 24 }); |
| 25 """) | 25 """) |
| 26 | 26 |
| 27 def RunAction(self, page, tab, previous_action): | 27 def RunAction(self, page, tab, previous_action): |
| 28 try: | 28 # scrollable_element_function is a function that passes the scrollable |
| 29 if tab.browser.platform.IsRawDisplayFrameRateSupported(): | 29 # element on the page to a callback. For example: |
| 30 tab.browser.platform.StartRawDisplayFrameRateMeasurement('') | 30 # function (callback) { |
| 31 # scrollable_element_function is a function that passes the scrollable | 31 # callback(document.getElementById('foo')); |
| 32 # element on the page to a callback. For example: | 32 # } |
| 33 # function (callback) { | 33 if hasattr(self, 'scrollable_element_function'): |
| 34 # callback(document.getElementById('foo')); | 34 tab.ExecuteJavaScript(""" |
| 35 # } | 35 (%s)(function(element) { |
| 36 if hasattr(self, 'scrollable_element_function'): | 36 window.__scrollingAction.start(element); |
| 37 tab.ExecuteJavaScript(""" | 37 });""" % (self.scrollable_element_function)) |
| 38 (%s)(function(element) { | 38 else: |
| 39 window.__scrollingAction.start(element); | 39 tab.ExecuteJavaScript( |
| 40 });""" % (self.scrollable_element_function)) | 40 'window.__scrollingAction.start(document.body);') |
| 41 else: | |
| 42 tab.ExecuteJavaScript( | |
| 43 'window.__scrollingAction.start(document.body);') | |
| 44 | 41 |
| 45 # Poll for scroll benchmark completion. | 42 # Poll for scroll benchmark completion. |
| 46 util.WaitFor(lambda: tab.EvaluateJavaScript( | 43 util.WaitFor(lambda: tab.EvaluateJavaScript( |
| 47 'window.__scrollingActionDone'), 60) | 44 'window.__scrollingActionDone'), 60) |
| 48 finally: | |
| 49 if tab.browser.platform.IsRawDisplayFrameRateSupported(): | |
| 50 tab.browser.platform.StopRawDisplayFrameRateMeasurement() | |
| 51 | 45 |
| 52 def CanBeBound(self): | 46 def CanBeBound(self): |
| 53 return True | 47 return True |
| 54 | 48 |
| 55 def BindMeasurementJavaScript(self, tab, start_js, stop_js): | 49 def BindMeasurementJavaScript(self, tab, start_js, stop_js): |
| 56 # Make the scrolling action start and stop measurement automatically. | 50 # Make the scrolling action start and stop measurement automatically. |
| 57 tab.ExecuteJavaScript(""" | 51 tab.ExecuteJavaScript(""" |
| 58 window.__scrollingAction.beginMeasuringHook = function() { %s }; | 52 window.__scrollingAction.beginMeasuringHook = function() { %s }; |
| 59 window.__scrollingAction.endMeasuringHook = function() { %s }; | 53 window.__scrollingAction.endMeasuringHook = function() { %s }; |
| 60 """ % (start_js, stop_js)) | 54 """ % (start_js, stop_js)) |
| OLD | NEW |