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