1# Copyright 2015 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 logging
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.common_lib.cros import chrome
9from autotest_lib.client.cros import touch_playback_test_base
10
11
12class touch_TouchscreenScroll(
13        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._set_default_scroll_position(is_vertical)
36        self._playback(filepath, touch_type='touchscreen')
37        self._wait_for_scroll_position_to_settle(is_vertical)
38        delta = self._get_scroll_position(is_vertical) - self._DEFAULT_SCROLL
39        logging.info('Scroll delta was %d', delta)
40
41        # Check if movement occured in correct direction.
42        if ((is_down_or_right and delta <= 0) or
43            (not is_down_or_right and delta >= 0)):
44            raise error.TestFail('Page scroll was in wrong direction! '
45                                 'Delta=%d' % delta)
46
47
48    def _is_testable(self):
49        """Return True if test can run on this device, else False.
50
51        @raises: TestError if host has no touchscreen.
52
53        """
54        # Raise error if no touchscreen detected.
55        if not self._has_touchscreen:
56            raise error.TestError('No touchscreen found on this device!')
57
58        # Check if playback files are available on DUT to run test.
59        self._filepaths = self._find_test_files_from_directions(
60                'touchscreen', self._FILENAME_FMT_STR, self._DIRECTIONS)
61        if not self._filepaths:
62            logging.info('Missing gesture files, Aborting test.')
63            return False
64
65        return True
66
67
68    def run_once(self):
69        """Entry point of this test."""
70        if not self._is_testable():
71            return
72
73        # Log in and start test.
74        with chrome.Chrome(autotest_ext=True) as cr:
75            self._open_test_page(cr)
76            for direction in self._DIRECTIONS:
77                self._check_scroll_direction(self._filepaths[direction],
78                                             self._REVERSES[direction])
79