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 <stdio.h>
6 #include <gtest/gtest.h>
7
8 extern "C" {
9 #include "cras_card_config.h"
10 #include "cras_types.h"
11 }
12
13 namespace {
14
15 static unsigned int cras_volume_curve_create_default_called;
16 static struct cras_volume_curve* cras_volume_curve_create_default_return;
17 static unsigned int cras_volume_curve_create_simple_step_called;
18 static long cras_volume_curve_create_simple_step_max_volume;
19 static long cras_volume_curve_create_simple_step_volume_step;
20 static struct cras_volume_curve* cras_volume_curve_create_simple_step_return;
21 static unsigned int cras_volume_curve_create_explicit_called;
22 static long cras_explicit_curve[101];
23 static struct cras_volume_curve* cras_volume_curve_create_explicit_return;
24
25 static const char CONFIG_PATH[] = CRAS_UT_TMPDIR;
26
CreateConfigFile(const char * name,const char * config_text)27 void CreateConfigFile(const char* name, const char* config_text) {
28 FILE* f;
29 char card_path[128];
30
31 snprintf(card_path, sizeof(card_path), "%s/%s", CONFIG_PATH, name);
32 f = fopen(card_path, "w");
33 if (f == NULL)
34 return;
35
36 fprintf(f, "%s", config_text);
37
38 fclose(f);
39 }
40
41 class CardConfigTestSuite : public testing::Test{
42 protected:
SetUp()43 virtual void SetUp() {
44 cras_volume_curve_create_default_called = 0;
45 cras_volume_curve_create_default_return =
46 reinterpret_cast<struct cras_volume_curve*>(0x55);
47 cras_volume_curve_create_simple_step_return =
48 reinterpret_cast<struct cras_volume_curve*>(0x56);
49 cras_volume_curve_create_explicit_return =
50 reinterpret_cast<struct cras_volume_curve*>(0x57);
51 cras_volume_curve_create_simple_step_called = 0;
52 cras_volume_curve_create_simple_step_return = NULL;
53 cras_volume_curve_create_explicit_called = 0;
54 cras_volume_curve_create_explicit_return = NULL;
55 }
56 };
57
58 // Test that no config is returned if the file doesn't exist.
TEST_F(CardConfigTestSuite,NoConfigFound)59 TEST_F(CardConfigTestSuite, NoConfigFound) {
60 struct cras_card_config* config;
61
62 config = cras_card_config_create(CONFIG_PATH, "no_effing_way_this_exists");
63 EXPECT_EQ(NULL, config);
64 }
65
66 // Test an empty config file, should return a null volume curve.
TEST_F(CardConfigTestSuite,EmptyConfigFileReturnsNullVolumeCurve)67 TEST_F(CardConfigTestSuite, EmptyConfigFileReturnsNullVolumeCurve) {
68 static const char empty_config_text[] = "";
69 static const char empty_config_name[] = "EmptyConfigCard";
70 struct cras_card_config* config;
71 struct cras_volume_curve* curve;
72
73 CreateConfigFile(empty_config_name, empty_config_text);
74
75 config = cras_card_config_create(CONFIG_PATH, empty_config_name);
76 EXPECT_NE(static_cast<struct cras_card_config*>(NULL), config);
77
78 curve = cras_card_config_get_volume_curve_for_control(config, "asdf");
79 EXPECT_EQ(0, cras_volume_curve_create_default_called);
80 EXPECT_EQ(static_cast<struct cras_volume_curve*>(NULL), curve);
81
82 cras_card_config_destroy(config);
83 }
84
85 // Getting a curve from a null config should always return null volume curve.
TEST_F(CardConfigTestSuite,NullConfigGivesDefaultVolumeCurve)86 TEST_F(CardConfigTestSuite, NullConfigGivesDefaultVolumeCurve) {
87 struct cras_volume_curve* curve;
88
89 curve = cras_card_config_get_volume_curve_for_control(NULL, "asdf");
90 EXPECT_EQ(0, cras_volume_curve_create_default_called);
91 EXPECT_EQ(static_cast<struct cras_volume_curve*>(NULL), curve);
92 }
93
94 // Test getting a curve from a simple_step configuration.
TEST_F(CardConfigTestSuite,SimpleStepConfig)95 TEST_F(CardConfigTestSuite, SimpleStepConfig) {
96 static const char simple_config_name[] = "simple";
97 static const char simple_config_text[] =
98 "[Card1]\n"
99 "volume_curve = simple_step\n"
100 "volume_step = 75\n"
101 "max_volume = -600\n";
102 struct cras_card_config* config;
103 struct cras_volume_curve* curve;
104
105 CreateConfigFile(simple_config_name, simple_config_text);
106
107 config = cras_card_config_create(CONFIG_PATH, simple_config_name);
108 EXPECT_NE(static_cast<struct cras_card_config*>(NULL), config);
109
110 // Unknown config should return default curve.
111 curve = cras_card_config_get_volume_curve_for_control(NULL, "asdf");
112 EXPECT_EQ(0, cras_volume_curve_create_default_called);
113 EXPECT_EQ(static_cast<struct cras_volume_curve*>(NULL), curve);
114
115 // Test a config that specifies simple_step.
116 curve = cras_card_config_get_volume_curve_for_control(config, "Card1");
117 EXPECT_EQ(cras_volume_curve_create_simple_step_return, curve);
118 EXPECT_EQ(0, cras_volume_curve_create_default_called);
119 EXPECT_EQ(1, cras_volume_curve_create_simple_step_called);
120 EXPECT_EQ(-600, cras_volume_curve_create_simple_step_max_volume);
121 EXPECT_EQ(75, cras_volume_curve_create_simple_step_volume_step);
122
123 cras_card_config_destroy(config);
124 }
125
126 // Test getting a curve from an explicit configuration.
TEST_F(CardConfigTestSuite,ExplicitCurveConfig)127 TEST_F(CardConfigTestSuite, ExplicitCurveConfig) {
128 static const char explicit_config_name[] = "explicit";
129 static const char explicit_config_text[] =
130 "[Card1]\n"
131 "volume_curve = explicit\n"
132 "dB_at_0 = -9950\n"
133 "dB_at_1 = -9850\n"
134 "dB_at_2 = -9750\n"
135 "dB_at_3 = -9650\n"
136 "dB_at_4 = -9550\n"
137 "dB_at_5 = -9450\n"
138 "dB_at_6 = -9350\n"
139 "dB_at_7 = -9250\n"
140 "dB_at_8 = -9150\n"
141 "dB_at_9 = -9050\n"
142 "dB_at_10 = -8950\n"
143 "dB_at_11 = -8850\n"
144 "dB_at_12 = -8750\n"
145 "dB_at_13 = -8650\n"
146 "dB_at_14 = -8550\n"
147 "dB_at_15 = -8450\n"
148 "dB_at_16 = -8350\n"
149 "dB_at_17 = -8250\n"
150 "dB_at_18 = -8150\n"
151 "dB_at_19 = -8050\n"
152 "dB_at_20 = -7950\n"
153 "dB_at_21 = -7850\n"
154 "dB_at_22 = -7750\n"
155 "dB_at_23 = -7650\n"
156 "dB_at_24 = -7550\n"
157 "dB_at_25 = -7450\n"
158 "dB_at_26 = -7350\n"
159 "dB_at_27 = -7250\n"
160 "dB_at_28 = -7150\n"
161 "dB_at_29 = -7050\n"
162 "dB_at_30 = -6950\n"
163 "dB_at_31 = -6850\n"
164 "dB_at_32 = -6750\n"
165 "dB_at_33 = -6650\n"
166 "dB_at_34 = -6550\n"
167 "dB_at_35 = -6450\n"
168 "dB_at_36 = -6350\n"
169 "dB_at_37 = -6250\n"
170 "dB_at_38 = -6150\n"
171 "dB_at_39 = -6050\n"
172 "dB_at_40 = -5950\n"
173 "dB_at_41 = -5850\n"
174 "dB_at_42 = -5750\n"
175 "dB_at_43 = -5650\n"
176 "dB_at_44 = -5550\n"
177 "dB_at_45 = -5450\n"
178 "dB_at_46 = -5350\n"
179 "dB_at_47 = -5250\n"
180 "dB_at_48 = -5150\n"
181 "dB_at_49 = -5050\n"
182 "dB_at_50 = -4950\n"
183 "dB_at_51 = -4850\n"
184 "dB_at_52 = -4750\n"
185 "dB_at_53 = -4650\n"
186 "dB_at_54 = -4550\n"
187 "dB_at_55 = -4450\n"
188 "dB_at_56 = -4350\n"
189 "dB_at_57 = -4250\n"
190 "dB_at_58 = -4150\n"
191 "dB_at_59 = -4050\n"
192 "dB_at_60 = -3950\n"
193 "dB_at_61 = -3850\n"
194 "dB_at_62 = -3750\n"
195 "dB_at_63 = -3650\n"
196 "dB_at_64 = -3550\n"
197 "dB_at_65 = -3450\n"
198 "dB_at_66 = -3350\n"
199 "dB_at_67 = -3250\n"
200 "dB_at_68 = -3150\n"
201 "dB_at_69 = -3050\n"
202 "dB_at_70 = -2950\n"
203 "dB_at_71 = -2850\n"
204 "dB_at_72 = -2750\n"
205 "dB_at_73 = -2650\n"
206 "dB_at_74 = -2550\n"
207 "dB_at_75 = -2450\n"
208 "dB_at_76 = -2350\n"
209 "dB_at_77 = -2250\n"
210 "dB_at_78 = -2150\n"
211 "dB_at_79 = -2050\n"
212 "dB_at_80 = -1950\n"
213 "dB_at_81 = -1850\n"
214 "dB_at_82 = -1750\n"
215 "dB_at_83 = -1650\n"
216 "dB_at_84 = -1550\n"
217 "dB_at_85 = -1450\n"
218 "dB_at_86 = -1350\n"
219 "dB_at_87 = -1250\n"
220 "dB_at_88 = -1150\n"
221 "dB_at_89 = -1050\n"
222 "dB_at_90 = -950\n"
223 "dB_at_91 = -850\n"
224 "dB_at_92 = -750\n"
225 "dB_at_93 = -650\n"
226 "dB_at_94 = -550\n"
227 "dB_at_95 = -450\n"
228 "dB_at_96 = -350\n"
229 "dB_at_97 = -250\n"
230 "dB_at_98 = -150\n"
231 "dB_at_99 = -50\n"
232 "dB_at_100 = 50\n";
233 struct cras_card_config* config;
234 struct cras_volume_curve* curve;
235
236 CreateConfigFile(explicit_config_name, explicit_config_text);
237
238 config = cras_card_config_create(CONFIG_PATH, explicit_config_name);
239 EXPECT_NE(static_cast<struct cras_card_config*>(NULL), config);
240
241 // Test a explicit curve config.
242 curve = cras_card_config_get_volume_curve_for_control(config, "Card1");
243 EXPECT_EQ(0, cras_volume_curve_create_default_called);
244 EXPECT_EQ(0, cras_volume_curve_create_simple_step_called);
245 EXPECT_EQ(1, cras_volume_curve_create_explicit_called);
246 EXPECT_EQ(cras_volume_curve_create_explicit_return, curve);
247 for (unsigned int i = 0; i < 101; i++) {
248 EXPECT_EQ((static_cast<long>(i) - 100) * 100 + 50, cras_explicit_curve[i]);
249 }
250
251 cras_card_config_destroy(config);
252 }
253
254 // Stubs.
255 extern "C" {
256
cras_volume_curve_create_default()257 struct cras_volume_curve* cras_volume_curve_create_default() {
258 cras_volume_curve_create_default_called++;
259 return cras_volume_curve_create_default_return;
260 }
261
cras_volume_curve_create_simple_step(long max_volume,long volume_step)262 struct cras_volume_curve *cras_volume_curve_create_simple_step(
263 long max_volume,
264 long volume_step) {
265 cras_volume_curve_create_simple_step_called++;
266 cras_volume_curve_create_simple_step_max_volume = max_volume;
267 cras_volume_curve_create_simple_step_volume_step = volume_step;
268 return cras_volume_curve_create_simple_step_return;
269 }
270
cras_volume_curve_create_explicit(long dB_vals[101])271 struct cras_volume_curve *cras_volume_curve_create_explicit(
272 long dB_vals[101]) {
273 cras_volume_curve_create_explicit_called++;
274 memcpy(cras_explicit_curve, dB_vals, sizeof(cras_explicit_curve));
275 return cras_volume_curve_create_explicit_return;
276 }
277
278 }
279
280 } // namespace
281
main(int argc,char ** argv)282 int main(int argc, char** argv) {
283 ::testing::InitGoogleTest(&argc, argv);
284 return RUN_ALL_TESTS();
285 }
286