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