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._events.clear_previous_events()
35        self._playback(filepath)
36        self._events.wait_for_events_to_complete()
37        delta = self._events.get_scroll_delta(is_vertical)
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            self._events.log_events()
44            raise error.TestFail('Page scroll was in wrong direction! '
45                                 'Delta=%d, Australian=%s, Touchscreen=%s'
46                                  % (delta, self._australian_state,
47                                     self._has_touchscreen))
48
49
50    def _verify_scrolling(self):
51        """Check scrolling direction for down then up."""
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,
89                           init_network_controller=True) as cr:
90            # Setup.
91            self._set_autotest_ext(cr.autotest_ext)
92            self._open_events_page(cr)
93            self._events.expand_page()
94            self._events.set_prevent_defaults(False)
95
96            self._emulate_mouse()
97            self._center_cursor()
98
99            # Check default scroll - Australian only for samus and link.
100            # See crbug.com/528083 for details.
101            self._australian_state = self._platform in ['link' , 'samus']
102            logging.info('Expecting Australian=%s', self._australian_state)
103            self._verify_scrolling()
104
105            # Toggle Australian scrolling and check again.
106            self._australian_state = not self._australian_state
107            self._set_australian_scrolling(value=self._australian_state)
108            self._verify_scrolling()
109