1# Lint as: python2, python3
2# Copyright 2015 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_TouchscreenScroll(
14        touch_playback_test_base.touch_playback_test_base):
15    """Plays back scrolls and checks for correct page movement."""
16    version = 1
17
18    _DIRECTIONS = ['down', 'up', 'right', 'left']
19    _REVERSES = {'down': 'up', 'up': 'down', 'right': 'left', 'left': 'right'}
20    _FILENAME_FMT_STR = 'scroll-%s'
21
22
23    def _check_scroll_direction(self, filepath, expected):
24        """Playback and raise error if scrolling does not match down value.
25
26        @param filepath: Gesture file's complete path for playback.
27        @param expected: String, expected direction in which test page scroll
28                         should move for the gesture file being played.
29
30        @raises TestFail if actual scrolling did not match expected.
31
32        """
33        is_vertical = expected == 'up' or expected == 'down'
34        is_down_or_right = expected == 'down' or expected == 'right'
35
36        self._events.set_default_scroll_position(is_vertical)
37        start_scroll = self._events.get_scroll_position(is_vertical)
38        self._events.clear_previous_events()
39
40        self._playback(filepath, touch_type='touchscreen')
41
42        self._events.wait_for_events_to_complete()
43        end_scroll = self._events.get_scroll_position(is_vertical)
44        delta = end_scroll - start_scroll
45        logging.info('Scroll delta was %d (%d to %d)',
46                     delta, start_scroll, end_scroll)
47
48        # Check if movement occured in correct direction.
49        if ((is_down_or_right and delta <= 0) or
50            (not is_down_or_right and delta >= 0)):
51            self._events.log_events()
52            raise error.TestFail('Page scroll was in wrong direction! '
53                                 'Delta=%d' % delta)
54
55
56    def _is_testable(self):
57        """Return True if test can run on this device, else False.
58
59        @raises: TestError if host has no touchscreen.
60
61        """
62        # Raise error if no touchscreen detected.
63        if not self._has_touchscreen:
64            raise error.TestError('No touchscreen found on this device!')
65
66        # Check if playback files are available on DUT to run test.
67        self._filepaths = self._find_test_files_from_directions(
68                'touchscreen', self._FILENAME_FMT_STR, self._DIRECTIONS)
69        if not self._filepaths:
70            logging.info('Missing gesture files, Aborting test.')
71            return False
72
73        return True
74
75
76    def run_once(self):
77        """Entry point of this test."""
78        if not self._is_testable():
79            raise error.TestNAError('Missing input data for this board name.')
80
81        # Log in and start test.
82        with chrome.Chrome(autotest_ext=True,
83                           init_network_controller=True) as cr:
84            self._open_events_page(cr)
85            self._events.expand_page()
86            self._events.set_prevent_defaults(False)
87            for direction in self._DIRECTIONS:
88                self._check_scroll_direction(self._filepaths[direction],
89                                             self._REVERSES[direction])
90