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"""This is a server side test to check nodes created for internal card."""
6
7from autotest_lib.client.cros.chameleon import audio_test_utils
8from autotest_lib.server.cros.audio import audio_test
9
10
11class audio_InternalCardNodes(audio_test.AudioTest):
12    """Server side test to check audio nodes for internal card.
13
14    This test talks to a Chameleon board and a Cros device to verify
15    audio nodes created for internal cards are correct.
16
17    """
18    version = 1
19
20    def get_expected_nodes(self, plugged):
21        """Gets expected nodes should should be created for internal cards.
22
23        @param plugged: True for plugged state, false otherwise.
24        @returns:
25            a tuple (output, input) containing lists of expected input and
26            output nodes.
27        """
28        nodes = ([], [
29                'POST_DSP_DELAYED_LOOPBACK', 'POST_DSP_LOOPBACK',
30                'POST_MIX_LOOPBACK'
31        ])
32        if plugged:
33            # Checks whether line-out or headphone is detected.
34            hp_jack_node_type = audio_test_utils.check_hp_or_lineout_plugged(
35                    self.facade)
36            nodes[0].append(hp_jack_node_type)
37            nodes[1].append('MIC')
38        if audio_test_utils.has_internal_speaker(self.host):
39            nodes[0].append('INTERNAL_SPEAKER')
40        if audio_test_utils.has_internal_microphone(self.host):
41            nodes[1].extend(
42                    audio_test_utils.get_plugged_internal_mics(self.host))
43        if audio_test_utils.has_hotwording(self.host):
44            nodes[1].append('HOTWORD')
45        if audio_test_utils.has_echo_reference(self.host):
46            nodes[1].append('ECHO_REFERENCE')
47        return nodes
48
49    def run_once(self, plug=True, blocked_boards=[]):
50        """Runs InternalCardNodes test."""
51        if self.host.get_board().split(':')[1] in blocked_boards:
52            raise error.TestNAError('Board not applicable to test!')
53        if not audio_test_utils.has_audio_jack(self.host):
54            audio_test_utils.check_plugged_nodes(
55                    self.facade, self.get_expected_nodes(False))
56            return
57
58        jack_plugger = self.host.chameleon.get_audio_board().get_jack_plugger()
59
60        if plug:
61            jack_plugger.plug()
62        else:
63            jack_plugger.unplug()
64
65        audio_test_utils.check_plugged_nodes(self.facade,
66                                             self.get_expected_nodes(plug))
67