• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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, time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.common_lib.cros import tpm_utils
9from autotest_lib.server import test
10from autotest_lib.server.cros.multimedia import remote_facade_factory
11
12
13_SHORT_TIMEOUT = 5
14_WAIT_DELAY = 15
15
16
17class enterprise_CFM_USBPeripheralHotplugStress(test.test):
18    """Uses servo to hotplug and unplug USB peripherals multiple times and
19    verify's that the hotrod app appropriately detects the peripherals using
20    app api's."""
21    version = 1
22
23
24    def _set_hub_power(self, on=True):
25        """Setting USB hub power status
26
27        @param on: To power on the servo usb hub or not.
28
29        """
30        reset = 'off'
31        if not on:
32            reset = 'on'
33        self.client.servo.set('dut_hub1_rst1', reset)
34        time.sleep(_WAIT_DELAY)
35
36
37    def _enroll_device_and_skip_oobe(self):
38        """Enroll device into CFM and skip CFM oobe."""
39        self.cfm_facade.enroll_device()
40        self.cfm_facade.restart_chrome_for_cfm()
41        self.cfm_facade.wait_for_telemetry_commands()
42        self.cfm_facade.wait_for_oobe_start_page()
43
44        if not self.cfm_facade.is_oobe_start_page():
45            raise error.TestFail('CFM did not reach oobe screen.')
46
47        self.cfm_facade.skip_oobe_screen()
48        time.sleep(_SHORT_TIMEOUT)
49
50
51    def _set_peripheral(self, peripheral_dict):
52        """Set perferred peripherals.
53
54        @param peripheral_dict: Dictionary of peripherals
55        """
56        avail_mics = self.cfm_facade.get_mic_devices()
57        avail_speakers = self.cfm_facade.get_speaker_devices()
58        avail_cameras = self.cfm_facade.get_camera_devices()
59
60        if peripheral_dict.get('Microphone') in avail_mics:
61            self.cfm_facade.set_preferred_mic(
62                    peripheral_dict.get('Microphone'))
63        if peripheral_dict.get('Speaker') in avail_speakers:
64            self.cfm_facade.set_preferred_speaker(
65                    peripheral_dict.get('Speaker'))
66        if peripheral_dict.get('Camera') in avail_cameras:
67            self.cfm_facade.set_preferred_camera(
68                    peripheral_dict.get('Camera'))
69
70
71    def _peripheral_detection(self, peripheral_dict, on_off):
72        """Detect attached peripheral.
73
74        @param peripheral_dict: Dictionary of peripherals
75        @param on_off: Is USB hub on or off.
76        """
77        if 'Microphone' in peripheral_dict.keys():
78            if (on_off and peripheral_dict.get('Microphone') not in
79                    self.cfm_facade.get_preferred_mic()):
80                raise error.TestFail('Microphone not detected.')
81            if (not on_off and peripheral_dict.get('Microphone') in
82                    self.cfm_facade.get_preferred_mic()):
83                raise error.TestFail('Microphone should not be detected.')
84
85        if 'Speaker' in peripheral_dict.keys():
86            if (on_off and peripheral_dict.get('Speaker') not in
87                    self.cfm_facade.get_preferred_speaker()):
88                raise error.TestFail('Speaker not detected.')
89            if not on_off and self.cfm_facade.get_preferred_speaker():
90                raise error.TestFail('Speaker should not be detected.')
91
92        if 'Camera' in peripheral_dict.keys():
93            if (on_off and peripheral_dict.get('Camera') not in
94                    self.cfm_facade.get_preferred_camera()):
95                raise error.TestFail('Camera not detected.')
96            if not on_off and self.cfm_facade.get_preferred_camera():
97                raise error.TestFail('Camera should not be detected.')
98
99
100    def run_once(self, host, repeat, peripheral_whitelist_dict):
101        """Main function to run autotest.
102
103        @param host: Host object representing the DUT.
104        @param repeat: Number of times peripheral should be hotplugged.
105        @param peripheral_whitelist_dict: Dictionary of peripherals to test.
106        """
107        self.client = host
108
109        factory = remote_facade_factory.RemoteFacadeFactory(
110                host, no_chrome=True)
111        self.cfm_facade = factory.create_cfm_facade()
112
113        tpm_utils.ClearTPMOwnerRequest(self.client)
114
115        if self.client.servo:
116            self.client.servo.switch_usbkey('dut')
117            self.client.servo.set('usb_mux_sel3', 'dut_sees_usbkey')
118            time.sleep(_SHORT_TIMEOUT)
119            self._set_hub_power(True)
120
121        try:
122            self._enroll_device_and_skip_oobe()
123            self._set_peripheral(peripheral_whitelist_dict)
124
125            on_off_list = [True, False]
126            on_off = itertools.cycle(on_off_list)
127            while repeat:
128                reset_ = on_off.next()
129                self._set_hub_power(reset_)
130                self._peripheral_detection(peripheral_whitelist_dict, reset_)
131                repeat -= 1
132        except Exception as e:
133            raise error.TestFail(str(e))
134
135        tpm_utils.ClearTPMOwnerRequest(self.client)
136