• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import os
6import logging
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import chrome
10from autotest_lib.client.cros import touch_playback_test_base
11
12
13class 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_RANGE_1 = [9, 11] # 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._events.clear_previous_events()
36        self._events.set_default_scroll_position(scroll_vertical)
37        start_scroll = self._events.get_scroll_position(scroll_vertical)
38        self._events.clear_previous_events()
39
40        self._playback(self._gest_file_path[name], touch_type='mouse')
41
42        self._events.wait_for_events_to_complete()
43        end_scroll = self._events.get_scroll_position(scroll_vertical)
44        delta = end_scroll - start_scroll
45        logging.info('Test %s: saw scroll delta of %d.  Expected direction %d.',
46                     name, delta, expected_direction)
47
48        if delta * expected_direction < 0:
49            self._events.log_events()
50            raise error.TestFail('Scroll was in wrong direction!  Delta '
51                                 'for %s was %d.' % (name, delta))
52        return delta
53
54
55    def _verify_single_tick(self, direction, scroll_vertical=True):
56        """Verify that using the scroll wheel goes the right distance.
57
58        Expects a file (playback gesture file) named direction + '_1'.
59
60        @param direction: string indicating direction up, down, right and left.
61        @param scroll_vertical: scroll_vertical is True for vertical scroll
62                                else False
63
64        """
65        name = direction + '_1'
66        expected_direction = self._EXPECTED_DIRECTION[direction]
67        expected_range = sorted([x * expected_direction for x in
68            self._EXPECTED_RANGE_1])
69        delta = self._get_scroll_delta(name, expected_direction,
70                                       scroll_vertical)
71
72        if not expected_range[0] <= delta <= expected_range[1]:
73            self._events.log_events()
74            raise error.TestFail(
75                'One tick scroll was wrong size: '
76                'direction=%s, actual=%d, expected=[%d, %d]' %
77                 (direction, delta, expected_range[0], expected_range[1]))
78
79
80    def _verify_fast_vs_slow(self, direction, scroll_vertical=True):
81        """Verify that fast scrolling goes farther than slow scrolling.
82
83        Expects files (playback gesture file) named direction + '_slow'
84        and direction + '_fast'.
85
86        @param direction: string indicating direction up, down, right and left.
87        @param scroll_vertical: True for vertical scroll,
88                                False for horizontal scroll.
89
90        """
91        slow = direction + '_slow'
92        fast = direction + '_fast'
93        expected = self._EXPECTED_DIRECTION[direction]
94        slow_delta = self._get_scroll_delta(slow, expected, scroll_vertical)
95        fast_delta = self._get_scroll_delta(fast, expected, scroll_vertical)
96
97        if abs(fast_delta) < self._TOLLERANCE * abs(slow_delta):
98            self._events.log_events()
99            raise error.TestFail('Fast scroll should be much farther than '
100                                 'slow! (%s).  %d vs. %d.' %
101                                  (direction, slow_delta, fast_delta))
102
103
104    def run_once(self):
105        """Entry point of this test."""
106
107        # Link path for files to playback on DUT.
108        self._gest_file_path = {}
109        gestures_dir = os.path.join(self.bindir, 'gestures')
110        for filename in os.listdir(gestures_dir):
111            self._gest_file_path[filename] = os.path.join(gestures_dir,
112                                                          filename)
113
114        with chrome.Chrome(init_network_controller=True) as cr:
115            # Open test page.
116            self._open_events_page(cr)
117            self._events.expand_page()
118            self._events.set_prevent_defaults(False)
119
120            # Emulate mouse with vertical scroll feature.
121            mouse_file = os.path.join(self.bindir, self._MOUSE_DESCRIPTION)
122            self._emulate_mouse(property_file=mouse_file)
123            if not self._has_mouse:
124                raise error.TestError('No USB mouse found on this device.')
125            # In test page, position cursor to center.
126            self._blocking_playback(self._gest_file_path['center_cursor'],
127                                    touch_type='mouse')
128            self._events.wait_for_events_to_complete()
129
130            # Test vertical scrolling.
131            for direction in ['down', 'up']:
132                self._verify_single_tick(direction)
133                self._verify_fast_vs_slow(direction)
134
135            # Emulate mouse with horizontal scroll feature.
136            apple_mouse_file = os.path.join(self.bindir, self._APPLE_MOUSE_DES)
137            self._emulate_mouse(property_file=apple_mouse_file)
138            if not self._has_mouse:
139                raise error.TestError('No USB mouse found on this device.')
140
141            # Test horizontal scrolling.
142            for direction in ['right', 'left']:
143                self._verify_single_tick(direction, scroll_vertical=False)
144                self._verify_fast_vs_slow(direction, scroll_vertical=False)
145