1# Copyright 2015 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
5from telemetry import decorators
6
7from telemetry.internal.actions import page_action
8from telemetry.internal.actions import repeatable_scroll
9from telemetry.internal.actions import utils
10from telemetry.internal.browser import browser_info as browser_info_module
11from telemetry.testing import tab_test_case
12
13
14class RepeatableScrollActionTest(tab_test_case.TabTestCase):
15
16  def setUp(self):
17    tab_test_case.TabTestCase.setUp(self)
18    self.Navigate('blank.html')
19    utils.InjectJavaScript(self._tab, 'gesture_common.js')
20
21    # Make page taller than window so it's scrollable.
22    self._tab.ExecuteJavaScript('document.body.style.height ='
23        '(3 * __GestureCommon_GetWindowHeight() + 1) + "px";')
24
25    self.assertEquals(
26        self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0)
27
28    self._browser_info = browser_info_module.BrowserInfo(self._tab.browser)
29    self._window_height = int(self._tab.EvaluateJavaScript(
30        '__GestureCommon_GetWindowHeight()'))
31
32  # https://github.com/catapult-project/catapult/issues/3099
33  @decorators.Disabled('android')
34  def testRepeatableScrollActionNoRepeats(self):
35    if not self._browser_info.HasRepeatableSynthesizeScrollGesture():
36      return
37
38    expected_scroll = (self._window_height / 2) - 1
39
40    i = repeatable_scroll.RepeatableScrollAction(y_scroll_distance_ratio=0.5)
41    i.WillRunAction(self._tab)
42
43    i.RunAction(self._tab)
44
45    scroll_position = self._tab.EvaluateJavaScript(
46        'document.scrollingElement.scrollTop')
47    # We can only expect the final scroll position to be approximatly equal.
48    self.assertTrue(abs(scroll_position - expected_scroll) < 20,
49                    msg='scroll_position=%d;expected %d' % (scroll_position,
50                                                            expected_scroll))
51
52  # https://github.com/catapult-project/catapult/issues/3099
53  @decorators.Disabled('android')
54  def testRepeatableScrollActionTwoRepeats(self):
55    if not self._browser_info.HasRepeatableSynthesizeScrollGesture():
56      return
57
58    expected_scroll = ((self._window_height / 2) - 1) * 3
59
60    i = repeatable_scroll.RepeatableScrollAction(y_scroll_distance_ratio=0.5,
61                                                 repeat_count=2,
62                                                 repeat_delay_ms=1)
63    i.WillRunAction(self._tab)
64
65    i.RunAction(self._tab)
66
67    scroll_position = self._tab.EvaluateJavaScript(
68        'document.scrollingElement.scrollTop')
69    # We can only expect the final scroll position to be approximatly equal.
70    self.assertTrue(abs(scroll_position - expected_scroll) < 20,
71                    msg='scroll_position=%d;expected %d' % (scroll_position,
72                                                            expected_scroll))
73
74  # Regression test for crbug.com/627166
75  # TODO(ulan): enable for Android after catapult:#2475 is fixed.
76  @decorators.Disabled('all')
77  def testRepeatableScrollActionNoRepeatsZoomed(self):
78    if (not self._browser_info.HasRepeatableSynthesizeScrollGesture() or
79        not page_action.IsGestureSourceTypeSupported(self._tab, 'touch')):
80      return
81
82    self._tab.action_runner.PinchPage(scale_factor=0.1)
83
84    inner_height = self._tab.EvaluateJavaScript('window.innerHeight')
85    outer_height = self._tab.EvaluateJavaScript('window.outerHeight')
86
87    self.assertGreater(inner_height, outer_height)
88
89    i = repeatable_scroll.RepeatableScrollAction(y_scroll_distance_ratio=0.5)
90    i.WillRunAction(self._tab)
91    i.RunAction(self._tab)
92    # If scroll coordinates are computed incorrectly Chrome will crash with
93    # [FATAL:synthetic_gesture_target_base.cc(62)] Check failed:
94    # web_touch.touches[i].state != WebTouchPoint::StatePressed ||
95    # PointIsWithinContents(web_touch.touches[i].position.x,
96    # web_touch.touches[i].position.y). Touch coordinates are not within content
97    # bounds on TouchStart.
98