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._events.set_default_scroll_position(is_vertical)
36        start_scroll = self._events.get_scroll_position(is_vertical)
37        self._events.clear_previous_events()
38
39        self._playback(filepath, touch_type='touchscreen')
40
41        self._events.wait_for_events_to_complete()
42        end_scroll = self._events.get_scroll_position(is_vertical)
43        delta = end_scroll - start_scroll
44        logging.info('Scroll delta was %d (%d to %d)',
45                     delta, start_scroll, end_scroll)
46
47        # Check if movement occured in correct direction.
48        if ((is_down_or_right and delta <= 0) or
49            (not is_down_or_right and delta >= 0)):
50            self._events.log_events()
51            raise error.TestFail('Page scroll was in wrong direction! '
52                                 'Delta=%d' % delta)
53
54
55    def _is_testable(self):
56        """Return True if test can run on this device, else False.
57
58        @raises: TestError if host has no touchscreen.
59
60        """
61        # Raise error if no touchscreen detected.
62        if not self._has_touchscreen:
63            raise error.TestError('No touchscreen found on this device!')
64
65        # Check if playback files are available on DUT to run test.
66        self._filepaths = self._find_test_files_from_directions(
67                'touchscreen', self._FILENAME_FMT_STR, self._DIRECTIONS)
68        if not self._filepaths:
69            logging.info('Missing gesture files, Aborting test.')
70            return False
71
72        return True
73
74
75    def run_once(self):
76        """Entry point of this test."""
77        if not self._is_testable():
78            return
79
80        # Log in and start test.
81        with chrome.Chrome(autotest_ext=True,
82                           init_network_controller=True) as cr:
83            self._open_events_page(cr)
84            self._events.expand_page()
85            self._events.set_prevent_defaults(False)
86            for direction in self._DIRECTIONS:
87                self._check_scroll_direction(self._filepaths[direction],
88                                             self._REVERSES[direction])
89