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