• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iniparser.h>
7 #include <map>
8 #include <stdio.h>
9 #include <syslog.h>
10 #include <sys/param.h>
11 
12 extern "C" {
13 #include "cras_alsa_card.h"
14 #include "cras_alsa_io.h"
15 #include "cras_alsa_mixer.h"
16 #include "cras_alsa_ucm.h"
17 #include "cras_iodev.h"
18 #include "cras_types.h"
19 #include "cras_util.h"
20 #include "utlist.h"
21 }
22 
23 namespace {
24 
25 static size_t cras_alsa_mixer_create_called;
26 static struct cras_alsa_mixer *cras_alsa_mixer_create_return;
27 static size_t cras_alsa_mixer_destroy_called;
28 static size_t cras_alsa_iodev_create_called;
29 static struct cras_iodev **cras_alsa_iodev_create_return;
30 static struct cras_iodev fake_dev1, fake_dev2, fake_dev3, fake_dev4;
31 static struct cras_iodev *cras_alsa_iodev_create_default_return[] = {
32   &fake_dev1,
33   &fake_dev2,
34   &fake_dev3,
35   &fake_dev4,
36 };
37 static size_t cras_alsa_iodev_create_return_size;
38 static size_t cras_alsa_iodev_legacy_complete_init_called;
39 static size_t cras_alsa_iodev_ucm_add_nodes_and_jacks_called;
40 static size_t cras_alsa_iodev_ucm_complete_init_called;
41 static size_t cras_alsa_iodev_destroy_called;
42 static struct cras_iodev *cras_alsa_iodev_destroy_arg;
43 static size_t cras_alsa_iodev_index_called;
44 static std::map<struct cras_iodev *, unsigned int> cras_alsa_iodev_index_return;
45 static int alsa_iodev_has_hctl_jacks_return;
46 static size_t snd_ctl_open_called;
47 static size_t snd_ctl_open_return;
48 static size_t snd_ctl_close_called;
49 static size_t snd_ctl_close_return;
50 static size_t snd_ctl_pcm_next_device_called;
51 static bool snd_ctl_pcm_next_device_return_error;
52 static int *snd_ctl_pcm_next_device_set_devs;
53 static size_t snd_ctl_pcm_next_device_set_devs_size;
54 static size_t snd_ctl_pcm_next_device_set_devs_index;
55 static size_t snd_ctl_pcm_info_called;
56 static int *snd_ctl_pcm_info_rets;
57 static size_t snd_ctl_pcm_info_rets_size;
58 static size_t snd_ctl_pcm_info_rets_index;
59 static size_t snd_ctl_card_info_called;
60 static int snd_ctl_card_info_ret;
61 static size_t snd_hctl_open_called;
62 static int snd_hctl_open_return_value;
63 static int snd_hctl_close_called;
64 static size_t snd_hctl_nonblock_called;
65 static snd_hctl_t *snd_hctl_open_pointer_val;
66 static size_t snd_hctl_load_called;
67 static int snd_hctl_load_return_value;
68 static struct pollfd *snd_hctl_poll_descriptors_fds;
69 static size_t snd_hctl_poll_descriptors_num_fds;
70 static size_t snd_hctl_poll_descriptors_called;
71 static size_t cras_system_add_select_fd_called;
72 static std::vector<int> cras_system_add_select_fd_values;
73 static size_t cras_system_rm_select_fd_called;
74 static std::vector<int> cras_system_rm_select_fd_values;
75 static size_t snd_hctl_handle_events_called;
76 static size_t iniparser_freedict_called;
77 static size_t iniparser_load_called;
78 static struct cras_device_blacklist *fake_blacklist;
79 static int cras_device_blacklist_check_retval;
80 static unsigned ucm_create_called;
81 static unsigned ucm_destroy_called;
82 static size_t ucm_get_dev_for_mixer_called;
83 static size_t ucm_get_flag_called;
84 static char ucm_get_flag_name[64];
85 static char* device_config_dir;
86 static const char* cras_card_config_dir;
87 static struct mixer_name *ucm_get_coupled_mixer_names_return_value;
88 static struct mixer_name *coupled_output_names_value;
89 static int ucm_has_fully_specified_ucm_flag_return_value;
90 static int ucm_get_sections_called;
91 static struct ucm_section *ucm_get_sections_return_value;
92 static size_t cras_alsa_mixer_add_controls_in_section_called;
93 static int cras_alsa_mixer_add_controls_in_section_return_value;
94 static int ucm_get_echo_reference_dev_name_for_dev_called;
95 static const char *ucm_get_echo_reference_dev_name_for_dev_return_value[4];
96 
ResetStubData()97 static void ResetStubData() {
98   cras_alsa_mixer_create_called = 0;
99   cras_alsa_mixer_create_return = reinterpret_cast<struct cras_alsa_mixer *>(1);
100   cras_alsa_mixer_destroy_called = 0;
101   cras_alsa_iodev_destroy_arg = NULL;
102   cras_alsa_iodev_create_called = 0;
103   cras_alsa_iodev_create_return = cras_alsa_iodev_create_default_return;
104   cras_alsa_iodev_create_return_size =
105       ARRAY_SIZE(cras_alsa_iodev_create_default_return);
106   cras_alsa_iodev_legacy_complete_init_called = 0;
107   cras_alsa_iodev_ucm_add_nodes_and_jacks_called = 0;
108   cras_alsa_iodev_ucm_complete_init_called = 0;
109   cras_alsa_iodev_destroy_called = 0;
110   cras_alsa_iodev_index_called = 0;
111   cras_alsa_iodev_index_return.clear();
112   alsa_iodev_has_hctl_jacks_return = 1;
113   snd_ctl_open_called = 0;
114   snd_ctl_open_return = 0;
115   snd_ctl_close_called = 0;
116   snd_ctl_close_return = 0;
117   snd_ctl_pcm_next_device_called = 0;
118   snd_ctl_pcm_next_device_return_error = false;
119   snd_ctl_pcm_next_device_set_devs_size = 0;
120   snd_ctl_pcm_next_device_set_devs_index = 0;
121   snd_ctl_pcm_info_called = 0;
122   snd_ctl_pcm_info_rets_size = 0;
123   snd_ctl_pcm_info_rets_index = 0;
124   snd_ctl_card_info_called = 0;
125   snd_ctl_card_info_ret = 0;
126   snd_hctl_open_called = 0;
127   snd_hctl_open_return_value = 0;
128   snd_hctl_open_pointer_val = reinterpret_cast<snd_hctl_t *>(0x4323);
129   snd_hctl_load_called = 0;
130   snd_hctl_load_return_value = 0;
131   snd_hctl_close_called = 0;
132   snd_hctl_nonblock_called = 0;
133   snd_hctl_poll_descriptors_num_fds = 0;
134   snd_hctl_poll_descriptors_called = 0;
135   snd_hctl_handle_events_called = 0;
136   snd_hctl_poll_descriptors_num_fds = 0;
137   snd_hctl_poll_descriptors_called = 0;
138   cras_system_add_select_fd_called = 0;
139   cras_system_add_select_fd_values.clear();
140   cras_system_rm_select_fd_called = 0;
141   cras_system_rm_select_fd_values.clear();
142   iniparser_freedict_called = 0;
143   iniparser_load_called = 0;
144   fake_blacklist = reinterpret_cast<struct cras_device_blacklist *>(3);
145   cras_device_blacklist_check_retval = 0;
146   ucm_create_called = 0;
147   ucm_destroy_called = 0;
148   ucm_get_dev_for_mixer_called = 0;
149   ucm_get_flag_called = 0;
150   memset(ucm_get_flag_name, 0, sizeof(ucm_get_flag_name));
151   device_config_dir = reinterpret_cast<char *>(3);
152   cras_card_config_dir = NULL;
153   ucm_get_coupled_mixer_names_return_value = NULL;
154   mixer_name_free(coupled_output_names_value);
155   coupled_output_names_value = NULL;
156   ucm_has_fully_specified_ucm_flag_return_value = 0;
157   ucm_get_sections_called = 0;
158   ucm_get_sections_return_value = NULL;
159   cras_alsa_mixer_add_controls_in_section_called = 0;
160   cras_alsa_mixer_add_controls_in_section_return_value = 0;
161   ucm_get_echo_reference_dev_name_for_dev_called = 0;
162 }
163 
TEST(AlsaCard,CreateFailInvalidCard)164 TEST(AlsaCard, CreateFailInvalidCard) {
165   struct cras_alsa_card *c;
166   cras_alsa_card_info card_info;
167 
168   ResetStubData();
169   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
170   card_info.card_index = 55;
171   c = cras_alsa_card_create(&card_info, device_config_dir,
172                             fake_blacklist, NULL);
173   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
174   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
175   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
176 }
177 
TEST(AlsaCard,CreateFailMixerInit)178 TEST(AlsaCard, CreateFailMixerInit) {
179   struct cras_alsa_card *c;
180   cras_alsa_card_info card_info;
181 
182   ResetStubData();
183   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
184   card_info.card_index = 0;
185   cras_alsa_mixer_create_return = static_cast<struct cras_alsa_mixer *>(NULL);
186   c = cras_alsa_card_create(&card_info, device_config_dir,
187                             fake_blacklist, NULL);
188   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
189   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
190   EXPECT_EQ(1, cras_alsa_mixer_create_called);
191   EXPECT_EQ(0, cras_alsa_mixer_destroy_called);
192 }
193 
TEST(AlsaCard,CreateFailCtlOpen)194 TEST(AlsaCard, CreateFailCtlOpen) {
195   struct cras_alsa_card *c;
196   cras_alsa_card_info card_info;
197 
198   ResetStubData();
199   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
200   card_info.card_index = 0;
201   snd_ctl_open_return = -1;
202   c = cras_alsa_card_create(&card_info, device_config_dir,
203                             fake_blacklist, NULL);
204   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
205   EXPECT_EQ(1, snd_ctl_open_called);
206   EXPECT_EQ(0, snd_ctl_close_called);
207   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
208   EXPECT_EQ(0, cras_alsa_mixer_create_called);
209 }
210 
TEST(AlsaCard,CreateFailHctlOpen)211 TEST(AlsaCard, CreateFailHctlOpen) {
212   struct cras_alsa_card *c;
213   cras_alsa_card_info card_info;
214 
215   ResetStubData();
216   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
217   card_info.card_index = 0;
218   snd_hctl_open_pointer_val = NULL;
219   snd_hctl_open_return_value = -1;
220 
221   c = cras_alsa_card_create(&card_info, device_config_dir,
222                             fake_blacklist, NULL);
223   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
224   EXPECT_EQ(1, snd_ctl_open_called);
225   EXPECT_EQ(1, snd_ctl_close_called);
226   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
227   EXPECT_EQ(1, snd_hctl_open_called);
228   EXPECT_EQ(0, snd_hctl_nonblock_called);
229   EXPECT_EQ(0, snd_hctl_load_called);
230   EXPECT_EQ(1, cras_alsa_mixer_create_called);
231   cras_alsa_card_destroy(c);
232 }
233 
TEST(AlsaCard,CreateFailHctlLoad)234 TEST(AlsaCard, CreateFailHctlLoad) {
235   struct cras_alsa_card *c;
236   cras_alsa_card_info card_info;
237 
238   ResetStubData();
239   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
240   card_info.card_index = 0;
241   snd_hctl_load_return_value = -1;
242 
243   c = cras_alsa_card_create(&card_info, device_config_dir,
244                             fake_blacklist, NULL);
245   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
246   EXPECT_EQ(1, snd_ctl_open_called);
247   EXPECT_EQ(1, snd_ctl_close_called);
248   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
249   EXPECT_EQ(1, snd_hctl_open_called);
250   EXPECT_EQ(1, snd_hctl_nonblock_called);
251   EXPECT_EQ(1, snd_hctl_load_called);
252   EXPECT_EQ(0, cras_alsa_mixer_create_called);
253 }
254 
TEST(AlsaCard,AddSelectForHctlNoDevices)255 TEST(AlsaCard, AddSelectForHctlNoDevices) {
256   struct pollfd poll_fds[] = {
257     {3, 0, 0},
258   };
259 
260   struct cras_alsa_card *c;
261   cras_alsa_card_info card_info;
262 
263   ResetStubData();
264   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
265   card_info.card_index = 0;
266   snd_hctl_poll_descriptors_fds = poll_fds;
267   snd_hctl_poll_descriptors_num_fds = ARRAY_SIZE(poll_fds);
268 
269   c = cras_alsa_card_create(&card_info, device_config_dir,
270                             fake_blacklist, NULL);
271   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
272   EXPECT_EQ(1, snd_ctl_open_called);
273   EXPECT_EQ(1, snd_ctl_close_called);
274   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
275   EXPECT_EQ(1, snd_hctl_open_called);
276   EXPECT_EQ(1, snd_hctl_nonblock_called);
277   EXPECT_EQ(1, snd_hctl_load_called);
278   EXPECT_EQ(1, cras_alsa_mixer_create_called);
279   EXPECT_EQ(0, cras_system_add_select_fd_called);
280   cras_alsa_card_destroy(c);
281   EXPECT_EQ(0, cras_system_rm_select_fd_called);
282 }
283 
TEST(AlsaCard,AddSelectForHctlWithDevices)284 TEST(AlsaCard, AddSelectForHctlWithDevices) {
285   struct pollfd poll_fds[] = {
286     {3, 0, 0},
287   };
288   int dev_nums[] = {0};
289   int info_rets[] = {0, -1};
290 
291   struct cras_alsa_card *c;
292   cras_alsa_card_info card_info;
293 
294   ResetStubData();
295   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
296   snd_ctl_pcm_next_device_set_devs = dev_nums;
297   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
298   snd_ctl_pcm_info_rets = info_rets;
299   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
300   card_info.card_index = 0;
301   snd_hctl_poll_descriptors_fds = poll_fds;
302   snd_hctl_poll_descriptors_num_fds = ARRAY_SIZE(poll_fds);
303 
304   c = cras_alsa_card_create(&card_info, device_config_dir,
305                             fake_blacklist, NULL);
306   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
307   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
308   EXPECT_EQ(2, snd_ctl_pcm_next_device_called);
309   EXPECT_EQ(1, cras_alsa_iodev_create_called);
310   EXPECT_EQ(0, cras_alsa_iodev_index_called);
311   EXPECT_EQ(1, snd_ctl_card_info_called);
312   EXPECT_EQ(1, ucm_create_called);
313   EXPECT_EQ(1, ucm_get_dev_for_mixer_called);
314   EXPECT_EQ(1, ucm_get_flag_called);
315   EXPECT_EQ(0, strcmp(ucm_get_flag_name, "ExtraMainVolume"));
316   EXPECT_EQ(cras_card_config_dir, device_config_dir);
317   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
318   EXPECT_EQ(1, snd_hctl_open_called);
319   EXPECT_EQ(1, snd_hctl_nonblock_called);
320   EXPECT_EQ(1, snd_hctl_load_called);
321   EXPECT_EQ(1, cras_alsa_mixer_create_called);
322   ASSERT_EQ(1, cras_system_add_select_fd_called);
323   EXPECT_EQ(3, cras_system_add_select_fd_values[0]);
324   cras_alsa_card_destroy(c);
325   EXPECT_EQ(ARRAY_SIZE(poll_fds), cras_system_rm_select_fd_called);
326 }
327 
TEST(AlsaCard,CreateFailCtlCardInfo)328 TEST(AlsaCard, CreateFailCtlCardInfo) {
329   struct cras_alsa_card *c;
330   cras_alsa_card_info card_info;
331 
332   ResetStubData();
333   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
334   card_info.card_index = 0;
335   snd_ctl_card_info_ret = -1;
336   c = cras_alsa_card_create(&card_info, device_config_dir,
337                             fake_blacklist, NULL);
338   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
339   EXPECT_EQ(1, snd_ctl_open_called);
340   EXPECT_EQ(1, snd_ctl_close_called);
341   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
342   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
343 }
344 
TEST(AlsaCard,CreateNoDevices)345 TEST(AlsaCard, CreateNoDevices) {
346   struct cras_alsa_card *c;
347   cras_alsa_card_info card_info;
348 
349   ResetStubData();
350   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
351   card_info.card_index = 1;
352   c = cras_alsa_card_create(&card_info, device_config_dir,
353                             fake_blacklist, NULL);
354   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
355   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
356   EXPECT_EQ(1, snd_ctl_pcm_next_device_called);
357   EXPECT_EQ(0, cras_alsa_iodev_create_called);
358   EXPECT_EQ(0, cras_alsa_iodev_legacy_complete_init_called);
359   EXPECT_EQ(1, cras_alsa_card_get_index(c));
360   EXPECT_EQ(0, ucm_get_sections_called);
361   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
362 
363   cras_alsa_card_destroy(c);
364   EXPECT_EQ(0, cras_alsa_iodev_destroy_called);
365   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
366   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
367 }
368 
TEST(AlsaCard,CreateOneOutputNextDevError)369 TEST(AlsaCard, CreateOneOutputNextDevError) {
370   struct cras_alsa_card *c;
371   cras_alsa_card_info card_info;
372 
373   ResetStubData();
374   snd_ctl_pcm_next_device_return_error = true;
375   card_info.card_type = ALSA_CARD_TYPE_USB;
376   card_info.card_index = 0;
377   c = cras_alsa_card_create(&card_info, device_config_dir,
378                             fake_blacklist, NULL);
379   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
380   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
381   EXPECT_EQ(snd_ctl_open_called, snd_ctl_close_called);
382   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
383 }
384 
TEST(AlsaCard,CreateOneOutput)385 TEST(AlsaCard, CreateOneOutput) {
386   struct cras_alsa_card *c;
387   int dev_nums[] = {0};
388   int info_rets[] = {0, -1};
389   cras_alsa_card_info card_info;
390 
391   ResetStubData();
392   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
393   snd_ctl_pcm_next_device_set_devs = dev_nums;
394   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
395   snd_ctl_pcm_info_rets = info_rets;
396   card_info.card_type = ALSA_CARD_TYPE_USB;
397   card_info.card_index = 0;
398   c = cras_alsa_card_create(&card_info, device_config_dir,
399                             fake_blacklist, NULL);
400   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
401   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
402   EXPECT_EQ(2, snd_ctl_pcm_next_device_called);
403   EXPECT_EQ(1, cras_alsa_iodev_create_called);
404   EXPECT_EQ(1, cras_alsa_iodev_legacy_complete_init_called);
405   EXPECT_EQ(0, cras_alsa_iodev_index_called);
406   EXPECT_EQ(1, snd_ctl_card_info_called);
407   EXPECT_EQ(1, ucm_create_called);
408   EXPECT_EQ(1, ucm_get_dev_for_mixer_called);
409   EXPECT_EQ(1, ucm_get_flag_called);
410   EXPECT_EQ(0, strcmp(ucm_get_flag_name, "ExtraMainVolume"));
411   EXPECT_EQ(cras_card_config_dir, device_config_dir);
412   EXPECT_EQ(0, ucm_get_sections_called);
413   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
414 
415   cras_alsa_card_destroy(c);
416   EXPECT_EQ(1, ucm_destroy_called);
417   EXPECT_EQ(1, cras_alsa_iodev_destroy_called);
418   EXPECT_EQ(cras_alsa_iodev_create_return[0], cras_alsa_iodev_destroy_arg);
419   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
420   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
421 }
422 
TEST(AlsaCard,CreateOneOutputBlacklisted)423 TEST(AlsaCard, CreateOneOutputBlacklisted) {
424   struct cras_alsa_card *c;
425   int dev_nums[] = {0};
426   int info_rets[] = {0, -1};
427   cras_alsa_card_info card_info;
428 
429   ResetStubData();
430   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
431   snd_ctl_pcm_next_device_set_devs = dev_nums;
432   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
433   snd_ctl_pcm_info_rets = info_rets;
434   alsa_iodev_has_hctl_jacks_return = 0;
435   cras_device_blacklist_check_retval = 1;
436   card_info.card_type = ALSA_CARD_TYPE_USB;
437   card_info.card_index = 0;
438   c = cras_alsa_card_create(&card_info, device_config_dir,
439                             fake_blacklist, NULL);
440   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
441   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
442   EXPECT_EQ(2, snd_ctl_pcm_next_device_called);
443   EXPECT_EQ(1, snd_ctl_card_info_called);
444   EXPECT_EQ(0, cras_alsa_iodev_create_called);
445   EXPECT_EQ(0, cras_alsa_iodev_legacy_complete_init_called);
446   EXPECT_EQ(cras_card_config_dir, device_config_dir);
447 
448   cras_alsa_card_destroy(c);
449   EXPECT_EQ(0, cras_alsa_iodev_destroy_called);
450   EXPECT_EQ(NULL, cras_alsa_iodev_destroy_arg);
451   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
452   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
453 }
454 
TEST(AlsaCard,CreateTwoOutputs)455 TEST(AlsaCard, CreateTwoOutputs) {
456   struct cras_alsa_card *c;
457   int dev_nums[] = {0, 3};
458   int info_rets[] = {0, -1, 0};
459   cras_alsa_card_info card_info;
460 
461   ResetStubData();
462   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
463   snd_ctl_pcm_next_device_set_devs = dev_nums;
464   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
465   snd_ctl_pcm_info_rets = info_rets;
466   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
467   card_info.card_index = 0;
468   c = cras_alsa_card_create(&card_info, device_config_dir,
469                             fake_blacklist, NULL);
470   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
471   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
472   EXPECT_EQ(3, snd_ctl_pcm_next_device_called);
473   EXPECT_EQ(2, cras_alsa_iodev_create_called);
474   EXPECT_EQ(2, cras_alsa_iodev_legacy_complete_init_called);
475   EXPECT_EQ(1, cras_alsa_iodev_index_called);
476   EXPECT_EQ(1, snd_ctl_card_info_called);
477   EXPECT_EQ(cras_card_config_dir, device_config_dir);
478   EXPECT_EQ(0, ucm_get_sections_called);
479   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
480 
481   cras_alsa_card_destroy(c);
482   EXPECT_EQ(2, cras_alsa_iodev_destroy_called);
483   EXPECT_EQ(cras_alsa_iodev_create_return[1], cras_alsa_iodev_destroy_arg);
484   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
485   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
486 }
487 
TEST(AlsaCard,CreateTwoDuplicateDeviceIndex)488 TEST(AlsaCard, CreateTwoDuplicateDeviceIndex) {
489   struct cras_alsa_card *c;
490   int dev_nums[] = {0, 0};
491   int info_rets[] = {0, -1, 0};
492   cras_alsa_card_info card_info;
493 
494   ResetStubData();
495   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
496   snd_ctl_pcm_next_device_set_devs = dev_nums;
497   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
498   snd_ctl_pcm_info_rets = info_rets;
499   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
500   card_info.card_index = 0;
501   c = cras_alsa_card_create(&card_info, device_config_dir,
502                             fake_blacklist, NULL);
503   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
504   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
505   EXPECT_EQ(3, snd_ctl_pcm_next_device_called);
506   EXPECT_EQ(1, cras_alsa_iodev_create_called);
507   EXPECT_EQ(2, cras_alsa_iodev_legacy_complete_init_called);
508   EXPECT_EQ(1, cras_alsa_iodev_index_called);
509   EXPECT_EQ(1, snd_ctl_card_info_called);
510   EXPECT_EQ(cras_card_config_dir, device_config_dir);
511   EXPECT_EQ(0, ucm_get_sections_called);
512   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
513 
514   cras_alsa_card_destroy(c);
515   EXPECT_EQ(1, cras_alsa_iodev_destroy_called);
516   EXPECT_EQ(cras_alsa_iodev_create_return[0], cras_alsa_iodev_destroy_arg);
517   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
518   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
519 }
520 
TEST(AlsaCard,CreateOneInput)521 TEST(AlsaCard, CreateOneInput) {
522   struct cras_alsa_card *c;
523   int dev_nums[] = {0};
524   int info_rets[] = {-1, 0};
525   cras_alsa_card_info card_info;
526 
527   ResetStubData();
528   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
529   snd_ctl_pcm_next_device_set_devs = dev_nums;
530   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
531   snd_ctl_pcm_info_rets = info_rets;
532   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
533   card_info.card_index = 0;
534   c = cras_alsa_card_create(&card_info, device_config_dir,
535                             fake_blacklist, NULL);
536   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
537   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
538   EXPECT_EQ(2, snd_ctl_pcm_next_device_called);
539   EXPECT_EQ(1, cras_alsa_iodev_create_called);
540   EXPECT_EQ(1, cras_alsa_iodev_legacy_complete_init_called);
541   EXPECT_EQ(0, cras_alsa_iodev_index_called);
542   EXPECT_EQ(cras_card_config_dir, device_config_dir);
543   EXPECT_EQ(0, ucm_get_sections_called);
544   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
545 
546   cras_alsa_card_destroy(c);
547   EXPECT_EQ(1, cras_alsa_iodev_destroy_called);
548   EXPECT_EQ(cras_alsa_iodev_create_return[0], cras_alsa_iodev_destroy_arg);
549   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
550   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
551 }
552 
TEST(AlsaCard,CreateOneInputAndOneOutput)553 TEST(AlsaCard, CreateOneInputAndOneOutput) {
554   struct cras_alsa_card *c;
555   int dev_nums[] = {0};
556   int info_rets[] = {0, 0};
557   cras_alsa_card_info card_info;
558 
559   ResetStubData();
560   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
561   snd_ctl_pcm_next_device_set_devs = dev_nums;
562   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
563   snd_ctl_pcm_info_rets = info_rets;
564   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
565   card_info.card_index = 0;
566   c = cras_alsa_card_create(&card_info, device_config_dir,
567                             fake_blacklist, NULL);
568   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
569   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
570   EXPECT_EQ(2, snd_ctl_pcm_next_device_called);
571   EXPECT_EQ(2, cras_alsa_iodev_create_called);
572   EXPECT_EQ(2, cras_alsa_iodev_legacy_complete_init_called);
573   EXPECT_EQ(0, cras_alsa_iodev_index_called);
574   EXPECT_EQ(cras_card_config_dir, device_config_dir);
575   EXPECT_EQ(0, ucm_get_sections_called);
576   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
577 
578   cras_alsa_card_destroy(c);
579   EXPECT_EQ(2, cras_alsa_iodev_destroy_called);
580   EXPECT_EQ(cras_alsa_iodev_create_return[1], cras_alsa_iodev_destroy_arg);
581   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
582   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
583 }
584 
TEST(AlsaCard,CreateOneInputAndOneOutputTwoDevices)585 TEST(AlsaCard, CreateOneInputAndOneOutputTwoDevices) {
586   struct cras_alsa_card *c;
587   int dev_nums[] = {0, 3};
588   int info_rets[] = {0, -1, -1, 0};
589   cras_alsa_card_info card_info;
590 
591   ResetStubData();
592   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
593   snd_ctl_pcm_next_device_set_devs = dev_nums;
594   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
595   snd_ctl_pcm_info_rets = info_rets;
596   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
597   card_info.card_index = 0;
598   c = cras_alsa_card_create(&card_info, device_config_dir,
599                             fake_blacklist, NULL);
600   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
601   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
602   EXPECT_EQ(3, snd_ctl_pcm_next_device_called);
603   EXPECT_EQ(2, cras_alsa_iodev_create_called);
604   EXPECT_EQ(2, cras_alsa_iodev_legacy_complete_init_called);
605   EXPECT_EQ(0, cras_alsa_iodev_index_called);
606   EXPECT_EQ(cras_card_config_dir, device_config_dir);
607   EXPECT_EQ(0, ucm_get_sections_called);
608   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
609 
610   cras_alsa_card_destroy(c);
611   EXPECT_EQ(2, cras_alsa_iodev_destroy_called);
612   EXPECT_EQ(cras_alsa_iodev_create_return[1], cras_alsa_iodev_destroy_arg);
613   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
614   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
615 }
616 
TEST(AlsaCard,CreateOneOutputWithCoupledMixers)617 TEST(AlsaCard, CreateOneOutputWithCoupledMixers) {
618   struct cras_alsa_card *c;
619   int dev_nums[] = {0};
620   int info_rets[] = {0, -1};
621   struct mixer_name *mixer_name_1, *mixer_name_2;
622   /* Use strdup because cras_alsa_card_create will delete it. */
623   const char *name1 = strdup("MixerName1"), *name2 = strdup("MixerName2");
624 
625   cras_alsa_card_info card_info;
626 
627   ResetStubData();
628   snd_ctl_pcm_next_device_set_devs_size = ARRAY_SIZE(dev_nums);
629   snd_ctl_pcm_next_device_set_devs = dev_nums;
630   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
631   snd_ctl_pcm_info_rets = info_rets;
632   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
633   card_info.card_index = 0;
634 
635   /* Creates a list of mixer names as return value of
636    * ucm_get_coupled_mixer_names. */
637   mixer_name_1 = (struct mixer_name*)malloc(sizeof(*mixer_name_1));
638   mixer_name_2 = (struct mixer_name*)malloc(sizeof(*mixer_name_2));
639   mixer_name_1->name = name1;
640   mixer_name_2->name = name2;
641   mixer_name_1->dir = CRAS_STREAM_OUTPUT;
642   mixer_name_2->dir = CRAS_STREAM_OUTPUT;
643   mixer_name_1->type = MIXER_NAME_VOLUME;
644   mixer_name_2->type = MIXER_NAME_VOLUME;
645 
646   DL_APPEND(ucm_get_coupled_mixer_names_return_value, mixer_name_1);
647   DL_APPEND(ucm_get_coupled_mixer_names_return_value, mixer_name_2);
648 
649   c = cras_alsa_card_create(&card_info, device_config_dir,
650                             fake_blacklist, NULL);
651 
652   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
653   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
654   EXPECT_EQ(2, snd_ctl_pcm_next_device_called);
655   EXPECT_EQ(1, cras_alsa_iodev_create_called);
656   EXPECT_EQ(1, cras_alsa_iodev_legacy_complete_init_called);
657   EXPECT_EQ(0, cras_alsa_iodev_index_called);
658   EXPECT_EQ(1, snd_ctl_card_info_called);
659   EXPECT_EQ(1, ucm_create_called);
660   EXPECT_EQ(1, ucm_get_dev_for_mixer_called);
661   EXPECT_EQ(1, ucm_get_flag_called);
662   EXPECT_EQ(0, strcmp(ucm_get_flag_name, "ExtraMainVolume"));
663   EXPECT_EQ(cras_card_config_dir, device_config_dir);
664   EXPECT_EQ(0, ucm_get_sections_called);
665   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
666 
667   /* Checks cras_alsa_card_create can handle the list and pass the names to
668    * cras_alsa_mixer_create. */
669   struct mixer_name *m_name = coupled_output_names_value;
670   EXPECT_EQ(0, m_name ? strcmp(m_name->name, "MixerName1") : 1);
671   if (m_name)
672     m_name = m_name->next;
673   EXPECT_EQ(0, m_name ? strcmp(m_name->name, "MixerName2") : 1);
674 
675   cras_alsa_card_destroy(c);
676   EXPECT_EQ(1, ucm_destroy_called);
677   EXPECT_EQ(1, cras_alsa_iodev_destroy_called);
678   EXPECT_EQ(cras_alsa_iodev_create_return[0], cras_alsa_iodev_destroy_arg);
679   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
680   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
681 
682   mixer_name_free(coupled_output_names_value);
683   coupled_output_names_value = NULL;
684 }
685 
TEST(AlsaCard,CreateFullyUCMNoSections)686 TEST(AlsaCard, CreateFullyUCMNoSections) {
687   struct cras_alsa_card *c;
688   cras_alsa_card_info card_info;
689 
690   ResetStubData();
691   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
692   card_info.card_index = 0;
693   ucm_has_fully_specified_ucm_flag_return_value = 1;
694   ucm_get_sections_return_value = NULL;
695   c = cras_alsa_card_create(&card_info, device_config_dir,
696                             fake_blacklist, NULL);
697   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
698   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
699   EXPECT_EQ(0, cras_alsa_iodev_create_called);
700   EXPECT_EQ(0, cras_alsa_iodev_ucm_complete_init_called);
701   EXPECT_EQ(1, snd_ctl_card_info_called);
702   EXPECT_EQ(1, ucm_get_sections_called);
703   EXPECT_EQ(0, cras_alsa_mixer_add_controls_in_section_called);
704 
705   cras_alsa_card_destroy(c);
706   EXPECT_EQ(1, ucm_destroy_called);
707   EXPECT_EQ(0, cras_alsa_iodev_destroy_called);
708   EXPECT_EQ(NULL, cras_alsa_iodev_destroy_arg);
709   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
710   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
711 }
712 
GenerateUcmSections(void)713 struct ucm_section *GenerateUcmSections (void) {
714   struct ucm_section *sections = NULL;
715   struct ucm_section *section;
716 
717   section = ucm_section_create("Headphone", 0, CRAS_STREAM_OUTPUT,
718                                "my-sound-card Headset Jack", "gpio");
719   ucm_section_add_coupled(section, "HP-L", MIXER_NAME_VOLUME);
720   ucm_section_add_coupled(section, "HP-R", MIXER_NAME_VOLUME);
721   DL_APPEND(sections, section);
722 
723   section = ucm_section_create("Speaker", 0, CRAS_STREAM_OUTPUT,
724                                NULL, NULL);
725   ucm_section_add_coupled(section, "SPK-L", MIXER_NAME_VOLUME);
726   ucm_section_add_coupled(section, "SPK-R", MIXER_NAME_VOLUME);
727   DL_APPEND(sections, section);
728 
729   section = ucm_section_create("Internal Mic", 0, CRAS_STREAM_INPUT,
730                                NULL, NULL);
731   ucm_section_add_coupled(section, "INT-MIC-L", MIXER_NAME_VOLUME);
732   ucm_section_add_coupled(section, "INT-MIC-R", MIXER_NAME_VOLUME);
733   DL_APPEND(sections, section);
734 
735   section = ucm_section_create("Mic", 1, CRAS_STREAM_INPUT,
736                                "my-sound-card Headset Jack", "gpio");
737   ucm_section_add_coupled(section, "MIC-L", MIXER_NAME_VOLUME);
738   ucm_section_add_coupled(section, "MIC-R", MIXER_NAME_VOLUME);
739   DL_APPEND(sections, section);
740 
741   section = ucm_section_create("HDMI", 2, CRAS_STREAM_OUTPUT,
742                                NULL, NULL);
743   ucm_section_set_mixer_name(section, "HDMI");
744   DL_APPEND(sections, section);
745 
746   return sections;
747 }
748 
TEST(AlsaCard,CreateFullyUCMFailureOnControls)749 TEST(AlsaCard, CreateFullyUCMFailureOnControls) {
750   struct cras_alsa_card *c;
751   cras_alsa_card_info card_info;
752 
753   ResetStubData();
754   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
755   card_info.card_index = 0;
756   ucm_has_fully_specified_ucm_flag_return_value = 1;
757   ucm_get_sections_return_value = GenerateUcmSections();
758   ASSERT_NE(ucm_get_sections_return_value, (struct ucm_section *)NULL);
759 
760   cras_alsa_mixer_add_controls_in_section_return_value = -EINVAL;
761 
762   c = cras_alsa_card_create(&card_info, device_config_dir,
763                             fake_blacklist, NULL);
764 
765   EXPECT_EQ(static_cast<struct cras_alsa_card *>(NULL), c);
766   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
767   EXPECT_EQ(1, snd_ctl_card_info_called);
768   EXPECT_EQ(1, ucm_get_sections_called);
769   EXPECT_EQ(1, cras_alsa_mixer_add_controls_in_section_called);
770   EXPECT_EQ(0, cras_alsa_iodev_create_called);
771   EXPECT_EQ(0, cras_alsa_iodev_ucm_complete_init_called);
772 
773   cras_alsa_card_destroy(c);
774   EXPECT_EQ(1, ucm_destroy_called);
775   EXPECT_EQ(0, cras_alsa_iodev_destroy_called);
776   EXPECT_EQ(NULL, cras_alsa_iodev_destroy_arg);
777   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
778   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
779 }
780 
TEST(AlsaCard,CreateFullyUCMFourDevicesFiveSections)781 TEST(AlsaCard, CreateFullyUCMFourDevicesFiveSections) {
782   struct cras_alsa_card *c;
783   cras_alsa_card_info card_info;
784   int info_rets[] = {0, 0, 0, 0, 0, -1};
785 
786   ResetStubData();
787   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
788   card_info.card_index = 0;
789   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
790   snd_ctl_pcm_info_rets = info_rets;
791   ucm_has_fully_specified_ucm_flag_return_value = 1;
792   ucm_get_sections_return_value = GenerateUcmSections();
793   cras_alsa_iodev_index_return[cras_alsa_iodev_create_return[0]] = 0;
794   cras_alsa_iodev_index_return[cras_alsa_iodev_create_return[1]] = 0;
795   cras_alsa_iodev_index_return[cras_alsa_iodev_create_return[2]] = 1;
796   cras_alsa_iodev_index_return[cras_alsa_iodev_create_return[3]] = 2;
797   ASSERT_NE(ucm_get_sections_return_value, (struct ucm_section *)NULL);
798 
799   c = cras_alsa_card_create(&card_info, device_config_dir,
800                             fake_blacklist, NULL);
801 
802   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
803   EXPECT_EQ(snd_ctl_close_called, snd_ctl_open_called);
804   EXPECT_EQ(1, snd_ctl_card_info_called);
805   EXPECT_EQ(1, ucm_get_sections_called);
806   EXPECT_EQ(5, snd_ctl_pcm_info_called);
807   EXPECT_EQ(5, cras_alsa_mixer_add_controls_in_section_called);
808   EXPECT_EQ(4, cras_alsa_iodev_create_called);
809   EXPECT_EQ(5, cras_alsa_iodev_ucm_add_nodes_and_jacks_called);
810   EXPECT_EQ(4, cras_alsa_iodev_ucm_complete_init_called);
811 
812   cras_alsa_card_destroy(c);
813   EXPECT_EQ(1, ucm_destroy_called);
814   EXPECT_EQ(4, cras_alsa_iodev_destroy_called);
815   EXPECT_EQ(cras_alsa_iodev_create_return[3], cras_alsa_iodev_destroy_arg);
816   EXPECT_EQ(cras_alsa_mixer_create_called, cras_alsa_mixer_destroy_called);
817   EXPECT_EQ(iniparser_load_called, iniparser_freedict_called);
818 }
819 
TEST(AlsaCard,GG)820 TEST(AlsaCard, GG) {
821   struct cras_alsa_card *c;
822   cras_alsa_card_info card_info;
823   int info_rets[] = {0, 0, 0, 0, 0, -1};
824   struct cras_ionode nodes[4];
825   const char *echo_ref = "echo ref";
826 
827   ResetStubData();
828   card_info.card_type = ALSA_CARD_TYPE_INTERNAL;
829   card_info.card_index = 0;
830   snd_ctl_pcm_info_rets_size = ARRAY_SIZE(info_rets);
831   snd_ctl_pcm_info_rets = info_rets;
832   ucm_has_fully_specified_ucm_flag_return_value = 1;
833   ucm_get_sections_return_value = GenerateUcmSections();
834 
835   fake_dev1.nodes = nodes;
836   fake_dev2.nodes = nodes + 1;
837   fake_dev3.nodes = nodes + 2;
838   fake_dev4.nodes = nodes + 3;
839   snprintf(nodes[0].name, CRAS_NODE_NAME_BUFFER_SIZE, "dev1");
840   snprintf(nodes[1].name, CRAS_NODE_NAME_BUFFER_SIZE, "dev2");
841   snprintf(nodes[2].name, CRAS_NODE_NAME_BUFFER_SIZE, "dev3");
842   snprintf(nodes[3].name, CRAS_NODE_NAME_BUFFER_SIZE, "echo ref");
843 
844   ucm_get_echo_reference_dev_name_for_dev_return_value[0] = strdup(echo_ref);
845 
846   c = cras_alsa_card_create(&card_info, device_config_dir,
847                             fake_blacklist, NULL);
848 
849   EXPECT_NE(static_cast<struct cras_alsa_card *>(NULL), c);
850   EXPECT_EQ(fake_dev1.echo_reference_dev, &fake_dev4);
851   cras_alsa_card_destroy(c);
852 }
853 
854 /* Stubs */
855 
856 extern "C" {
cras_alsa_mixer_create(const char * card_name)857 struct cras_alsa_mixer *cras_alsa_mixer_create(const char *card_name) {
858   cras_alsa_mixer_create_called++;
859   return cras_alsa_mixer_create_return;
860 }
861 
cras_alsa_mixer_add_controls_by_name_matching(struct cras_alsa_mixer * cmix,struct mixer_name * extra_controls,struct mixer_name * coupled_controls)862 int cras_alsa_mixer_add_controls_by_name_matching(
863     struct cras_alsa_mixer* cmix,
864     struct mixer_name *extra_controls,
865     struct mixer_name *coupled_controls) {
866   /* Duplicate coupled_output_names to verify in the end of unittest
867    * because names will get freed later in cras_alsa_card_create. */
868   struct mixer_name *control;
869   DL_FOREACH(coupled_controls, control) {
870     coupled_output_names_value =
871       mixer_name_add(coupled_output_names_value,
872                      control->name,
873                      CRAS_STREAM_OUTPUT,
874                      control->type);
875   }
876   return 0;
877 }
878 
cras_alsa_mixer_destroy(struct cras_alsa_mixer * cras_mixer)879 void cras_alsa_mixer_destroy(struct cras_alsa_mixer *cras_mixer) {
880   cras_alsa_mixer_destroy_called++;
881 }
882 
alsa_iodev_create(size_t card_index,const char * card_name,size_t device_index,const char * dev_name,const char * dev_id,enum CRAS_ALSA_CARD_TYPE card_type,int is_first,struct cras_alsa_mixer * mixer,const struct cras_card_config * config,struct cras_use_case_mgr * ucm,snd_hctl_t * hctl,enum CRAS_STREAM_DIRECTION direction,size_t usb_vid,size_t usb_pid,char * usb_serial_number)883 struct cras_iodev *alsa_iodev_create(size_t card_index,
884 				     const char *card_name,
885 				     size_t device_index,
886 				     const char *dev_name,
887 				     const char *dev_id,
888 				     enum CRAS_ALSA_CARD_TYPE card_type,
889 				     int is_first,
890 				     struct cras_alsa_mixer *mixer,
891 				     const struct cras_card_config *config,
892 				     struct cras_use_case_mgr *ucm,
893 				     snd_hctl_t *hctl,
894 				     enum CRAS_STREAM_DIRECTION direction,
895 				     size_t usb_vid,
896 				     size_t usb_pid,
897 				     char *usb_serial_number) {
898   struct cras_iodev *result = NULL;
899   if (cras_alsa_iodev_create_called < cras_alsa_iodev_create_return_size)
900     result = cras_alsa_iodev_create_return[cras_alsa_iodev_create_called];
901   cras_alsa_iodev_create_called++;
902   return result;
903 }
alsa_iodev_destroy(struct cras_iodev * iodev)904 void alsa_iodev_destroy(struct cras_iodev *iodev) {
905   cras_alsa_iodev_destroy_called++;
906   cras_alsa_iodev_destroy_arg = iodev;
907 }
alsa_iodev_legacy_complete_init(struct cras_iodev * iodev)908 int alsa_iodev_legacy_complete_init(struct cras_iodev *iodev) {
909   cras_alsa_iodev_legacy_complete_init_called++;
910   return 0;
911 }
alsa_iodev_ucm_add_nodes_and_jacks(struct cras_iodev * iodev,struct ucm_section * section)912 int alsa_iodev_ucm_add_nodes_and_jacks(struct cras_iodev *iodev,
913 				       struct ucm_section *section) {
914   cras_alsa_iodev_ucm_add_nodes_and_jacks_called++;
915   return 0;
916 }
alsa_iodev_ucm_complete_init(struct cras_iodev * iodev)917 void alsa_iodev_ucm_complete_init(struct cras_iodev *iodev) {
918   cras_alsa_iodev_ucm_complete_init_called++;
919 }
alsa_iodev_index(struct cras_iodev * iodev)920 unsigned alsa_iodev_index(struct cras_iodev *iodev) {
921   std::map<struct cras_iodev *, unsigned int>::iterator i;
922   cras_alsa_iodev_index_called++;
923   i = cras_alsa_iodev_index_return.find(iodev);
924   if (i != cras_alsa_iodev_index_return.end())
925     return i->second;
926   return 0;
927 }
alsa_iodev_has_hctl_jacks(struct cras_iodev * iodev)928 int alsa_iodev_has_hctl_jacks(struct cras_iodev *iodev) {
929   return alsa_iodev_has_hctl_jacks_return;
930 }
931 
snd_pcm_info_sizeof()932 size_t snd_pcm_info_sizeof() {
933   return 10;
934 }
snd_ctl_card_info_sizeof()935 size_t snd_ctl_card_info_sizeof() {
936   return 10;
937 }
snd_ctl_open(snd_ctl_t ** handle,const char * name,int card)938 int snd_ctl_open(snd_ctl_t **handle, const char *name, int card) {
939   snd_ctl_open_called++;
940   if (snd_ctl_open_return == 0)
941     *handle = reinterpret_cast<snd_ctl_t*>(0xff);
942   else
943     *handle = NULL;
944   return snd_ctl_open_return;
945 }
snd_ctl_close(snd_ctl_t * handle)946 int snd_ctl_close(snd_ctl_t *handle) {
947   snd_ctl_close_called++;
948   return snd_ctl_close_return;
949 }
snd_ctl_pcm_next_device(snd_ctl_t * ctl,int * device)950 int snd_ctl_pcm_next_device(snd_ctl_t *ctl, int *device) {
951   if (snd_ctl_pcm_next_device_return_error) {
952     *device = 10;
953     return -1;
954   }
955   snd_ctl_pcm_next_device_called++;
956   if (snd_ctl_pcm_next_device_set_devs_index >=
957       snd_ctl_pcm_next_device_set_devs_size) {
958     *device = -1;
959     return 0;
960   }
961   *device =
962       snd_ctl_pcm_next_device_set_devs[snd_ctl_pcm_next_device_set_devs_index];
963   snd_ctl_pcm_next_device_set_devs_index++;
964   return 0;
965 }
snd_pcm_info_set_device(snd_pcm_info_t * obj,unsigned int val)966 void snd_pcm_info_set_device(snd_pcm_info_t *obj, unsigned int val) {
967 }
snd_pcm_info_set_subdevice(snd_pcm_info_t * obj,unsigned int val)968 void snd_pcm_info_set_subdevice(snd_pcm_info_t *obj, unsigned int val) {
969 }
snd_pcm_info_set_stream(snd_pcm_info_t * obj,snd_pcm_stream_t val)970 void snd_pcm_info_set_stream(snd_pcm_info_t *obj, snd_pcm_stream_t val) {
971 }
snd_pcm_info_get_name(const snd_pcm_info_t * obj)972 const char *snd_pcm_info_get_name(const snd_pcm_info_t *obj) {
973   return "Fake device name";
974 }
snd_pcm_info_get_id(const snd_pcm_info_t * obj)975 const char *snd_pcm_info_get_id(const snd_pcm_info_t *obj) {
976   return "Fake device id";
977 }
snd_ctl_pcm_info(snd_ctl_t * ctl,snd_pcm_info_t * info)978 int snd_ctl_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t *info) {
979   int ret;
980   snd_ctl_pcm_info_called++;
981   if (snd_ctl_pcm_info_rets_index >=
982       snd_ctl_pcm_info_rets_size) {
983     return -1;
984   }
985   ret = snd_ctl_pcm_info_rets[snd_ctl_pcm_info_rets_index];
986   snd_ctl_pcm_info_rets_index++;
987   return ret;
988 }
snd_ctl_card_info(snd_ctl_t * ctl,snd_ctl_card_info_t * info)989 int snd_ctl_card_info(snd_ctl_t *ctl, snd_ctl_card_info_t *info) {
990   snd_ctl_card_info_called++;
991   return snd_ctl_card_info_ret;
992 }
snd_ctl_card_info_get_name(const snd_ctl_card_info_t * obj)993 const char *snd_ctl_card_info_get_name(const snd_ctl_card_info_t *obj) {
994   return "TestName";
995 }
snd_ctl_card_info_get_id(const snd_ctl_card_info_t * obj)996 const char *snd_ctl_card_info_get_id(const snd_ctl_card_info_t *obj) {
997   return "TestId";
998 }
snd_hctl_open(snd_hctl_t ** hctlp,const char * name,int mode)999 int snd_hctl_open(snd_hctl_t **hctlp, const char *name, int mode) {
1000   *hctlp = snd_hctl_open_pointer_val;
1001   snd_hctl_open_called++;
1002   return snd_hctl_open_return_value;
1003 }
snd_hctl_nonblock(snd_hctl_t * hctl,int nonblock)1004 int snd_hctl_nonblock(snd_hctl_t *hctl, int nonblock) {
1005   snd_hctl_nonblock_called++;
1006   return 0;
1007 }
snd_hctl_load(snd_hctl_t * hctl)1008 int snd_hctl_load(snd_hctl_t *hctl) {
1009   snd_hctl_load_called++;
1010   return snd_hctl_load_return_value;
1011 }
snd_hctl_close(snd_hctl_t * hctl)1012 int snd_hctl_close(snd_hctl_t *hctl) {
1013   snd_hctl_close_called++;
1014   return 0;
1015 }
snd_hctl_poll_descriptors_count(snd_hctl_t * hctl)1016 int snd_hctl_poll_descriptors_count(snd_hctl_t *hctl) {
1017   return snd_hctl_poll_descriptors_num_fds;
1018 }
snd_hctl_poll_descriptors(snd_hctl_t * hctl,struct pollfd * pfds,unsigned int space)1019 int snd_hctl_poll_descriptors(snd_hctl_t *hctl,
1020                               struct pollfd *pfds,
1021                               unsigned int space) {
1022   unsigned int num = MIN(space, snd_hctl_poll_descriptors_num_fds);
1023   memcpy(pfds, snd_hctl_poll_descriptors_fds, num * sizeof(*pfds));
1024   snd_hctl_poll_descriptors_called++;
1025   return num;
1026 }
snd_hctl_handle_events(snd_hctl_t * hctl)1027 int snd_hctl_handle_events(snd_hctl_t *hctl) {
1028   snd_hctl_handle_events_called++;
1029   return 0;
1030 }
1031 
cras_system_add_select_fd(int fd,void (* callback)(void * data),void * callback_data)1032 int cras_system_add_select_fd(int fd,
1033 			      void (*callback)(void *data),
1034 			      void *callback_data)
1035 {
1036   cras_system_add_select_fd_called++;
1037   cras_system_add_select_fd_values.push_back(fd);
1038   return 0;
1039 }
cras_system_rm_select_fd(int fd)1040 void cras_system_rm_select_fd(int fd)
1041 {
1042   cras_system_rm_select_fd_called++;
1043   cras_system_rm_select_fd_values.push_back(fd);
1044 }
1045 
cras_card_config_create(const char * config_path,const char * card_name)1046 struct cras_card_config *cras_card_config_create(const char *config_path,
1047 						 const char *card_name)
1048 {
1049   cras_card_config_dir = config_path;
1050   return NULL;
1051 }
1052 
cras_card_config_destroy(struct cras_card_config * card_config)1053 void cras_card_config_destroy(struct cras_card_config *card_config)
1054 {
1055 }
1056 
cras_card_config_get_volume_curve_for_control(const struct cras_card_config * card_config,const char * control_name)1057 struct cras_volume_curve *cras_card_config_get_volume_curve_for_control(
1058 		const struct cras_card_config *card_config,
1059 		const char *control_name)
1060 {
1061   return NULL;
1062 }
1063 
cras_device_blacklist_check(struct cras_device_blacklist * blacklist,unsigned vendor_id,unsigned product_id,unsigned device_index)1064 int cras_device_blacklist_check(
1065     struct cras_device_blacklist *blacklist,
1066     unsigned vendor_id,
1067     unsigned product_id,
1068     unsigned device_index) {
1069   EXPECT_EQ(fake_blacklist, blacklist);
1070 
1071   return cras_device_blacklist_check_retval;
1072 }
1073 
ucm_create(const char * name)1074 struct cras_use_case_mgr *ucm_create(const char* name) {
1075   ucm_create_called++;
1076   return reinterpret_cast<struct cras_use_case_mgr *>(0x44);
1077 }
1078 
ucm_destroy(struct cras_use_case_mgr * mgr)1079 void ucm_destroy(struct cras_use_case_mgr *mgr) {
1080   ucm_destroy_called++;
1081 }
1082 
ucm_get_dev_for_mixer(struct cras_use_case_mgr * mgr,const char * mixer,enum CRAS_STREAM_DIRECTION dir)1083 char *ucm_get_dev_for_mixer(struct cras_use_case_mgr *mgr, const char *mixer,
1084                             enum CRAS_STREAM_DIRECTION dir)
1085 {
1086   ucm_get_dev_for_mixer_called++;
1087   return strdup("device");
1088 }
1089 
ucm_get_flag(struct cras_use_case_mgr * mgr,const char * flag_name)1090 char *ucm_get_flag(struct cras_use_case_mgr *mgr, const char *flag_name) {
1091   ucm_get_flag_called++;
1092   strncpy(ucm_get_flag_name, flag_name, sizeof(ucm_get_flag_name));
1093   return NULL;
1094 }
1095 
ucm_get_coupled_mixer_names(struct cras_use_case_mgr * mgr,const char * dev)1096 struct mixer_name *ucm_get_coupled_mixer_names(
1097     struct cras_use_case_mgr *mgr, const char *dev)
1098 {
1099   return ucm_get_coupled_mixer_names_return_value;
1100 }
1101 
ucm_has_fully_specified_ucm_flag(struct cras_use_case_mgr * mgr)1102 int ucm_has_fully_specified_ucm_flag(struct cras_use_case_mgr *mgr)
1103 {
1104   return ucm_has_fully_specified_ucm_flag_return_value;
1105 }
1106 
ucm_get_sections(struct cras_use_case_mgr * mgr)1107 struct ucm_section *ucm_get_sections(struct cras_use_case_mgr *mgr)
1108 {
1109   ucm_get_sections_called++;
1110   return ucm_get_sections_return_value;
1111 }
ucm_get_echo_reference_dev_name_for_dev(struct cras_use_case_mgr * mgr,const char * dev)1112 const char *ucm_get_echo_reference_dev_name_for_dev(
1113     struct cras_use_case_mgr *mgr, const char *dev)
1114 {
1115   int idx = ucm_get_echo_reference_dev_name_for_dev_called++;
1116   return ucm_get_echo_reference_dev_name_for_dev_return_value[idx];
1117 }
1118 
cras_alsa_mixer_add_controls_in_section(struct cras_alsa_mixer * cmix,struct ucm_section * section)1119 int cras_alsa_mixer_add_controls_in_section(
1120 		struct cras_alsa_mixer *cmix,
1121 		struct ucm_section *section)
1122 {
1123   cras_alsa_mixer_add_controls_in_section_called++;
1124   return cras_alsa_mixer_add_controls_in_section_return_value;
1125 }
1126 
ucm_free_mixer_names(struct mixer_name * names)1127 void ucm_free_mixer_names(struct mixer_name *names)
1128 {
1129   struct mixer_name *m;
1130   DL_FOREACH(names, m) {
1131     DL_DELETE(names, m);
1132     free((void*)m->name);
1133     free(m);
1134   }
1135 }
1136 
1137 } /* extern "C" */
1138 
1139 }  //  namespace
1140 
main(int argc,char ** argv)1141 int main(int argc, char **argv) {
1142   ::testing::InitGoogleTest(&argc, argv);
1143   openlog(NULL, LOG_PERROR, LOG_USER);
1144   return RUN_ALL_TESTS();
1145 }
1146