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 <stdio.h>
7 #include <syslog.h>
8 
9 #include <map>
10 
11 extern "C" {
12 #include "cras_alsa_ucm.h"
13 #include "cras_types.h"
14 #include "cras_util.h"
15 #include "utlist.h"
16 
17 //  Include C file to test static functions.
18 #include "cras_alsa_ucm.c"
19 }
20 
21 namespace {
22 
23 static int snd_use_case_mgr_open_return;
24 static snd_use_case_mgr_t* snd_use_case_mgr_open_mgr_ptr;
25 static unsigned snd_use_case_mgr_open_called;
26 static unsigned snd_use_case_mgr_close_called;
27 static unsigned snd_use_case_get_called;
28 static std::vector<std::string> snd_use_case_get_id;
29 static int snd_use_case_set_return;
30 static std::map<std::string, std::string> snd_use_case_get_value;
31 static std::map<std::string, unsigned> snd_use_case_geti_value;
32 static unsigned snd_use_case_set_called;
33 static std::vector<std::pair<std::string, std::string> > snd_use_case_set_param;
34 static std::map<std::string, const char**> fake_list;
35 static std::map<std::string, unsigned> fake_list_size;
36 static unsigned snd_use_case_free_list_called;
37 static unsigned snd_use_case_geti_called;
38 static std::vector<std::string> list_devices_callback_names;
39 static std::vector<void*> list_devices_callback_args;
40 static struct cras_use_case_mgr cras_ucm_mgr;
41 static const char* avail_verbs[] = {"HiFi", "Comment for Verb1"};
42 
ResetStubData()43 static void ResetStubData() {
44   snd_use_case_mgr_open_called = 0;
45   snd_use_case_mgr_open_return = 0;
46   snd_use_case_mgr_close_called = 0;
47   snd_use_case_set_return = 0;
48   snd_use_case_get_called = 0;
49   snd_use_case_set_called = 0;
50   snd_use_case_geti_called = 0;
51   snd_use_case_set_param.clear();
52   snd_use_case_free_list_called = 0;
53   snd_use_case_get_id.clear();
54   snd_use_case_get_value.clear();
55   snd_use_case_geti_value.clear();
56   fake_list.clear();
57   fake_list_size.clear();
58   fake_list["_verbs"] = avail_verbs;
59   fake_list_size["_verbs"] = 2;
60   list_devices_callback_names.clear();
61   list_devices_callback_args.clear();
62   snd_use_case_mgr_open_mgr_ptr = reinterpret_cast<snd_use_case_mgr_t*>(0x55);
63   cras_ucm_mgr.use_case = CRAS_STREAM_TYPE_DEFAULT;
64   cras_ucm_mgr.hotword_modifier = NULL;
65 }
66 
list_devices_callback(const char * section_name,void * arg)67 static void list_devices_callback(const char* section_name, void* arg) {
68   list_devices_callback_names.push_back(std::string(section_name));
69   list_devices_callback_args.push_back(arg);
70 }
71 
SetSectionDeviceData()72 static void SetSectionDeviceData() {
73   static const char* sections[] = {
74       "Speaker",   "Comment for Dev1", "IntMic", "Comment for Dev2",
75       "Headphone", "Comment for Dev3", "ExtMic", "Comment for Dev4",
76       "HDMI",      "Comment for Dev5"};
77   fake_list["_devices/HiFi"] = sections;
78   fake_list_size["_devices/HiFi"] = 10;
79   std::string id_1 = "=PlaybackPCM/Speaker/HiFi";
80   std::string id_2 = "=CapturePCM/IntMic/HiFi";
81   std::string id_3 = "=PlaybackPCM/Headphone/HiFi";
82   std::string id_4 = "=CapturePCM/ExtMic/HiFi";
83   std::string id_5 = "=PlaybackPCM/HDMI/HiFi";
84   std::string value_1 = "test_card:0";
85   std::string value_2 = "test_card:0";
86   std::string value_3 = "test_card:0";
87   std::string value_4 = "test_card:0";
88   std::string value_5 = "test_card:1";
89 
90   snd_use_case_get_value[id_1] = value_1;
91   snd_use_case_get_value[id_2] = value_2;
92   snd_use_case_get_value[id_3] = value_3;
93   snd_use_case_get_value[id_4] = value_4;
94   snd_use_case_get_value[id_5] = value_5;
95 }
96 
TEST(AlsaUcm,CreateFailInvalidCard)97 TEST(AlsaUcm, CreateFailInvalidCard) {
98   ResetStubData();
99   EXPECT_EQ(NULL, ucm_create(NULL));
100   EXPECT_EQ(0, snd_use_case_mgr_open_called);
101 }
102 
TEST(AlsaUcm,CreateFailCardNotFound)103 TEST(AlsaUcm, CreateFailCardNotFound) {
104   ResetStubData();
105   snd_use_case_mgr_open_return = -1;
106   EXPECT_EQ(NULL, ucm_create("foo"));
107   EXPECT_EQ(1, snd_use_case_mgr_open_called);
108 }
109 
TEST(AlsaUcm,CreateFailNoHiFi)110 TEST(AlsaUcm, CreateFailNoHiFi) {
111   ResetStubData();
112   snd_use_case_set_return = -1;
113   EXPECT_EQ(NULL, ucm_create("foo"));
114   EXPECT_EQ(1, snd_use_case_mgr_open_called);
115   EXPECT_EQ(1, snd_use_case_set_called);
116   EXPECT_EQ(1, snd_use_case_mgr_close_called);
117 }
118 
TEST(AlsaUcm,CreateSuccess)119 TEST(AlsaUcm, CreateSuccess) {
120   struct cras_use_case_mgr* mgr;
121 
122   ResetStubData();
123 
124   mgr = ucm_create("foo");
125   EXPECT_NE(static_cast<snd_use_case_mgr_t*>(NULL), mgr->mgr);
126   EXPECT_EQ(1, snd_use_case_mgr_open_called);
127   EXPECT_EQ(1, snd_use_case_set_called);
128   EXPECT_EQ(0, snd_use_case_mgr_close_called);
129 
130   ucm_destroy(mgr);
131   EXPECT_EQ(1, snd_use_case_mgr_close_called);
132 }
133 
TEST(AlsaUcm,CheckEnabledEmptyList)134 TEST(AlsaUcm, CheckEnabledEmptyList) {
135   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
136 
137   ResetStubData();
138   fake_list["_enadevs"] = NULL;
139   fake_list_size["_enadevs"] = 0;
140 
141   EXPECT_EQ(0, ucm_set_enabled(mgr, "Dev1", 0));
142   EXPECT_EQ(0, snd_use_case_set_called);
143 
144   EXPECT_EQ(0, ucm_set_enabled(mgr, "Dev1", 1));
145   EXPECT_EQ(1, snd_use_case_set_called);
146 
147   EXPECT_EQ(0, snd_use_case_free_list_called);
148 }
149 
TEST(AlsaUcm,CheckEnabledAlready)150 TEST(AlsaUcm, CheckEnabledAlready) {
151   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
152   const char* enabled[] = {"Dev2", "Dev1"};
153 
154   ResetStubData();
155 
156   fake_list["_enadevs"] = enabled;
157   fake_list_size["_enadevs"] = 2;
158 
159   EXPECT_EQ(0, ucm_set_enabled(mgr, "Dev1", 1));
160   EXPECT_EQ(0, snd_use_case_set_called);
161 
162   EXPECT_EQ(0, ucm_set_enabled(mgr, "Dev1", 0));
163   EXPECT_EQ(1, snd_use_case_set_called);
164 
165   EXPECT_EQ(2, snd_use_case_free_list_called);
166 }
167 
TEST(AlsaUcm,GetEdidForDev)168 TEST(AlsaUcm, GetEdidForDev) {
169   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
170   std::string id = "=EDIDFile/Dev1/HiFi";
171   std::string value = "EdidFileName";
172   const char* file_name;
173 
174   ResetStubData();
175 
176   snd_use_case_get_value[id] = value;
177 
178   file_name = ucm_get_edid_file_for_dev(mgr, "Dev1");
179   ASSERT_TRUE(file_name);
180   EXPECT_EQ(0, strcmp(file_name, value.c_str()));
181   free((void*)file_name);
182 
183   ASSERT_EQ(1, snd_use_case_get_called);
184   EXPECT_EQ(snd_use_case_get_id[0], id);
185 }
186 
TEST(AlsaUcm,GetCapControlForDev)187 TEST(AlsaUcm, GetCapControlForDev) {
188   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
189   char* cap_control;
190   std::string id = "=CaptureControl/Dev1/HiFi";
191   std::string value = "MIC";
192 
193   ResetStubData();
194 
195   snd_use_case_get_value[id] = value;
196 
197   cap_control = ucm_get_cap_control(mgr, "Dev1");
198   ASSERT_TRUE(cap_control);
199   EXPECT_EQ(0, strcmp(cap_control, value.c_str()));
200   free(cap_control);
201 
202   ASSERT_EQ(1, snd_use_case_get_called);
203   EXPECT_EQ(snd_use_case_get_id[0], id);
204 }
205 
TEST(AlsaUcm,GetOverrideType)206 TEST(AlsaUcm, GetOverrideType) {
207   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
208   const char* override_type_name;
209   std::string id = "=OverrideNodeType/Dev1/HiFi";
210   std::string value = "HDMI";
211 
212   ResetStubData();
213 
214   snd_use_case_get_value[id] = value;
215 
216   override_type_name = ucm_get_override_type_name(mgr, "Dev1");
217   ASSERT_TRUE(override_type_name);
218   EXPECT_EQ(0, strcmp(override_type_name, value.c_str()));
219   free((void*)override_type_name);
220 
221   ASSERT_EQ(1, snd_use_case_get_called);
222   EXPECT_EQ(snd_use_case_get_id[0], id);
223 }
224 
TEST(AlsaUcm,GetSectionsForVar)225 TEST(AlsaUcm, GetSectionsForVar) {
226   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
227   struct section_name *section_names, *c;
228 
229   ResetStubData();
230 
231   const char* sections[] = {"Sec1", "Comment for Sec1",
232                             "Sec2", "Comment for Sec2",
233                             "Sec3", "Comment for Sec3"};
234   fake_list["Identifier"] = sections;
235   fake_list_size["Identifier"] = 6;
236   std::string id_1 = "=Var/Sec1/HiFi";
237   std::string id_2 = "=Var/Sec2/HiFi";
238   std::string id_3 = "=Var/Sec3/HiFi";
239   std::string value_1 = "Value1";
240   std::string value_2 = "Value2";
241   std::string value_3 = "Value2";
242 
243   snd_use_case_get_value[id_1] = value_1;
244   snd_use_case_get_value[id_2] = value_2;
245   snd_use_case_get_value[id_3] = value_3;
246 
247   section_names = ucm_get_sections_for_var(mgr, "Var", "Value2", "Identifier",
248                                            CRAS_STREAM_OUTPUT);
249 
250   ASSERT_TRUE(section_names);
251   EXPECT_EQ(0, strcmp(section_names->name, "Sec2"));
252   EXPECT_EQ(0, strcmp(section_names->next->name, "Sec3"));
253 
254   ASSERT_EQ(3, snd_use_case_get_called);
255   EXPECT_EQ(snd_use_case_get_id[0], id_1);
256   EXPECT_EQ(snd_use_case_get_id[1], id_2);
257   EXPECT_EQ(snd_use_case_get_id[2], id_3);
258 
259   DL_FOREACH (section_names, c) {
260     DL_DELETE(section_names, c);
261     free((void*)c->name);
262     free(c);
263   }
264 }
265 
TEST(AlsaUcm,GetDevForJack)266 TEST(AlsaUcm, GetDevForJack) {
267   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
268   const char* dev_name;
269   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
270                            "Comment for Dev2"};
271 
272   ResetStubData();
273 
274   fake_list["_devices/HiFi"] = devices;
275   fake_list_size["_devices/HiFi"] = 4;
276   std::string id_1 = "=JackDev/Dev1/HiFi";
277   std::string id_2 = "=JackDev/Dev2/HiFi";
278   std::string value_1 = "Value1";
279   std::string value_2 = "Value2";
280 
281   snd_use_case_get_value[id_1] = value_1;
282   snd_use_case_get_value[id_2] = value_2;
283   dev_name = ucm_get_dev_for_jack(mgr, value_2.c_str(), CRAS_STREAM_OUTPUT);
284   ASSERT_TRUE(dev_name);
285   EXPECT_EQ(0, strcmp(dev_name, "Dev2"));
286   free((void*)dev_name);
287 
288   ASSERT_EQ(2, snd_use_case_get_called);
289   EXPECT_EQ(snd_use_case_get_id[0], id_1);
290   EXPECT_EQ(snd_use_case_get_id[1], id_2);
291 }
292 
TEST(AlsaUcm,GetDevForHeadphoneJack)293 TEST(AlsaUcm, GetDevForHeadphoneJack) {
294   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
295   const char* dev_name;
296   const char* devices[] = {"Mic", "Comment for Dev1", "Headphone",
297                            "Comment for Dev2"};
298 
299   ResetStubData();
300 
301   fake_list["_devices/HiFi"] = devices;
302   fake_list_size["_devices/HiFi"] = 4;
303   std::string id_1 = "=JackDev/Mic/HiFi";
304   std::string id_2 = "=JackDev/Headphone/HiFi";
305   std::string value = "JackValue";
306 
307   snd_use_case_get_value[id_1] = value;
308   snd_use_case_get_value[id_2] = value;
309 
310   /* Looking for jack with matched value with output direction, Headphone will
311    * be found even though Mic section has the matched value too. */
312   dev_name = ucm_get_dev_for_jack(mgr, value.c_str(), CRAS_STREAM_OUTPUT);
313 
314   ASSERT_TRUE(dev_name);
315   EXPECT_EQ(0, strcmp(dev_name, "Headphone"));
316   free((void*)dev_name);
317 }
318 
TEST(AlsaUcm,GetDevForMicJack)319 TEST(AlsaUcm, GetDevForMicJack) {
320   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
321   const char* dev_name;
322   const char* devices[] = {"Headphone", "Comment for Dev1", "Mic",
323                            "Comment for Dev2"};
324 
325   ResetStubData();
326 
327   fake_list["_devices/HiFi"] = devices;
328   fake_list_size["_devices/HiFi"] = 4;
329   std::string id_1 = "=JackDev/Headphone/HiFi";
330   std::string id_2 = "=JackDev/Mic/HiFi";
331   std::string value = "JackValue";
332 
333   snd_use_case_get_value[id_1] = value;
334   snd_use_case_get_value[id_2] = value;
335 
336   /* Looking for jack with matched value with input direction, Mic will be found
337    * even though Headphone section has the matched value too. */
338   dev_name = ucm_get_dev_for_jack(mgr, value.c_str(), CRAS_STREAM_INPUT);
339 
340   ASSERT_TRUE(dev_name);
341   EXPECT_EQ(0, strcmp(dev_name, "Mic"));
342   free((void*)dev_name);
343 }
344 
TEST(AlsaUcm,GetDevForMixer)345 TEST(AlsaUcm, GetDevForMixer) {
346   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
347   const char *dev_name_out, *dev_name_in;
348   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
349                            "Comment for Dev2"};
350 
351   ResetStubData();
352 
353   fake_list["_devices/HiFi"] = devices;
354   fake_list_size["_devices/HiFi"] = 4;
355   std::string id_1 = "=PlaybackMixerElem/Dev1/HiFi";
356   std::string id_2 = "=CaptureMixerElem/Dev2/HiFi";
357   std::string value_1 = "Value1";
358   std::string value_2 = "Value2";
359 
360   snd_use_case_get_value[id_1] = value_1;
361   snd_use_case_get_value[id_2] = value_2;
362   dev_name_out =
363       ucm_get_dev_for_mixer(mgr, value_1.c_str(), CRAS_STREAM_OUTPUT);
364   dev_name_in = ucm_get_dev_for_mixer(mgr, value_2.c_str(), CRAS_STREAM_INPUT);
365 
366   ASSERT_TRUE(dev_name_out);
367   EXPECT_EQ(0, strcmp(dev_name_out, "Dev1"));
368   free((void*)dev_name_out);
369 
370   ASSERT_TRUE(dev_name_in);
371   EXPECT_EQ(0, strcmp(dev_name_in, "Dev2"));
372   free((void*)dev_name_in);
373 }
374 
TEST(AlsaUcm,GetAlsaDeviceIndexForDevice)375 TEST(AlsaUcm, GetAlsaDeviceIndexForDevice) {
376   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
377   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
378                            "Comment for Dev2"};
379 
380   ResetStubData();
381 
382   fake_list["_devices/HiFi"] = devices;
383   fake_list_size["_devices/HiFi"] = 4;
384   std::string id_1 = "=CapturePCM/Dev1/HiFi";
385   std::string id_2 = "=PlaybackPCM/Dev2/HiFi";
386   std::string value_1 = "PCMName,1";
387   std::string value_2 = "PCMName,2";
388 
389   snd_use_case_get_value[id_1] = value_1;
390   snd_use_case_get_value[id_2] = value_2;
391   EXPECT_EQ(1, ucm_get_alsa_dev_idx_for_dev(mgr, "Dev1", CRAS_STREAM_INPUT));
392   EXPECT_EQ(2, ucm_get_alsa_dev_idx_for_dev(mgr, "Dev2", CRAS_STREAM_OUTPUT));
393 
394   ASSERT_EQ(2, snd_use_case_get_called);
395   EXPECT_EQ(snd_use_case_get_id[0], id_1);
396   EXPECT_EQ(snd_use_case_get_id[1], id_2);
397 }
398 
TEST(AlsaUcm,GetDeviceRateForDevice)399 TEST(AlsaUcm, GetDeviceRateForDevice) {
400   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
401   int input_dev_rate, output_dev_rate;
402   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
403                            "Comment for Dev2"};
404 
405   ResetStubData();
406 
407   fake_list["_devices/HiFi"] = devices;
408   fake_list_size["_devices/HiFi"] = 4;
409   std::string id_1 = "=CaptureRate/Dev1/HiFi";
410   std::string id_2 = "=PlaybackRate/Dev2/HiFi";
411   std::string value_1 = "44100";
412   std::string value_2 = "48000";
413 
414   snd_use_case_get_value[id_1] = value_1;
415   snd_use_case_get_value[id_2] = value_2;
416   input_dev_rate = ucm_get_sample_rate_for_dev(mgr, "Dev1", CRAS_STREAM_INPUT);
417   output_dev_rate =
418       ucm_get_sample_rate_for_dev(mgr, "Dev2", CRAS_STREAM_OUTPUT);
419   EXPECT_EQ(44100, input_dev_rate);
420   EXPECT_EQ(48000, output_dev_rate);
421 
422   ASSERT_EQ(2, snd_use_case_get_called);
423   EXPECT_EQ(snd_use_case_get_id[0], id_1);
424   EXPECT_EQ(snd_use_case_get_id[1], id_2);
425 }
426 
TEST(AlsaUcm,GetDeviceChannelsForDevice)427 TEST(AlsaUcm, GetDeviceChannelsForDevice) {
428   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
429   int rc;
430   size_t input_dev_channels, output_dev_channels;
431   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
432                            "Comment for Dev2"};
433 
434   ResetStubData();
435 
436   fake_list["_devices/HiFi"] = devices;
437   fake_list_size["_devices/HiFi"] = 4;
438   std::string id_1 = "=CaptureChannels/Dev1/HiFi";
439   std::string id_2 = "=PlaybackChannels/Dev2/HiFi";
440   std::string value_1 = "4";
441   std::string value_2 = "8";
442 
443   snd_use_case_get_value[id_1] = value_1;
444   snd_use_case_get_value[id_2] = value_2;
445   rc = ucm_get_channels_for_dev(mgr, "Dev1", CRAS_STREAM_INPUT,
446                                 &input_dev_channels);
447   EXPECT_EQ(0, rc);
448   EXPECT_EQ(4, input_dev_channels);
449   rc = ucm_get_channels_for_dev(mgr, "Dev2", CRAS_STREAM_OUTPUT,
450                                 &output_dev_channels);
451   EXPECT_EQ(0, rc);
452   EXPECT_EQ(8, output_dev_channels);
453 
454   ASSERT_EQ(2, snd_use_case_get_called);
455   EXPECT_EQ(snd_use_case_get_id[0], id_1);
456   EXPECT_EQ(snd_use_case_get_id[1], id_2);
457 }
458 
TEST(AlsaUcm,GetCaptureChannelMapForDevice)459 TEST(AlsaUcm, GetCaptureChannelMapForDevice) {
460   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
461   int8_t channel_layout[CRAS_CH_MAX];
462   int rc;
463 
464   ResetStubData();
465 
466   std::string id_1 = "=CaptureChannelMap/Dev1/HiFi";
467   std::string value_1 = "-1 -1 0 1 -1 -1 -1 -1 -1 -1 -1";
468 
469   snd_use_case_get_value[id_1] = value_1;
470   rc = ucm_get_capture_chmap_for_dev(mgr, "Dev1", channel_layout);
471 
472   EXPECT_EQ(0, rc);
473 
474   ASSERT_EQ(1, snd_use_case_get_called);
475   EXPECT_EQ(snd_use_case_get_id[0], id_1);
476   EXPECT_EQ(channel_layout[0], -1);
477   EXPECT_EQ(channel_layout[1], -1);
478   EXPECT_EQ(channel_layout[2], 0);
479   EXPECT_EQ(channel_layout[3], 1);
480   EXPECT_EQ(channel_layout[4], -1);
481   EXPECT_EQ(channel_layout[5], -1);
482   EXPECT_EQ(channel_layout[6], -1);
483   EXPECT_EQ(channel_layout[7], -1);
484   EXPECT_EQ(channel_layout[8], -1);
485   EXPECT_EQ(channel_layout[9], -1);
486   EXPECT_EQ(channel_layout[10], -1);
487 }
488 
TEST(AlsaUcm,GetEchoReferenceDev)489 TEST(AlsaUcm, GetEchoReferenceDev) {
490   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
491   const char* echo_ref_dev;
492 
493   ResetStubData();
494 
495   std::string id_1 = "=EchoReferenceDev/Dev1/HiFi";
496   std::string value_1 = "Echo Ref";
497 
498   snd_use_case_get_value[id_1] = value_1;
499   echo_ref_dev = ucm_get_echo_reference_dev_name_for_dev(mgr, "Dev1");
500 
501   ASSERT_EQ(1, snd_use_case_get_called);
502   EXPECT_EQ(snd_use_case_get_id[0], id_1);
503   EXPECT_EQ(0, strcmp(echo_ref_dev, value_1.c_str()));
504   free((void*)echo_ref_dev);
505 }
506 
TEST(AlsaUcm,GetHotwordModels)507 TEST(AlsaUcm, GetHotwordModels) {
508   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
509   const char* models;
510   const char* modifiers[] = {"Mod1",     "Comment1",         "Hotword Model en",
511                              "Comment2", "Hotword Model jp", "Comment3",
512                              "Mod2",     "Comment4",         "Hotword Model de",
513                              "Comment5"};
514   ResetStubData();
515 
516   fake_list["_modifiers/HiFi"] = modifiers;
517   fake_list_size["_modifiers/HiFi"] = 10;
518 
519   models = ucm_get_hotword_models(mgr);
520   ASSERT_TRUE(models);
521   EXPECT_EQ(0, strcmp(models, "en,jp,de"));
522   free((void*)models);
523 }
524 
TEST(AlsaUcm,SetHotwordModel)525 TEST(AlsaUcm, SetHotwordModel) {
526   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
527   const char* modifiers[] = {"Hotword Model en", "Comment1",
528                              "Hotword Model jp", "Comment2",
529                              "Hotword Model de", "Comment3"};
530   const char* enabled_mods[] = {"Hotword Model jp"};
531   int ret;
532   std::string id = "_modstatus/Hotword Model jp";
533   ResetStubData();
534 
535   snd_use_case_geti_value[id] = 1;
536   fake_list["_modifiers/HiFi"] = modifiers;
537   fake_list_size["_modifiers/HiFi"] = 6;
538 
539   EXPECT_EQ(-EINVAL, ucm_set_hotword_model(mgr, "zh"));
540   EXPECT_EQ(0, snd_use_case_set_called);
541 
542   ret = ucm_set_hotword_model(mgr, "jp");
543 
544   EXPECT_EQ(0, ret);
545   EXPECT_EQ(0, snd_use_case_set_called);
546   EXPECT_EQ(0, strcmp(mgr->hotword_modifier, "Hotword Model jp"));
547 
548   fake_list["_enamods"] = enabled_mods;
549   fake_list_size["_enamods"] = 1;
550   ret = ucm_set_hotword_model(mgr, "de");
551   EXPECT_EQ(0, ret);
552   EXPECT_EQ(2, snd_use_case_set_called);
553   EXPECT_EQ(1, snd_use_case_geti_called);
554   EXPECT_EQ(
555       snd_use_case_set_param[0],
556       std::make_pair(std::string("_dismod"), std::string("Hotword Model jp")));
557   EXPECT_EQ(
558       snd_use_case_set_param[1],
559       std::make_pair(std::string("_enamod"), std::string("Hotword Model de")));
560   free(mgr->hotword_modifier);
561 }
562 
TEST(AlsaUcm,DisableAllHotwordModels)563 TEST(AlsaUcm, DisableAllHotwordModels) {
564   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
565   const char* modifiers[] = {"Hotword Model en", "Comment1",
566                              "Hotword Model jp", "Comment2",
567                              "Hotword Model de", "Comment3"};
568   const char* enabled_mods[] = {"Hotword Model en"};
569   ResetStubData();
570 
571   fake_list["_modifiers/HiFi"] = modifiers;
572   fake_list_size["_modifiers/HiFi"] = 6;
573   fake_list["_enamods"] = enabled_mods;
574   fake_list_size["_enamods"] = 1;
575 
576   ucm_disable_all_hotword_models(mgr);
577 
578   EXPECT_EQ(1, snd_use_case_set_called);
579   EXPECT_EQ(
580       snd_use_case_set_param[0],
581       std::make_pair(std::string("_dismod"), std::string("Hotword Model en")));
582 }
583 
TEST(AlsaUcm,EnableHotwordModel)584 TEST(AlsaUcm, EnableHotwordModel) {
585   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
586   const char* modifiers[] = {"Hotword Model en", "Comment1",
587                              "Hotword Model jp", "Comment2",
588                              "Hotword Model de", "Comment3"};
589   const char* enabled_mods[] = {""};
590   int ret;
591   ResetStubData();
592 
593   fake_list["_modifiers/HiFi"] = modifiers;
594   fake_list_size["_modifiers/HiFi"] = 6;
595   fake_list["_enamods"] = enabled_mods;
596   fake_list_size["_enamods"] = 0;
597 
598   EXPECT_EQ(-EINVAL, ucm_enable_hotword_model(mgr));
599 
600   mgr->hotword_modifier = strdup("Hotword Model de");
601   ret = ucm_enable_hotword_model(mgr);
602 
603   EXPECT_EQ(0, ret);
604   EXPECT_EQ(1, snd_use_case_set_called);
605   EXPECT_EQ(
606       snd_use_case_set_param[0],
607       std::make_pair(std::string("_enamod"), std::string("Hotword Model de")));
608   free(mgr->hotword_modifier);
609 }
610 
TEST(AlsaUcm,SwapModeExists)611 TEST(AlsaUcm, SwapModeExists) {
612   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
613   int rc;
614   const char* modifiers_1[] = {
615       "Speaker Swap Mode", "Comment for Speaker Swap Mode",
616       "Microphone Swap Mode", "Comment for Microphone Swap Mode"};
617   const char* modifiers_2[] = {
618       "Speaker Some Mode", "Comment for Speaker Some Mode",
619       "Microphone Some Mode", "Comment for Microphone Some Mode"};
620 
621   ResetStubData();
622 
623   fake_list["_modifiers/HiFi"] = modifiers_1;
624   fake_list_size["_modifiers/HiFi"] = 4;
625   rc = ucm_swap_mode_exists(mgr);
626   EXPECT_EQ(1, rc);
627 
628   fake_list["_modifiers/HiFi"] = modifiers_2;
629   fake_list_size["_modifiers/HiFi"] = 4;
630   rc = ucm_swap_mode_exists(mgr);
631   EXPECT_EQ(0, rc);
632 }
633 
TEST(AlsaUcm,EnableSwapMode)634 TEST(AlsaUcm, EnableSwapMode) {
635   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
636   int rc;
637   const char* modifiers[] = {
638       "Speaker Swap Mode", "Comment for Speaker Swap Mode",
639       "Microphone Swap Mode", "Comment for Microphone Swap Mode"};
640   const char* modifiers_enabled[] = {"Speaker Swap Mode"};
641 
642   ResetStubData();
643 
644   fake_list["_modifiers/HiFi"] = modifiers;
645   fake_list_size["_modifiers/HiFi"] = 4;
646 
647   fake_list["_enamods"] = modifiers_enabled;
648   fake_list_size["_enamods"] = 1;
649 
650   snd_use_case_set_return = 0;
651 
652   rc = ucm_enable_swap_mode(mgr, "Headphone", 1);
653   EXPECT_EQ(-EPERM, rc);
654   EXPECT_EQ(0, snd_use_case_set_called);
655 
656   rc = ucm_enable_swap_mode(mgr, "Speaker", 1);
657   EXPECT_EQ(0, rc);
658   EXPECT_EQ(0, snd_use_case_set_called);
659 
660   rc = ucm_enable_swap_mode(mgr, "Microphone", 1);
661   EXPECT_EQ(0, rc);
662   EXPECT_EQ(1, snd_use_case_set_called);
663 }
664 
TEST(AlsaUcm,DisableSwapMode)665 TEST(AlsaUcm, DisableSwapMode) {
666   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
667   int rc;
668   const char* modifiers[] = {
669       "Speaker Swap Mode", "Comment for Speaker Swap Mode",
670       "Microphone Swap Mode", "Comment for Microphone Swap Mode"};
671   const char* modifiers_enabled[] = {"Speaker Swap Mode"};
672 
673   ResetStubData();
674 
675   fake_list["_modifiers/HiFi"] = modifiers;
676   fake_list_size["_modifiers/HiFi"] = 4;
677 
678   fake_list["_enamods"] = modifiers_enabled;
679   fake_list_size["_enamods"] = 1;
680 
681   snd_use_case_set_return = 0;
682 
683   rc = ucm_enable_swap_mode(mgr, "Headphone", 0);
684   EXPECT_EQ(-EPERM, rc);
685   EXPECT_EQ(0, snd_use_case_set_called);
686 
687   rc = ucm_enable_swap_mode(mgr, "Microphone", 0);
688   EXPECT_EQ(0, rc);
689   EXPECT_EQ(0, snd_use_case_set_called);
690 
691   rc = ucm_enable_swap_mode(mgr, "Speaker", 0);
692   EXPECT_EQ(0, rc);
693   EXPECT_EQ(1, snd_use_case_set_called);
694 }
695 
TEST(AlsaUcm,NoiseCancellationExists)696 TEST(AlsaUcm, NoiseCancellationExists) {
697   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
698   int rc;
699   const char* node = "Internal Mic";
700   const char* modifiers_1[] = {"Internal Mic Noise Cancellation", "Comment"};
701   const char* modifiers_2[] = {"Internal Mic Noise Augmentation", "Comment"};
702   const char* modifiers_3[] = {"Microphone Noise Cancellation", "Comment"};
703 
704   ResetStubData();
705 
706   fake_list["_modifiers/HiFi"] = modifiers_1;
707   fake_list_size["_modifiers/HiFi"] = 2;
708   rc = ucm_node_noise_cancellation_exists(mgr, node);
709   EXPECT_EQ(1, rc);
710 
711   fake_list["_modifiers/HiFi"] = modifiers_2;
712   fake_list_size["_modifiers/HiFi"] = 2;
713   rc = ucm_node_noise_cancellation_exists(mgr, node);
714   EXPECT_EQ(0, rc);
715 
716   fake_list["_modifiers/HiFi"] = modifiers_3;
717   fake_list_size["_modifiers/HiFi"] = 2;
718   rc = ucm_node_noise_cancellation_exists(mgr, node);
719   EXPECT_EQ(0, rc);
720 }
721 
TEST(AlsaUcm,EnableDisableNoiseCancellation)722 TEST(AlsaUcm, EnableDisableNoiseCancellation) {
723   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
724   int rc;
725   const char* modifiers[] = {"Internal Mic Noise Cancellation", "Comment1",
726                              "Microphone Noise Cancellation", "Comment2"};
727   const char* modifiers_enabled[] = {"Internal Mic Noise Cancellation"};
728 
729   ResetStubData();
730 
731   fake_list["_modifiers/HiFi"] = modifiers;
732   fake_list_size["_modifiers/HiFi"] = 4;
733 
734   fake_list["_enamods"] = modifiers_enabled;
735   fake_list_size["_enamods"] = 1;
736 
737   snd_use_case_set_return = 0;
738 
739   rc = ucm_enable_node_noise_cancellation(mgr, "Line In", 1);
740   EXPECT_EQ(-EPERM, rc);  // Modifier is not existed
741   EXPECT_EQ(0, snd_use_case_set_called);
742 
743   rc = ucm_enable_node_noise_cancellation(mgr, "Line In", 0);
744   EXPECT_EQ(-EPERM, rc);  // Modifier is not existed
745   EXPECT_EQ(0, snd_use_case_set_called);
746 
747   rc = ucm_enable_node_noise_cancellation(mgr, "Microphone", 0);
748   EXPECT_EQ(0, rc);  // Modifier is already disabled
749   EXPECT_EQ(0, snd_use_case_set_called);
750 
751   rc = ucm_enable_node_noise_cancellation(mgr, "Microphone", 1);
752   EXPECT_EQ(0, rc);
753   EXPECT_EQ(1, snd_use_case_set_called);
754 
755   snd_use_case_set_called = 0;
756 
757   rc = ucm_enable_node_noise_cancellation(mgr, "Internal Mic", 1);
758   EXPECT_EQ(0, rc);  // Modifier is already enabled
759   EXPECT_EQ(0, snd_use_case_set_called);
760 
761   rc = ucm_enable_node_noise_cancellation(mgr, "Internal Mic", 0);
762   EXPECT_EQ(0, rc);
763   EXPECT_EQ(1, snd_use_case_set_called);
764 }
765 
TEST(AlsaFlag,GetFlag)766 TEST(AlsaFlag, GetFlag) {
767   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
768   char* flag_value;
769 
770   std::string id = "=FlagName//HiFi";
771   std::string value = "1";
772   ResetStubData();
773 
774   snd_use_case_get_value[id] = value;
775 
776   flag_value = ucm_get_flag(mgr, "FlagName");
777   ASSERT_TRUE(flag_value);
778   EXPECT_EQ(0, strcmp(flag_value, value.c_str()));
779   free(flag_value);
780 
781   ASSERT_EQ(1, snd_use_case_get_called);
782   EXPECT_EQ(snd_use_case_get_id[0], id);
783 }
784 
TEST(AlsaUcm,ModifierEnabled)785 TEST(AlsaUcm, ModifierEnabled) {
786   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
787   int enabled;
788 
789   ResetStubData();
790 
791   const char* mods[] = {"Mod1", "Mod2"};
792   fake_list["_enamods"] = mods;
793   fake_list_size["_enamods"] = 2;
794 
795   enabled = modifier_enabled(mgr, "Mod1");
796   EXPECT_EQ(1, enabled);
797   enabled = modifier_enabled(mgr, "Mod2");
798   EXPECT_EQ(1, enabled);
799   enabled = modifier_enabled(mgr, "Mod3");
800   EXPECT_EQ(0, enabled);
801 }
802 
TEST(AlsaUcm,SetModifierEnabled)803 TEST(AlsaUcm, SetModifierEnabled) {
804   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
805 
806   ResetStubData();
807 
808   ucm_set_modifier_enabled(mgr, "Mod1", 1);
809   EXPECT_EQ(snd_use_case_set_param[0],
810             std::make_pair(std::string("_enamod"), std::string("Mod1")));
811   EXPECT_EQ(1, snd_use_case_set_called);
812   ucm_set_modifier_enabled(mgr, "Mod1", 0);
813   EXPECT_EQ(snd_use_case_set_param[1],
814             std::make_pair(std::string("_dismod"), std::string("Mod1")));
815   EXPECT_EQ(2, snd_use_case_set_called);
816 }
817 
TEST(AlsaUcm,EndWithSuffix)818 TEST(AlsaUcm, EndWithSuffix) {
819   EXPECT_EQ(1, ucm_str_ends_with_suffix("Foo bar", "bar"));
820   EXPECT_EQ(1, ucm_str_ends_with_suffix("bar", "bar"));
821   EXPECT_EQ(0, ucm_str_ends_with_suffix("Foo car", "bar"));
822 }
823 
TEST(AlsaUcm,SectionExistsWithName)824 TEST(AlsaUcm, SectionExistsWithName) {
825   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
826   const char* sections[] = {"Sec1", "Comment for Sec1", "Sec2",
827                             "Comment for Sec2"};
828 
829   ResetStubData();
830 
831   fake_list["Identifier"] = sections;
832   fake_list_size["Identifier"] = 4;
833   EXPECT_EQ(1, ucm_section_exists_with_name(mgr, "Sec1", "Identifier"));
834   EXPECT_EQ(1, ucm_section_exists_with_name(mgr, "Sec2", "Identifier"));
835   EXPECT_EQ(0, ucm_section_exists_with_name(mgr, "Sec3", "Identifier"));
836 }
837 
TEST(AlsaUcm,SectionExistsWithSuffix)838 TEST(AlsaUcm, SectionExistsWithSuffix) {
839   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
840 
841   ResetStubData();
842 
843   const char* sections[] = {"Sec1 Suffix1", "Comment for Sec1", "Sec2 Suffix2",
844                             "Comment for Sec2"};
845   fake_list["Identifier"] = sections;
846   fake_list_size["Identifier"] = 4;
847   EXPECT_EQ(1, ucm_section_exists_with_suffix(mgr, "Suffix1", "Identifier"));
848   EXPECT_EQ(1, ucm_section_exists_with_suffix(mgr, "Suffix2", "Identifier"));
849   EXPECT_EQ(0, ucm_section_exists_with_suffix(mgr, "Suffix3", "Identifier"));
850 }
851 
TEST(AlsaUcm,DisableSoftwareVolume)852 TEST(AlsaUcm, DisableSoftwareVolume) {
853   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
854   unsigned int disable_software_volume;
855   std::string id = "=DisableSoftwareVolume//HiFi";
856   std::string value = "1";
857 
858   ResetStubData();
859 
860   snd_use_case_get_value[id] = value;
861 
862   disable_software_volume = ucm_get_disable_software_volume(mgr);
863   ASSERT_TRUE(disable_software_volume);
864 
865   ASSERT_EQ(1, snd_use_case_get_called);
866   EXPECT_EQ(snd_use_case_get_id[0], id);
867 }
868 
TEST(AlsaUcm,GetCoupledMixersForDevice)869 TEST(AlsaUcm, GetCoupledMixersForDevice) {
870   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
871   struct mixer_name *mixer_names_1, *mixer_names_2, *c;
872   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
873                            "Comment for Dev2"};
874 
875   ResetStubData();
876 
877   fake_list["_devices/HiFi"] = devices;
878   fake_list_size["_devices/HiFi"] = 4;
879   std::string id_1 = "=CoupledMixers/Dev1/HiFi";
880   std::string value_1 = "Mixer Name1,Mixer Name2,Mixer Name3";
881   std::string id_2 = "=CoupledMixers/Dev2/HiFi";
882   std::string value_2 = "";
883   snd_use_case_get_value[id_1] = value_1;
884   snd_use_case_get_value[id_2] = value_2;
885   mixer_names_1 = ucm_get_coupled_mixer_names(mgr, "Dev1");
886   mixer_names_2 = ucm_get_coupled_mixer_names(mgr, "Dev2");
887 
888   ASSERT_TRUE(mixer_names_1);
889   EXPECT_EQ(0, strcmp(mixer_names_1->name, "Mixer Name1"));
890   EXPECT_EQ(0, strcmp(mixer_names_1->next->name, "Mixer Name2"));
891   EXPECT_EQ(0, strcmp(mixer_names_1->next->next->name, "Mixer Name3"));
892   EXPECT_EQ(NULL, mixer_names_1->next->next->next);
893 
894   EXPECT_EQ(NULL, mixer_names_2);
895 
896   DL_FOREACH (mixer_names_1, c) {
897     DL_DELETE(mixer_names_1, c);
898     free((void*)c->name);
899     free(c);
900   }
901 }
902 
TEST(AlsaUcm,FreeMixerNames)903 TEST(AlsaUcm, FreeMixerNames) {
904   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
905   struct mixer_name* mixer_names_1;
906   const char* devices[] = {"Dev1", "Comment for Dev1"};
907 
908   ResetStubData();
909 
910   fake_list["_devices/HiFi"] = devices;
911   fake_list_size["_devices/HiFi"] = 2;
912   std::string id_1 = "=CoupledMixers/Dev1/HiFi";
913   std::string value_1 = "Mixer Name1,Mixer Name2,Mixer Name3";
914   snd_use_case_get_value[id_1] = value_1;
915   mixer_names_1 = ucm_get_coupled_mixer_names(mgr, "Dev1");
916 
917   ASSERT_TRUE(mixer_names_1);
918   EXPECT_EQ(0, strcmp(mixer_names_1->name, "Mixer Name1"));
919   EXPECT_EQ(0, strcmp(mixer_names_1->next->name, "Mixer Name2"));
920   EXPECT_EQ(0, strcmp(mixer_names_1->next->next->name, "Mixer Name3"));
921   EXPECT_EQ(NULL, mixer_names_1->next->next->next);
922 
923   /* No way to actually check if memory is freed. */
924   mixer_name_free(mixer_names_1);
925 }
926 
TEST(AlsaUcm,DefaultNodeGain)927 TEST(AlsaUcm, DefaultNodeGain) {
928   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
929   long default_node_gain;
930   int ret;
931   std::string id = "=DefaultNodeGain/Internal Mic/HiFi";
932   std::string value = "-2000";
933 
934   ResetStubData();
935 
936   /* Value can be found in UCM. */
937   snd_use_case_get_value[id] = value;
938 
939   ret = ucm_get_default_node_gain(mgr, "Internal Mic", &default_node_gain);
940 
941   EXPECT_EQ(0, ret);
942   EXPECT_EQ(-2000, default_node_gain);
943 
944   ResetStubData();
945 
946   /* Value can not be found in UCM. */
947   ret = ucm_get_default_node_gain(mgr, "Internal Mic", &default_node_gain);
948 
949   ASSERT_TRUE(ret);
950 }
951 
TEST(AlsaUcm,IntrinsicSensitivity)952 TEST(AlsaUcm, IntrinsicSensitivity) {
953   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
954   long intrinsic_vol;
955   int ret;
956   std::string id = "=IntrinsicSensitivity/Internal Mic/HiFi";
957   std::string value = "-2000";
958 
959   ResetStubData();
960 
961   /* Value can be found in UCM. */
962   snd_use_case_get_value[id] = value;
963 
964   ret = ucm_get_intrinsic_sensitivity(mgr, "Internal Mic", &intrinsic_vol);
965 
966   EXPECT_EQ(0, ret);
967   EXPECT_EQ(-2000, intrinsic_vol);
968 
969   ResetStubData();
970 
971   /* Value can not be found in UCM. */
972   ret = ucm_get_intrinsic_sensitivity(mgr, "Internal Mic", &intrinsic_vol);
973 
974   ASSERT_TRUE(ret);
975 }
976 
TEST(AlsaUcm,UseFullySpecifiedUCMConfig)977 TEST(AlsaUcm, UseFullySpecifiedUCMConfig) {
978   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
979   int fully_specified_flag;
980 
981   std::string id = "=FullySpecifiedUCM//HiFi";
982   ResetStubData();
983 
984   /* Flag is not set */
985   fully_specified_flag = ucm_has_fully_specified_ucm_flag(mgr);
986   ASSERT_FALSE(fully_specified_flag);
987 
988   /* Flag is set to "1". */
989   snd_use_case_get_value[id] = std::string("1");
990   fully_specified_flag = ucm_has_fully_specified_ucm_flag(mgr);
991   ASSERT_TRUE(fully_specified_flag);
992 
993   /* Flag is set to "0". */
994   snd_use_case_get_value[id] = std::string("0");
995   fully_specified_flag = ucm_has_fully_specified_ucm_flag(mgr);
996   ASSERT_FALSE(fully_specified_flag);
997 }
998 
TEST(AlsaUcm,GetMixerNameForDevice)999 TEST(AlsaUcm, GetMixerNameForDevice) {
1000   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1001   const char *mixer_name_1, *mixer_name_2;
1002   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
1003                            "Comment for Dev2"};
1004 
1005   ResetStubData();
1006 
1007   fake_list["_devices/HiFi"] = devices;
1008   fake_list_size["_devices/HiFi"] = 4;
1009   std::string id_1 = "=PlaybackMixerElem/Dev1/HiFi";
1010   std::string id_2 = "=CaptureMixerElem/Dev2/HiFi";
1011   std::string value_1 = "MixerName1";
1012   std::string value_2 = "MixerName2";
1013 
1014   snd_use_case_get_value[id_1] = value_1;
1015   snd_use_case_get_value[id_2] = value_2;
1016   mixer_name_1 = ucm_get_playback_mixer_elem_for_dev(mgr, "Dev1");
1017   mixer_name_2 = ucm_get_capture_mixer_elem_for_dev(mgr, "Dev2");
1018 
1019   EXPECT_EQ(0, strcmp(mixer_name_1, value_1.c_str()));
1020   EXPECT_EQ(0, strcmp(mixer_name_2, value_2.c_str()));
1021   free((void*)mixer_name_1);
1022   free((void*)mixer_name_2);
1023 }
1024 
TEST(AlsaUcm,GetMainVolumeMixerName)1025 TEST(AlsaUcm, GetMainVolumeMixerName) {
1026   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1027   struct mixer_name *mixer_names_1, *mixer_names_2, *c;
1028 
1029   ResetStubData();
1030 
1031   std::string id = "=MainVolumeNames//HiFi";
1032   std::string value_1 = "Mixer Name1,Mixer Name2,Mixer Name3";
1033 
1034   snd_use_case_get_value[id] = value_1;
1035   mixer_names_1 = ucm_get_main_volume_names(mgr);
1036 
1037   ResetStubData();
1038 
1039   /* Can not find MainVolumeNames */
1040   mixer_names_2 = ucm_get_main_volume_names(mgr);
1041 
1042   ASSERT_TRUE(mixer_names_1);
1043   EXPECT_EQ(0, strcmp(mixer_names_1->name, "Mixer Name1"));
1044   EXPECT_EQ(0, strcmp(mixer_names_1->next->name, "Mixer Name2"));
1045   EXPECT_EQ(0, strcmp(mixer_names_1->next->next->name, "Mixer Name3"));
1046   EXPECT_EQ(NULL, mixer_names_1->next->next->next);
1047 
1048   DL_FOREACH (mixer_names_1, c) {
1049     DL_DELETE(mixer_names_1, c);
1050     free((void*)c->name);
1051     free(c);
1052   }
1053 
1054   EXPECT_EQ(NULL, mixer_names_2);
1055 }
1056 
TEST(AlsaUcm,ListSectionsByDeviceNameOutput)1057 TEST(AlsaUcm, ListSectionsByDeviceNameOutput) {
1058   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1059   void* callback_arg = reinterpret_cast<void*>(0x56);
1060   int listed = 0;
1061 
1062   ResetStubData();
1063   SetSectionDeviceData();
1064 
1065   listed = ucm_list_section_devices_by_device_name(
1066       mgr, CRAS_STREAM_OUTPUT, "test_card:0", list_devices_callback,
1067       callback_arg);
1068 
1069   EXPECT_EQ(2, listed);
1070   EXPECT_EQ(2, list_devices_callback_names.size());
1071   EXPECT_EQ(2, list_devices_callback_args.size());
1072 
1073   EXPECT_EQ(0, strcmp(list_devices_callback_names[0].c_str(), "Speaker"));
1074   EXPECT_EQ(callback_arg, list_devices_callback_args[0]);
1075 
1076   EXPECT_EQ(0, strcmp(list_devices_callback_names[1].c_str(), "Headphone"));
1077   EXPECT_EQ(callback_arg, list_devices_callback_args[1]);
1078 }
1079 
TEST(AlsaUcm,ListSectionsByDeviceNameInput)1080 TEST(AlsaUcm, ListSectionsByDeviceNameInput) {
1081   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1082   void* callback_arg = reinterpret_cast<void*>(0x56);
1083   int listed = 0;
1084 
1085   ResetStubData();
1086   SetSectionDeviceData();
1087 
1088   listed = ucm_list_section_devices_by_device_name(
1089       mgr, CRAS_STREAM_INPUT, "test_card:0", list_devices_callback,
1090       callback_arg);
1091 
1092   EXPECT_EQ(2, listed);
1093   EXPECT_EQ(2, list_devices_callback_names.size());
1094   EXPECT_EQ(2, list_devices_callback_args.size());
1095 
1096   EXPECT_EQ(0, strcmp(list_devices_callback_names[0].c_str(), "IntMic"));
1097   EXPECT_EQ(callback_arg, list_devices_callback_args[0]);
1098 
1099   EXPECT_EQ(0, strcmp(list_devices_callback_names[1].c_str(), "ExtMic"));
1100   EXPECT_EQ(callback_arg, list_devices_callback_args[1]);
1101 }
1102 
TEST(AlsaUcm,GetJackDevForDevice)1103 TEST(AlsaUcm, GetJackDevForDevice) {
1104   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1105   const char *jack_name_1, *jack_name_2;
1106   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
1107                            "Comment for Dev2"};
1108 
1109   ResetStubData();
1110 
1111   fake_list["_devices/HiFi"] = devices;
1112   fake_list_size["_devices/HiFi"] = 4;
1113   std::string id_1 = "=JackDev/Dev1/HiFi";
1114   std::string value_1 = "JackDev1";
1115 
1116   snd_use_case_get_value[id_1] = value_1;
1117   jack_name_1 = ucm_get_jack_dev_for_dev(mgr, "Dev1");
1118   jack_name_2 = ucm_get_jack_dev_for_dev(mgr, "Dev2");
1119 
1120   EXPECT_EQ(0, strcmp(jack_name_1, value_1.c_str()));
1121   EXPECT_EQ(NULL, jack_name_2);
1122 
1123   free((void*)jack_name_1);
1124   free((void*)jack_name_2);
1125 }
1126 
TEST(AlsaUcm,GetJackControlForDevice)1127 TEST(AlsaUcm, GetJackControlForDevice) {
1128   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1129   const char *jack_name_1, *jack_name_2;
1130   const char* devices[] = {"Dev1", "Comment for Dev1", "Dev2",
1131                            "Comment for Dev2"};
1132 
1133   ResetStubData();
1134 
1135   fake_list["_devices/HiFi"] = devices;
1136   fake_list_size["_devices/HiFi"] = 4;
1137   std::string id_1 = "=JackControl/Dev1/HiFi";
1138   std::string value_1 = "JackControl1";
1139 
1140   snd_use_case_get_value[id_1] = value_1;
1141   jack_name_1 = ucm_get_jack_control_for_dev(mgr, "Dev1");
1142   jack_name_2 = ucm_get_jack_control_for_dev(mgr, "Dev2");
1143 
1144   EXPECT_EQ(0, strcmp(jack_name_1, value_1.c_str()));
1145   EXPECT_EQ(NULL, jack_name_2);
1146 
1147   free((void*)jack_name_1);
1148   free((void*)jack_name_2);
1149 }
1150 
TEST(AlsaUcm,GetPeriodFramesForDevice)1151 TEST(AlsaUcm, GetPeriodFramesForDevice) {
1152   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1153   int dma_period_1, dma_period_2, dma_period_3;
1154   const char* devices[] = {"Dev1", "Comment for Dev1",
1155                            "Dev2", "Comment for Dev2",
1156                            "Dev3", "Comment for Dev3"};
1157 
1158   ResetStubData();
1159 
1160   fake_list["_devices/HiFi"] = devices;
1161   fake_list_size["_devices/HiFi"] = 6;
1162   std::string id_1 = "=DmaPeriodMicrosecs/Dev1/HiFi";
1163   std::string value_1 = "1000";
1164   std::string id_2 = "=DmaPeriodMicrosecs/Dev2/HiFi";
1165   std::string value_2 = "-10";
1166 
1167   snd_use_case_get_value[id_1] = value_1;
1168   snd_use_case_get_value[id_2] = value_2;
1169 
1170   dma_period_1 = ucm_get_dma_period_for_dev(mgr, "Dev1");
1171   dma_period_2 = ucm_get_dma_period_for_dev(mgr, "Dev2");
1172   dma_period_3 = ucm_get_dma_period_for_dev(mgr, "Dev3");
1173 
1174   EXPECT_EQ(1000, dma_period_1);
1175   EXPECT_EQ(0, dma_period_2);
1176   EXPECT_EQ(0, dma_period_3);
1177 }
1178 
TEST(AlsaUcm,UcmSection)1179 TEST(AlsaUcm, UcmSection) {
1180   struct ucm_section* section_list = NULL;
1181   struct ucm_section* section;
1182   struct mixer_name* controls = NULL;
1183   struct mixer_name* m_name;
1184   int dev_idx = 0;
1185   int dependent_dev_idx = -1;
1186   size_t i;
1187   enum CRAS_STREAM_DIRECTION dir = CRAS_STREAM_OUTPUT;
1188   static const char* name = "Headphone";
1189   static const char* pcm_name = "hw:0,1";
1190   static const char* jack_name = "my-card-name Headset Jack";
1191   static const char* jack_type = "gpio";
1192   static const char* mixer_name = "Control1";
1193   static const char* coupled_names[] = {"Coupled1", "Coupled2"};
1194 
1195   section =
1196       ucm_section_create(NULL, NULL, 0, -1, CRAS_STREAM_OUTPUT, NULL, NULL);
1197   EXPECT_EQ(reinterpret_cast<struct ucm_section*>(NULL), section);
1198 
1199   section = ucm_section_create(name, pcm_name, dev_idx, dependent_dev_idx, dir,
1200                                jack_name, jack_type);
1201   EXPECT_NE(reinterpret_cast<struct ucm_section*>(NULL), section);
1202   EXPECT_NE(name, section->name);
1203   EXPECT_EQ(0, strcmp(name, section->name));
1204   EXPECT_EQ(dev_idx, section->dev_idx);
1205   EXPECT_EQ(dir, section->dir);
1206   EXPECT_NE(jack_name, section->jack_name);
1207   EXPECT_NE(jack_type, section->jack_type);
1208   EXPECT_EQ(section->prev, section);
1209   EXPECT_EQ(reinterpret_cast<const char*>(NULL), section->mixer_name);
1210   EXPECT_EQ(reinterpret_cast<struct mixer_name*>(NULL), section->coupled);
1211 
1212   EXPECT_EQ(-EINVAL, ucm_section_set_mixer_name(section, NULL));
1213   EXPECT_EQ(-EINVAL, ucm_section_set_mixer_name(NULL, mixer_name));
1214   EXPECT_EQ(0, ucm_section_set_mixer_name(section, mixer_name));
1215 
1216   EXPECT_NE(section->mixer_name, mixer_name);
1217   EXPECT_EQ(0, strcmp(section->mixer_name, mixer_name));
1218 
1219   EXPECT_EQ(-EINVAL, ucm_section_add_coupled(section, NULL, MIXER_NAME_VOLUME));
1220   EXPECT_EQ(-EINVAL,
1221             ucm_section_add_coupled(NULL, coupled_names[0], MIXER_NAME_VOLUME));
1222   EXPECT_EQ(
1223       0, ucm_section_add_coupled(section, coupled_names[0], MIXER_NAME_VOLUME));
1224 
1225   EXPECT_EQ(-EINVAL, ucm_section_concat_coupled(section, NULL));
1226   EXPECT_EQ(-EINVAL, ucm_section_concat_coupled(
1227                          NULL, reinterpret_cast<struct mixer_name*>(0x1111)));
1228 
1229   controls = NULL;
1230   for (i = 1; i < ARRAY_SIZE(coupled_names); i++) {
1231     controls = mixer_name_add(controls, coupled_names[i], CRAS_STREAM_OUTPUT,
1232                               MIXER_NAME_VOLUME);
1233   }
1234   /* Add controls to the list of coupled controls for this section. */
1235   EXPECT_EQ(0, ucm_section_concat_coupled(section, controls));
1236 
1237   i = 0;
1238   DL_FOREACH (section->coupled, m_name) {
1239     EXPECT_NE(m_name->name, coupled_names[i]);
1240     EXPECT_EQ(0, strcmp(m_name->name, coupled_names[i]));
1241     i++;
1242   }
1243   EXPECT_EQ(i, ARRAY_SIZE(coupled_names));
1244 
1245   DL_APPEND(section_list, section);
1246   ucm_section_free_list(section_list);
1247 }
1248 
TEST(AlsaUcm,GetSections)1249 TEST(AlsaUcm, GetSections) {
1250   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1251   struct ucm_section* sections;
1252   struct ucm_section* section;
1253   struct mixer_name* m_name;
1254   int section_count = 0;
1255   int dev_idx;
1256   int i = 0;
1257   const char* devices[] = {"Headphone",    "The headphones jack.",
1258                            "Speaker",      "The speakers.",
1259                            "Mic",          "Microphone jack.",
1260                            "Internal Mic", "Internal Microphones",
1261                            "HDMI",         "HDMI output"};
1262   const char* ids[] = {"=PlaybackPCM/Headphone/HiFi",
1263                        "=JackDev/Headphone/HiFi",
1264                        "=JackSwitch/Headphone/HiFi",
1265                        "=CoupledMixers/Headphone/HiFi",
1266 
1267                        "=PlaybackPCM/Speaker/HiFi",
1268                        "=CoupledMixers/Speaker/HiFi",
1269                        "=DependentPCM/Speaker/HiFi",
1270 
1271                        "=CapturePCM/Mic/HiFi",
1272                        "=JackDev/Mic/HiFi",
1273                        "=JackSwitch/Mic/HiFi",
1274                        "=CaptureMixerElem/Mic/HiFi",
1275 
1276                        "=CapturePCM/Internal Mic/HiFi",
1277                        "=CoupledMixers/Internal Mic/HiFi",
1278                        "=JackSwitch/Internal Mic/HiFi",
1279 
1280                        "=PlaybackPCM/HDMI/HiFi",
1281                        "=PlaybackMixerElem/HDMI/HiFi",
1282 
1283                        NULL};
1284   const char* values[] = {
1285       "hw:my-sound-card,0",
1286       "my-sound-card Headset Jack",
1287       "2",
1288       "HP-L,HP-R",
1289 
1290       "hw:my-sound-card,1",
1291       "SPK-L,SPK-R",
1292       "hw:my-sound-card,0",
1293 
1294       "hw:my-sound-card,2",
1295       "my-sound-card Headset Jack",
1296       "0",
1297       "CAPTURE",
1298 
1299       "hw:my-sound-card,3",
1300       "MIC-L,MIC-R",
1301       "-10",
1302 
1303       "hw:my-sound-card,4",
1304       "HDMI",
1305   };
1306 
1307   ResetStubData();
1308 
1309   fake_list["_devices/HiFi"] = devices;
1310   fake_list_size["_devices/HiFi"] = ARRAY_SIZE(devices);
1311 
1312   while (ids[i]) {
1313     snd_use_case_get_value[ids[i]] = values[i];
1314     i++;
1315   }
1316 
1317   sections = ucm_get_sections(mgr);
1318   ASSERT_NE(sections, (struct ucm_section*)NULL);
1319   DL_FOREACH (sections, section) { section_count++; }
1320   EXPECT_EQ(section_count, ARRAY_SIZE(devices) / 2);
1321 
1322   // Headphone
1323   section = sections;
1324   EXPECT_EQ(0, strcmp(section->pcm_name, "hw:my-sound-card,0"));
1325   EXPECT_EQ(0, strcmp(section->name, "Headphone"));
1326   EXPECT_EQ(0, section->dev_idx);
1327   EXPECT_EQ(CRAS_STREAM_OUTPUT, section->dir);
1328   EXPECT_EQ(0, strcmp(section->jack_name, values[1]));
1329   EXPECT_EQ(0, strcmp(section->jack_type, "gpio"));
1330   EXPECT_EQ(NULL, section->mixer_name);
1331   ASSERT_NE((struct mixer_name*)NULL, section->coupled);
1332   m_name = section->coupled;
1333   EXPECT_EQ(0, strcmp(m_name->name, "HP-L"));
1334   m_name = m_name->next;
1335   EXPECT_EQ(0, strcmp(m_name->name, "HP-R"));
1336   EXPECT_EQ(NULL, m_name->next);
1337   EXPECT_EQ(2, section->jack_switch);
1338   dev_idx = section->dev_idx;
1339 
1340   // Speaker
1341   section = section->next;
1342   EXPECT_EQ(0, strcmp(section->pcm_name, "hw:my-sound-card,1"));
1343   EXPECT_EQ(0, strcmp(section->name, "Speaker"));
1344   EXPECT_EQ(1, section->dev_idx);
1345   EXPECT_EQ(CRAS_STREAM_OUTPUT, section->dir);
1346   EXPECT_EQ(NULL, section->jack_name);
1347   EXPECT_EQ(NULL, section->jack_type);
1348   EXPECT_EQ(-1, section->jack_switch);
1349   EXPECT_EQ(NULL, section->mixer_name);
1350   EXPECT_EQ(dev_idx, section->dependent_dev_idx);
1351   ASSERT_NE((struct mixer_name*)NULL, section->coupled);
1352   m_name = section->coupled;
1353   EXPECT_EQ(0, strcmp(m_name->name, "SPK-L"));
1354   m_name = m_name->next;
1355   EXPECT_EQ(0, strcmp(m_name->name, "SPK-R"));
1356   EXPECT_EQ(NULL, m_name->next);
1357 
1358   // Mic
1359   section = section->next;
1360   EXPECT_EQ(0, strcmp(section->pcm_name, "hw:my-sound-card,2"));
1361   EXPECT_EQ(0, strcmp(section->name, "Mic"));
1362   EXPECT_EQ(2, section->dev_idx);
1363   EXPECT_EQ(CRAS_STREAM_INPUT, section->dir);
1364   EXPECT_EQ(0, strcmp(section->jack_name, values[1]));
1365   EXPECT_EQ(0, strcmp(section->jack_type, "gpio"));
1366   EXPECT_EQ(0, section->jack_switch);
1367   ASSERT_NE((const char*)NULL, section->mixer_name);
1368   EXPECT_EQ(0, strcmp(section->mixer_name, "CAPTURE"));
1369   EXPECT_EQ(NULL, section->coupled);
1370 
1371   // Internal Mic
1372   section = section->next;
1373   EXPECT_EQ(0, strcmp(section->pcm_name, "hw:my-sound-card,3"));
1374   EXPECT_EQ(0, strcmp(section->name, "Internal Mic"));
1375   EXPECT_EQ(3, section->dev_idx);
1376   EXPECT_EQ(CRAS_STREAM_INPUT, section->dir);
1377   EXPECT_EQ(NULL, section->jack_name);
1378   EXPECT_EQ(NULL, section->jack_type);
1379   EXPECT_EQ(-1, section->jack_switch);
1380   EXPECT_EQ(NULL, section->mixer_name);
1381   ASSERT_NE((struct mixer_name*)NULL, section->coupled);
1382   m_name = section->coupled;
1383   EXPECT_EQ(0, strcmp(m_name->name, "MIC-L"));
1384   m_name = m_name->next;
1385   EXPECT_EQ(0, strcmp(m_name->name, "MIC-R"));
1386 
1387   // HDMI
1388   section = section->next;
1389   EXPECT_EQ(0, strcmp(section->pcm_name, "hw:my-sound-card,4"));
1390   EXPECT_EQ(0, strcmp(section->name, "HDMI"));
1391   EXPECT_EQ(4, section->dev_idx);
1392   EXPECT_EQ(CRAS_STREAM_OUTPUT, section->dir);
1393   EXPECT_EQ(NULL, section->jack_name);
1394   EXPECT_EQ(NULL, section->jack_type);
1395   EXPECT_EQ(-1, section->jack_switch);
1396   ASSERT_NE((const char*)NULL, section->mixer_name);
1397   EXPECT_EQ(0, strcmp(section->mixer_name, "HDMI"));
1398 
1399   EXPECT_EQ(NULL, section->next);
1400   ucm_section_free_list(sections);
1401 }
1402 
TEST(AlsaUcm,GetSectionsMissingPCM)1403 TEST(AlsaUcm, GetSectionsMissingPCM) {
1404   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1405   struct ucm_section* sections;
1406   int i = 0;
1407   const char* devices[] = {"Headphone", "The headphones jack."};
1408   const char* ids[] = {"=JackDev/Headphone/HiFi",
1409                        "=CoupledMixers/Headphone/HiFi", NULL};
1410   const char* values[] = {
1411       "my-sound-card Headset Jack",
1412       "HP-L,HP-R",
1413   };
1414 
1415   ResetStubData();
1416 
1417   fake_list["_devices/HiFi"] = devices;
1418   fake_list_size["_devices/HiFi"] = ARRAY_SIZE(devices);
1419 
1420   while (ids[i]) {
1421     snd_use_case_get_value[ids[i]] = values[i];
1422     i++;
1423   }
1424 
1425   sections = ucm_get_sections(mgr);
1426   EXPECT_EQ(NULL, sections);
1427 }
1428 
TEST(AlsaUcm,CheckUseCaseVerbs)1429 TEST(AlsaUcm, CheckUseCaseVerbs) {
1430   struct cras_use_case_mgr* mgr = &cras_ucm_mgr;
1431 
1432   /* Verifies the mapping between stream types and verbs are correct. */
1433   mgr->use_case = CRAS_STREAM_TYPE_DEFAULT;
1434   EXPECT_EQ(0, strcmp("HiFi", uc_verb(mgr)));
1435   mgr->use_case = CRAS_STREAM_TYPE_MULTIMEDIA;
1436   EXPECT_EQ(0, strcmp("Multimedia", uc_verb(mgr)));
1437   mgr->use_case = CRAS_STREAM_TYPE_VOICE_COMMUNICATION;
1438   EXPECT_EQ(0, strcmp("Voice Call", uc_verb(mgr)));
1439   mgr->use_case = CRAS_STREAM_TYPE_SPEECH_RECOGNITION;
1440   EXPECT_EQ(0, strcmp("Speech", uc_verb(mgr)));
1441   mgr->use_case = CRAS_STREAM_TYPE_PRO_AUDIO;
1442   EXPECT_EQ(0, strcmp("Pro Audio", uc_verb(mgr)));
1443 }
1444 
TEST(AlsaUcm,GetAvailUseCases)1445 TEST(AlsaUcm, GetAvailUseCases) {
1446   struct cras_use_case_mgr* mgr;
1447   const char* verbs[] = {"HiFi",       "Comment for Verb1",
1448                          "Voice Call", "Comment for Verb2",
1449                          "Speech",     "Comment for Verb3"};
1450 
1451   ResetStubData();
1452 
1453   fake_list["_verbs"] = verbs;
1454   fake_list_size["_verbs"] = 6;
1455 
1456   mgr = ucm_create("foo");
1457   EXPECT_EQ(0x0D, mgr->avail_use_cases);
1458   ucm_destroy(mgr);
1459 }
1460 
TEST(AlsaUcm,SetUseCase)1461 TEST(AlsaUcm, SetUseCase) {
1462   struct cras_use_case_mgr* mgr;
1463   const char* verbs[] = {"HiFi",       "Comment for Verb1",
1464                          "Voice Call", "Comment for Verb2",
1465                          "Speech",     "Comment for Verb3"};
1466   int rc;
1467 
1468   ResetStubData();
1469 
1470   fake_list["_verbs"] = verbs;
1471   fake_list_size["_verbs"] = 6;
1472 
1473   mgr = ucm_create("foo");
1474   EXPECT_EQ(snd_use_case_set_param[0],
1475             std::make_pair(std::string("_verb"), std::string("HiFi")));
1476 
1477   rc = ucm_set_use_case(mgr, CRAS_STREAM_TYPE_VOICE_COMMUNICATION);
1478   EXPECT_EQ(0, rc);
1479   EXPECT_EQ(mgr->use_case, CRAS_STREAM_TYPE_VOICE_COMMUNICATION);
1480   EXPECT_EQ(snd_use_case_set_param[1],
1481             std::make_pair(std::string("_verb"), std::string("Voice Call")));
1482 
1483   /* Request unavailable use case will fail. */
1484   rc = ucm_set_use_case(mgr, CRAS_STREAM_TYPE_PRO_AUDIO);
1485   EXPECT_EQ(-1, rc);
1486   /* cras_use_case_mgr's use case should not be changed. */
1487   EXPECT_EQ(mgr->use_case, CRAS_STREAM_TYPE_VOICE_COMMUNICATION);
1488   /* And snd_use_case_set not being called. */
1489   EXPECT_EQ(2, snd_use_case_set_param.size());
1490 
1491   ucm_destroy(mgr);
1492 }
1493 
1494 /* Stubs */
1495 
1496 extern "C" {
1497 
snd_use_case_mgr_open(snd_use_case_mgr_t ** uc_mgr,const char * card_name)1498 int snd_use_case_mgr_open(snd_use_case_mgr_t** uc_mgr, const char* card_name) {
1499   snd_use_case_mgr_open_called++;
1500   *uc_mgr = snd_use_case_mgr_open_mgr_ptr;
1501   return snd_use_case_mgr_open_return;
1502 }
1503 
snd_use_case_mgr_close(snd_use_case_mgr_t * uc_mgr)1504 int snd_use_case_mgr_close(snd_use_case_mgr_t* uc_mgr) {
1505   snd_use_case_mgr_close_called++;
1506   return 0;
1507 }
1508 
snd_use_case_get(snd_use_case_mgr_t * uc_mgr,const char * identifier,const char ** value)1509 int snd_use_case_get(snd_use_case_mgr_t* uc_mgr,
1510                      const char* identifier,
1511                      const char** value) {
1512   snd_use_case_get_called++;
1513   snd_use_case_get_id.push_back(std::string(identifier));
1514   if (snd_use_case_get_value.find(identifier) == snd_use_case_get_value.end()) {
1515     *value = NULL;
1516     return -1;
1517   }
1518   *value = strdup(snd_use_case_get_value[identifier].c_str());
1519   return 0;
1520 }
1521 
snd_use_case_set(snd_use_case_mgr_t * uc_mgr,const char * identifier,const char * value)1522 int snd_use_case_set(snd_use_case_mgr_t* uc_mgr,
1523                      const char* identifier,
1524                      const char* value) {
1525   snd_use_case_set_called++;
1526   snd_use_case_set_param.push_back(
1527       std::make_pair(std::string(identifier), std::string(value)));
1528   return snd_use_case_set_return;
1529 }
1530 
snd_use_case_get_list(snd_use_case_mgr_t * uc_mgr,const char * identifier,const char ** list[])1531 int snd_use_case_get_list(snd_use_case_mgr_t* uc_mgr,
1532                           const char* identifier,
1533                           const char** list[]) {
1534   *list = fake_list[identifier];
1535   return fake_list_size[identifier];
1536 }
1537 
snd_use_case_free_list(const char * list[],int items)1538 int snd_use_case_free_list(const char* list[], int items) {
1539   snd_use_case_free_list_called++;
1540   return 0;
1541 }
1542 
snd_use_case_geti(snd_use_case_mgr_t * uc_mgr,const char * identifier,long * value)1543 int snd_use_case_geti(snd_use_case_mgr_t* uc_mgr,
1544                       const char* identifier,
1545                       long* value) {
1546   snd_use_case_geti_called++;
1547   if (snd_use_case_geti_value.find(identifier) ==
1548       snd_use_case_geti_value.end()) {
1549     *value = 0;
1550     return -1;
1551   }
1552   *value = snd_use_case_geti_value[identifier];
1553   return 0;
1554 }
1555 
1556 } /* extern "C" */
1557 
1558 }  //  namespace
1559 
main(int argc,char ** argv)1560 int main(int argc, char** argv) {
1561   ::testing::InitGoogleTest(&argc, argv);
1562   openlog(NULL, LOG_PERROR, LOG_USER);
1563   return RUN_ALL_TESTS();
1564 }
1565