OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 import os | |
5 | |
6 from telemetry.page import Page | |
7 from telemetry import scrolling_action | |
8 from telemetry import tab_test_case | |
9 | |
10 class ScrollingActionTest(tab_test_case.TabTestCase): | |
11 def CreateAndNavigateToPageFromUnittestDataDir( | |
12 self, filename, page_attributes): | |
13 unittest_data_dir = os.path.join(os.path.dirname(__file__), | |
14 '..', 'unittest_data') | |
15 self._browser.SetHTTPServerDirectory(unittest_data_dir) | |
16 page = Page( | |
17 self._browser.http_server.UrlOf(filename), | |
18 None, # In this test, we don't need a page set. | |
19 attributes=page_attributes) | |
20 | |
21 self._tab.Navigate(page.url) | |
22 self._tab.WaitForDocumentReadyStateToBeComplete() | |
23 | |
24 return page | |
25 | |
26 def testScrollingAction(self): | |
27 page = self.CreateAndNavigateToPageFromUnittestDataDir( | |
28 "blank.html", | |
29 page_attributes={"smoothness": { | |
30 "action": "scrolling_action" | |
31 }}) | |
32 # Make page bigger than window so it's scrollable. | |
33 self._tab.ExecuteJavaScript("""document.body.style.height = | |
34 (2 * window.innerHeight + 1) + 'px';""") | |
35 | |
36 self.assertEquals( | |
37 self._tab.EvaluateJavaScript('document.body.scrollTop'), 0) | |
38 | |
39 i = scrolling_action.ScrollingAction() | |
40 i.WillRunAction(page, self._tab) | |
41 | |
42 self._tab.ExecuteJavaScript(""" | |
43 window.__scrollingAction.beginMeasuringHook = function() { | |
44 window.__didBeginMeasuring = true; | |
45 }; | |
46 window.__scrollingAction.endMeasuringHook = function() { | |
47 window.__didEndMeasuring = true; | |
48 };""") | |
49 i.RunAction(page, self._tab, None) | |
50 | |
51 self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring')) | |
52 self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring')) | |
53 | |
54 # Allow for roundoff error in scaled viewport. | |
55 scroll_position = self._tab.EvaluateJavaScript( | |
56 'document.body.scrollTop + window.innerHeight') | |
57 scroll_height = self._tab.EvaluateJavaScript('document.body.scrollHeight') | |
58 difference = scroll_position - scroll_height | |
59 self.assertTrue(abs(difference) <= 1) | |
60 | |
61 def testBoundingClientRect(self): | |
62 self.CreateAndNavigateToPageFromUnittestDataDir('blank.html', {}) | |
63 with open( | |
64 os.path.join(os.path.dirname(__file__), | |
65 'scrolling_action.js')) as f: | |
66 js = f.read() | |
67 self._tab.ExecuteJavaScript(js) | |
68 | |
69 # Verify that the rect returned by getBoundingVisibleRect() in | |
70 # scroll.js is completely contained within the viewport. Scroll | |
71 # events dispatched by the benchmarks use the center of this rect | |
72 # as their location, and this location needs to be within the | |
73 # viewport bounds to correctly decide between main-thread and | |
74 # impl-thread scrolling. If the scrollable area were not clipped | |
75 # to the viewport bounds, then the instance used here (the scrollable | |
76 # area being more than twice as tall as the viewport) would | |
77 # result in a scroll location outside of the viewport bounds. | |
78 self._tab.ExecuteJavaScript("""document.body.style.height = | |
79 (2 * window.innerHeight + 1) + 'px';""") | |
80 | |
81 rect_bottom = int(self._tab.EvaluateJavaScript(""" | |
82 __ScrollingAction_GetBoundingVisibleRect(document.body).top + | |
83 __ScrollingAction_GetBoundingVisibleRect(document.body).height""")) | |
84 rect_right = int(self._tab.EvaluateJavaScript(""" | |
85 __ScrollingAction_GetBoundingVisibleRect(document.body).left + | |
86 __ScrollingAction_GetBoundingVisibleRect(document.body).width""")) | |
87 viewport_width = int(self._tab.EvaluateJavaScript('window.innerWidth')) | |
88 viewport_height = int(self._tab.EvaluateJavaScript('window.innerHeight')) | |
89 | |
90 self.assertTrue(rect_bottom <= viewport_height) | |
91 self.assertTrue(rect_right <= viewport_width) | |
OLD | NEW |