1# Lint as: python2, python3 2# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import os 7import logging 8 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.common_lib.cros import chrome 11from autotest_lib.client.cros import touch_playback_test_base 12 13 14class touch_MouseScroll(touch_playback_test_base.touch_playback_test_base): 15 """Plays back mouse scrolls and checks for correct page movement.""" 16 version = 1 17 18 _MOUSE_DESCRIPTION = 'amazon_mouse.prop' 19 _APPLE_MOUSE_DES = 'apple_mouse.prop' 20 _EXPECTED_RANGE_1 = [9, 11] # Expected value of one scroll wheel turn. 21 _EXPECTED_DIRECTION = {'down': 1, 'up': -1, 'right': 1, 'left': -1} 22 _TOLLERANCE = 4 # Fast scroll should go at least X times slow scroll. 23 24 25 def _get_scroll_delta(self, name, expected_direction, scroll_vertical=True): 26 """Playback the given test and return the amount the page moved. 27 28 @param name: name of test filename. 29 @param expected_direction: an integer that is + for down and - for up. 30 @param scroll_vertical: True for vertical scroll, 31 False for horizontal scroll. 32 33 @raise: TestFail if scrolling did not occur in expected direction. 34 35 """ 36 self._events.clear_previous_events() 37 self._events.set_default_scroll_position(scroll_vertical) 38 start_scroll = self._events.get_scroll_position(scroll_vertical) 39 self._events.clear_previous_events() 40 41 self._playback(self._gest_file_path[name], touch_type='mouse') 42 43 self._events.wait_for_events_to_complete() 44 end_scroll = self._events.get_scroll_position(scroll_vertical) 45 delta = end_scroll - start_scroll 46 logging.info('Test %s: saw scroll delta of %d. Expected direction %d.', 47 name, delta, expected_direction) 48 49 if delta * expected_direction < 0: 50 self._events.log_events() 51 raise error.TestFail('Scroll was in wrong direction! Delta ' 52 'for %s was %d.' % (name, delta)) 53 return delta 54 55 56 def _verify_single_tick(self, direction, scroll_vertical=True): 57 """Verify that using the scroll wheel goes the right distance. 58 59 Expects a file (playback gesture file) named direction + '_1'. 60 61 @param direction: string indicating direction up, down, right and left. 62 @param scroll_vertical: scroll_vertical is True for vertical scroll 63 else False 64 65 """ 66 name = direction + '_1' 67 expected_direction = self._EXPECTED_DIRECTION[direction] 68 expected_range = sorted([x * expected_direction for x in 69 self._EXPECTED_RANGE_1]) 70 delta = self._get_scroll_delta(name, expected_direction, 71 scroll_vertical) 72 73 if not expected_range[0] <= delta <= expected_range[1]: 74 self._events.log_events() 75 raise error.TestFail( 76 'One tick scroll was wrong size: ' 77 'direction=%s, actual=%d, expected=[%d, %d]' % 78 (direction, delta, expected_range[0], expected_range[1])) 79 80 81 def _verify_fast_vs_slow(self, direction, scroll_vertical=True): 82 """Verify that fast scrolling goes farther than slow scrolling. 83 84 Expects files (playback gesture file) named direction + '_slow' 85 and direction + '_fast'. 86 87 @param direction: string indicating direction up, down, right and left. 88 @param scroll_vertical: True for vertical scroll, 89 False for horizontal scroll. 90 91 """ 92 slow = direction + '_slow' 93 fast = direction + '_fast' 94 expected = self._EXPECTED_DIRECTION[direction] 95 slow_delta = self._get_scroll_delta(slow, expected, scroll_vertical) 96 fast_delta = self._get_scroll_delta(fast, expected, scroll_vertical) 97 98 if abs(fast_delta) < self._TOLLERANCE * abs(slow_delta): 99 self._events.log_events() 100 raise error.TestFail('Fast scroll should be much farther than ' 101 'slow! (%s). %d vs. %d.' % 102 (direction, slow_delta, fast_delta)) 103 104 105 def run_once(self): 106 """Entry point of this test.""" 107 108 # Link path for files to playback on DUT. 109 self._gest_file_path = {} 110 gestures_dir = os.path.join(self.bindir, 'gestures') 111 for filename in os.listdir(gestures_dir): 112 self._gest_file_path[filename] = os.path.join(gestures_dir, 113 filename) 114 115 with chrome.Chrome(init_network_controller=True) as cr: 116 # Open test page. 117 self._open_events_page(cr) 118 self._events.expand_page() 119 self._events.set_prevent_defaults(False) 120 121 # Emulate mouse with vertical scroll feature. 122 mouse_file = os.path.join(self.bindir, self._MOUSE_DESCRIPTION) 123 self._emulate_mouse(property_file=mouse_file) 124 if not self._has_mouse: 125 raise error.TestError('No USB mouse found on this device.') 126 # In test page, position cursor to center. 127 self._blocking_playback(self._gest_file_path['center_cursor'], 128 touch_type='mouse') 129 self._events.wait_for_events_to_complete() 130 131 # Test vertical scrolling. 132 for direction in ['down', 'up']: 133 self._verify_single_tick(direction) 134 self._verify_fast_vs_slow(direction) 135 136 # Emulate mouse with horizontal scroll feature. 137 apple_mouse_file = os.path.join(self.bindir, self._APPLE_MOUSE_DES) 138 self._emulate_mouse(property_file=apple_mouse_file) 139 if not self._has_mouse: 140 raise error.TestError('No USB mouse found on this device.') 141 142 # Test horizontal scrolling. 143 for direction in ['right', 'left']: 144 self._verify_single_tick(direction, scroll_vertical=False) 145 self._verify_fast_vs_slow(direction, scroll_vertical=False) 146