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 itertools
6import logging
7import time
8
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.common_lib.cros import chrome
11from autotest_lib.client.cros import touch_playback_test_base
12
13
14class touch_TapSettings(touch_playback_test_base.touch_playback_test_base):
15    """Toggles tap-to-click and tap dragging settings to ensure correctness."""
16    version = 1
17
18    _TEST_TIMEOUT = 1  # Number of seconds the test will wait for a click.
19    _MOUSE_DESCRIPTION = 'apple_mouse.prop'
20    _CLICK_NAME = 'tap-click'
21    _DRAG_NAME = 'tap-drag-right'
22
23
24    def _check_for_click(self, expected):
25        """Playback and check whether tap-to-click occurred.  Fail if needed.
26
27        @param expected: True if clicking should happen, else False.
28        @raises: TestFail if actual value does not match expected.
29
30        """
31        expected_count = 1 if expected else 0
32        self._reload_page()
33        self._playback(self._filepaths[self._CLICK_NAME])
34        time.sleep(self._TEST_TIMEOUT)
35        actual_count = int(self._tab.EvaluateJavaScript('clickCount'))
36        if actual_count is not expected_count:
37            raise error.TestFail('Expected clicks=%s, actual=%s.'
38                                 % (expected_count, actual_count))
39
40
41    def _check_for_drag(self, expected):
42        """Playback and check whether tap dragging occurred.  Fail if needed.
43
44        @param expected: True if dragging should happen, else False.
45        @raises: TestFail if actual value does not match expected.
46
47        """
48        self._reload_page()
49        self._wait_for_page_ready()
50        self._blocking_playback(self._filepaths[self._DRAG_NAME])
51        actual = self._tab.EvaluateJavaScript('movementOccurred')
52        if actual is not expected:
53            raise error.TestFail('Tap dragging movement was %s; expected %s.'
54                                 % (actual, expected))
55
56
57    def _is_testable(self):
58        """Return True if test can run on this device, else False.
59
60        @raises: TestError if host has no touchpad when it should.
61
62        """
63        # Raise error if no touchpad detected.
64        if not self._has_touchpad:
65            raise error.TestError('No touchpad found on this device!')
66
67        # Check if playback files are available on DUT to run test.
68        self._filepaths = self._find_test_files(
69                'touchpad', [self._CLICK_NAME, self._DRAG_NAME])
70        if not self._filepaths:
71            logging.info('Missing gesture files, Aborting test.')
72            return False
73
74        return True
75
76
77    def run_once(self):
78        """Entry point of this test."""
79        if not self._is_testable():
80            return
81
82        # Log in and start test.
83        with chrome.Chrome(autotest_ext=True) as cr:
84            # Setup.
85            self._set_autotest_ext(cr.autotest_ext)
86            self._open_test_page(cr)
87            self._emulate_mouse()
88            self._center_cursor()
89
90            # Check default setting values.
91            logging.info('Checking for default setting values.')
92            self._check_for_click(True)
93            self._check_for_drag(False)
94
95            # Toggle settings in all combinations and check.
96            options = [True, False]
97            option_pairs = itertools.product(options, options)
98            for (click_value, drag_value) in option_pairs:
99                self._center_cursor()
100                self._set_tap_to_click(click_value)
101                self._set_tap_dragging(drag_value)
102                self._check_for_click(click_value)
103                self._check_for_drag(click_value and drag_value)
104