xref: /aosp_15_r20/external/autotest/client/site_tests/touch_ScrollDirection/touch_ScrollDirection.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
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 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_ScrollDirection(touch_playback_test_base.touch_playback_test_base):
14    """Plays back scrolls and checks for correct page movement."""
15    version = 1
16
17    _DIRECTIONS = ['down', 'up', 'right', 'left']
18    _REVERSES = {'down': 'up', 'up': 'down', 'right': 'left', 'left': 'right'}
19    _FILENAME_FMT_STR = 'scroll-%s'
20
21
22    def _check_scroll_direction(self, filepath, expected):
23        """Playback and raise error if scrolling does not match down value.
24
25        @param filepath: Gesture file's complete path for playback.
26        @param expected: String, expected direction in which test page scroll
27                         should move for the gesture file being played.
28
29        @raises TestFail if actual scrolling did not match expected.
30
31        """
32        is_vertical = expected == 'up' or expected == 'down'
33        is_down_or_right = expected == 'down' or expected == 'right'
34
35        self._events.clear_previous_events()
36        self._playback(filepath)
37        self._events.wait_for_events_to_complete()
38        delta = self._events.get_scroll_delta(is_vertical)
39        logging.info('Scroll delta was %d', delta)
40
41        # Check if scrolling went in correct direction.
42        if ((is_down_or_right and delta <= 0) or
43            (not is_down_or_right and delta >= 0)):
44            self._events.log_events()
45            raise error.TestFail('Page scroll was in wrong direction! '
46                                 'Delta=%d, Australian=%s, Touchscreen=%s'
47                                  % (delta, self._australian_state,
48                                     self._has_touchscreen))
49
50
51    def _verify_scrolling(self):
52        """Check scrolling direction for down then up."""
53        if not self._australian_state:
54            for direction in self._DIRECTIONS:
55                self._check_scroll_direction(self._filepaths[direction],
56                                             direction)
57        else:
58            for direction in self._DIRECTIONS:
59                self._check_scroll_direction(self._filepaths[direction],
60                                             self._REVERSES[direction])
61
62
63    def _is_testable(self):
64        """Return True if test can run on this device, else False.
65
66        @raises: TestError if host has no touchpad when it should.
67
68        """
69        # Raise error if no touchpad detected.
70        if not self._has_touchpad:
71            raise error.TestError('No touchpad found on this device!')
72
73        # Check if playback files are available on DUT to run test.
74        self._filepaths = self._find_test_files_from_directions(
75                'touchpad', self._FILENAME_FMT_STR, self._DIRECTIONS)
76        if not self._filepaths:
77            logging.info('Missing gesture files, Aborting test.')
78            return False
79
80        return True
81
82
83    def run_once(self):
84        """Entry point of this test."""
85        if not self._is_testable():
86            raise error.TestNAError('Missing input data for this board name.')
87
88        # Log in and start test.
89        with chrome.Chrome(autotest_ext=True,
90                           init_network_controller=True) as cr:
91            # Setup.
92            self._set_autotest_ext(cr.autotest_ext)
93            self._open_events_page(cr)
94            self._events.expand_page()
95            self._events.set_prevent_defaults(False)
96
97            self._emulate_mouse()
98            self._center_cursor()
99
100            # Check default scroll - Australian only for samus and link.
101            # See crbug.com/528083 for details.
102            self._australian_state = self._platform in ['link' , 'samus']
103            logging.info('Expecting Australian=%s', self._australian_state)
104            self._verify_scrolling()
105
106            # Toggle Australian scrolling and check again.
107            self._australian_state = not self._australian_state
108            self._set_australian_scrolling(value=self._australian_state)
109            self._verify_scrolling()
110