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
6import os
7
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros import touch_playback_test_base
11
12
13class touch_WakeupSource(touch_playback_test_base.touch_playback_test_base):
14    """Check that touchpad/touchscreen are set/not set as wake sources."""
15    version = 1
16
17    # Devices whose touchpads should not be a wake source.
18    _NO_TOUCHPAD_WAKE = ['clapper', 'glimmer', 'veyron_minnie']
19
20    # Devices with Synaptics touchpads that do not report wake source,
21    # or reference platforms like Rambi which are broken but do not ship,
22    # or devices like Cyan which don't report this way: crosbug.com/p/46019.
23    _INVALID_TOUCHPADS = ['x86-alex', 'x86-alex_he', 'x86-zgb', 'x86-zgb_he',
24                          'x86-mario', 'stout', 'rambi', 'cyan']
25    _INVALID_TOUCHSCREENS = ['cyan', 'sumo']
26
27    def _find_wakeup_file(self, input_type):
28        """Return path to wakeup file or None.
29
30        If the file does not exist, check the parent bus for wakeup rules
31        as well, as is the setup for some devices.
32
33        @param input_type: e.g. 'touchpad' or 'mouse'. See parent class for
34                all options.
35
36        @raises: TestError if input_type lacks required information.
37
38        """
39        device_dir = self.player.devices[input_type].device_dir
40        if not device_dir:
41            raise error.TestError('No device directory for %s!' % input_type)
42
43        filename = os.path.join(device_dir, 'power', 'wakeup')
44        if not os.path.isfile(filename):
45            logging.info('%s not found for %s', filename, input_type)
46
47            # Look for wakeup file on parent bus instead.
48            event = self.player.devices[input_type].node.split('/')[-1]
49
50            parent = None
51            i2c_devices_dir = os.path.join('/', 'sys', 'bus', 'i2c', 'devices')
52            for device_dir in os.listdir(i2c_devices_dir):
53                event_search = os.path.join(i2c_devices_dir, device_dir, '*',
54                                            'input', 'input*', event)
55                match_count = utils.run('ls %s 2>/dev/null | wc -l' % (
56                        event_search)).stdout.strip()
57                if int(match_count) > 0:
58                    parent = os.path.join(i2c_devices_dir, device_dir)
59                    break
60            if parent is None:
61                logging.info('Could not find parent bus for %s.', input_type)
62                return None
63
64            logging.info('Parent bus of %s is %s.', input_type, parent)
65            filename = os.path.join(parent, 'power', 'wakeup')
66            if not os.path.isfile(filename):
67                logging.info('%s not found either.', filename)
68                return None
69
70        return filename
71
72    def _is_wake_source(self, input_type):
73        """Return True if the given device is a wake source, else False.
74
75        If the file does not exist, return False.
76
77        @param input_type: e.g. 'touchpad' or 'mouse'. See parent class for
78                all options.
79
80        @raises: TestError if test cannot interpret the file contents.
81
82        """
83        filename = self._find_wakeup_file(input_type)
84        if filename is None:
85            return False
86
87        result = utils.run('cat %s' % filename).stdout.strip()
88        if result == 'enabled':
89            logging.info('Found that %s is a wake source.', input_type)
90            return True
91        elif result == 'disabled':
92            logging.info('Found that %s is not a wake source.', input_type)
93            return False
94        raise error.TestError('Wakeup file for %s said "%s".' %
95                              (input_type, result))
96
97    def run_once(self):
98        """Entry point of this test."""
99        # Check that touchpad is a wake source for all but the excepted boards.
100        if (self._has_touchpad and
101            self._platform not in self._INVALID_TOUCHPADS):
102            if self._platform in self._NO_TOUCHPAD_WAKE:
103                if self._is_wake_source('touchpad'):
104                    raise error.TestFail('Touchpad is a wake source!')
105            else:
106                if not self._is_wake_source('touchpad'):
107                    raise error.TestFail('Touchpad is not a wake source!')
108
109        # Check that touchscreen is not a wake source (if present).
110        # Devices without a touchpad should have touchscreen as wake source.
111        if (self._has_touchscreen and
112            self._platform not in self._INVALID_TOUCHSCREENS):
113            touchscreen_wake = self._is_wake_source('touchscreen')
114            if self._has_touchpad and touchscreen_wake:
115                raise error.TestFail('Touchscreen is a wake source!')
116            if not self._has_touchpad and not touchscreen_wake:
117                raise error.TestFail('Touchscreen is not a wake source!')
118
119