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 audio nodes s test using the Chameleon board.""" 6 7import os 8import time 9 10from autotest_lib.client.common_lib import error 11from autotest_lib.client.cros.chameleon import audio_test_utils 12from autotest_lib.client.cros.chameleon import chameleon_audio_ids 13from autotest_lib.client.cros.chameleon import chameleon_audio_helper 14from autotest_lib.client.cros.chameleon import chameleon_port_finder 15 16from autotest_lib.client.cros.chameleon import edid as edid_lib 17from autotest_lib.server.cros.audio import audio_test 18from autotest_lib.server.cros.multimedia import remote_facade_factory 19 20 21 22class audio_AudioNodeSwitch(audio_test.AudioTest): 23 """Server side audio test. 24 25 This test talks to a Chameleon board and a Cros device to verify 26 audio nodes switch correctly. 27 28 """ 29 version = 1 30 _APPLY_EDID_DELAY = 5 31 _PLUG_DELAY = 5 32 _VOLUMES = {'INTERNAL_SPEAKER': 100, 33 'HEADPHONE': 80, 34 'HDMI': 60, 35 'USB': 40,} 36 37 def check_default_nodes(self): 38 """Checks default audio nodes for devices with onboard audio support.""" 39 if audio_test_utils.has_internal_microphone(self.host): 40 audio_test_utils.check_audio_nodes(self.audio_facade, 41 (None, ['INTERNAL_MIC'])) 42 if audio_test_utils.has_internal_speaker(self.host): 43 audio_test_utils.check_audio_nodes(self.audio_facade, 44 (['INTERNAL_SPEAKER'], None)) 45 46 47 def set_active_volume_to_node_volume(self, node): 48 """Sets Chrome volume to the specified volume of node. 49 50 @param node: One of node type in self._VOLUMES. 51 52 """ 53 self.audio_facade.set_chrome_active_volume(self._VOLUMES[node]) 54 55 56 def check_active_node_volume(self, node): 57 """Checks the active node type and checks if its volume is as expected. 58 59 @param node: The expected node. 60 61 @raises: TestFail if node volume is not as expected. 62 63 """ 64 # Checks the node type is the active node type. 65 audio_test_utils.check_audio_nodes(self.audio_facade, ([node], None)) 66 # Checks if active volume is the node volume. 67 volume, mute = self.audio_facade.get_chrome_active_volume_mute() 68 expected_volume = self._VOLUMES[node] 69 if volume != expected_volume: 70 raise error.TestFail( 71 'Node %s volume %d != %d' % (node, volume, expected_volume)) 72 73 74 def switch_nodes_and_check_volume(self, nodes): 75 """Switches between nodes and check the node volumes. 76 77 @param nodes: A list of node types to check. 78 79 """ 80 if len(nodes) == 1: 81 self.check_active_node_volume(nodes[0]) 82 for node in nodes: 83 # Switch nodes and check their volume. 84 self.audio_facade.set_chrome_active_node_type(node, None) 85 self.check_active_node_volume(node) 86 87 88 def run_once(self, host, jack_node=False, hdmi_node=False, usb_node=False): 89 self.host = host 90 chameleon_board = host.chameleon 91 audio_board = chameleon_board.get_audio_board() 92 factory = remote_facade_factory.RemoteFacadeFactory( 93 host, results_dir=self.resultsdir) 94 95 chameleon_board.setup_and_reset(self.outputdir) 96 self.audio_facade = factory.create_audio_facade() 97 self.display_facade = factory.create_display_facade() 98 99 self.check_default_nodes() 100 nodes = [] 101 if audio_test_utils.has_internal_speaker(self.host): 102 self.set_active_volume_to_node_volume('INTERNAL_SPEAKER') 103 nodes.append('INTERNAL_SPEAKER') 104 self.switch_nodes_and_check_volume(nodes) 105 106 107 if hdmi_node: 108 edid_path = os.path.join(self.bindir, 109 'test_data/edids/HDMI_DELL_U2410.txt') 110 finder = chameleon_port_finder.ChameleonVideoInputFinder( 111 chameleon_board, self.display_facade) 112 hdmi_port = finder.find_port('HDMI') 113 hdmi_port.apply_edid(edid_lib.Edid.from_file(edid_path)) 114 time.sleep(self._APPLY_EDID_DELAY) 115 hdmi_port.set_plug(True) 116 time.sleep(self._PLUG_DELAY * 2) 117 118 audio_test_utils.check_audio_nodes(self.audio_facade, 119 (['HDMI'], None)) 120 121 self.set_active_volume_to_node_volume('HDMI') 122 nodes.append('HDMI') 123 self.switch_nodes_and_check_volume(nodes) 124 125 if jack_node: 126 jack_plugger = audio_board.get_jack_plugger() 127 jack_plugger.plug() 128 time.sleep(self._PLUG_DELAY) 129 audio_test_utils.dump_cros_audio_logs(host, self.audio_facade, 130 self.resultsdir) 131 audio_test_utils.check_audio_nodes(self.audio_facade, 132 (['HEADPHONE'], ['MIC'])) 133 134 self.set_active_volume_to_node_volume('HEADPHONE') 135 nodes.append('HEADPHONE') 136 self.switch_nodes_and_check_volume(nodes) 137 138 if usb_node: 139 widget_factory = chameleon_audio_helper.AudioWidgetFactory( 140 factory, host) 141 142 source = widget_factory.create_widget( 143 chameleon_audio_ids.CrosIds.USBOUT) 144 recorder = widget_factory.create_widget( 145 chameleon_audio_ids.ChameleonIds.USBIN) 146 binder = widget_factory.create_binder(source, recorder) 147 148 with chameleon_audio_helper.bind_widgets(binder): 149 time.sleep(self._PLUG_DELAY) 150 audio_test_utils.check_audio_nodes(self.audio_facade, 151 (['USB'], ['USB'])) 152 self.set_active_volume_to_node_volume('USB') 153 nodes.append('USB') 154 self.switch_nodes_and_check_volume(nodes) 155 time.sleep(self._PLUG_DELAY) 156 nodes.remove('USB') 157 self.switch_nodes_and_check_volume(nodes) 158 159 if jack_node: 160 if usb_node: 161 audio_test_utils.check_audio_nodes(self.audio_facade, 162 (['HEADPHONE'], ['MIC'])) 163 jack_plugger.unplug() 164 time.sleep(self._PLUG_DELAY) 165 nodes.remove('HEADPHONE') 166 self.switch_nodes_and_check_volume(nodes) 167 168 if hdmi_node: 169 if usb_node or jack_node : 170 audio_test_utils.check_audio_nodes(self.audio_facade, 171 (['HDMI'], None)) 172 hdmi_port.set_plug(False) 173 time.sleep(self._PLUG_DELAY) 174 nodes.remove('HDMI') 175 self.switch_nodes_and_check_volume(nodes) 176 177 self.check_default_nodes() 178 179