1# Copyright 2015 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
5"""This is a server side bluetooth connection test using the Chameleon board."""
6
7import logging
8import time
9
10from autotest_lib.client.cros.chameleon import audio_test_utils
11from autotest_lib.client.cros.chameleon import chameleon_audio_helper
12from autotest_lib.client.cros.chameleon import chameleon_audio_ids
13from autotest_lib.server.cros.audio import audio_test
14from autotest_lib.server.cros.multimedia import remote_facade_factory
15
16
17class audio_AudioBluetoothConnectionStability(audio_test.AudioTest):
18    """Server side bluetooth connection audio test.
19
20    This test talks to a Chameleon board and a Cros device to verify
21    bluetooth connection between Cros device and bluetooth module is stable.
22
23    """
24    version = 1
25    CONNECTION_TEST_TIME_SECS = 300
26
27    def dump_logs_after_nodes_changed(self):
28        """Dumps the log after unexpected NodesChanged signal happens."""
29        audio_test_utils.dump_cros_audio_logs(
30                self.host, self.audio_facade, self.resultsdir,
31                'after_nodes_changed')
32
33
34    def run_once(self, host, suspend=False,
35                 disable=False, disconnect=False):
36        """Runs bluetooth audio connection test."""
37        self.host = host
38
39        factory = remote_facade_factory.RemoteFacadeFactory(
40                host, results_dir=self.resultsdir)
41        self.audio_facade = factory.create_audio_facade()
42
43        chameleon_board = host.chameleon
44        chameleon_board.setup_and_reset(self.outputdir)
45
46        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
47                factory, host)
48
49        source = widget_factory.create_widget(
50            chameleon_audio_ids.CrosIds.BLUETOOTH_HEADPHONE)
51        bluetooth_widget = widget_factory.create_widget(
52            chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_RX)
53        recorder = widget_factory.create_widget(
54            chameleon_audio_ids.ChameleonIds.LINEIN)
55
56        binder = widget_factory.create_binder(
57                source, bluetooth_widget, recorder)
58
59        with chameleon_audio_helper.bind_widgets(binder):
60
61            audio_test_utils.dump_cros_audio_logs(
62                    host, self.audio_facade, self.resultsdir, 'after_binding')
63
64            if audio_test_utils.has_internal_microphone(host):
65                self.audio_facade.set_chrome_active_node_type(None, 'BLUETOOTH')
66
67            audio_test_utils.check_audio_nodes(self.audio_facade,
68                                               (['BLUETOOTH'], ['BLUETOOTH']))
69
70            # Monitors there is no node change in this time period.
71            with audio_test_utils.monitor_no_nodes_changed(
72                    self.audio_facade, self.dump_logs_after_nodes_changed):
73                logging.debug('Monitoring NodesChanged signal for %s seconds',
74                              self.CONNECTION_TEST_TIME_SECS)
75                time.sleep(self.CONNECTION_TEST_TIME_SECS)
76