1 // Copyright (c) 2012 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 #include <gtest/gtest.h>
6 #include <map>
7 #include <stdio.h>
8 #include <syslog.h>
9 #include <vector>
10 
11 extern "C" {
12 
13 #include "cras_iodev.h"
14 #include "cras_shm.h"
15 #include "cras_system_state.h"
16 #include "cras_types.h"
17 #include "cras_alsa_mixer.h"
18 
19 //  Include C file to test static functions.
20 #include "cras_alsa_io.c"
21 }
22 
23 #define BUFFER_SIZE 8192
24 
25 //  Data for simulating functions stubbed below.
26 static int cras_alsa_open_called;
27 static int cras_iodev_append_stream_ret;
28 static int cras_alsa_get_avail_frames_ret;
29 static int cras_alsa_get_avail_frames_avail;
30 static int cras_alsa_start_called;
31 static uint8_t *cras_alsa_mmap_begin_buffer;
32 static size_t cras_alsa_mmap_begin_frames;
33 static size_t cras_alsa_fill_properties_called;
34 static size_t alsa_mixer_set_dBFS_called;
35 static int alsa_mixer_set_dBFS_value;
36 static const struct mixer_control *alsa_mixer_set_dBFS_output;
37 static size_t alsa_mixer_set_capture_dBFS_called;
38 static int alsa_mixer_set_capture_dBFS_value;
39 static const struct mixer_control *alsa_mixer_set_capture_dBFS_input;
40 static const struct mixer_control
41     *cras_alsa_mixer_get_minimum_capture_gain_mixer_input;
42 static const struct mixer_control
43     *cras_alsa_mixer_get_maximum_capture_gain_mixer_input;
44 static size_t cras_alsa_mixer_list_outputs_called;
45 static size_t cras_alsa_mixer_list_inputs_called;
46 static size_t cras_alsa_mixer_get_control_for_section_called;
47 static struct mixer_control *
48     cras_alsa_mixer_get_control_for_section_return_value;
49 static size_t sys_get_volume_called;
50 static size_t sys_get_volume_return_value;
51 static size_t sys_get_capture_gain_called;
52 static long sys_get_capture_gain_return_value;
53 static size_t alsa_mixer_set_mute_called;
54 static int alsa_mixer_set_mute_value;
55 static size_t alsa_mixer_get_dB_range_called;
56 static long alsa_mixer_get_dB_range_value;
57 static size_t alsa_mixer_get_output_dB_range_called;
58 static long alsa_mixer_get_output_dB_range_value;
59 static const struct mixer_control *alsa_mixer_set_mute_output;
60 static size_t alsa_mixer_set_capture_mute_called;
61 static int alsa_mixer_set_capture_mute_value;
62 static const struct mixer_control *alsa_mixer_set_capture_mute_input;
63 static size_t sys_get_mute_called;
64 static int sys_get_mute_return_value;
65 static size_t sys_get_capture_mute_called;
66 static int sys_get_capture_mute_return_value;
67 static struct cras_alsa_mixer *fake_mixer = (struct cras_alsa_mixer *)1;
68 static struct cras_card_config *fake_config = (struct cras_card_config *)2;
69 static struct mixer_control **cras_alsa_mixer_list_outputs_outputs;
70 static size_t cras_alsa_mixer_list_outputs_outputs_length;
71 static struct mixer_control **cras_alsa_mixer_list_inputs_outputs;
72 static size_t cras_alsa_mixer_list_inputs_outputs_length;
73 static size_t cras_alsa_mixer_set_output_active_state_called;
74 static std::vector<struct mixer_control *>
75     cras_alsa_mixer_set_output_active_state_outputs;
76 static std::vector<int> cras_alsa_mixer_set_output_active_state_values;
77 static cras_audio_format *fake_format;
78 static size_t sys_set_volume_limits_called;
79 static size_t sys_set_capture_gain_limits_called;
80 static size_t cras_alsa_mixer_get_minimum_capture_gain_called;
81 static size_t cras_alsa_mixer_get_maximum_capture_gain_called;
82 static struct mixer_control *cras_alsa_jack_get_mixer_output_ret;
83 static struct mixer_control *cras_alsa_jack_get_mixer_input_ret;
84 static size_t cras_alsa_mixer_get_output_volume_curve_called;
85 typedef std::map<const struct mixer_control*, std::string> ControlNameMap;
86 static ControlNameMap cras_alsa_mixer_get_control_name_values;
87 static size_t cras_alsa_mixer_get_control_name_called;
88 static size_t cras_alsa_jack_list_create_called;
89 static size_t cras_alsa_jack_list_find_jacks_by_name_matching_called;
90 static size_t cras_alsa_jack_list_add_jack_for_section_called;
91 static struct cras_alsa_jack *
92     cras_alsa_jack_list_add_jack_for_section_result_jack;
93 static size_t cras_alsa_jack_list_destroy_called;
94 static int cras_alsa_jack_list_has_hctl_jacks_return_val;
95 static jack_state_change_callback *cras_alsa_jack_list_create_cb;
96 static void *cras_alsa_jack_list_create_cb_data;
97 static char test_card_name[] = "TestCard";
98 static char test_dev_name[] = "TestDev";
99 static char test_dev_id[] = "TestDevId";
100 static size_t cras_iodev_add_node_called;
101 static struct cras_ionode *cras_iodev_set_node_attr_ionode;
102 static size_t cras_iodev_set_node_attr_called;
103 static enum ionode_attr cras_iodev_set_node_attr_attr;
104 static int cras_iodev_set_node_attr_value;
105 static unsigned cras_alsa_jack_enable_ucm_called;
106 static unsigned ucm_set_enabled_called;
107 static size_t cras_iodev_update_dsp_called;
108 static const char *cras_iodev_update_dsp_name;
109 static size_t ucm_get_dsp_name_default_called;
110 static const char *ucm_get_dsp_name_default_value;
111 static size_t cras_alsa_jack_get_dsp_name_called;
112 static const char *cras_alsa_jack_get_dsp_name_value;
113 static size_t cras_iodev_free_resources_called;
114 static size_t cras_alsa_jack_update_node_type_called;
115 static int ucm_swap_mode_exists_ret_value;
116 static int ucm_enable_swap_mode_ret_value;
117 static size_t ucm_enable_swap_mode_called;
118 static int is_utf8_string_ret_value;
119 static const char *cras_alsa_jack_update_monitor_fake_name = 0;
120 static int cras_alsa_jack_get_name_called;
121 static const char *cras_alsa_jack_get_name_ret_value = 0;
122 static char default_jack_name[] = "Something Jack";
123 static int auto_unplug_input_node_ret = 0;
124 static int auto_unplug_output_node_ret = 0;
125 static int ucm_get_min_software_gain_called;
126 static int ucm_get_min_software_gain_ret_value;
127 static long ucm_get_min_software_gain_value;
128 static int ucm_get_max_software_gain_called;
129 static int ucm_get_max_software_gain_ret_value;
130 static long ucm_get_max_software_gain_value;
131 static long cras_system_set_capture_gain_limits_set_value[2];
132 static long cras_alsa_mixer_get_minimum_capture_gain_ret_value;
133 static long cras_alsa_mixer_get_maximum_capture_gain_ret_value;
134 static snd_pcm_state_t snd_pcm_state_ret;
135 static int cras_alsa_attempt_resume_called;
136 static snd_hctl_t *fake_hctl = (snd_hctl_t *)2;
137 static size_t ucm_get_dma_period_for_dev_called;
138 static unsigned int ucm_get_dma_period_for_dev_ret;
139 static int cras_card_config_get_volume_curve_for_control_called;
140 typedef std::map<std::string, struct cras_volume_curve *> VolCurveMap;
141 static VolCurveMap cras_card_config_get_volume_curve_vals;
142 static int cras_alsa_mmap_get_whole_buffer_called;
143 static int cras_iodev_fill_odev_zeros_called;
144 static unsigned int cras_iodev_fill_odev_zeros_frames;
145 static int cras_iodev_frames_queued_ret;
146 static int cras_iodev_buffer_avail_ret;
147 static int cras_alsa_resume_appl_ptr_called;
148 static int cras_alsa_resume_appl_ptr_ahead;
149 static int ucm_get_enable_htimestamp_flag_ret;
150 static const struct cras_volume_curve *fake_get_dBFS_volume_curve_val;
151 static int cras_iodev_dsp_set_swap_mode_for_node_called;
152 static std::map<std::string, long> ucm_get_default_node_gain_values;
153 static thread_callback audio_thread_cb;
154 static void *audio_thread_cb_data;
155 static int hotword_send_triggered_msg_called;
156 
ResetStubData()157 void ResetStubData() {
158   cras_alsa_open_called = 0;
159   cras_iodev_append_stream_ret = 0;
160   cras_alsa_get_avail_frames_ret = 0;
161   cras_alsa_get_avail_frames_avail = 0;
162   cras_alsa_start_called = 0;
163   cras_alsa_fill_properties_called = 0;
164   sys_get_volume_called = 0;
165   sys_get_capture_gain_called = 0;
166   alsa_mixer_set_dBFS_called = 0;
167   alsa_mixer_set_capture_dBFS_called = 0;
168   sys_get_mute_called = 0;
169   sys_get_capture_mute_called = 0;
170   alsa_mixer_set_mute_called = 0;
171   alsa_mixer_get_dB_range_called = 0;
172   alsa_mixer_get_output_dB_range_called = 0;
173   alsa_mixer_set_capture_mute_called = 0;
174   cras_alsa_mixer_get_control_for_section_called = 0;
175   cras_alsa_mixer_get_control_for_section_return_value = NULL;
176   cras_alsa_mixer_list_outputs_called = 0;
177   cras_alsa_mixer_list_outputs_outputs_length = 0;
178   cras_alsa_mixer_list_inputs_called = 0;
179   cras_alsa_mixer_list_inputs_outputs_length = 0;
180   cras_alsa_mixer_set_output_active_state_called = 0;
181   cras_alsa_mixer_set_output_active_state_outputs.clear();
182   cras_alsa_mixer_set_output_active_state_values.clear();
183   sys_set_volume_limits_called = 0;
184   sys_set_capture_gain_limits_called = 0;
185   sys_get_capture_gain_return_value = 0;
186   cras_alsa_mixer_get_minimum_capture_gain_called = 0;
187   cras_alsa_mixer_get_maximum_capture_gain_called = 0;
188   cras_alsa_mixer_get_output_volume_curve_called = 0;
189   cras_alsa_jack_get_mixer_output_ret = NULL;
190   cras_alsa_jack_get_mixer_input_ret = NULL;
191   cras_alsa_mixer_get_control_name_values.clear();
192   cras_alsa_mixer_get_control_name_called = 0;
193   cras_alsa_jack_list_create_called = 0;
194   cras_alsa_jack_list_find_jacks_by_name_matching_called = 0;
195   cras_alsa_jack_list_add_jack_for_section_called = 0;
196   cras_alsa_jack_list_add_jack_for_section_result_jack = NULL;
197   cras_alsa_jack_list_destroy_called = 0;
198   cras_alsa_jack_list_has_hctl_jacks_return_val = 1;
199   cras_iodev_add_node_called = 0;
200   cras_iodev_set_node_attr_called = 0;
201   cras_alsa_jack_enable_ucm_called = 0;
202   ucm_set_enabled_called = 0;
203   cras_iodev_update_dsp_called = 0;
204   cras_iodev_update_dsp_name = 0;
205   ucm_get_dsp_name_default_called = 0;
206   ucm_get_dsp_name_default_value = NULL;
207   cras_alsa_jack_get_dsp_name_called = 0;
208   cras_alsa_jack_get_dsp_name_value = NULL;
209   cras_iodev_free_resources_called = 0;
210   cras_alsa_jack_update_node_type_called = 0;
211   ucm_swap_mode_exists_ret_value = 0;
212   ucm_enable_swap_mode_ret_value = 0;
213   ucm_enable_swap_mode_called = 0;
214   is_utf8_string_ret_value = 1;
215   cras_alsa_jack_get_name_called = 0;
216   cras_alsa_jack_get_name_ret_value = default_jack_name;
217   cras_alsa_jack_update_monitor_fake_name = 0;
218   ucm_get_min_software_gain_called = 0;
219   ucm_get_min_software_gain_ret_value = -1;
220   ucm_get_min_software_gain_value = 0;
221   ucm_get_max_software_gain_called = 0;
222   ucm_get_max_software_gain_ret_value = -1;
223   ucm_get_max_software_gain_value = 0;
224   cras_card_config_get_volume_curve_for_control_called = 0;
225   cras_card_config_get_volume_curve_vals.clear();
226   cras_system_set_capture_gain_limits_set_value[0] = -1;
227   cras_system_set_capture_gain_limits_set_value[1] = -1;
228   cras_alsa_mixer_get_minimum_capture_gain_ret_value = 0;
229   cras_alsa_mixer_get_maximum_capture_gain_ret_value = 0;
230   snd_pcm_state_ret = SND_PCM_STATE_RUNNING;
231   cras_alsa_attempt_resume_called = 0;
232   ucm_get_dma_period_for_dev_called = 0;
233   ucm_get_dma_period_for_dev_ret = 0;
234   cras_alsa_mmap_get_whole_buffer_called = 0;
235   cras_iodev_fill_odev_zeros_called = 0;
236   cras_iodev_fill_odev_zeros_frames = 0;
237   cras_iodev_frames_queued_ret = 0;
238   cras_iodev_buffer_avail_ret = 0;
239   cras_alsa_resume_appl_ptr_called = 0;
240   cras_alsa_resume_appl_ptr_ahead = 0;
241   ucm_get_enable_htimestamp_flag_ret = 0;
242   fake_get_dBFS_volume_curve_val = NULL;
243   cras_iodev_dsp_set_swap_mode_for_node_called = 0;
244   ucm_get_default_node_gain_values.clear();
245 }
246 
fake_get_dBFS(const struct cras_volume_curve * curve,size_t volume)247 static long fake_get_dBFS(const struct cras_volume_curve *curve, size_t volume)
248 {
249   fake_get_dBFS_volume_curve_val = curve;
250   return (volume - 100) * 100;
251 }
252 static cras_volume_curve default_curve = {
253   .get_dBFS = fake_get_dBFS,
254 };
255 
alsa_iodev_create_with_default_parameters(size_t card_index,const char * dev_id,enum CRAS_ALSA_CARD_TYPE card_type,int is_first,struct cras_alsa_mixer * mixer,struct cras_card_config * config,struct cras_use_case_mgr * ucm,enum CRAS_STREAM_DIRECTION direction)256 static struct cras_iodev *alsa_iodev_create_with_default_parameters(
257     size_t card_index,
258     const char *dev_id,
259     enum CRAS_ALSA_CARD_TYPE card_type,
260     int is_first,
261     struct cras_alsa_mixer *mixer,
262     struct cras_card_config *config,
263     struct cras_use_case_mgr *ucm,
264     enum CRAS_STREAM_DIRECTION direction) {
265   return alsa_iodev_create(card_index, test_card_name, 0, test_dev_name,
266                            dev_id, card_type, is_first,
267                            mixer, config, ucm, fake_hctl,
268                            direction, 0, 0, (char *)"123");
269 }
270 
271 namespace {
272 
TEST(AlsaIoInit,InitializeInvalidDirection)273 TEST(AlsaIoInit, InitializeInvalidDirection) {
274   struct alsa_io *aio;
275 
276   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
277       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, NULL,
278       CRAS_NUM_DIRECTIONS);
279   ASSERT_EQ(aio, (void *)NULL);
280 }
281 
TEST(AlsaIoInit,InitializePlayback)282 TEST(AlsaIoInit, InitializePlayback) {
283   struct alsa_io *aio;
284   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
285 
286   ResetStubData();
287   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
288       0, test_dev_id, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
289       CRAS_STREAM_OUTPUT);
290   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
291   /* Get volume curve twice for iodev, and default node. */
292   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
293   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
294   EXPECT_EQ(0, cras_alsa_fill_properties_called);
295   EXPECT_EQ(1, cras_alsa_mixer_list_outputs_called);
296   EXPECT_EQ(0, strncmp(test_card_name,
297                        aio->base.info.name,
298 		       strlen(test_card_name)));
299   EXPECT_EQ(0, ucm_get_dsp_name_default_called);
300   EXPECT_EQ(NULL, cras_iodev_update_dsp_name);
301   ASSERT_NE(reinterpret_cast<const char *>(NULL), aio->dev_name);
302   EXPECT_EQ(0, strcmp(test_dev_name, aio->dev_name));
303   ASSERT_NE(reinterpret_cast<const char *>(NULL), aio->dev_id);
304   EXPECT_EQ(0, strcmp(test_dev_id, aio->dev_id));
305 
306   alsa_iodev_destroy((struct cras_iodev *)aio);
307   EXPECT_EQ(1, cras_iodev_free_resources_called);
308 }
309 
TEST(AlsaIoInit,DefaultNodeInternalCard)310 TEST(AlsaIoInit, DefaultNodeInternalCard) {
311   struct alsa_io *aio;
312   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
313 
314   ResetStubData();
315   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
316       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, NULL,
317       CRAS_STREAM_OUTPUT);
318   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
319   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
320   ASSERT_STREQ("(default)", aio->base.active_node->name);
321   ASSERT_EQ(1, aio->base.active_node->plugged);
322   ASSERT_EQ((void *)no_stream, (void *)aio->base.no_stream);
323   ASSERT_EQ((void *)output_should_wake, (void *)aio->base.output_should_wake);
324   alsa_iodev_destroy((struct cras_iodev *)aio);
325 
326   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
327       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
328       CRAS_STREAM_OUTPUT);
329   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
330   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
331   ASSERT_STREQ("Speaker", aio->base.active_node->name);
332   ASSERT_EQ(1, aio->base.active_node->plugged);
333   ASSERT_EQ((void *)no_stream, (void *)aio->base.no_stream);
334   ASSERT_EQ((void *)output_should_wake, (void *)aio->base.output_should_wake);
335   alsa_iodev_destroy((struct cras_iodev *)aio);
336 
337   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
338       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, NULL,
339       CRAS_STREAM_INPUT);
340   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
341   /* No more call to get volume curve for input device. */
342   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
343   ASSERT_STREQ("(default)", aio->base.active_node->name);
344   ASSERT_EQ(1, aio->base.active_node->plugged);
345   ASSERT_EQ((void *)no_stream, (void *)aio->base.no_stream);
346   ASSERT_EQ((void *)output_should_wake, (void *)aio->base.output_should_wake);
347   alsa_iodev_destroy((struct cras_iodev *)aio);
348 
349   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
350       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
351       CRAS_STREAM_INPUT);
352   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
353   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
354   ASSERT_STREQ("Internal Mic", aio->base.active_node->name);
355   ASSERT_EQ(1, aio->base.active_node->plugged);
356   ASSERT_EQ((void *)no_stream, (void *)aio->base.no_stream);
357   ASSERT_EQ((void *)output_should_wake, (void *)aio->base.output_should_wake);
358   alsa_iodev_destroy((struct cras_iodev *)aio);
359 }
360 
TEST(AlsaIoInit,DefaultNodeUSBCard)361 TEST(AlsaIoInit, DefaultNodeUSBCard) {
362   struct alsa_io *aio;
363   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
364 
365   ResetStubData();
366   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
367       0, NULL, ALSA_CARD_TYPE_USB, 1, fake_mixer, fake_config, NULL,
368       CRAS_STREAM_OUTPUT);
369   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
370   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
371   ASSERT_STREQ("(default)", aio->base.active_node->name);
372   ASSERT_EQ(1, aio->base.active_node->plugged);
373   EXPECT_EQ(1, cras_iodev_set_node_attr_called);
374   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
375   EXPECT_EQ(1, cras_iodev_set_node_attr_value);
376   alsa_iodev_destroy((struct cras_iodev *)aio);
377 
378   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
379       0, NULL, ALSA_CARD_TYPE_USB, 1, fake_mixer, fake_config, NULL,
380       CRAS_STREAM_INPUT);
381   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
382   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
383   ASSERT_STREQ("(default)", aio->base.active_node->name);
384   ASSERT_EQ(1, aio->base.active_node->plugged);
385   EXPECT_EQ(2, cras_iodev_set_node_attr_called);
386   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
387   EXPECT_EQ(1, cras_iodev_set_node_attr_value);
388   alsa_iodev_destroy((struct cras_iodev *)aio);
389 }
390 
TEST(AlsaIoInit,OpenPlayback)391 TEST(AlsaIoInit, OpenPlayback) {
392   struct cras_iodev *iodev;
393   struct cras_audio_format format;
394   struct alsa_io *aio;
395 
396   ResetStubData();
397   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
398                                                     ALSA_CARD_TYPE_INTERNAL, 0,
399                                                     fake_mixer, fake_config,
400                                                     NULL, CRAS_STREAM_OUTPUT);
401   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
402   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
403   aio = (struct alsa_io *)iodev;
404   format.frame_rate = 48000;
405   format.num_channels = 1;
406   cras_iodev_set_format(iodev, &format);
407 
408   // Test that these flags are cleared after open_dev.
409   aio->is_free_running = 1;
410   aio->filled_zeros_for_draining = 512;
411   iodev->open_dev(iodev);
412   EXPECT_EQ(1, cras_alsa_open_called);
413   iodev->configure_dev(iodev);
414   EXPECT_EQ(1, cras_alsa_open_called);
415   EXPECT_EQ(1, sys_set_volume_limits_called);
416   EXPECT_EQ(1, alsa_mixer_set_dBFS_called);
417   EXPECT_EQ(0, cras_alsa_start_called);
418   EXPECT_EQ(0, cras_iodev_set_node_attr_called);
419   EXPECT_EQ(0, aio->is_free_running);
420   EXPECT_EQ(0, aio->filled_zeros_for_draining);
421   EXPECT_EQ(SEVERE_UNDERRUN_MS * format.frame_rate / 1000,
422             aio->severe_underrun_frames);
423 
424   alsa_iodev_destroy(iodev);
425   free(fake_format);
426 }
427 
TEST(AlsaIoInit,UsbCardAutoPlug)428 TEST(AlsaIoInit, UsbCardAutoPlug) {
429   struct cras_iodev *iodev;
430 
431   ResetStubData();
432   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
433                                                     ALSA_CARD_TYPE_INTERNAL, 1,
434                                                     fake_mixer, fake_config,
435                                                     NULL, CRAS_STREAM_OUTPUT);
436   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
437   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
438   EXPECT_EQ(0, cras_iodev_set_node_attr_called);
439   alsa_iodev_destroy(iodev);
440 
441   ResetStubData();
442   iodev = alsa_iodev_create_with_default_parameters(0, NULL, ALSA_CARD_TYPE_USB,
443                                                     0, fake_mixer, fake_config,
444                                                     NULL, CRAS_STREAM_OUTPUT);
445   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
446   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
447   EXPECT_EQ(0, cras_iodev_set_node_attr_called);
448   alsa_iodev_destroy(iodev);
449 
450   ResetStubData();
451   iodev = alsa_iodev_create_with_default_parameters(0, NULL, ALSA_CARD_TYPE_USB,
452                                                     1, fake_mixer, fake_config,
453                                                     NULL, CRAS_STREAM_OUTPUT);
454   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
455   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
456   // Should assume USB devs are plugged when they appear.
457   EXPECT_EQ(1, cras_iodev_set_node_attr_called);
458   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
459   EXPECT_EQ(1, cras_iodev_set_node_attr_value);
460   alsa_iodev_destroy(iodev);
461 }
462 
TEST(AlsaIoInit,UsbCardUseSoftwareVolume)463 TEST(AlsaIoInit, UsbCardUseSoftwareVolume) {
464   struct cras_iodev *iodev;
465 
466   alsa_mixer_get_dB_range_value = 1000;
467   alsa_mixer_get_output_dB_range_value = 1000;
468   ResetStubData();
469   iodev = alsa_iodev_create_with_default_parameters(0, NULL, ALSA_CARD_TYPE_USB,
470                                                     1, fake_mixer, fake_config,
471                                                     NULL, CRAS_STREAM_OUTPUT);
472   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
473   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
474   EXPECT_EQ(1, alsa_mixer_get_dB_range_called);
475   EXPECT_EQ(1, alsa_mixer_get_output_dB_range_called);
476   EXPECT_EQ(1, iodev->active_node->software_volume_needed);
477   alsa_iodev_destroy(iodev);
478 
479   alsa_mixer_get_dB_range_value = 3000;
480   alsa_mixer_get_output_dB_range_value = 2000;
481   ResetStubData();
482   iodev = alsa_iodev_create_with_default_parameters(0, NULL, ALSA_CARD_TYPE_USB,
483                                                     1, fake_mixer, fake_config,
484                                                     NULL, CRAS_STREAM_OUTPUT);
485   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
486   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
487   EXPECT_EQ(1, alsa_mixer_get_dB_range_called);
488   EXPECT_EQ(1, alsa_mixer_get_output_dB_range_called);
489   EXPECT_EQ(0, iodev->active_node->software_volume_needed);
490   alsa_iodev_destroy(iodev);
491 }
492 
TEST(AlsaIoInit,UseSoftwareGain)493 TEST(AlsaIoInit, UseSoftwareGain) {
494   struct cras_iodev *iodev;
495   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
496 
497   /* MaxSoftwareGain is specified in UCM */
498   ResetStubData();
499   ucm_get_min_software_gain_ret_value = 1;
500   ucm_get_min_software_gain_value = 1;
501   ucm_get_max_software_gain_ret_value = 0;
502   ucm_get_max_software_gain_value = 2000;
503   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
504                                                     ALSA_CARD_TYPE_INTERNAL, 1,
505                                                     fake_mixer, fake_config,
506                                                     fake_ucm,
507                                                     CRAS_STREAM_INPUT);
508   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
509   EXPECT_EQ(1, iodev->active_node->software_volume_needed);
510   EXPECT_EQ(DEFAULT_MIN_CAPTURE_GAIN, iodev->active_node->min_software_gain);
511   EXPECT_EQ(2000, iodev->active_node->max_software_gain);
512   ASSERT_EQ(1, sys_set_capture_gain_limits_called);
513   /* The gain range is [DEFAULT_MIN_CAPTURE_GAIN, maximum software gain]. */
514   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[0],
515       DEFAULT_MIN_CAPTURE_GAIN);
516   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[1], 2000);
517 
518   alsa_iodev_destroy(iodev);
519 
520   /* MaxSoftwareGain and MinSoftwareGain are specified in UCM. */
521   ResetStubData();
522   ucm_get_min_software_gain_ret_value = 0;
523   ucm_get_min_software_gain_value = 1000;
524   ucm_get_max_software_gain_ret_value = 0;
525   ucm_get_max_software_gain_value = 2000;
526   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
527                                                     ALSA_CARD_TYPE_INTERNAL, 1,
528                                                     fake_mixer, fake_config,
529                                                     fake_ucm,
530                                                     CRAS_STREAM_INPUT);
531   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
532   EXPECT_EQ(1, iodev->active_node->software_volume_needed);
533   EXPECT_EQ(1000, iodev->active_node->min_software_gain);
534   EXPECT_EQ(2000, iodev->active_node->max_software_gain);
535   ASSERT_EQ(1, sys_set_capture_gain_limits_called);
536   /* The gain range is [minimum software gain, maximum software gain]. */
537   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[0], 1000);
538   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[1], 2000);
539 
540   alsa_iodev_destroy(iodev);
541 
542   /* MinSoftwareGain is larger than MaxSoftwareGain in UCM. */
543   ResetStubData();
544   ucm_get_min_software_gain_ret_value = 0;
545   ucm_get_min_software_gain_value = 3000;
546   ucm_get_max_software_gain_ret_value = 0;
547   ucm_get_max_software_gain_value = 2000;
548   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
549                                                     ALSA_CARD_TYPE_INTERNAL, 1,
550                                                     fake_mixer, fake_config,
551                                                     fake_ucm,
552                                                     CRAS_STREAM_INPUT);
553   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
554   EXPECT_EQ(1, iodev->active_node->software_volume_needed);
555   EXPECT_EQ(DEFAULT_MIN_CAPTURE_GAIN, iodev->active_node->min_software_gain);
556   EXPECT_EQ(2000, iodev->active_node->max_software_gain);
557   ASSERT_EQ(1, sys_set_capture_gain_limits_called);
558   /* The gain range is [DEFAULT_MIN_CAPTURE_GAIN, maximum software gain]. */
559   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[0],
560       DEFAULT_MIN_CAPTURE_GAIN);
561   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[1], 2000);
562 
563   alsa_iodev_destroy(iodev);
564 
565   /* MaxSoftwareGain is not specified in UCM. */
566   ResetStubData();
567   ucm_get_max_software_gain_ret_value = 1;
568   ucm_get_max_software_gain_value = 1;
569   cras_alsa_mixer_get_minimum_capture_gain_ret_value = -500;
570   cras_alsa_mixer_get_maximum_capture_gain_ret_value = 500;
571   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
572                                                     ALSA_CARD_TYPE_INTERNAL, 1,
573                                                     fake_mixer, fake_config,
574                                                     fake_ucm,
575                                                     CRAS_STREAM_INPUT);
576   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
577   EXPECT_EQ(0, iodev->active_node->software_volume_needed);
578   EXPECT_EQ(0, iodev->active_node->max_software_gain);
579   ASSERT_EQ(1, sys_set_capture_gain_limits_called);
580   /* The gain range is reported by controls. */
581   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[0], -500);
582   ASSERT_EQ(cras_system_set_capture_gain_limits_set_value[1], 500);
583 
584   alsa_iodev_destroy(iodev);
585 }
586 
TEST(AlsaIoInit,SoftwareGainWithDefaultNodeGain)587 TEST(AlsaIoInit, SoftwareGainWithDefaultNodeGain) {
588   struct cras_iodev *iodev;
589   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
590   long system_gain = 500;
591   long default_node_gain = -1000;
592 
593   ResetStubData();
594 
595   // Use software gain.
596   ucm_get_max_software_gain_ret_value = 0;
597   ucm_get_max_software_gain_value = 2000;
598 
599   // Set default node gain to -1000 * 0.01 dB.
600   ucm_get_default_node_gain_values["Internal Mic"] = default_node_gain;
601 
602   // Assume this is the first device so it gets internal mic node name.
603   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
604                                                     ALSA_CARD_TYPE_INTERNAL, 1,
605                                                     fake_mixer, fake_config,
606                                                     fake_ucm,
607                                                     CRAS_STREAM_INPUT);
608   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
609 
610   // Gain on node is 300 * 0.01 dB.
611   iodev->active_node->capture_gain = default_node_gain;
612 
613   // cras_iodev will call cras_iodev_adjust_active_node_gain to get gain for
614   // software gain.
615   ASSERT_EQ(system_gain + default_node_gain,
616             cras_iodev_adjust_active_node_gain(iodev, system_gain));
617 
618   alsa_iodev_destroy(iodev);
619 }
620 
TEST(AlsaIoInit,RouteBasedOnJackCallback)621 TEST(AlsaIoInit, RouteBasedOnJackCallback) {
622   struct alsa_io *aio;
623   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
624 
625   ResetStubData();
626   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
627       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
628       CRAS_STREAM_OUTPUT);
629   ASSERT_NE(aio, (void *)NULL);
630   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
631   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
632   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
633   EXPECT_EQ(0, cras_alsa_fill_properties_called);
634   EXPECT_EQ(1, cras_alsa_mixer_list_outputs_called);
635   EXPECT_EQ(1, cras_alsa_jack_list_create_called);
636   EXPECT_EQ(1, cras_alsa_jack_list_find_jacks_by_name_matching_called);
637   EXPECT_EQ(0, cras_alsa_jack_list_add_jack_for_section_called);
638 
639   cras_alsa_jack_list_create_cb(NULL, 1, cras_alsa_jack_list_create_cb_data);
640   EXPECT_EQ(1, cras_iodev_set_node_attr_called);
641   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
642   EXPECT_EQ(1, cras_iodev_set_node_attr_value);
643   cras_alsa_jack_list_create_cb(NULL, 0, cras_alsa_jack_list_create_cb_data);
644   EXPECT_EQ(2, cras_iodev_set_node_attr_called);
645   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
646   EXPECT_EQ(0, cras_iodev_set_node_attr_value);
647 
648   alsa_iodev_destroy((struct cras_iodev *)aio);
649   EXPECT_EQ(1, cras_alsa_jack_list_destroy_called);
650 }
651 
TEST(AlsaIoInit,RouteBasedOnInputJackCallback)652 TEST(AlsaIoInit, RouteBasedOnInputJackCallback) {
653   struct alsa_io *aio;
654   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
655 
656   ResetStubData();
657   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
658       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, NULL,
659       CRAS_STREAM_INPUT);
660   ASSERT_NE(aio, (void *)NULL);
661   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
662 
663   EXPECT_EQ(SND_PCM_STREAM_CAPTURE, aio->alsa_stream);
664   EXPECT_EQ(0, cras_alsa_fill_properties_called);
665   EXPECT_EQ(1, cras_alsa_jack_list_create_called);
666   EXPECT_EQ(1, cras_alsa_jack_list_find_jacks_by_name_matching_called);
667   EXPECT_EQ(0, cras_alsa_jack_list_add_jack_for_section_called);
668 
669   cras_alsa_jack_list_create_cb(NULL, 1, cras_alsa_jack_list_create_cb_data);
670   EXPECT_EQ(1, cras_iodev_set_node_attr_called);
671   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
672   EXPECT_EQ(1, cras_iodev_set_node_attr_value);
673   cras_alsa_jack_list_create_cb(NULL, 0, cras_alsa_jack_list_create_cb_data);
674   EXPECT_EQ(2, cras_iodev_set_node_attr_called);
675   EXPECT_EQ(IONODE_ATTR_PLUGGED, cras_iodev_set_node_attr_attr);
676   EXPECT_EQ(0, cras_iodev_set_node_attr_value);
677 
678   alsa_iodev_destroy((struct cras_iodev *)aio);
679   EXPECT_EQ(1, cras_alsa_jack_list_destroy_called);
680 }
681 
TEST(AlsaIoInit,InitializeCapture)682 TEST(AlsaIoInit, InitializeCapture) {
683   struct alsa_io *aio;
684 
685   ResetStubData();
686   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
687       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
688       CRAS_STREAM_INPUT);
689   ASSERT_NE(aio, (void *)NULL);
690   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
691 
692   EXPECT_EQ(SND_PCM_STREAM_CAPTURE, aio->alsa_stream);
693   EXPECT_EQ(0, cras_alsa_fill_properties_called);
694   EXPECT_EQ(1, cras_alsa_mixer_list_inputs_called);
695 
696   alsa_iodev_destroy((struct cras_iodev *)aio);
697 }
698 
TEST(AlsaIoInit,OpenCapture)699 TEST(AlsaIoInit, OpenCapture) {
700   struct cras_iodev *iodev;
701   struct cras_audio_format format;
702   struct alsa_io *aio;
703 
704   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
705                                                     ALSA_CARD_TYPE_INTERNAL, 0,
706                                                     fake_mixer, fake_config,
707                                                     NULL, CRAS_STREAM_INPUT);
708   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
709 
710   aio = (struct alsa_io *)iodev;
711   format.frame_rate = 48000;
712   format.num_channels = 1;
713   cras_iodev_set_format(iodev, &format);
714 
715   ResetStubData();
716   iodev->open_dev(iodev);
717   EXPECT_EQ(1, cras_alsa_open_called);
718   iodev->configure_dev(iodev);
719   EXPECT_EQ(1, cras_alsa_open_called);
720   EXPECT_EQ(1, cras_alsa_mixer_get_minimum_capture_gain_called);
721   EXPECT_EQ(1, cras_alsa_mixer_get_maximum_capture_gain_called);
722   EXPECT_EQ(1, sys_set_capture_gain_limits_called);
723   EXPECT_EQ(1, sys_get_capture_gain_called);
724   EXPECT_EQ(1, alsa_mixer_set_capture_dBFS_called);
725   EXPECT_EQ(1, sys_get_capture_mute_called);
726   EXPECT_EQ(1, alsa_mixer_set_capture_mute_called);
727   EXPECT_EQ(1, cras_alsa_start_called);
728   EXPECT_EQ(SEVERE_UNDERRUN_MS * format.frame_rate / 1000,
729             aio->severe_underrun_frames);
730 
731   alsa_iodev_destroy(iodev);
732   free(fake_format);
733 }
734 
TEST(AlsaIoInit,OpenCaptureSetCaptureGainWithDefaultNodeGain)735 TEST(AlsaIoInit, OpenCaptureSetCaptureGainWithDefaultNodeGain) {
736   struct cras_iodev *iodev;
737   struct cras_audio_format format;
738   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
739   long system_gain = 2000;
740   long default_node_gain = -1000;
741 
742   ResetStubData();
743   // Set default node gain to -1000 * 0.01 dB.
744   ucm_get_default_node_gain_values["Internal Mic"] = default_node_gain;
745 
746   // Assume this is the first device so it gets internal mic node name.
747   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
748                                                     ALSA_CARD_TYPE_INTERNAL, 1,
749                                                     fake_mixer, fake_config,
750                                                     fake_ucm,
751                                                     CRAS_STREAM_INPUT);
752   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
753 
754   cras_iodev_set_format(iodev, &format);
755 
756   // Check the default node gain is the same as what specified in UCM.
757   EXPECT_EQ(default_node_gain, iodev->active_node->capture_gain);
758   // System gain is set to 2000 * 0.01 dB.
759   sys_get_capture_gain_return_value = system_gain;
760 
761   iodev->open_dev(iodev);
762   iodev->configure_dev(iodev);
763   iodev->close_dev(iodev);
764 
765   // Hardware gain is set to (2000 - 1000) * 0.01 dB.
766   EXPECT_EQ(system_gain + default_node_gain, alsa_mixer_set_capture_dBFS_value);
767 
768   alsa_iodev_destroy(iodev);
769   free(fake_format);
770 }
771 
TEST(AlsaIoInit,OpenCaptureSetCaptureGainWithSoftwareGain)772 TEST(AlsaIoInit, OpenCaptureSetCaptureGainWithSoftwareGain) {
773   struct cras_iodev *iodev;
774   struct cras_audio_format format;
775   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
776 
777   /* Meet the requirements of using software gain. */
778   ResetStubData();
779   ucm_get_max_software_gain_ret_value = 0;
780   ucm_get_max_software_gain_value = 2000;
781 
782   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
783                                                     ALSA_CARD_TYPE_INTERNAL, 0,
784                                                     fake_mixer, fake_config,
785                                                     fake_ucm,
786                                                     CRAS_STREAM_INPUT);
787   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
788 
789   format.frame_rate = 48000;
790   format.num_channels = 1;
791   cras_iodev_set_format(iodev, &format);
792 
793   /* System gain is set to 1000 * 0.01 dB */
794   sys_get_capture_gain_return_value = 1000;
795 
796   iodev->open_dev(iodev);
797   iodev->configure_dev(iodev);
798   iodev->close_dev(iodev);
799 
800   /* Hardware gain is set to 0dB when software gain is used. */
801   EXPECT_EQ(0, alsa_mixer_set_capture_dBFS_value);
802 
803   /* Test the case where software gain is not needed. */
804   iodev->active_node->software_volume_needed = 0;
805   iodev->open_dev(iodev);
806   iodev->configure_dev(iodev);
807   iodev->close_dev(iodev);
808 
809   /* Hardware gain is set to 1000 * 0.01 dB as got from system capture gain.*/
810   EXPECT_EQ(1000, alsa_mixer_set_capture_dBFS_value);
811 
812   alsa_iodev_destroy(iodev);
813   free(fake_format);
814 }
815 
TEST(AlsaIoInit,UpdateActiveNode)816 TEST(AlsaIoInit, UpdateActiveNode) {
817   struct cras_iodev *iodev;
818   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
819 
820   ResetStubData();
821   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
822                                                     ALSA_CARD_TYPE_INTERNAL, 0,
823                                                     fake_mixer, fake_config,
824                                                     NULL,
825                                                     CRAS_STREAM_OUTPUT);
826   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
827   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
828 
829   iodev->update_active_node(iodev, 0, 1);
830 
831   alsa_iodev_destroy(iodev);
832 }
833 
TEST(AlsaIoInit,StartDevice)834 TEST(AlsaIoInit, StartDevice) {
835   struct cras_iodev *iodev;
836   int rc;
837 
838   ResetStubData();
839   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
840                                                     ALSA_CARD_TYPE_INTERNAL, 0,
841                                                     NULL, fake_config, NULL,
842                                                     CRAS_STREAM_OUTPUT);
843   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
844   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
845 
846   // Return right away if it is already running.
847   snd_pcm_state_ret = SND_PCM_STATE_RUNNING;
848   rc = iodev->start(iodev);
849   EXPECT_EQ(0, rc);
850   EXPECT_EQ(0, cras_alsa_start_called);
851 
852   // Otherwise, start the device.
853   snd_pcm_state_ret = SND_PCM_STATE_SETUP;
854   rc = iodev->start(iodev);
855   EXPECT_EQ(0, rc);
856   EXPECT_EQ(1, cras_alsa_start_called);
857 
858   alsa_iodev_destroy(iodev);
859 }
860 
TEST(AlsaIoInit,ResumeDevice)861 TEST(AlsaIoInit, ResumeDevice) {
862   struct cras_iodev *iodev;
863   int rc;
864 
865   ResetStubData();
866   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
867                                                     ALSA_CARD_TYPE_INTERNAL, 0,
868                                                     NULL, fake_config, NULL,
869                                                     CRAS_STREAM_OUTPUT);
870   ASSERT_EQ(0, alsa_iodev_legacy_complete_init(iodev));
871   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
872 
873   // Attempt to resume if the device is suspended.
874   snd_pcm_state_ret = SND_PCM_STATE_SUSPENDED;
875   rc = iodev->start(iodev);
876   EXPECT_EQ(0, rc);
877   EXPECT_EQ(1, cras_alsa_attempt_resume_called);
878 
879   alsa_iodev_destroy(iodev);
880 }
881 
TEST(AlsaIoInit,DspNameDefault)882 TEST(AlsaIoInit, DspNameDefault) {
883   struct alsa_io *aio;
884   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
885   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
886 
887   ResetStubData();
888   ucm_get_dsp_name_default_value = "hello";
889   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
890       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, fake_ucm,
891       CRAS_STREAM_OUTPUT);
892   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
893   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
894   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
895   EXPECT_EQ(1, ucm_get_dsp_name_default_called);
896   EXPECT_EQ(1, cras_alsa_jack_get_dsp_name_called);
897   EXPECT_STREQ("hello", cras_iodev_update_dsp_name);
898 
899   alsa_iodev_destroy((struct cras_iodev *)aio);
900 }
901 
TEST(AlsaIoInit,DspNameJackOverride)902 TEST(AlsaIoInit, DspNameJackOverride) {
903   struct alsa_io *aio;
904   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
905   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
906   const struct cras_alsa_jack *jack = (struct cras_alsa_jack*)4;
907 
908   ResetStubData();
909   ucm_get_dsp_name_default_value = "default_dsp";
910   cras_alsa_jack_get_dsp_name_value = "override_dsp";
911   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
912       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, fake_ucm,
913       CRAS_STREAM_OUTPUT);
914   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
915   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
916   EXPECT_EQ(1, ucm_get_dsp_name_default_called);
917   EXPECT_EQ(1, cras_alsa_jack_get_dsp_name_called);
918   EXPECT_EQ(1, cras_iodev_update_dsp_called);
919   EXPECT_STREQ("default_dsp", cras_iodev_update_dsp_name);
920 
921   // Add the jack node.
922   cras_alsa_jack_list_create_cb(jack, 1, cras_alsa_jack_list_create_cb_data);
923   EXPECT_EQ(1, ucm_get_dsp_name_default_called);
924 
925   // Mark the jack node as active.
926   alsa_iodev_set_active_node(&aio->base, aio->base.nodes->next, 1);
927   EXPECT_EQ(2, cras_alsa_jack_get_dsp_name_called);
928   EXPECT_EQ(2, cras_iodev_update_dsp_called);
929   EXPECT_STREQ("override_dsp", cras_iodev_update_dsp_name);
930 
931   // Mark the default node as active.
932   alsa_iodev_set_active_node(&aio->base, aio->base.nodes, 1);
933   EXPECT_EQ(1, ucm_get_dsp_name_default_called);
934   EXPECT_EQ(3, cras_alsa_jack_get_dsp_name_called);
935   EXPECT_EQ(3, cras_iodev_update_dsp_called);
936   EXPECT_STREQ("default_dsp", cras_iodev_update_dsp_name);
937 
938   alsa_iodev_destroy((struct cras_iodev *)aio);
939 }
940 
TEST(AlsaIoInit,NodeTypeOverride)941 TEST(AlsaIoInit, NodeTypeOverride) {
942   struct alsa_io *aio;
943   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
944   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
945   const struct cras_alsa_jack *jack = (struct cras_alsa_jack*)4;
946 
947   ResetStubData();
948   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
949       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, fake_ucm,
950       CRAS_STREAM_OUTPUT);
951   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
952   // Add the jack node.
953   cras_alsa_jack_list_create_cb(jack, 1, cras_alsa_jack_list_create_cb_data);
954   // Verify that cras_alsa_jack_update_node_type is called when an output device
955   // is created.
956   EXPECT_EQ(1, cras_alsa_jack_update_node_type_called);
957 
958   alsa_iodev_destroy((struct cras_iodev *)aio);
959 }
960 
TEST(AlsaIoInit,SwapMode)961 TEST(AlsaIoInit, SwapMode) {
962   struct alsa_io *aio;
963   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
964   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
965   struct cras_ionode * const fake_node = (cras_ionode *)calloc(
966       1, sizeof(struct cras_ionode));
967   ResetStubData();
968   // Stub replies that swap mode does not exist.
969   ucm_swap_mode_exists_ret_value = 0;
970 
971   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
972       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, fake_ucm,
973       CRAS_STREAM_OUTPUT);
974   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
975 
976   aio->base.set_swap_mode_for_node((cras_iodev*)aio, fake_node, 1);
977   /* Swap mode is implemented by dsp. */
978   EXPECT_EQ(1, cras_iodev_dsp_set_swap_mode_for_node_called);
979 
980   // Stub replies that swap mode exists.
981   ucm_swap_mode_exists_ret_value = 1;
982   alsa_iodev_destroy((struct cras_iodev *)aio);
983 
984   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
985       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, fake_ucm,
986       CRAS_STREAM_OUTPUT);
987   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
988   // Enable swap mode.
989   aio->base.set_swap_mode_for_node((cras_iodev*)aio, fake_node, 1);
990 
991   // Verify that ucm_enable_swap_mode is called when callback to enable
992   // swap mode is called.
993   EXPECT_EQ(1, ucm_enable_swap_mode_called);
994 
995   alsa_iodev_destroy((struct cras_iodev *)aio);
996   free(fake_node);
997 }
998 
999 // Test that system settins aren't touched if no streams active.
TEST(AlsaOutputNode,SystemSettingsWhenInactive)1000 TEST(AlsaOutputNode, SystemSettingsWhenInactive) {
1001   int rc;
1002   struct alsa_io *aio;
1003   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1004   struct mixer_control *outputs[2];
1005 
1006   ResetStubData();
1007   outputs[0] = reinterpret_cast<struct mixer_control *>(3);
1008   outputs[1] = reinterpret_cast<struct mixer_control *>(4);
1009   cras_alsa_mixer_list_outputs_outputs = outputs;
1010   cras_alsa_mixer_list_outputs_outputs_length = ARRAY_SIZE(outputs);
1011   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
1012       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
1013       CRAS_STREAM_OUTPUT);
1014   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
1015   /* Two mixer controls calls get volume curve. */
1016   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
1017   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
1018   EXPECT_EQ(1, cras_alsa_mixer_list_outputs_called);
1019 
1020   ResetStubData();
1021   rc = alsa_iodev_set_active_node((struct cras_iodev *)aio,
1022                                   aio->base.nodes->next, 1);
1023   EXPECT_EQ(0, rc);
1024   EXPECT_EQ(0, alsa_mixer_set_mute_called);
1025   EXPECT_EQ(0, alsa_mixer_set_dBFS_called);
1026   ASSERT_EQ(2, cras_alsa_mixer_set_output_active_state_called);
1027   EXPECT_EQ(outputs[0], cras_alsa_mixer_set_output_active_state_outputs[0]);
1028   EXPECT_EQ(0, cras_alsa_mixer_set_output_active_state_values[0]);
1029   EXPECT_EQ(outputs[1], cras_alsa_mixer_set_output_active_state_outputs[1]);
1030   EXPECT_EQ(1, cras_alsa_mixer_set_output_active_state_values[1]);
1031   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1032   // No jack is defined, and UCM is not used.
1033   EXPECT_EQ(0, cras_alsa_jack_enable_ucm_called);
1034   EXPECT_EQ(0, ucm_set_enabled_called);
1035 
1036   alsa_iodev_destroy((struct cras_iodev *)aio);
1037 }
1038 
1039 //  Test handling of different amounts of outputs.
TEST(AlsaOutputNode,TwoOutputs)1040 TEST(AlsaOutputNode, TwoOutputs) {
1041   int rc;
1042   struct alsa_io *aio;
1043   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1044   struct mixer_control *outputs[2];
1045 
1046   ResetStubData();
1047   outputs[0] = reinterpret_cast<struct mixer_control *>(3);
1048   outputs[1] = reinterpret_cast<struct mixer_control *>(4);
1049   cras_alsa_mixer_list_outputs_outputs = outputs;
1050   cras_alsa_mixer_list_outputs_outputs_length = ARRAY_SIZE(outputs);
1051   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
1052       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, NULL,
1053       CRAS_STREAM_OUTPUT);
1054   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
1055   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
1056   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
1057   EXPECT_EQ(1, cras_alsa_mixer_list_outputs_called);
1058 
1059   aio->handle = (snd_pcm_t *)0x24;
1060 
1061   ResetStubData();
1062   rc = alsa_iodev_set_active_node((struct cras_iodev *)aio,
1063                                   aio->base.nodes->next, 1);
1064   EXPECT_EQ(0, rc);
1065   EXPECT_EQ(2, alsa_mixer_set_mute_called);
1066   EXPECT_EQ(outputs[1], alsa_mixer_set_mute_output);
1067   EXPECT_EQ(1, alsa_mixer_set_dBFS_called);
1068   EXPECT_EQ(outputs[1], alsa_mixer_set_dBFS_output);
1069   ASSERT_EQ(2, cras_alsa_mixer_set_output_active_state_called);
1070   EXPECT_EQ(outputs[0], cras_alsa_mixer_set_output_active_state_outputs[0]);
1071   EXPECT_EQ(0, cras_alsa_mixer_set_output_active_state_values[0]);
1072   EXPECT_EQ(outputs[1], cras_alsa_mixer_set_output_active_state_outputs[1]);
1073   EXPECT_EQ(1, cras_alsa_mixer_set_output_active_state_values[1]);
1074   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1075   // No jacks defined, and UCM is not used.
1076   EXPECT_EQ(0, cras_alsa_jack_enable_ucm_called);
1077   EXPECT_EQ(0, ucm_set_enabled_called);
1078 
1079   alsa_iodev_destroy((struct cras_iodev *)aio);
1080 }
1081 
TEST(AlsaOutputNode,TwoJacksHeadphoneLineout)1082 TEST(AlsaOutputNode, TwoJacksHeadphoneLineout) {
1083   struct alsa_io *aio;
1084   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer *)2;
1085   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr *)3;
1086   struct cras_iodev *iodev;
1087   struct mixer_control *output;
1088   struct ucm_section *section;
1089 
1090   ResetStubData();
1091   output = reinterpret_cast<struct mixer_control *>(3);
1092   cras_alsa_mixer_get_control_name_values[output] = "Headphone";
1093 
1094   // Create the iodev
1095   iodev = alsa_iodev_create_with_default_parameters(
1096       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, fake_ucm,
1097       CRAS_STREAM_OUTPUT);
1098   ASSERT_NE(iodev, (void *)NULL);
1099   aio = reinterpret_cast<struct alsa_io *>(iodev);
1100   EXPECT_EQ(1, cras_card_config_get_volume_curve_for_control_called);
1101 
1102   // First node 'Headphone'
1103   section = ucm_section_create("Headphone", 0, CRAS_STREAM_OUTPUT,
1104                                "fake-jack", "gpio");
1105   ucm_section_set_mixer_name(section, "Headphone");
1106   cras_alsa_jack_list_add_jack_for_section_result_jack =
1107       reinterpret_cast<struct cras_alsa_jack *>(10);
1108   cras_alsa_mixer_get_control_for_section_return_value = output;
1109   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1110   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
1111   ucm_section_free_list(section);
1112 
1113   // Second node 'Line Out'
1114   section = ucm_section_create("Line Out", 0, CRAS_STREAM_OUTPUT,
1115                                "fake-jack", "gpio");
1116   ucm_section_set_mixer_name(section, "Headphone");
1117   cras_alsa_jack_list_add_jack_for_section_result_jack =
1118       reinterpret_cast<struct cras_alsa_jack *>(20);
1119   cras_alsa_mixer_get_control_for_section_return_value = output;
1120   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1121   EXPECT_EQ(7, cras_card_config_get_volume_curve_for_control_called);
1122   ucm_section_free_list(section);
1123 
1124   // Both nodes are associated with the same mixer output. Different jack plug
1125   // report should trigger different node attribute change.
1126   cras_alsa_jack_get_mixer_output_ret = output;
1127   jack_output_plug_event(reinterpret_cast<struct cras_alsa_jack *>(10), 0, aio);
1128   EXPECT_STREQ(cras_iodev_set_node_attr_ionode->name, "Headphone");
1129 
1130   jack_output_plug_event(reinterpret_cast<struct cras_alsa_jack *>(20), 0, aio);
1131   EXPECT_STREQ(cras_iodev_set_node_attr_ionode->name, "Line Out");
1132 
1133   alsa_iodev_destroy(iodev);
1134 }
1135 
TEST(AlsaOutputNode,OutputsFromUCM)1136 TEST(AlsaOutputNode, OutputsFromUCM) {
1137   struct alsa_io *aio;
1138   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1139   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1140   struct cras_iodev *iodev;
1141   static const char *jack_name = "TestCard - Headset Jack";
1142   struct mixer_control *outputs[2];
1143   int rc;
1144   struct ucm_section *section;
1145 
1146   ResetStubData();
1147   outputs[0] = reinterpret_cast<struct mixer_control *>(3);
1148   outputs[1] = reinterpret_cast<struct mixer_control *>(4);
1149   cras_alsa_mixer_list_outputs_outputs = outputs;
1150   cras_alsa_mixer_list_outputs_outputs_length = ARRAY_SIZE(outputs);
1151   cras_alsa_mixer_get_control_name_values[outputs[0]] = INTERNAL_SPEAKER;
1152   cras_alsa_mixer_get_control_name_values[outputs[1]] = "Headphone";
1153   ucm_get_dma_period_for_dev_ret = 1000;
1154 
1155   // Create the IO device.
1156   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
1157                                                     ALSA_CARD_TYPE_INTERNAL, 1,
1158                                                     fake_mixer, fake_config,
1159                                                     fake_ucm,
1160                                                     CRAS_STREAM_OUTPUT);
1161   ASSERT_NE(iodev, (void *)NULL);
1162   aio = reinterpret_cast<struct alsa_io *>(iodev);
1163   EXPECT_EQ(1, cras_card_config_get_volume_curve_for_control_called);
1164 
1165   // First node.
1166   section = ucm_section_create(INTERNAL_SPEAKER, 0, CRAS_STREAM_OUTPUT,
1167                                NULL, NULL);
1168   ucm_section_set_mixer_name(section, INTERNAL_SPEAKER);
1169   cras_alsa_jack_list_add_jack_for_section_result_jack =
1170       reinterpret_cast<struct cras_alsa_jack *>(1);
1171   cras_alsa_mixer_get_control_for_section_return_value = outputs[0];
1172   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1173   ucm_section_free_list(section);
1174   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
1175 
1176   // Add a second node (will use the same iodev).
1177   section = ucm_section_create("Headphone", 0, CRAS_STREAM_OUTPUT,
1178                                jack_name, "hctl");
1179   ucm_section_add_coupled(section, "HP-L", MIXER_NAME_VOLUME);
1180   ucm_section_add_coupled(section, "HP-R", MIXER_NAME_VOLUME);
1181   cras_alsa_jack_list_add_jack_for_section_result_jack = NULL;
1182   cras_alsa_mixer_get_control_for_section_return_value = outputs[1];
1183   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1184   ucm_section_free_list(section);
1185   /* New nodes creation calls get volume curve once, NULL jack doesn't make
1186    * more calls. */
1187   EXPECT_EQ(5, cras_card_config_get_volume_curve_for_control_called);
1188 
1189   // Jack plug of an unkonwn device should do nothing.
1190   cras_alsa_jack_get_mixer_output_ret = NULL;
1191   cras_alsa_jack_get_name_ret_value = "Some other jack";
1192   jack_output_plug_event(reinterpret_cast<struct cras_alsa_jack *>(4), 0, aio);
1193   EXPECT_EQ(0, cras_iodev_set_node_attr_called);
1194 
1195   // Complete initialization, and make first node active.
1196   alsa_iodev_ucm_complete_init(iodev);
1197   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
1198   EXPECT_EQ(2, cras_alsa_jack_list_add_jack_for_section_called);
1199   EXPECT_EQ(2, cras_alsa_mixer_get_control_for_section_called);
1200   EXPECT_EQ(1, ucm_get_dma_period_for_dev_called);
1201   EXPECT_EQ(ucm_get_dma_period_for_dev_ret, aio->dma_period_set_microsecs);
1202 
1203   aio->handle = (snd_pcm_t *)0x24;
1204 
1205   ResetStubData();
1206   rc = alsa_iodev_set_active_node(iodev, aio->base.nodes->next, 1);
1207   EXPECT_EQ(0, rc);
1208   EXPECT_EQ(2, alsa_mixer_set_mute_called);
1209   EXPECT_EQ(outputs[1], alsa_mixer_set_mute_output);
1210   EXPECT_EQ(1, alsa_mixer_set_dBFS_called);
1211   EXPECT_EQ(outputs[1], alsa_mixer_set_dBFS_output);
1212   ASSERT_EQ(2, cras_alsa_mixer_set_output_active_state_called);
1213   EXPECT_EQ(outputs[0], cras_alsa_mixer_set_output_active_state_outputs[0]);
1214   EXPECT_EQ(0, cras_alsa_mixer_set_output_active_state_values[0]);
1215   EXPECT_EQ(outputs[1], cras_alsa_mixer_set_output_active_state_outputs[1]);
1216   EXPECT_EQ(1, cras_alsa_mixer_set_output_active_state_values[1]);
1217   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1218   EXPECT_EQ(1, cras_alsa_jack_enable_ucm_called);
1219   EXPECT_EQ(1, ucm_set_enabled_called);
1220 
1221   // Simulate jack plug event.
1222   cras_alsa_jack_get_mixer_output_ret = outputs[1];
1223   cras_alsa_jack_get_name_ret_value = jack_name;
1224   jack_output_plug_event(reinterpret_cast<struct cras_alsa_jack *>(4), 0, aio);
1225   EXPECT_EQ(1, cras_iodev_set_node_attr_called);
1226 
1227   alsa_iodev_destroy(iodev);
1228 }
1229 
TEST(AlsaOutputNode,OutputNoControlsUCM)1230 TEST(AlsaOutputNode, OutputNoControlsUCM) {
1231   struct alsa_io *aio;
1232   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1233   struct cras_iodev *iodev;
1234   struct ucm_section *section;
1235 
1236   ResetStubData();
1237 
1238   // Create the IO device.
1239   iodev = alsa_iodev_create_with_default_parameters(1, NULL,
1240                                                     ALSA_CARD_TYPE_INTERNAL, 1,
1241                                                     fake_mixer, fake_config,
1242                                                     fake_ucm,
1243                                                     CRAS_STREAM_OUTPUT);
1244   ASSERT_NE(iodev, (void *)NULL);
1245   aio = reinterpret_cast<struct alsa_io *>(iodev);
1246   EXPECT_EQ(1, cras_card_config_get_volume_curve_for_control_called);
1247 
1248   // Node without controls or jacks.
1249   section = ucm_section_create(INTERNAL_SPEAKER, 1, CRAS_STREAM_OUTPUT,
1250                                NULL, NULL);
1251   // Device index doesn't match.
1252   EXPECT_EQ(-22, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1253   section->dev_idx = 0;
1254   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1255   EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
1256   EXPECT_EQ(1, cras_alsa_mixer_get_control_for_section_called);
1257   EXPECT_EQ(1, cras_iodev_add_node_called);
1258   ucm_section_free_list(section);
1259 
1260   // Complete initialization, and make first node active.
1261   alsa_iodev_ucm_complete_init(iodev);
1262   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
1263   EXPECT_EQ(0, cras_alsa_mixer_get_control_name_called);
1264   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1265   EXPECT_EQ(0, cras_alsa_jack_enable_ucm_called);
1266   EXPECT_EQ(1, ucm_set_enabled_called);
1267 
1268   alsa_iodev_destroy(iodev);
1269 }
1270 
TEST(AlsaOutputNode,OutputFromJackUCM)1271 TEST(AlsaOutputNode, OutputFromJackUCM) {
1272   struct alsa_io *aio;
1273   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1274   struct cras_iodev *iodev;
1275   static const char *jack_name = "TestCard - Headset Jack";
1276   struct ucm_section *section;
1277 
1278   ResetStubData();
1279 
1280   // Create the IO device.
1281   iodev = alsa_iodev_create_with_default_parameters(1, NULL,
1282                                                     ALSA_CARD_TYPE_INTERNAL, 1,
1283                                                     fake_mixer, fake_config,
1284                                                     fake_ucm,
1285                                                     CRAS_STREAM_OUTPUT);
1286   ASSERT_NE(iodev, (void *)NULL);
1287   aio = reinterpret_cast<struct alsa_io *>(iodev);
1288   EXPECT_EQ(1, cras_card_config_get_volume_curve_for_control_called);
1289 
1290   // Node without controls or jacks.
1291   cras_alsa_jack_list_add_jack_for_section_result_jack =
1292     reinterpret_cast<struct cras_alsa_jack *>(1);
1293   section = ucm_section_create("Headphone", 0, CRAS_STREAM_OUTPUT,
1294       jack_name, "hctl");
1295   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1296   EXPECT_EQ(4, cras_card_config_get_volume_curve_for_control_called);
1297   EXPECT_EQ(1, cras_alsa_mixer_get_control_for_section_called);
1298   EXPECT_EQ(1, cras_iodev_add_node_called);
1299   EXPECT_EQ(1, cras_alsa_jack_list_add_jack_for_section_called);
1300   ucm_section_free_list(section);
1301 
1302   // Complete initialization, and make first node active.
1303   alsa_iodev_ucm_complete_init(iodev);
1304   EXPECT_EQ(SND_PCM_STREAM_PLAYBACK, aio->alsa_stream);
1305   EXPECT_EQ(0, cras_alsa_mixer_get_control_name_called);
1306   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1307   EXPECT_EQ(1, cras_alsa_jack_enable_ucm_called);
1308   EXPECT_EQ(0, ucm_set_enabled_called);
1309 
1310   alsa_iodev_destroy(iodev);
1311 }
1312 
TEST(AlsaOutputNode,InputsFromUCM)1313 TEST(AlsaOutputNode, InputsFromUCM) {
1314   struct alsa_io *aio;
1315   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1316   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1317   struct mixer_control *inputs[2];
1318   struct cras_iodev *iodev;
1319   static const char *jack_name = "TestCard - Headset Jack";
1320   int rc;
1321   struct ucm_section *section;
1322 
1323   ResetStubData();
1324   inputs[0] = reinterpret_cast<struct mixer_control *>(3);
1325   inputs[1] = reinterpret_cast<struct mixer_control *>(4);
1326   cras_alsa_mixer_list_inputs_outputs = inputs;
1327   cras_alsa_mixer_list_inputs_outputs_length = ARRAY_SIZE(inputs);
1328   cras_alsa_mixer_get_control_name_values[inputs[0]] = "Internal Mic";
1329   cras_alsa_mixer_get_control_name_values[inputs[1]] = "Mic";
1330 
1331   // Create the IO device.
1332   iodev = alsa_iodev_create_with_default_parameters(0, NULL,
1333                                                     ALSA_CARD_TYPE_INTERNAL, 1,
1334                                                     fake_mixer, fake_config,
1335                                                     fake_ucm,
1336                                                     CRAS_STREAM_INPUT);
1337   ASSERT_NE(iodev, (void *)NULL);
1338   aio = reinterpret_cast<struct alsa_io *>(iodev);
1339 
1340   // First node.
1341   cras_alsa_mixer_get_control_for_section_return_value = inputs[0];
1342   ucm_get_max_software_gain_ret_value = -1;
1343   section = ucm_section_create(INTERNAL_MICROPHONE, 0, CRAS_STREAM_INPUT,
1344                                NULL, NULL);
1345   ucm_section_add_coupled(section, "MIC-L", MIXER_NAME_VOLUME);
1346   ucm_section_add_coupled(section, "MIC-R", MIXER_NAME_VOLUME);
1347   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1348   ucm_section_free_list(section);
1349 
1350   // Add a second node (will use the same iodev).
1351   cras_alsa_mixer_get_control_name_called = 0;
1352   ucm_get_max_software_gain_ret_value = 0;
1353   ucm_get_max_software_gain_value = 2000;
1354   cras_alsa_jack_list_add_jack_for_section_result_jack =
1355       reinterpret_cast<struct cras_alsa_jack *>(1);
1356   cras_alsa_mixer_get_control_for_section_return_value = inputs[1];
1357   section = ucm_section_create("Mic", 0, CRAS_STREAM_INPUT, jack_name, "hctl");
1358   ucm_section_set_mixer_name(section, "Mic");
1359   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1360   ucm_section_free_list(section);
1361 
1362   // Jack plug of an unkonwn device should do nothing.
1363   cras_alsa_jack_get_mixer_input_ret = NULL;
1364   cras_alsa_jack_get_name_ret_value = "Some other jack";
1365   jack_input_plug_event(reinterpret_cast<struct cras_alsa_jack *>(4), 0, aio);
1366   EXPECT_EQ(0, cras_iodev_set_node_attr_called);
1367 
1368   // Simulate jack plug event.
1369   cras_alsa_jack_get_mixer_input_ret = inputs[1];
1370   cras_alsa_jack_get_name_ret_value = jack_name;
1371   jack_input_plug_event(reinterpret_cast<struct cras_alsa_jack *>(4), 0, aio);
1372   EXPECT_EQ(1, cras_iodev_set_node_attr_called);
1373 
1374   // Complete initialization, and make first node active.
1375   alsa_iodev_ucm_complete_init(iodev);
1376   EXPECT_EQ(SND_PCM_STREAM_CAPTURE, aio->alsa_stream);
1377   EXPECT_EQ(2, cras_alsa_jack_list_add_jack_for_section_called);
1378   EXPECT_EQ(2, cras_alsa_mixer_get_control_for_section_called);
1379   EXPECT_EQ(1, cras_alsa_mixer_get_control_name_called);
1380   EXPECT_EQ(1, sys_set_capture_gain_limits_called);
1381   EXPECT_EQ(2, cras_iodev_add_node_called);
1382   EXPECT_EQ(2, ucm_get_dma_period_for_dev_called);
1383   EXPECT_EQ(0, aio->dma_period_set_microsecs);
1384 
1385   aio->handle = (snd_pcm_t *)0x24;
1386 
1387   ResetStubData();
1388   rc = alsa_iodev_set_active_node(iodev, aio->base.nodes->next, 1);
1389   EXPECT_EQ(0, rc);
1390   EXPECT_EQ(1, alsa_mixer_set_capture_dBFS_called);
1391   EXPECT_EQ(inputs[1], alsa_mixer_set_capture_dBFS_input);
1392   EXPECT_EQ(0, alsa_mixer_set_capture_dBFS_value);
1393   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1394   EXPECT_EQ(1, cras_alsa_jack_enable_ucm_called);
1395   EXPECT_EQ(1, ucm_set_enabled_called);
1396   EXPECT_EQ(1, sys_set_capture_gain_limits_called);
1397   EXPECT_EQ(1, alsa_mixer_set_capture_mute_called);
1398   EXPECT_EQ(1, iodev->active_node->software_volume_needed);
1399   EXPECT_EQ(2000, iodev->active_node->max_software_gain);
1400 
1401   alsa_iodev_destroy(iodev);
1402 }
1403 
TEST(AlsaOutputNode,InputNoControlsUCM)1404 TEST(AlsaOutputNode, InputNoControlsUCM) {
1405   struct alsa_io *aio;
1406   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1407   struct cras_iodev *iodev;
1408   struct ucm_section *section;
1409 
1410   ResetStubData();
1411 
1412   // Create the IO device.
1413   iodev = alsa_iodev_create_with_default_parameters(1, NULL,
1414                                                     ALSA_CARD_TYPE_INTERNAL, 1,
1415                                                     fake_mixer, fake_config,
1416                                                     fake_ucm,
1417                                                     CRAS_STREAM_INPUT);
1418   ASSERT_NE(iodev, (void *)NULL);
1419   aio = reinterpret_cast<struct alsa_io *>(iodev);
1420 
1421   // Node without controls or jacks.
1422   section = ucm_section_create(INTERNAL_MICROPHONE, 1, CRAS_STREAM_INPUT,
1423                                NULL, NULL);
1424   // Device index doesn't match.
1425   EXPECT_EQ(-22, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1426   section->dev_idx = 0;
1427   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1428   EXPECT_EQ(1, cras_alsa_jack_list_add_jack_for_section_called);
1429   EXPECT_EQ(1, cras_alsa_mixer_get_control_for_section_called);
1430   EXPECT_EQ(0, cras_alsa_mixer_get_control_name_called);
1431   EXPECT_EQ(1, cras_iodev_add_node_called);
1432   ucm_section_free_list(section);
1433 
1434   // Complete initialization, and make first node active.
1435   alsa_iodev_ucm_complete_init(iodev);
1436   EXPECT_EQ(SND_PCM_STREAM_CAPTURE, aio->alsa_stream);
1437   EXPECT_EQ(0, cras_alsa_mixer_get_control_name_called);
1438   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1439   EXPECT_EQ(0, cras_alsa_jack_enable_ucm_called);
1440   EXPECT_EQ(1, ucm_set_enabled_called);
1441 
1442   alsa_iodev_destroy(iodev);
1443 }
1444 
TEST(AlsaOutputNode,InputFromJackUCM)1445 TEST(AlsaOutputNode, InputFromJackUCM) {
1446   struct alsa_io *aio;
1447   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1448   struct cras_iodev *iodev;
1449   static const char *jack_name = "TestCard - Headset Jack";
1450   struct ucm_section *section;
1451 
1452   ResetStubData();
1453 
1454   // Create the IO device.
1455   iodev = alsa_iodev_create_with_default_parameters(1, NULL,
1456                                                     ALSA_CARD_TYPE_INTERNAL, 1,
1457                                                     fake_mixer, fake_config,
1458                                                     fake_ucm,
1459                                                     CRAS_STREAM_INPUT);
1460   ASSERT_NE(iodev, (void *)NULL);
1461   aio = reinterpret_cast<struct alsa_io *>(iodev);
1462 
1463   // Node without controls or jacks.
1464   cras_alsa_jack_list_add_jack_for_section_result_jack =
1465       reinterpret_cast<struct cras_alsa_jack *>(1);
1466   section = ucm_section_create("Mic", 0, CRAS_STREAM_INPUT, jack_name, "hctl");
1467   ASSERT_EQ(0, alsa_iodev_ucm_add_nodes_and_jacks(iodev, section));
1468   EXPECT_EQ(1, cras_alsa_mixer_get_control_for_section_called);
1469   EXPECT_EQ(1, cras_iodev_add_node_called);
1470   EXPECT_EQ(1, cras_alsa_jack_list_add_jack_for_section_called);
1471   ucm_section_free_list(section);
1472 
1473   // Complete initialization, and make first node active.
1474   alsa_iodev_ucm_complete_init(iodev);
1475   EXPECT_EQ(SND_PCM_STREAM_CAPTURE, aio->alsa_stream);
1476   EXPECT_EQ(0, cras_alsa_mixer_get_control_name_called);
1477   EXPECT_EQ(1, cras_iodev_update_dsp_called);
1478   EXPECT_EQ(1, cras_alsa_jack_enable_ucm_called);
1479   EXPECT_EQ(0, ucm_set_enabled_called);
1480 
1481   alsa_iodev_destroy(iodev);
1482 }
1483 
TEST(AlsaOutputNode,AutoUnplugOutputNode)1484 TEST(AlsaOutputNode, AutoUnplugOutputNode) {
1485   struct alsa_io *aio;
1486   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1487   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1488   struct mixer_control *outputs[2];
1489   const struct cras_alsa_jack *jack = (struct cras_alsa_jack*)4;
1490 
1491   ResetStubData();
1492   outputs[0] = reinterpret_cast<struct mixer_control *>(5);
1493   outputs[1] = reinterpret_cast<struct mixer_control *>(6);
1494 
1495   cras_alsa_mixer_list_outputs_outputs = outputs;
1496   cras_alsa_mixer_list_outputs_outputs_length = ARRAY_SIZE(outputs);
1497 
1498   cras_alsa_mixer_get_control_name_values[outputs[0]] = INTERNAL_SPEAKER;
1499   cras_alsa_mixer_get_control_name_values[outputs[1]] = "Headphone";
1500   auto_unplug_output_node_ret = 1;
1501 
1502   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
1503       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, fake_ucm,
1504       CRAS_STREAM_OUTPUT);
1505 
1506   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
1507   EXPECT_EQ(3, cras_card_config_get_volume_curve_for_control_called);
1508   EXPECT_EQ(1, cras_alsa_mixer_list_outputs_called);
1509   EXPECT_EQ(2, cras_alsa_mixer_get_control_name_called);
1510 
1511   // Assert that the the internal speaker is plugged and other nodes aren't.
1512   ASSERT_NE(aio->base.nodes, (void *)NULL);
1513   EXPECT_EQ(aio->base.nodes->plugged, 1);
1514   ASSERT_NE(aio->base.nodes->next, (void *)NULL);
1515   EXPECT_EQ(aio->base.nodes->next->plugged, 0);
1516 
1517   // Plug headphone jack
1518   cras_alsa_jack_get_name_ret_value = "Headphone Jack";
1519   is_utf8_string_ret_value = 1;
1520   cras_alsa_jack_get_mixer_output_ret = outputs[1];
1521   cras_alsa_jack_list_create_cb(jack, 1, cras_alsa_jack_list_create_cb_data);
1522 
1523   // Assert internal speaker is auto unplugged
1524   EXPECT_EQ(aio->base.nodes->plugged, 0);
1525   EXPECT_EQ(aio->base.nodes->next->plugged, 1);
1526 
1527   alsa_iodev_destroy((struct cras_iodev *)aio);
1528 }
1529 
TEST(AlsaOutputNode,AutoUnplugInputNode)1530 TEST(AlsaOutputNode, AutoUnplugInputNode) {
1531   struct alsa_io *aio;
1532   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1533   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1534   struct mixer_control *inputs[2];
1535   const struct cras_alsa_jack *jack = (struct cras_alsa_jack*)4;
1536 
1537   ResetStubData();
1538   inputs[0] = reinterpret_cast<struct mixer_control *>(5);
1539   inputs[1] = reinterpret_cast<struct mixer_control *>(6);
1540 
1541   cras_alsa_mixer_list_inputs_outputs = inputs;
1542   cras_alsa_mixer_list_inputs_outputs_length = ARRAY_SIZE(inputs);
1543 
1544   cras_alsa_mixer_get_control_name_values[inputs[0]] = INTERNAL_MICROPHONE;
1545   cras_alsa_mixer_get_control_name_values[inputs[1]] = "Mic";
1546   auto_unplug_input_node_ret = 1;
1547 
1548   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
1549       0, NULL, ALSA_CARD_TYPE_INTERNAL, 1, fake_mixer, fake_config, fake_ucm,
1550       CRAS_STREAM_INPUT);
1551   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
1552   EXPECT_EQ(1, cras_alsa_mixer_list_inputs_called);
1553   EXPECT_EQ(2, cras_alsa_mixer_get_control_name_called);
1554 
1555   // Assert that the the internal speaker is plugged and other nodes aren't.
1556   ASSERT_NE(aio->base.nodes, (void *)NULL);
1557   EXPECT_EQ(aio->base.nodes->plugged, 1);
1558   ASSERT_NE(aio->base.nodes->next, (void *)NULL);
1559   EXPECT_EQ(aio->base.nodes->next->plugged, 0);
1560 
1561   // Plug headphone jack
1562   cras_alsa_jack_get_name_ret_value = "Mic Jack";
1563   is_utf8_string_ret_value = 1;
1564   cras_alsa_jack_get_mixer_input_ret = inputs[1];
1565   cras_alsa_jack_list_create_cb(jack, 1, cras_alsa_jack_list_create_cb_data);
1566 
1567   // Assert internal speaker is auto unplugged
1568   EXPECT_EQ(aio->base.nodes->plugged, 0);
1569   EXPECT_EQ(aio->base.nodes->next->plugged, 1);
1570 
1571   alsa_iodev_destroy((struct cras_iodev *)aio);
1572 }
1573 
TEST(AlsaInitNode,SetNodeInitialState)1574 TEST(AlsaInitNode, SetNodeInitialState) {
1575   struct cras_ionode node;
1576   struct cras_iodev dev;
1577 
1578   memset(&dev, 0, sizeof(dev));
1579   memset(&node, 0, sizeof(node));
1580   node.dev = &dev;
1581   strcpy(node.name, "Unknown");
1582   dev.direction = CRAS_STREAM_OUTPUT;
1583   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1584   ASSERT_EQ(0, node.plugged);
1585   ASSERT_EQ(0, node.plugged_time.tv_sec);
1586   ASSERT_EQ(CRAS_NODE_TYPE_UNKNOWN, node.type);
1587   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1588 
1589   memset(&node, 0, sizeof(node));
1590   node.dev = &dev;
1591   strcpy(node.name, "Speaker");
1592   dev.direction = CRAS_STREAM_OUTPUT;
1593   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1594   ASSERT_EQ(1, node.plugged);
1595   ASSERT_GT(node.plugged_time.tv_sec, 0);
1596   ASSERT_EQ(CRAS_NODE_TYPE_INTERNAL_SPEAKER, node.type);
1597   ASSERT_EQ(NODE_POSITION_INTERNAL, node.position);
1598 
1599   memset(&node, 0, sizeof(node));
1600   node.dev = &dev;
1601   strcpy(node.name, "Internal Mic");
1602   dev.direction = CRAS_STREAM_INPUT;
1603   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1604   ASSERT_EQ(1, node.plugged);
1605   ASSERT_GT(node.plugged_time.tv_sec, 0);
1606   ASSERT_EQ(CRAS_NODE_TYPE_MIC, node.type);
1607   ASSERT_EQ(NODE_POSITION_INTERNAL, node.position);
1608 
1609   memset(&node, 0, sizeof(node));
1610   node.dev = &dev;
1611   strcpy(node.name, "HDMI");
1612   dev.direction = CRAS_STREAM_OUTPUT;
1613   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1614   ASSERT_EQ(0, node.plugged);
1615   ASSERT_EQ(0, node.plugged_time.tv_sec);
1616   ASSERT_EQ(CRAS_NODE_TYPE_HDMI, node.type);
1617   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1618 
1619   memset(&node, 0, sizeof(node));
1620   node.dev = &dev;
1621   strcpy(node.name, "IEC958");
1622   dev.direction = CRAS_STREAM_OUTPUT;
1623   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1624   ASSERT_EQ(0, node.plugged);
1625   ASSERT_EQ(CRAS_NODE_TYPE_HDMI, node.type);
1626   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1627 
1628   memset(&node, 0, sizeof(node));
1629   node.dev = &dev;
1630   strcpy(node.name, "HDMI Jack");
1631   dev.direction = CRAS_STREAM_OUTPUT;
1632   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1633   ASSERT_EQ(0, node.plugged);
1634   ASSERT_EQ(CRAS_NODE_TYPE_HDMI, node.type);
1635   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1636 
1637   memset(&node, 0, sizeof(node));
1638   node.dev = &dev;
1639   strcpy(node.name, "Something HDMI Jack");
1640   dev.direction = CRAS_STREAM_OUTPUT;
1641   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1642   ASSERT_EQ(0, node.plugged);
1643   ASSERT_EQ(CRAS_NODE_TYPE_HDMI, node.type);
1644   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1645 
1646   memset(&node, 0, sizeof(node));
1647   node.dev = &dev;
1648   strcpy(node.name, "Headphone");
1649   dev.direction = CRAS_STREAM_OUTPUT;
1650   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1651   ASSERT_EQ(0, node.plugged);
1652   ASSERT_EQ(CRAS_NODE_TYPE_HEADPHONE, node.type);
1653   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1654 
1655   memset(&node, 0, sizeof(node));
1656   node.dev = &dev;
1657   strcpy(node.name, "Headphone Jack");
1658   dev.direction = CRAS_STREAM_OUTPUT;
1659   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1660   ASSERT_EQ(0, node.plugged);
1661   ASSERT_EQ(CRAS_NODE_TYPE_HEADPHONE, node.type);
1662   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1663 
1664   memset(&node, 0, sizeof(node));
1665   node.dev = &dev;
1666   strcpy(node.name, "Mic");
1667   dev.direction = CRAS_STREAM_INPUT;
1668   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1669   ASSERT_EQ(0, node.plugged);
1670   ASSERT_EQ(CRAS_NODE_TYPE_MIC, node.type);
1671   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1672 
1673   memset(&node, 0, sizeof(node));
1674   node.dev = &dev;
1675   strcpy(node.name, "Front Mic");
1676   dev.direction = CRAS_STREAM_INPUT;
1677   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1678   ASSERT_EQ(1, node.plugged);
1679   ASSERT_EQ(CRAS_NODE_TYPE_MIC, node.type);
1680   ASSERT_EQ(NODE_POSITION_FRONT, node.position);
1681 
1682   memset(&node, 0, sizeof(node));
1683   node.dev = &dev;
1684   strcpy(node.name, "Rear Mic");
1685   dev.direction = CRAS_STREAM_INPUT;
1686   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1687   ASSERT_EQ(1, node.plugged);
1688   ASSERT_EQ(CRAS_NODE_TYPE_MIC, node.type);
1689   ASSERT_EQ(NODE_POSITION_REAR, node.position);
1690 
1691   memset(&node, 0, sizeof(node));
1692   node.dev = &dev;
1693   strcpy(node.name, "Mic Jack");
1694   dev.direction = CRAS_STREAM_INPUT;
1695   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1696   ASSERT_EQ(0, node.plugged);
1697   ASSERT_EQ(CRAS_NODE_TYPE_MIC, node.type);
1698   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1699 
1700   memset(&node, 0, sizeof(node));
1701   node.dev = &dev;
1702   strcpy(node.name, "Unknown");
1703   dev.direction = CRAS_STREAM_OUTPUT;
1704   set_node_initial_state(&node, ALSA_CARD_TYPE_USB);
1705   ASSERT_EQ(0, node.plugged);
1706   ASSERT_EQ(CRAS_NODE_TYPE_USB, node.type);
1707   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1708 
1709   memset(&node, 0, sizeof(node));
1710   node.dev = &dev;
1711   dev.direction = CRAS_STREAM_INPUT;
1712   strcpy(node.name, "DAISY-I2S Mic Jack");
1713   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1714   ASSERT_EQ(0, node.plugged);
1715   ASSERT_EQ(CRAS_NODE_TYPE_MIC, node.type);
1716   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1717   // Node name is changed to "Mic".
1718   ASSERT_EQ(0, strcmp(node.name, "Mic"));
1719 
1720   memset(&node, 0, sizeof(node));
1721   node.dev = &dev;
1722   dev.direction = CRAS_STREAM_OUTPUT;
1723   strcpy(node.name, "DAISY-I2S Headphone Jack");
1724   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1725   ASSERT_EQ(0, node.plugged);
1726   ASSERT_EQ(CRAS_NODE_TYPE_HEADPHONE, node.type);
1727   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1728   // Node name is changed to "Headphone".
1729   ASSERT_EQ(0, strcmp(node.name, "Headphone"));
1730 
1731   memset(&node, 0, sizeof(node));
1732   node.dev = &dev;
1733   strcpy(node.name, "Speaker");
1734   dev.direction = CRAS_STREAM_OUTPUT;
1735   set_node_initial_state(&node, ALSA_CARD_TYPE_USB);
1736   ASSERT_EQ(1, node.plugged);
1737   ASSERT_GT(node.plugged_time.tv_sec, 0);
1738   ASSERT_EQ(CRAS_NODE_TYPE_USB, node.type);
1739   ASSERT_EQ(NODE_POSITION_EXTERNAL, node.position);
1740 
1741   memset(&node, 0, sizeof(node));
1742   node.dev = &dev;
1743   strcpy(node.name, "Haptic");
1744   dev.direction = CRAS_STREAM_OUTPUT;
1745   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1746   ASSERT_EQ(1, node.plugged);
1747   ASSERT_GT(node.plugged_time.tv_sec, 0);
1748   ASSERT_EQ(CRAS_NODE_TYPE_HAPTIC, node.type);
1749   ASSERT_EQ(NODE_POSITION_INTERNAL, node.position);
1750 
1751   memset(&node, 0, sizeof(node));
1752   node.dev = &dev;
1753   strcpy(node.name, "Rumbler");
1754   dev.direction = CRAS_STREAM_OUTPUT;
1755   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1756   ASSERT_EQ(1, node.plugged);
1757   ASSERT_GT(node.plugged_time.tv_sec, 0);
1758   ASSERT_EQ(CRAS_NODE_TYPE_HAPTIC, node.type);
1759   ASSERT_EQ(NODE_POSITION_INTERNAL, node.position);
1760 }
1761 
TEST(AlsaInitNode,SetNodeInitialStateDropInvalidUTF8NodeName)1762 TEST(AlsaInitNode, SetNodeInitialStateDropInvalidUTF8NodeName) {
1763   struct cras_ionode node;
1764   struct cras_iodev dev;
1765 
1766   memset(&dev, 0, sizeof(dev));
1767   memset(&node, 0, sizeof(node));
1768   node.dev = &dev;
1769 
1770   memset(&node, 0, sizeof(node));
1771   node.dev = &dev;
1772   strcpy(node.name, "Something USB");
1773   //0xfe can not appear in a valid UTF-8 string.
1774   node.name[0] = 0xfe;
1775   is_utf8_string_ret_value = 0;
1776   dev.direction = CRAS_STREAM_OUTPUT;
1777   set_node_initial_state(&node, ALSA_CARD_TYPE_USB);
1778   ASSERT_EQ(CRAS_NODE_TYPE_USB, node.type);
1779   ASSERT_STREQ("USB", node.name);
1780 
1781   memset(&node, 0, sizeof(node));
1782   node.dev = &dev;
1783   strcpy(node.name, "Something HDMI Jack");
1784   //0xfe can not appear in a valid UTF-8 string.
1785   node.name[0] = 0xfe;
1786   is_utf8_string_ret_value = 0;
1787   dev.direction = CRAS_STREAM_OUTPUT;
1788   set_node_initial_state(&node, ALSA_CARD_TYPE_INTERNAL);
1789   ASSERT_EQ(CRAS_NODE_TYPE_HDMI, node.type);
1790   ASSERT_STREQ("HDMI", node.name);
1791 }
1792 
TEST(AlsaIoInit,HDMIJackUpdateInvalidUTF8MonitorName)1793 TEST(AlsaIoInit, HDMIJackUpdateInvalidUTF8MonitorName) {
1794   struct alsa_io *aio;
1795   struct cras_alsa_mixer * const fake_mixer = (struct cras_alsa_mixer*)2;
1796   struct cras_use_case_mgr * const fake_ucm = (struct cras_use_case_mgr*)3;
1797   const struct cras_alsa_jack *jack = (struct cras_alsa_jack*)4;
1798 
1799   ResetStubData();
1800   aio = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
1801       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, fake_ucm,
1802       CRAS_STREAM_OUTPUT);
1803   ASSERT_EQ(0, alsa_iodev_legacy_complete_init((struct cras_iodev *)aio));
1804 
1805   // Prepare the stub data such that the jack will be identified as an
1806   // HDMI jack, and thus the callback creates an HDMI node.
1807   cras_alsa_jack_get_name_ret_value = "HDMI Jack";
1808   // Set the jack name updated from monitor to be an invalid UTF8 string.
1809   cras_alsa_jack_update_monitor_fake_name = "\xfeomething";
1810   is_utf8_string_ret_value = 0;
1811 
1812   // Add the jack node.
1813   cras_alsa_jack_list_create_cb(jack, 1, cras_alsa_jack_list_create_cb_data);
1814 
1815   EXPECT_EQ(2, cras_alsa_jack_get_name_called);
1816   ASSERT_EQ(CRAS_NODE_TYPE_HDMI, aio->base.nodes->next->type);
1817   // The node name should be "HDMI".
1818   ASSERT_STREQ("HDMI", aio->base.nodes->next->name);
1819 
1820   alsa_iodev_destroy((struct cras_iodev *)aio);
1821 }
1822 
1823 //  Test thread add/rm stream, open_alsa, and iodev config.
1824 class AlsaVolumeMuteSuite : public testing::Test {
1825   protected:
SetUp()1826     virtual void SetUp() {
1827       ResetStubData();
1828       output_control_ = reinterpret_cast<struct mixer_control *>(10);
1829       cras_alsa_mixer_list_outputs_outputs = &output_control_;
1830       cras_alsa_mixer_list_outputs_outputs_length = 1;
1831       cras_alsa_mixer_get_control_name_values[output_control_] = "Speaker";
1832       cras_alsa_mixer_list_outputs_outputs_length = 1;
1833       aio_output_ = (struct alsa_io *)alsa_iodev_create_with_default_parameters(
1834           0, NULL,
1835           ALSA_CARD_TYPE_INTERNAL, 1,
1836           fake_mixer, fake_config, NULL,
1837           CRAS_STREAM_OUTPUT);
1838       alsa_iodev_legacy_complete_init((struct cras_iodev *)aio_output_);
1839       EXPECT_EQ(2, cras_card_config_get_volume_curve_for_control_called);
1840 
1841       struct cras_ionode *node;
1842       int count = 0;
1843       DL_FOREACH(aio_output_->base.nodes, node) {
1844         printf("node %d \n", count);
1845       }
1846       aio_output_->base.direction = CRAS_STREAM_OUTPUT;
1847       fmt_.frame_rate = 44100;
1848       fmt_.num_channels = 2;
1849       fmt_.format = SND_PCM_FORMAT_S16_LE;
1850       aio_output_->base.format = &fmt_;
1851       cras_alsa_get_avail_frames_ret = -1;
1852     }
1853 
TearDown()1854     virtual void TearDown() {
1855       alsa_iodev_destroy((struct cras_iodev *)aio_output_);
1856       cras_alsa_get_avail_frames_ret = 0;
1857     }
1858 
1859   struct mixer_control *output_control_;
1860   struct alsa_io *aio_output_;
1861   struct cras_audio_format fmt_;
1862 };
1863 
TEST_F(AlsaVolumeMuteSuite,GetDefaultVolumeCurve)1864 TEST_F(AlsaVolumeMuteSuite, GetDefaultVolumeCurve) {
1865   int rc;
1866   struct cras_audio_format *fmt;
1867 
1868   fmt = (struct cras_audio_format *)malloc(sizeof(*fmt));
1869   memcpy(fmt, &fmt_, sizeof(fmt_));
1870   aio_output_->base.format = fmt;
1871   aio_output_->handle = (snd_pcm_t *)0x24;
1872 
1873   rc = aio_output_->base.configure_dev(&aio_output_->base);
1874   ASSERT_EQ(0, rc);
1875   EXPECT_EQ(&default_curve, fake_get_dBFS_volume_curve_val);
1876 
1877   aio_output_->base.set_volume(&aio_output_->base);
1878   EXPECT_EQ(&default_curve, fake_get_dBFS_volume_curve_val);
1879   free(fmt);
1880 }
1881 
TEST_F(AlsaVolumeMuteSuite,GetVolumeCurveFromNode)1882 TEST_F(AlsaVolumeMuteSuite, GetVolumeCurveFromNode)
1883 {
1884   int rc;
1885   struct cras_audio_format *fmt;
1886   struct cras_alsa_jack *jack = (struct cras_alsa_jack*)4;
1887   struct cras_ionode *node;
1888   struct cras_volume_curve hp_curve = {
1889     .get_dBFS = fake_get_dBFS,
1890   };
1891 
1892   fmt = (struct cras_audio_format *)malloc(sizeof(*fmt));
1893   memcpy(fmt, &fmt_, sizeof(fmt_));
1894   aio_output_->base.format = fmt;
1895   aio_output_->handle = (snd_pcm_t *)0x24;
1896 
1897   // Headphone jack plugged and has its own volume curve.
1898   cras_alsa_jack_get_mixer_output_ret = NULL;
1899   cras_alsa_jack_get_name_ret_value = "Headphone";
1900   cras_card_config_get_volume_curve_vals["Headphone"] = &hp_curve;
1901   cras_alsa_jack_list_create_cb(jack, 1, cras_alsa_jack_list_create_cb_data);
1902   EXPECT_EQ(1, cras_alsa_jack_update_node_type_called);
1903   EXPECT_EQ(3, cras_card_config_get_volume_curve_for_control_called);
1904 
1905   // Switch to node 'Headphone'.
1906   node = aio_output_->base.nodes->next;
1907   aio_output_->base.active_node = node;
1908 
1909   rc = aio_output_->base.configure_dev(&aio_output_->base);
1910   ASSERT_EQ(0, rc);
1911   EXPECT_EQ(&hp_curve, fake_get_dBFS_volume_curve_val);
1912 
1913   aio_output_->base.set_volume(&aio_output_->base);
1914   EXPECT_EQ(&hp_curve, fake_get_dBFS_volume_curve_val);
1915   free(fmt);
1916 }
1917 
TEST_F(AlsaVolumeMuteSuite,SetVolume)1918 TEST_F(AlsaVolumeMuteSuite, SetVolume) {
1919   int rc;
1920   struct cras_audio_format *fmt;
1921   const size_t fake_system_volume = 55;
1922   const size_t fake_system_volume_dB = (fake_system_volume - 100) * 100;
1923 
1924   fmt = (struct cras_audio_format *)malloc(sizeof(*fmt));
1925   memcpy(fmt, &fmt_, sizeof(fmt_));
1926   aio_output_->base.format = fmt;
1927   aio_output_->handle = (snd_pcm_t *)0x24;
1928 
1929   aio_output_->num_underruns = 3; //  Something non-zero.
1930   sys_get_volume_return_value = fake_system_volume;
1931   rc = aio_output_->base.configure_dev(&aio_output_->base);
1932   ASSERT_EQ(0, rc);
1933   EXPECT_EQ(1, alsa_mixer_set_dBFS_called);
1934   EXPECT_EQ(fake_system_volume_dB, alsa_mixer_set_dBFS_value);
1935 
1936   alsa_mixer_set_dBFS_called = 0;
1937   alsa_mixer_set_dBFS_value = 0;
1938   sys_get_volume_return_value = 50;
1939   sys_get_volume_called = 0;
1940   aio_output_->base.set_volume(&aio_output_->base);
1941   EXPECT_EQ(1, sys_get_volume_called);
1942   EXPECT_EQ(1, alsa_mixer_set_dBFS_called);
1943   EXPECT_EQ(-5000, alsa_mixer_set_dBFS_value);
1944   EXPECT_EQ(output_control_, alsa_mixer_set_dBFS_output);
1945 
1946   alsa_mixer_set_dBFS_called = 0;
1947   alsa_mixer_set_dBFS_value = 0;
1948   sys_get_volume_return_value = 0;
1949   sys_get_volume_called = 0;
1950   aio_output_->base.set_volume(&aio_output_->base);
1951   EXPECT_EQ(1, sys_get_volume_called);
1952   EXPECT_EQ(1, alsa_mixer_set_dBFS_called);
1953   EXPECT_EQ(-10000, alsa_mixer_set_dBFS_value);
1954 
1955   sys_get_volume_return_value = 80;
1956   aio_output_->base.active_node->volume = 90;
1957   aio_output_->base.set_volume(&aio_output_->base);
1958   EXPECT_EQ(-3000, alsa_mixer_set_dBFS_value);
1959 
1960   // close the dev.
1961   rc = aio_output_->base.close_dev(&aio_output_->base);
1962   EXPECT_EQ(0, rc);
1963   EXPECT_EQ((void *)NULL, aio_output_->handle);
1964 
1965   free(fmt);
1966 }
1967 
TEST_F(AlsaVolumeMuteSuite,SetMute)1968 TEST_F(AlsaVolumeMuteSuite, SetMute) {
1969   int muted;
1970 
1971   aio_output_->handle = (snd_pcm_t *)0x24;
1972 
1973   // Test mute.
1974   ResetStubData();
1975   muted = 1;
1976 
1977   sys_get_mute_return_value = muted;
1978 
1979   aio_output_->base.set_mute(&aio_output_->base);
1980 
1981   EXPECT_EQ(1, sys_get_mute_called);
1982   EXPECT_EQ(1, alsa_mixer_set_mute_called);
1983   EXPECT_EQ(muted, alsa_mixer_set_mute_value);
1984   EXPECT_EQ(output_control_, alsa_mixer_set_mute_output);
1985 
1986   // Test unmute.
1987   ResetStubData();
1988   muted = 0;
1989 
1990   sys_get_mute_return_value = muted;
1991 
1992   aio_output_->base.set_mute(&aio_output_->base);
1993 
1994   EXPECT_EQ(1, sys_get_mute_called);
1995   EXPECT_EQ(1, alsa_mixer_set_mute_called);
1996   EXPECT_EQ(muted, alsa_mixer_set_mute_value);
1997   EXPECT_EQ(output_control_, alsa_mixer_set_mute_output);
1998 }
1999 
2000 //  Test free run.
2001 class AlsaFreeRunTestSuite: public testing::Test {
2002   protected:
SetUp()2003     virtual void SetUp() {
2004       ResetStubData();
2005       memset(&aio, 0, sizeof(aio));
2006       fmt_.format = SND_PCM_FORMAT_S16_LE;
2007       fmt_.frame_rate = 48000;
2008       fmt_.num_channels = 2;
2009       aio.base.frames_queued = frames_queued;
2010       aio.base.direction = CRAS_STREAM_OUTPUT;
2011       aio.base.format = &fmt_;
2012       aio.base.buffer_size = BUFFER_SIZE;
2013       aio.base.min_cb_level = 240;
2014       aio.base.min_buffer_level = 0;
2015       aio.filled_zeros_for_draining = 0;
2016       cras_alsa_mmap_begin_buffer = (uint8_t *)calloc(
2017         BUFFER_SIZE * 2 * 2,
2018         sizeof(*cras_alsa_mmap_begin_buffer));
2019       memset(cras_alsa_mmap_begin_buffer, 0xff,
2020              sizeof(*cras_alsa_mmap_begin_buffer));
2021     }
2022 
TearDown()2023     virtual void TearDown() {
2024       free(cras_alsa_mmap_begin_buffer);
2025     }
2026 
2027   struct alsa_io aio;
2028   struct cras_audio_format fmt_;
2029 };
2030 
TEST_F(AlsaFreeRunTestSuite,FillWholeBufferWithZeros)2031 TEST_F(AlsaFreeRunTestSuite, FillWholeBufferWithZeros) {
2032   int rc;
2033   int16_t *zeros;
2034 
2035 
2036   rc = fill_whole_buffer_with_zeros(&aio.base);
2037 
2038   EXPECT_EQ(0, rc);
2039   zeros = (int16_t *)calloc(BUFFER_SIZE * 2, sizeof(*zeros));
2040   EXPECT_EQ(0, memcmp(zeros, cras_alsa_mmap_begin_buffer, BUFFER_SIZE * 2 * 2));
2041 
2042   free(zeros);
2043 }
2044 
TEST_F(AlsaFreeRunTestSuite,EnterFreeRunAlreadyFreeRunning)2045 TEST_F(AlsaFreeRunTestSuite, EnterFreeRunAlreadyFreeRunning) {
2046   int rc;
2047 
2048   // Device is in free run state, no need to fill zeros or fill whole buffer.
2049   aio.is_free_running = 1;
2050 
2051   rc = no_stream(&aio.base, 1);
2052 
2053   EXPECT_EQ(0, rc);
2054   EXPECT_EQ(0, cras_alsa_mmap_get_whole_buffer_called);
2055   EXPECT_EQ(0, cras_iodev_fill_odev_zeros_called);
2056   EXPECT_EQ(0, cras_iodev_fill_odev_zeros_frames);
2057 }
2058 
TEST_F(AlsaFreeRunTestSuite,EnterFreeRunNotDrainedYetNeedToFillZeros)2059 TEST_F(AlsaFreeRunTestSuite, EnterFreeRunNotDrainedYetNeedToFillZeros) {
2060   int rc, real_hw_level;
2061   struct timespec hw_tstamp;
2062   // Device is not in free run state. There are still valid samples to play.
2063   // The number of valid samples is less than min_cb_level * 2.
2064   // Need to fill zeros targeting min_cb_level * 2 = 480.
2065   // The number of zeros to be filled is 480 - 200 = 280.
2066   real_hw_level = 200;
2067   cras_alsa_get_avail_frames_avail = BUFFER_SIZE - real_hw_level;
2068 
2069   rc = aio.base.frames_queued(&aio.base, &hw_tstamp);
2070   EXPECT_EQ(200, rc);
2071 
2072   rc = no_stream(&aio.base, 1);
2073 
2074   EXPECT_EQ(0, rc);
2075   EXPECT_EQ(0, cras_alsa_mmap_get_whole_buffer_called);
2076   EXPECT_EQ(1, cras_iodev_fill_odev_zeros_called);
2077   EXPECT_EQ(280, cras_iodev_fill_odev_zeros_frames);
2078   EXPECT_EQ(280, aio.filled_zeros_for_draining);
2079   EXPECT_EQ(0, aio.is_free_running);
2080 }
2081 
TEST_F(AlsaFreeRunTestSuite,EnterFreeRunNotDrainedYetNoNeedToFillZeros)2082 TEST_F(AlsaFreeRunTestSuite, EnterFreeRunNotDrainedYetNoNeedToFillZeros) {
2083   int rc, real_hw_level;
2084 
2085   // Device is not in free run state. There are still valid samples to play.
2086   // The number of valid samples is more than min_cb_level * 2.
2087   // No need to fill zeros.
2088   real_hw_level = 500;
2089   cras_alsa_get_avail_frames_avail = BUFFER_SIZE - real_hw_level;
2090 
2091   rc = no_stream(&aio.base, 1);
2092 
2093   EXPECT_EQ(0, rc);
2094   EXPECT_EQ(0, cras_alsa_mmap_get_whole_buffer_called);
2095   EXPECT_EQ(0, cras_iodev_fill_odev_zeros_called);
2096   EXPECT_EQ(0, aio.is_free_running);
2097 }
2098 
TEST_F(AlsaFreeRunTestSuite,EnterFreeRunDrained)2099 TEST_F(AlsaFreeRunTestSuite, EnterFreeRunDrained) {
2100   int rc, real_hw_level;
2101 
2102   // Device is not in free run state. There are still valid samples to play.
2103   // The number of valid samples is less than filled zeros.
2104   // Should enter free run state and fill whole buffer with zeros.
2105   real_hw_level = 40;
2106   cras_alsa_get_avail_frames_avail = BUFFER_SIZE - real_hw_level;
2107   aio.filled_zeros_for_draining = 100;
2108 
2109   rc = no_stream(&aio.base, 1);
2110 
2111   EXPECT_EQ(0, rc);
2112   EXPECT_EQ(1, cras_alsa_mmap_get_whole_buffer_called);
2113   EXPECT_EQ(0, cras_iodev_fill_odev_zeros_called);
2114   EXPECT_EQ(1, aio.is_free_running);
2115 }
2116 
TEST_F(AlsaFreeRunTestSuite,EnterFreeRunNoSamples)2117 TEST_F(AlsaFreeRunTestSuite, EnterFreeRunNoSamples) {
2118   int rc, real_hw_level;
2119 
2120   // Device is not in free run state. There is no sample to play.
2121   // Should enter free run state and fill whole buffer with zeros.
2122   real_hw_level = 0;
2123   cras_alsa_get_avail_frames_avail = BUFFER_SIZE - real_hw_level;
2124 
2125   rc = no_stream(&aio.base, 1);
2126 
2127   EXPECT_EQ(0, rc);
2128   EXPECT_EQ(1, cras_alsa_mmap_get_whole_buffer_called);
2129   EXPECT_EQ(0, cras_iodev_fill_odev_zeros_called);
2130   EXPECT_EQ(1, aio.is_free_running);
2131 }
2132 
TEST_F(AlsaFreeRunTestSuite,OutputShouldWake)2133 TEST_F(AlsaFreeRunTestSuite, OutputShouldWake) {
2134 
2135   aio.is_free_running = 1;
2136 
2137   EXPECT_EQ(0, output_should_wake(&aio.base));
2138 
2139   aio.is_free_running = 0;
2140   aio.base.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
2141   EXPECT_EQ(1, output_should_wake(&aio.base));
2142 
2143   aio.base.state = CRAS_IODEV_STATE_NORMAL_RUN;
2144   EXPECT_EQ(1, output_should_wake(&aio.base));
2145 
2146   aio.base.state = CRAS_IODEV_STATE_OPEN;
2147   EXPECT_EQ(0, output_should_wake(&aio.base));
2148 }
2149 
TEST_F(AlsaFreeRunTestSuite,LeaveFreeRunNotInFreeRunMoreRemain)2150 TEST_F(AlsaFreeRunTestSuite, LeaveFreeRunNotInFreeRunMoreRemain) {
2151   int rc, real_hw_level;
2152 
2153   // Compare min_buffer_level + min_cb_level with valid samples left.
2154   // 240 + 512 < 900 - 100, so we will get 900 - 100 in appl_ptr_ahead.
2155 
2156   aio.is_free_running = 0;
2157   aio.filled_zeros_for_draining = 100;
2158   aio.base.min_buffer_level = 512;
2159   real_hw_level = 900;
2160   cras_alsa_get_avail_frames_avail = BUFFER_SIZE - real_hw_level;
2161 
2162   rc = no_stream(&aio.base, 0);
2163 
2164   EXPECT_EQ(0, rc);
2165   EXPECT_EQ(1, cras_alsa_resume_appl_ptr_called);
2166   EXPECT_EQ(800, cras_alsa_resume_appl_ptr_ahead);
2167   EXPECT_EQ(0, cras_iodev_fill_odev_zeros_frames);
2168   EXPECT_EQ(0, aio.is_free_running);
2169   EXPECT_EQ(0, aio.filled_zeros_for_draining);
2170 }
2171 
TEST_F(AlsaFreeRunTestSuite,LeaveFreeRunNotInFreeRunLessRemain)2172 TEST_F(AlsaFreeRunTestSuite, LeaveFreeRunNotInFreeRunLessRemain) {
2173   int rc, real_hw_level;
2174 
2175   // Compare min_buffer_level + min_cb_level with valid samples left.
2176   // 240 + 256 > 400 - 500, so we will get 240 + 256 in appl_ptr_ahead.
2177   // And it will fill 240 + 256 - 400 = 96 zeros frames into device.
2178 
2179   aio.is_free_running = 0;
2180   aio.filled_zeros_for_draining = 500;
2181   aio.base.min_buffer_level = 256;
2182   real_hw_level = 400;
2183   cras_alsa_get_avail_frames_avail = BUFFER_SIZE - real_hw_level;
2184 
2185   rc = no_stream(&aio.base, 0);
2186 
2187   EXPECT_EQ(0, rc);
2188   EXPECT_EQ(1, cras_alsa_resume_appl_ptr_called);
2189   EXPECT_EQ(aio.base.min_buffer_level + aio.base.min_cb_level,
2190             cras_alsa_resume_appl_ptr_ahead);
2191   EXPECT_EQ(96, cras_iodev_fill_odev_zeros_frames);
2192   EXPECT_EQ(0, aio.is_free_running);
2193   EXPECT_EQ(0, aio.filled_zeros_for_draining);
2194 }
2195 
TEST_F(AlsaFreeRunTestSuite,LeaveFreeRunInFreeRun)2196 TEST_F(AlsaFreeRunTestSuite, LeaveFreeRunInFreeRun) {
2197   int rc;
2198 
2199   aio.is_free_running = 1;
2200   aio.filled_zeros_for_draining = 100;
2201   aio.base.min_buffer_level = 512;
2202 
2203   rc = no_stream(&aio.base, 0);
2204 
2205   EXPECT_EQ(0, rc);
2206   EXPECT_EQ(1, cras_alsa_resume_appl_ptr_called);
2207   EXPECT_EQ(aio.base.min_buffer_level + aio.base.min_cb_level,
2208             cras_alsa_resume_appl_ptr_ahead);
2209   EXPECT_EQ(0, aio.is_free_running);
2210   EXPECT_EQ(0, aio.filled_zeros_for_draining);
2211 }
2212 
2213 // Reuse AlsaFreeRunTestSuite for output underrun handling because they are
2214 // similar.
TEST_F(AlsaFreeRunTestSuite,OutputUnderrun)2215 TEST_F(AlsaFreeRunTestSuite, OutputUnderrun) {
2216   int rc;
2217   int16_t *zeros;
2218 
2219   aio.num_underruns = 0;
2220 
2221   // Ask alsa_io to handle output underrun.
2222   rc = alsa_output_underrun(&aio.base);
2223   EXPECT_EQ(0, rc);
2224   EXPECT_EQ(1, aio.num_underruns);
2225 
2226   // mmap buffer should be filled with zeros.
2227   zeros = (int16_t *)calloc(BUFFER_SIZE * 2, sizeof(*zeros));
2228   EXPECT_EQ(0, memcmp(zeros, cras_alsa_mmap_begin_buffer, BUFFER_SIZE * 2 * 2));
2229 
2230   // appl_ptr should be moved to min_buffer_level + min_cb_level ahead of
2231   // hw_ptr.
2232   EXPECT_EQ(1, cras_alsa_resume_appl_ptr_called);
2233   EXPECT_EQ(aio.base.min_buffer_level + aio.base.min_cb_level,
2234             cras_alsa_resume_appl_ptr_ahead);
2235 
2236   free(zeros);
2237 }
2238 
TEST(AlsaHotwordNode,HotwordTriggeredSendMessage)2239 TEST(AlsaHotwordNode, HotwordTriggeredSendMessage) {
2240   struct cras_iodev *iodev;
2241   struct cras_audio_format format;
2242   struct alsa_input_node alsa_node;
2243   struct cras_ionode *node = &alsa_node.base;
2244   int rc;
2245 
2246   ResetStubData();
2247   iodev = alsa_iodev_create_with_default_parameters(
2248       0, NULL, ALSA_CARD_TYPE_INTERNAL, 0, fake_mixer, fake_config, NULL,
2249       CRAS_STREAM_INPUT);
2250   format.frame_rate = 16000;
2251   format.num_channels = 1;
2252   cras_iodev_set_format(iodev, &format);
2253 
2254   memset(&alsa_node, 0, sizeof(alsa_node));
2255   node->dev = iodev;
2256   strcpy(node->name, "Wake on Voice");
2257   set_node_initial_state(node, ALSA_CARD_TYPE_INTERNAL);
2258   EXPECT_EQ(CRAS_NODE_TYPE_HOTWORD, node->type);
2259 
2260   iodev->active_node = node;
2261   iodev->open_dev(iodev);
2262   rc = iodev->configure_dev(iodev);
2263   free(fake_format);
2264   ASSERT_EQ(0, rc);
2265 
2266   ASSERT_NE(reinterpret_cast<thread_callback>(NULL), audio_thread_cb);
2267   audio_thread_cb(audio_thread_cb_data);
2268   EXPECT_EQ(1, hotword_send_triggered_msg_called);
2269   alsa_iodev_destroy(iodev);
2270 }
2271 
2272 }  //  namespace
2273 
main(int argc,char ** argv)2274 int main(int argc, char **argv) {
2275   ::testing::InitGoogleTest(&argc, argv);
2276   openlog(NULL, LOG_PERROR, LOG_USER);
2277   return RUN_ALL_TESTS();
2278 }
2279 
2280 //  Stubs
2281 
2282 extern "C" {
2283 
2284 //  From iodev.
cras_iodev_list_add_output(struct cras_iodev * output)2285 int cras_iodev_list_add_output(struct cras_iodev *output)
2286 {
2287   return 0;
2288 }
cras_iodev_list_rm_output(struct cras_iodev * dev)2289 int cras_iodev_list_rm_output(struct cras_iodev *dev)
2290 {
2291   return 0;
2292 }
2293 
cras_iodev_list_add_input(struct cras_iodev * input)2294 int cras_iodev_list_add_input(struct cras_iodev *input)
2295 {
2296   return 0;
2297 }
cras_iodev_list_rm_input(struct cras_iodev * dev)2298 int cras_iodev_list_rm_input(struct cras_iodev *dev)
2299 {
2300   return 0;
2301 }
2302 
cras_iodev_list_get_hotword_models(cras_node_id_t node_id)2303 char *cras_iodev_list_get_hotword_models(cras_node_id_t node_id)
2304 {
2305 	return NULL;
2306 }
2307 
cras_iodev_list_set_hotword_model(cras_node_id_t node_id,const char * model_name)2308 int cras_iodev_list_set_hotword_model(cras_node_id_t node_id,
2309 				      const char *model_name)
2310 {
2311 	return 0;
2312 }
2313 
cras_iodev_list_suspend_hotword_streams()2314 int cras_iodev_list_suspend_hotword_streams()
2315 {
2316   return 0;
2317 }
2318 
cras_iodev_list_resume_hotword_stream()2319 int cras_iodev_list_resume_hotword_stream()
2320 {
2321   return 0;
2322 }
2323 
cras_iodev_list_get_audio_thread()2324 struct audio_thread *cras_iodev_list_get_audio_thread()
2325 {
2326   return NULL;
2327 }
2328 
2329 //  From alsa helper.
cras_alsa_set_channel_map(snd_pcm_t * handle,struct cras_audio_format * fmt)2330 int cras_alsa_set_channel_map(snd_pcm_t *handle,
2331 			      struct cras_audio_format *fmt)
2332 {
2333   return 0;
2334 }
cras_alsa_get_channel_map(snd_pcm_t * handle,struct cras_audio_format * fmt)2335 int cras_alsa_get_channel_map(snd_pcm_t *handle,
2336 			      struct cras_audio_format *fmt)
2337 {
2338   return 0;
2339 }
cras_alsa_pcm_open(snd_pcm_t ** handle,const char * dev,snd_pcm_stream_t stream)2340 int cras_alsa_pcm_open(snd_pcm_t **handle, const char *dev,
2341 		       snd_pcm_stream_t stream)
2342 {
2343   *handle = (snd_pcm_t *)0x24;
2344   cras_alsa_open_called++;
2345   return 0;
2346 }
cras_alsa_pcm_close(snd_pcm_t * handle)2347 int cras_alsa_pcm_close(snd_pcm_t *handle)
2348 {
2349   return 0;
2350 }
cras_alsa_pcm_start(snd_pcm_t * handle)2351 int cras_alsa_pcm_start(snd_pcm_t *handle)
2352 {
2353   cras_alsa_start_called++;
2354   return 0;
2355 }
cras_alsa_pcm_drain(snd_pcm_t * handle)2356 int cras_alsa_pcm_drain(snd_pcm_t *handle)
2357 {
2358   return 0;
2359 }
cras_alsa_fill_properties(snd_pcm_t * handle,size_t ** rates,size_t ** channel_counts,snd_pcm_format_t ** formats)2360 int cras_alsa_fill_properties(snd_pcm_t *handle,
2361 			      size_t **rates,
2362 			      size_t **channel_counts,
2363 			      snd_pcm_format_t **formats)
2364 {
2365   *rates = (size_t *)malloc(sizeof(**rates) * 3);
2366   (*rates)[0] = 44100;
2367   (*rates)[1] = 48000;
2368   (*rates)[2] = 0;
2369   *channel_counts = (size_t *)malloc(sizeof(**channel_counts) * 2);
2370   (*channel_counts)[0] = 2;
2371   (*channel_counts)[1] = 0;
2372   *formats = (snd_pcm_format_t *)malloc(sizeof(**formats) * 2);
2373   (*formats)[0] = SND_PCM_FORMAT_S16_LE;
2374   (*formats)[1] = (snd_pcm_format_t)0;
2375 
2376   cras_alsa_fill_properties_called++;
2377   return 0;
2378 }
cras_alsa_set_hwparams(snd_pcm_t * handle,struct cras_audio_format * format,snd_pcm_uframes_t * buffer_size,int period_wakeup,unsigned int dma_period_time)2379 int cras_alsa_set_hwparams(snd_pcm_t *handle, struct cras_audio_format *format,
2380 			   snd_pcm_uframes_t *buffer_size, int period_wakeup,
2381 			   unsigned int dma_period_time)
2382 {
2383   return 0;
2384 }
cras_alsa_set_swparams(snd_pcm_t * handle,int * enable_htimestamp)2385 int cras_alsa_set_swparams(snd_pcm_t *handle, int *enable_htimestamp)
2386 {
2387   return 0;
2388 }
cras_alsa_get_avail_frames(snd_pcm_t * handle,snd_pcm_uframes_t buf_size,snd_pcm_uframes_t severe_underrun_frames,const char * dev_name,snd_pcm_uframes_t * used,struct timespec * tstamp)2389 int cras_alsa_get_avail_frames(snd_pcm_t *handle, snd_pcm_uframes_t buf_size,
2390                                snd_pcm_uframes_t severe_underrun_frames,
2391                                const char* dev_name,
2392                                snd_pcm_uframes_t *used,
2393                                struct timespec *tstamp)
2394 {
2395   *used = cras_alsa_get_avail_frames_avail;
2396   clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
2397   return cras_alsa_get_avail_frames_ret;
2398 }
cras_alsa_get_delay_frames(snd_pcm_t * handle,snd_pcm_uframes_t buf_size,snd_pcm_sframes_t * delay)2399 int cras_alsa_get_delay_frames(snd_pcm_t *handle, snd_pcm_uframes_t buf_size,
2400 			       snd_pcm_sframes_t *delay)
2401 {
2402   *delay = 0;
2403   return 0;
2404 }
cras_alsa_mmap_begin(snd_pcm_t * handle,unsigned int format_bytes,uint8_t ** dst,snd_pcm_uframes_t * offset,snd_pcm_uframes_t * frames)2405 int cras_alsa_mmap_begin(snd_pcm_t *handle, unsigned int format_bytes,
2406 			 uint8_t **dst, snd_pcm_uframes_t *offset,
2407 			 snd_pcm_uframes_t *frames)
2408 {
2409   *dst = cras_alsa_mmap_begin_buffer;
2410   *frames = cras_alsa_mmap_begin_frames;
2411   return 0;
2412 }
cras_alsa_mmap_commit(snd_pcm_t * handle,snd_pcm_uframes_t offset,snd_pcm_uframes_t frames)2413 int cras_alsa_mmap_commit(snd_pcm_t *handle, snd_pcm_uframes_t offset,
2414 			  snd_pcm_uframes_t frames)
2415 {
2416   return 0;
2417 }
cras_alsa_attempt_resume(snd_pcm_t * handle)2418 int cras_alsa_attempt_resume(snd_pcm_t *handle)
2419 {
2420   cras_alsa_attempt_resume_called++;
2421   return 0;
2422 }
2423 
2424 //  ALSA stubs.
snd_pcm_format_physical_width(snd_pcm_format_t format)2425 int snd_pcm_format_physical_width(snd_pcm_format_t format)
2426 {
2427   return 16;
2428 }
2429 
snd_pcm_state(snd_pcm_t * handle)2430 snd_pcm_state_t snd_pcm_state(snd_pcm_t *handle)
2431 {
2432   return snd_pcm_state_ret;
2433 }
2434 
snd_strerror(int errnum)2435 const char *snd_strerror(int errnum)
2436 {
2437   return "Alsa Error in UT";
2438 }
2439 
cras_alsa_mixer_get_control_for_section(struct cras_alsa_mixer * cras_mixer,const struct ucm_section * section)2440 struct mixer_control *cras_alsa_mixer_get_control_for_section(
2441 		struct cras_alsa_mixer *cras_mixer,
2442 		const struct ucm_section *section)
2443 {
2444   cras_alsa_mixer_get_control_for_section_called++;
2445   return cras_alsa_mixer_get_control_for_section_return_value;
2446 }
2447 
cras_alsa_mixer_get_control_name(const struct mixer_control * control)2448 const char *cras_alsa_mixer_get_control_name(
2449 		const struct mixer_control *control)
2450 {
2451   ControlNameMap::iterator it;
2452   cras_alsa_mixer_get_control_name_called++;
2453   it = cras_alsa_mixer_get_control_name_values.find(control);
2454   if (it == cras_alsa_mixer_get_control_name_values.end())
2455     return "";
2456   return it->second.c_str();
2457 }
2458 
2459 //  From system_state.
cras_system_get_volume()2460 size_t cras_system_get_volume()
2461 {
2462   sys_get_volume_called++;
2463   return sys_get_volume_return_value;
2464 }
2465 
cras_system_get_capture_gain()2466 long cras_system_get_capture_gain()
2467 {
2468   sys_get_capture_gain_called++;
2469   return sys_get_capture_gain_return_value;
2470 }
2471 
cras_system_get_mute()2472 int cras_system_get_mute()
2473 {
2474   sys_get_mute_called++;
2475   return sys_get_mute_return_value;
2476 }
2477 
cras_system_get_capture_mute()2478 int cras_system_get_capture_mute()
2479 {
2480   sys_get_capture_mute_called++;
2481   return sys_get_capture_mute_return_value;
2482 }
2483 
cras_system_set_volume_limits(long min,long max)2484 void cras_system_set_volume_limits(long min, long max)
2485 {
2486   sys_set_volume_limits_called++;
2487 }
2488 
cras_system_set_capture_gain_limits(long min,long max)2489 void cras_system_set_capture_gain_limits(long min, long max)
2490 {
2491   cras_system_set_capture_gain_limits_set_value[0] = min;
2492   cras_system_set_capture_gain_limits_set_value[1] = max;
2493   sys_set_capture_gain_limits_called++;
2494 }
2495 
2496 //  From cras_alsa_mixer.
cras_alsa_mixer_set_dBFS(struct cras_alsa_mixer * m,long dB_level,struct mixer_control * output)2497 void cras_alsa_mixer_set_dBFS(struct cras_alsa_mixer *m,
2498 			      long dB_level,
2499 			      struct mixer_control *output)
2500 {
2501   alsa_mixer_set_dBFS_called++;
2502   alsa_mixer_set_dBFS_value = dB_level;
2503   alsa_mixer_set_dBFS_output = output;
2504 }
2505 
cras_alsa_mixer_set_mute(struct cras_alsa_mixer * cras_mixer,int muted,struct mixer_control * mixer_output)2506 void cras_alsa_mixer_set_mute(struct cras_alsa_mixer *cras_mixer,
2507 			      int muted,
2508 			      struct mixer_control *mixer_output)
2509 {
2510   alsa_mixer_set_mute_called++;
2511   alsa_mixer_set_mute_value = muted;
2512   alsa_mixer_set_mute_output = mixer_output;
2513 }
2514 
cras_alsa_mixer_get_dB_range(struct cras_alsa_mixer * cras_mixer)2515 long cras_alsa_mixer_get_dB_range(struct cras_alsa_mixer *cras_mixer)
2516 {
2517   alsa_mixer_get_dB_range_called++;
2518   return alsa_mixer_get_dB_range_value;
2519 }
2520 
cras_alsa_mixer_get_output_dB_range(struct mixer_control * mixer_output)2521 long cras_alsa_mixer_get_output_dB_range(
2522     struct mixer_control *mixer_output)
2523 {
2524   alsa_mixer_get_output_dB_range_called++;
2525   return alsa_mixer_get_output_dB_range_value;
2526 }
2527 
cras_alsa_mixer_set_capture_dBFS(struct cras_alsa_mixer * m,long dB_level,struct mixer_control * mixer_input)2528 void cras_alsa_mixer_set_capture_dBFS(struct cras_alsa_mixer *m, long dB_level,
2529 		                      struct mixer_control *mixer_input)
2530 {
2531   alsa_mixer_set_capture_dBFS_called++;
2532   alsa_mixer_set_capture_dBFS_value = dB_level;
2533   alsa_mixer_set_capture_dBFS_input = mixer_input;
2534 }
2535 
cras_alsa_mixer_set_capture_mute(struct cras_alsa_mixer * m,int mute,struct mixer_control * mixer_input)2536 void cras_alsa_mixer_set_capture_mute(struct cras_alsa_mixer *m, int mute,
2537 				      struct mixer_control *mixer_input)
2538 {
2539   alsa_mixer_set_capture_mute_called++;
2540   alsa_mixer_set_capture_mute_value = mute;
2541   alsa_mixer_set_capture_mute_input = mixer_input;
2542 }
2543 
cras_alsa_mixer_list_outputs(struct cras_alsa_mixer * cras_mixer,cras_alsa_mixer_control_callback cb,void * callback_arg)2544 void cras_alsa_mixer_list_outputs(struct cras_alsa_mixer *cras_mixer,
2545 				  cras_alsa_mixer_control_callback cb,
2546 				  void *callback_arg)
2547 {
2548   cras_alsa_mixer_list_outputs_called++;
2549   for (size_t i = 0; i < cras_alsa_mixer_list_outputs_outputs_length; i++) {
2550     cb(cras_alsa_mixer_list_outputs_outputs[i], callback_arg);
2551   }
2552 }
2553 
cras_alsa_mixer_list_inputs(struct cras_alsa_mixer * cras_mixer,cras_alsa_mixer_control_callback cb,void * callback_arg)2554 void cras_alsa_mixer_list_inputs(struct cras_alsa_mixer *cras_mixer,
2555 				  cras_alsa_mixer_control_callback cb,
2556 				  void *callback_arg)
2557 {
2558   cras_alsa_mixer_list_inputs_called++;
2559   for (size_t i = 0; i < cras_alsa_mixer_list_inputs_outputs_length; i++) {
2560     cb(cras_alsa_mixer_list_inputs_outputs[i], callback_arg);
2561   }
2562 }
2563 
cras_alsa_mixer_set_output_active_state(struct mixer_control * output,int active)2564 int cras_alsa_mixer_set_output_active_state(
2565 		struct mixer_control *output,
2566 		int active)
2567 {
2568   cras_alsa_mixer_set_output_active_state_called++;
2569   cras_alsa_mixer_set_output_active_state_outputs.push_back(output);
2570   cras_alsa_mixer_set_output_active_state_values.push_back(active);
2571   return 0;
2572 }
2573 
cras_volume_curve_destroy(struct cras_volume_curve * curve)2574 void cras_volume_curve_destroy(struct cras_volume_curve *curve)
2575 {
2576 }
2577 
cras_alsa_mixer_get_minimum_capture_gain(struct cras_alsa_mixer * cmix,struct mixer_control * mixer_input)2578 long cras_alsa_mixer_get_minimum_capture_gain(struct cras_alsa_mixer *cmix,
2579 		struct mixer_control *mixer_input)
2580 {
2581 	cras_alsa_mixer_get_minimum_capture_gain_called++;
2582 	cras_alsa_mixer_get_minimum_capture_gain_mixer_input = mixer_input;
2583 	return cras_alsa_mixer_get_minimum_capture_gain_ret_value;
2584 }
2585 
cras_alsa_mixer_get_maximum_capture_gain(struct cras_alsa_mixer * cmix,struct mixer_control * mixer_input)2586 long cras_alsa_mixer_get_maximum_capture_gain(struct cras_alsa_mixer *cmix,
2587 		struct mixer_control *mixer_input)
2588 {
2589 	cras_alsa_mixer_get_maximum_capture_gain_called++;
2590 	cras_alsa_mixer_get_maximum_capture_gain_mixer_input = mixer_input;
2591 	return cras_alsa_mixer_get_maximum_capture_gain_ret_value;
2592 }
2593 
cras_alsa_mixer_has_main_volume(const struct cras_alsa_mixer * cras_mixer)2594 int cras_alsa_mixer_has_main_volume(const struct cras_alsa_mixer *cras_mixer)
2595 {
2596   return 1;
2597 }
2598 
cras_alsa_mixer_has_volume(const struct mixer_control * mixer_control)2599 int cras_alsa_mixer_has_volume(const struct mixer_control *mixer_control)
2600 {
2601   return 1;
2602 }
2603 
2604 // From cras_alsa_jack
cras_alsa_jack_list_create(unsigned int card_index,const char * card_name,unsigned int device_index,int check_gpio_jack,struct cras_alsa_mixer * mixer,struct cras_use_case_mgr * ucm,snd_hctl_t * hctl,enum CRAS_STREAM_DIRECTION direction,jack_state_change_callback * cb,void * cb_data)2605 struct cras_alsa_jack_list *cras_alsa_jack_list_create(
2606 		unsigned int card_index,
2607 		const char *card_name,
2608 		unsigned int device_index,
2609 		int check_gpio_jack,
2610 		struct cras_alsa_mixer *mixer,
2611                 struct cras_use_case_mgr *ucm,
2612 		snd_hctl_t *hctl,
2613 		enum CRAS_STREAM_DIRECTION direction,
2614 		jack_state_change_callback *cb,
2615 		void *cb_data)
2616 {
2617   cras_alsa_jack_list_create_called++;
2618   cras_alsa_jack_list_create_cb = cb;
2619   cras_alsa_jack_list_create_cb_data = cb_data;
2620   return (struct cras_alsa_jack_list *)0xfee;
2621 }
2622 
cras_alsa_jack_list_find_jacks_by_name_matching(struct cras_alsa_jack_list * jack_list)2623 int cras_alsa_jack_list_find_jacks_by_name_matching(
2624 	struct cras_alsa_jack_list *jack_list)
2625 {
2626   cras_alsa_jack_list_find_jacks_by_name_matching_called++;
2627   return 0;
2628 }
2629 
cras_alsa_jack_list_add_jack_for_section(struct cras_alsa_jack_list * jack_list,struct ucm_section * ucm_section,struct cras_alsa_jack ** result_jack)2630 int cras_alsa_jack_list_add_jack_for_section(
2631 	struct cras_alsa_jack_list *jack_list,
2632 	struct ucm_section *ucm_section,
2633 	struct cras_alsa_jack **result_jack)
2634 {
2635   cras_alsa_jack_list_add_jack_for_section_called++;
2636   if (result_jack)
2637     *result_jack = cras_alsa_jack_list_add_jack_for_section_result_jack;
2638   return 0;
2639 }
2640 
cras_alsa_jack_list_destroy(struct cras_alsa_jack_list * jack_list)2641 void cras_alsa_jack_list_destroy(struct cras_alsa_jack_list *jack_list)
2642 {
2643   cras_alsa_jack_list_destroy_called++;
2644 }
2645 
cras_alsa_jack_list_has_hctl_jacks(struct cras_alsa_jack_list * jack_list)2646 int cras_alsa_jack_list_has_hctl_jacks(struct cras_alsa_jack_list *jack_list)
2647 {
2648   return cras_alsa_jack_list_has_hctl_jacks_return_val;
2649 }
2650 
cras_alsa_jack_list_report(const struct cras_alsa_jack_list * jack_list)2651 void cras_alsa_jack_list_report(const struct cras_alsa_jack_list *jack_list)
2652 {
2653 }
2654 
cras_alsa_jack_enable_ucm(const struct cras_alsa_jack * jack,int enable)2655 void cras_alsa_jack_enable_ucm(const struct cras_alsa_jack *jack, int enable) {
2656   cras_alsa_jack_enable_ucm_called++;
2657 }
2658 
cras_alsa_jack_get_name(const struct cras_alsa_jack * jack)2659 const char *cras_alsa_jack_get_name(const struct cras_alsa_jack *jack)
2660 {
2661   cras_alsa_jack_get_name_called++;
2662   return cras_alsa_jack_get_name_ret_value;
2663 }
2664 
cras_alsa_jack_get_dsp_name(const struct cras_alsa_jack * jack)2665 const char *cras_alsa_jack_get_dsp_name(const struct cras_alsa_jack *jack)
2666 {
2667   cras_alsa_jack_get_dsp_name_called++;
2668   return jack ? cras_alsa_jack_get_dsp_name_value : NULL;
2669 }
2670 
ucm_get_dsp_name_default(struct cras_use_case_mgr * mgr,int direction)2671 const char *ucm_get_dsp_name_default(struct cras_use_case_mgr *mgr,
2672                                      int direction)
2673 {
2674   ucm_get_dsp_name_default_called++;
2675   if (ucm_get_dsp_name_default_value)
2676     return strdup(ucm_get_dsp_name_default_value);
2677   else
2678     return NULL;
2679 }
2680 
cras_alsa_jack_get_mixer_output(const struct cras_alsa_jack * jack)2681 struct mixer_control *cras_alsa_jack_get_mixer_output(
2682     const struct cras_alsa_jack *jack)
2683 {
2684   return cras_alsa_jack_get_mixer_output_ret;
2685 }
2686 
cras_alsa_jack_get_mixer_input(const struct cras_alsa_jack * jack)2687 struct mixer_control *cras_alsa_jack_get_mixer_input(
2688 		const struct cras_alsa_jack *jack)
2689 {
2690   return cras_alsa_jack_get_mixer_input_ret;
2691 }
2692 
ucm_set_enabled(struct cras_use_case_mgr * mgr,const char * dev,int enabled)2693 int ucm_set_enabled(
2694     struct cras_use_case_mgr *mgr, const char *dev, int enabled) {
2695   ucm_set_enabled_called++;
2696   return 0;
2697 }
2698 
ucm_get_flag(struct cras_use_case_mgr * mgr,const char * flag_name)2699 char *ucm_get_flag(struct cras_use_case_mgr *mgr, const char *flag_name) {
2700   if ((!strcmp(flag_name, "AutoUnplugInputNode") &&
2701        auto_unplug_input_node_ret) ||
2702       (!strcmp(flag_name, "AutoUnplugOutputNode") &&
2703        auto_unplug_output_node_ret)) {
2704     char *ret = (char *)malloc(8);
2705     snprintf(ret, 8, "%s", "1");
2706     return ret;
2707   }
2708 
2709   return NULL;
2710 }
2711 
ucm_get_mic_positions(struct cras_use_case_mgr * mgr)2712 char *ucm_get_mic_positions(struct cras_use_case_mgr *mgr) {
2713   return NULL;
2714 }
2715 
ucm_swap_mode_exists(struct cras_use_case_mgr * mgr)2716 int ucm_swap_mode_exists(struct cras_use_case_mgr *mgr)
2717 {
2718   return ucm_swap_mode_exists_ret_value;
2719 }
2720 
ucm_enable_swap_mode(struct cras_use_case_mgr * mgr,const char * node_name,int enable)2721 int ucm_enable_swap_mode(struct cras_use_case_mgr *mgr, const char *node_name,
2722                          int enable)
2723 {
2724   ucm_enable_swap_mode_called++;
2725   return ucm_enable_swap_mode_ret_value;
2726 }
2727 
ucm_get_min_buffer_level(struct cras_use_case_mgr * mgr,unsigned int * level)2728 int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr,
2729 			     unsigned int *level)
2730 {
2731   *level = 0;
2732   return 0;
2733 }
2734 
ucm_get_enable_htimestamp_flag(struct cras_use_case_mgr * mgr)2735 unsigned int ucm_get_enable_htimestamp_flag(struct cras_use_case_mgr *mgr)
2736 {
2737   return ucm_get_enable_htimestamp_flag_ret;
2738 }
2739 
ucm_get_disable_software_volume(struct cras_use_case_mgr * mgr)2740 unsigned int ucm_get_disable_software_volume(struct cras_use_case_mgr *mgr)
2741 {
2742   return 0;
2743 }
2744 
ucm_get_min_software_gain(struct cras_use_case_mgr * mgr,const char * dev,long * gain)2745 int ucm_get_min_software_gain(struct cras_use_case_mgr *mgr, const char *dev,
2746     long *gain)
2747 {
2748   ucm_get_min_software_gain_called++;
2749   *gain = ucm_get_min_software_gain_value;
2750   return ucm_get_min_software_gain_ret_value;
2751 }
2752 
ucm_get_max_software_gain(struct cras_use_case_mgr * mgr,const char * dev,long * gain)2753 int ucm_get_max_software_gain(struct cras_use_case_mgr *mgr, const char *dev,
2754     long *gain)
2755 {
2756   ucm_get_max_software_gain_called++;
2757   *gain = ucm_get_max_software_gain_value;
2758   return ucm_get_max_software_gain_ret_value;
2759 }
2760 
ucm_get_hotword_models(struct cras_use_case_mgr * mgr)2761 char *ucm_get_hotword_models(struct cras_use_case_mgr *mgr)
2762 {
2763   return NULL;
2764 }
2765 
ucm_set_hotword_model(struct cras_use_case_mgr * mgr,const char * model)2766 int ucm_set_hotword_model(struct cras_use_case_mgr *mgr, const char *model)
2767 {
2768   return 0;
2769 }
2770 
ucm_get_dma_period_for_dev(struct cras_use_case_mgr * mgr,const char * dev)2771 unsigned int ucm_get_dma_period_for_dev(struct cras_use_case_mgr *mgr,
2772                                         const char *dev)
2773 {
2774   ucm_get_dma_period_for_dev_called++;
2775   return ucm_get_dma_period_for_dev_ret;
2776 }
2777 
ucm_get_sample_rate_for_dev(struct cras_use_case_mgr * mgr,const char * dev,enum CRAS_STREAM_DIRECTION direction)2778 int ucm_get_sample_rate_for_dev(struct cras_use_case_mgr *mgr, const char *dev,
2779 				enum CRAS_STREAM_DIRECTION direction)
2780 {
2781   return -EINVAL;
2782 }
2783 
ucm_get_capture_chmap_for_dev(struct cras_use_case_mgr * mgr,const char * dev,int8_t * channel_layout)2784 int ucm_get_capture_chmap_for_dev(struct cras_use_case_mgr *mgr,
2785           const char *dev,
2786           int8_t *channel_layout)
2787 {
2788   return -EINVAL;
2789 }
2790 
ucm_get_preempt_hotword(struct cras_use_case_mgr * mgr,const char * dev)2791 int ucm_get_preempt_hotword(struct cras_use_case_mgr *mgr, const char *dev)
2792 {
2793   return 0;
2794 }
2795 
cras_volume_curve_create_default()2796 struct cras_volume_curve *cras_volume_curve_create_default()
2797 {
2798   return &default_curve;
2799 }
2800 
cras_card_config_get_volume_curve_for_control(const struct cras_card_config * card_config,const char * control_name)2801 struct cras_volume_curve *cras_card_config_get_volume_curve_for_control(
2802     const struct cras_card_config *card_config,
2803     const char *control_name)
2804 {
2805   VolCurveMap::iterator it;
2806   cras_card_config_get_volume_curve_for_control_called++;
2807   if (!control_name)
2808     return NULL;
2809   it = cras_card_config_get_volume_curve_vals.find(control_name);
2810   if (it == cras_card_config_get_volume_curve_vals.end())
2811     return NULL;
2812   return it->second;
2813 }
2814 
cras_iodev_free_format(struct cras_iodev * iodev)2815 void cras_iodev_free_format(struct cras_iodev *iodev)
2816 {
2817 }
2818 
cras_iodev_set_format(struct cras_iodev * iodev,const struct cras_audio_format * fmt)2819 int cras_iodev_set_format(struct cras_iodev *iodev,
2820 			  const struct cras_audio_format *fmt)
2821 {
2822   fake_format = (struct cras_audio_format *)calloc(
2823         1,
2824         sizeof(cras_audio_format));
2825   // Copy the content of format from fmt into format of iodev.
2826   memcpy(fake_format, fmt, sizeof(cras_audio_format));
2827   iodev->format = fake_format;
2828   return 0;
2829 }
2830 
audio_thread_create()2831 struct audio_thread *audio_thread_create() {
2832   return reinterpret_cast<audio_thread*>(0x323);
2833 }
2834 
audio_thread_destroy(audio_thread * thread)2835 void audio_thread_destroy(audio_thread* thread) {
2836 }
2837 
2838 
2839 
cras_iodev_update_dsp(struct cras_iodev * iodev)2840 void cras_iodev_update_dsp(struct cras_iodev *iodev)
2841 {
2842   cras_iodev_update_dsp_called++;
2843   cras_iodev_update_dsp_name = iodev->dsp_name;
2844 }
2845 
cras_iodev_set_node_attr(struct cras_ionode * ionode,enum ionode_attr attr,int value)2846 int cras_iodev_set_node_attr(struct cras_ionode *ionode,
2847 			     enum ionode_attr attr, int value)
2848 {
2849   cras_iodev_set_node_attr_called++;
2850   cras_iodev_set_node_attr_ionode = ionode;
2851   cras_iodev_set_node_attr_attr = attr;
2852   cras_iodev_set_node_attr_value = value;
2853   if (ionode && (attr == IONODE_ATTR_PLUGGED))
2854     ionode->plugged = value;
2855   return 0;
2856 }
2857 
cras_iodev_add_node(struct cras_iodev * iodev,struct cras_ionode * node)2858 void cras_iodev_add_node(struct cras_iodev *iodev, struct cras_ionode *node)
2859 {
2860   cras_iodev_add_node_called++;
2861   DL_APPEND(iodev->nodes, node);
2862 }
2863 
cras_iodev_rm_node(struct cras_iodev * iodev,struct cras_ionode * node)2864 void cras_iodev_rm_node(struct cras_iodev *iodev, struct cras_ionode *node)
2865 {
2866   DL_DELETE(iodev->nodes, node);
2867 }
2868 
cras_iodev_set_active_node(struct cras_iodev * iodev,struct cras_ionode * node)2869 void cras_iodev_set_active_node(struct cras_iodev *iodev,
2870                                 struct cras_ionode *node)
2871 {
2872   iodev->active_node = node;
2873 }
2874 
cras_iodev_free_resources(struct cras_iodev * iodev)2875 void cras_iodev_free_resources(struct cras_iodev *iodev)
2876 {
2877   cras_iodev_free_resources_called++;
2878 }
2879 
cras_alsa_jack_update_monitor_name(const struct cras_alsa_jack * jack,char * name_buf,unsigned int buf_size)2880 void cras_alsa_jack_update_monitor_name(const struct cras_alsa_jack *jack,
2881 					char *name_buf,
2882 					unsigned int buf_size)
2883 {
2884   if (cras_alsa_jack_update_monitor_fake_name)
2885     strcpy(name_buf, cras_alsa_jack_update_monitor_fake_name);
2886 }
2887 
cras_alsa_jack_update_node_type(const struct cras_alsa_jack * jack,enum CRAS_NODE_TYPE * type)2888 void cras_alsa_jack_update_node_type(const struct cras_alsa_jack *jack,
2889 				     enum CRAS_NODE_TYPE *type)
2890 {
2891   cras_alsa_jack_update_node_type_called++;
2892 }
2893 
cras_alsa_jack_get_ucm_device(const struct cras_alsa_jack * jack)2894 const char *cras_alsa_jack_get_ucm_device(const struct cras_alsa_jack *jack)
2895 {
2896   return NULL;
2897 }
2898 
ucm_get_default_node_gain(struct cras_use_case_mgr * mgr,const char * dev,long * gain)2899 int ucm_get_default_node_gain(struct cras_use_case_mgr *mgr, const char *dev,
2900                               long *gain)
2901 {
2902   if (ucm_get_default_node_gain_values.find(dev) ==
2903       ucm_get_default_node_gain_values.end())
2904     return 1;
2905 
2906   *gain = ucm_get_default_node_gain_values[dev];
2907   return 0;
2908 }
2909 
cras_iodev_init_audio_area(struct cras_iodev * iodev,int num_channels)2910 void cras_iodev_init_audio_area(struct cras_iodev *iodev,
2911                                 int num_channels) {
2912 }
2913 
cras_iodev_free_audio_area(struct cras_iodev * iodev)2914 void cras_iodev_free_audio_area(struct cras_iodev *iodev) {
2915 }
2916 
cras_iodev_reset_rate_estimator(const struct cras_iodev * iodev)2917 int cras_iodev_reset_rate_estimator(const struct cras_iodev *iodev)
2918 {
2919   return 0;
2920 }
2921 
cras_iodev_frames_queued(struct cras_iodev * iodev,struct timespec * tstamp)2922 int cras_iodev_frames_queued(struct cras_iodev *iodev, struct timespec *tstamp)
2923 {
2924   clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
2925   return cras_iodev_frames_queued_ret;
2926 }
2927 
cras_iodev_buffer_avail(struct cras_iodev * iodev,unsigned hw_level)2928 int cras_iodev_buffer_avail(struct cras_iodev *iodev, unsigned hw_level)
2929 {
2930   return cras_iodev_buffer_avail_ret;
2931 }
2932 
cras_iodev_fill_odev_zeros(struct cras_iodev * odev,unsigned int frames)2933 int cras_iodev_fill_odev_zeros(struct cras_iodev *odev, unsigned int frames)
2934 {
2935   cras_iodev_fill_odev_zeros_called++;
2936   cras_iodev_fill_odev_zeros_frames = frames;
2937   return 0;
2938 }
2939 
cras_audio_area_config_buf_pointers(struct cras_audio_area * area,const struct cras_audio_format * fmt,uint8_t * base_buffer)2940 void cras_audio_area_config_buf_pointers(struct cras_audio_area *area,
2941 					 const struct cras_audio_format *fmt,
2942 					 uint8_t *base_buffer)
2943 {
2944 }
2945 
audio_thread_add_callback(int fd,thread_callback cb,void * data)2946 void audio_thread_add_callback(int fd, thread_callback cb, void *data)
2947 {
2948   audio_thread_cb = cb;
2949   audio_thread_cb_data = data;
2950 }
2951 
audio_thread_rm_callback(int fd)2952 void audio_thread_rm_callback(int fd)
2953 {
2954 }
2955 
audio_thread_rm_callback_sync(struct audio_thread * thread,int fd)2956 int audio_thread_rm_callback_sync(struct audio_thread *thread, int fd) {
2957   return 0;
2958 }
2959 
cras_hotword_send_triggered_msg()2960 int cras_hotword_send_triggered_msg()
2961 {
2962   hotword_send_triggered_msg_called++;
2963   return 0;
2964 }
2965 
snd_pcm_poll_descriptors_count(snd_pcm_t * pcm)2966 int snd_pcm_poll_descriptors_count(snd_pcm_t *pcm)
2967 {
2968   return 1;
2969 }
2970 
snd_pcm_poll_descriptors(snd_pcm_t * pcm,struct pollfd * pfds,unsigned int space)2971 int snd_pcm_poll_descriptors(snd_pcm_t *pcm, struct pollfd *pfds,
2972                              unsigned int space)
2973 {
2974   if (space >= 1) {
2975     pfds[0].events = POLLIN;
2976     pfds[0].fd = 99;
2977   }
2978   return 0;
2979 }
2980 
is_utf8_string(const char * string)2981 int is_utf8_string(const char* string)
2982 {
2983   return is_utf8_string_ret_value;
2984 }
2985 
cras_alsa_mmap_get_whole_buffer(snd_pcm_t * handle,uint8_t ** dst)2986 int cras_alsa_mmap_get_whole_buffer(snd_pcm_t *handle, uint8_t **dst)
2987 {
2988   snd_pcm_uframes_t offset, frames;
2989 
2990   cras_alsa_mmap_get_whole_buffer_called++;
2991   return cras_alsa_mmap_begin(handle, 0, dst, &offset, &frames);
2992 }
2993 
cras_alsa_resume_appl_ptr(snd_pcm_t * handle,snd_pcm_uframes_t ahead)2994 int cras_alsa_resume_appl_ptr(snd_pcm_t *handle, snd_pcm_uframes_t ahead)
2995 {
2996   cras_alsa_resume_appl_ptr_called++;
2997   cras_alsa_resume_appl_ptr_ahead = ahead;
2998   return 0;
2999 }
3000 
cras_iodev_default_no_stream_playback(struct cras_iodev * odev,int enable)3001 int cras_iodev_default_no_stream_playback(struct cras_iodev *odev, int enable)
3002 {
3003   return 0;
3004 }
3005 
cras_iodev_state(const struct cras_iodev * iodev)3006 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev)
3007 {
3008   return iodev->state;
3009 }
3010 
cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev * iodev,struct cras_ionode * node,int enable)3011 int cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev *iodev,
3012                                           struct cras_ionode *node,
3013                                           int enable)
3014 {
3015   cras_iodev_dsp_set_swap_mode_for_node_called++;
3016   return 0;
3017 }
3018 
cras_ramp_create()3019 struct cras_ramp* cras_ramp_create() {
3020   return (struct cras_ramp*)0x1;
3021 }
3022 
3023 }
3024