1 // Copyright 2016 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_ramp.c"
10 }
11
12 static int callback_called;
13 static void *callback_arg;
14
ResetStubData()15 void ResetStubData() {
16 callback_called = 0;
17 callback_arg = NULL;
18 }
19
20 namespace {
21
TEST(RampTestSuite,Init)22 TEST(RampTestSuite, Init) {
23 struct cras_ramp *ramp;
24 struct cras_ramp_action action;
25
26 ResetStubData();
27
28 ramp = cras_ramp_create();
29 action = cras_ramp_get_current_action(ramp);
30
31 EXPECT_EQ(action.type, CRAS_RAMP_ACTION_NONE);
32 EXPECT_FLOAT_EQ(1.0, action.scaler);
33 EXPECT_FLOAT_EQ(0.0, action.increment);
34
35 cras_ramp_destroy(ramp);
36 }
37
TEST(RampTestSuite,RampUpInitialIncrement)38 TEST(RampTestSuite, RampUpInitialIncrement) {
39 int ramp_up = 1;
40 int duration_frames = 48000;
41 float increment = 1.0 / 48000;
42 cras_ramp *ramp;
43 cras_ramp_action action;
44
45 ResetStubData();
46
47 ramp = cras_ramp_create();
48 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
49
50 action = cras_ramp_get_current_action(ramp);
51
52 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
53 EXPECT_FLOAT_EQ(0.0, action.scaler);
54 EXPECT_FLOAT_EQ(increment, action.increment);
55
56 cras_ramp_destroy(ramp);
57 }
58
TEST(RampTestSuite,RampUpUpdateRampedFrames)59 TEST(RampTestSuite, RampUpUpdateRampedFrames) {
60 int ramp_up = 1;
61 int duration_frames = 48000;
62 float increment = 1.0 / 48000;
63 int rc;
64 int ramped_frames = 512;
65 struct cras_ramp *ramp;
66 struct cras_ramp_action action;
67 float scaler = increment * ramped_frames;
68
69 ResetStubData();
70
71 ramp = cras_ramp_create();
72 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
73
74 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
75
76 action = cras_ramp_get_current_action(ramp);
77
78 EXPECT_EQ(rc, 0);
79 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
80 EXPECT_FLOAT_EQ(scaler, action.scaler);
81 EXPECT_FLOAT_EQ(increment, action.increment);
82
83 cras_ramp_destroy(ramp);
84 }
85
TEST(RampTestSuite,RampUpPassedRamp)86 TEST(RampTestSuite, RampUpPassedRamp) {
87 int ramp_up = 1;
88 int duration_frames = 48000;
89 int rc;
90 int ramped_frames = 48000;
91 struct cras_ramp *ramp;
92 struct cras_ramp_action action;
93
94 ResetStubData();
95
96 ramp = cras_ramp_create();
97 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
98
99 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
100
101 action = cras_ramp_get_current_action(ramp);
102
103 EXPECT_EQ(0, rc);
104 EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
105 EXPECT_FLOAT_EQ(1.0, action.scaler);
106 EXPECT_FLOAT_EQ(0.0, action.increment);
107
108 cras_ramp_destroy(ramp);
109 }
110
TEST(RampTestSuite,RampUpWhileHalfWayRampDown)111 TEST(RampTestSuite, RampUpWhileHalfWayRampDown) {
112 int ramp_up;
113 int duration_frames = 48000;
114 int rc;
115 int ramped_frames = 24000;
116 struct cras_ramp *ramp;
117 struct cras_ramp_action action;
118 float down_increment = -1.0 / 48000;
119 float up_increment;
120 float scaler;
121
122 ResetStubData();
123
124 ramp = cras_ramp_create();
125 ramp_up = 0;
126 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
127
128 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
129
130 // Get expected current scaler.
131 scaler = 1 + down_increment * ramped_frames;
132 // The increment will be calculated by ramping to 1 starting from scaler.
133 up_increment = (1 - scaler) / 48000;
134
135 ramp_up = 1;
136 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
137
138 action = cras_ramp_get_current_action(ramp);
139
140 EXPECT_EQ(0, rc);
141 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
142 EXPECT_FLOAT_EQ(scaler, action.scaler);
143 EXPECT_FLOAT_EQ(up_increment, action.increment);
144
145 cras_ramp_destroy(ramp);
146 }
147
TEST(RampTestSuite,RampUpWhileHalfWayRampUp)148 TEST(RampTestSuite, RampUpWhileHalfWayRampUp) {
149 int ramp_up;
150 int duration_frames = 48000;
151 int rc;
152 int ramped_frames = 24000;
153 struct cras_ramp *ramp;
154 struct cras_ramp_action action;
155 float first_increment = 1.0 / 48000;
156 float second_increment;
157 float scaler;
158
159 ResetStubData();
160
161 ramp = cras_ramp_create();
162 ramp_up = 1;
163 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
164
165 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
166
167 // Get expected current scaler.
168 scaler = first_increment * ramped_frames;
169 // The increment will be calculated by ramping to 1 starting from scaler.
170 second_increment = (1 - scaler) / 48000;
171
172 ramp_up = 1;
173 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
174
175 action = cras_ramp_get_current_action(ramp);
176
177 EXPECT_EQ(0, rc);
178 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
179 EXPECT_FLOAT_EQ(scaler, action.scaler);
180 EXPECT_FLOAT_EQ(second_increment, action.increment);
181
182 cras_ramp_destroy(ramp);
183 }
184
TEST(RampTestSuite,RampDownInitialIncrement)185 TEST(RampTestSuite, RampDownInitialIncrement) {
186 int ramp_up = 0;
187 int duration_frames = 48000;
188 float increment = -1.0 / 48000;
189 cras_ramp *ramp;
190 cras_ramp_action action;
191
192 ResetStubData();
193
194 ramp = cras_ramp_create();
195 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
196
197 action = cras_ramp_get_current_action(ramp);
198
199 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
200 EXPECT_FLOAT_EQ(1.0, action.scaler);
201 EXPECT_FLOAT_EQ(increment, action.increment);
202
203 cras_ramp_destroy(ramp);
204 }
205
TEST(RampTestSuite,RampDownUpdateRampedFrames)206 TEST(RampTestSuite, RampDownUpdateRampedFrames) {
207 int ramp_up = 0;
208 int duration_frames = 48000;
209 float increment = -1.0 / 48000;
210 int rc;
211 int ramped_frames = 512;
212 struct cras_ramp *ramp;
213 struct cras_ramp_action action;
214 float scaler = 1 + increment * ramped_frames;
215
216 ResetStubData();
217
218 ramp = cras_ramp_create();
219 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
220
221 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
222
223 action = cras_ramp_get_current_action(ramp);
224
225 EXPECT_EQ(rc, 0);
226 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
227 EXPECT_FLOAT_EQ(scaler, action.scaler);
228 EXPECT_FLOAT_EQ(increment, action.increment);
229
230 cras_ramp_destroy(ramp);
231 }
232
TEST(RampTestSuite,RampDownPassedRamp)233 TEST(RampTestSuite, RampDownPassedRamp) {
234 int ramp_up = 0;
235 int duration_frames = 48000;
236 int rc;
237 int ramped_frames = 48000;
238 struct cras_ramp *ramp;
239 struct cras_ramp_action action;
240
241 ResetStubData();
242
243 ramp = cras_ramp_create();
244 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
245
246 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
247
248 action = cras_ramp_get_current_action(ramp);
249
250 EXPECT_EQ(0, rc);
251 EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
252 EXPECT_FLOAT_EQ(1.0, action.scaler);
253 EXPECT_FLOAT_EQ(0.0, action.increment);
254
255 cras_ramp_destroy(ramp);
256 }
257
TEST(RampTestSuite,RampDownWhileHalfWayRampUp)258 TEST(RampTestSuite, RampDownWhileHalfWayRampUp) {
259 int ramp_up;
260 int duration_frames = 48000;
261 int rc;
262 int ramped_frames = 24000;
263 struct cras_ramp *ramp;
264 struct cras_ramp_action action;
265 float up_increment = 1.0 / 48000;
266 float down_increment;
267 float scaler;
268
269 ResetStubData();
270
271 ramp = cras_ramp_create();
272 // Ramp up first.
273 ramp_up = 1;
274 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
275
276 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
277
278 // Get expected current scaler.
279 scaler = up_increment * ramped_frames;
280 // The increment will be calculated by ramping to 0 starting from scaler.
281 down_increment = -scaler / duration_frames;
282
283
284 // Ramp down will start from current scaler.
285 ramp_up = 0;
286 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
287
288 action = cras_ramp_get_current_action(ramp);
289
290 EXPECT_EQ(0, rc);
291 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
292 EXPECT_FLOAT_EQ(scaler, action.scaler);
293 EXPECT_FLOAT_EQ(down_increment, action.increment);
294
295 cras_ramp_destroy(ramp);
296 }
297
TEST(RampTestSuite,RampDownWhileHalfWayRampDown)298 TEST(RampTestSuite, RampDownWhileHalfWayRampDown) {
299 int ramp_up;
300 int duration_frames = 48000;
301 int rc;
302 int ramped_frames = 24000;
303 struct cras_ramp *ramp;
304 struct cras_ramp_action action;
305 float down_increment = -1.0 / 48000;
306 float second_down_increment;
307 float scaler;
308
309 ResetStubData();
310
311 ramp = cras_ramp_create();
312 // Ramp down.
313 ramp_up = 0;
314 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
315
316 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
317
318 // Get expected current scaler.
319 scaler = 1 + down_increment * ramped_frames;
320 // The increment will be calculated by ramping to 0 starting from scaler.
321 second_down_increment = -scaler / duration_frames;
322
323
324 // Ramp down starting from current scaler.
325 ramp_up = 0;
326 cras_ramp_start(ramp, ramp_up, duration_frames, NULL, NULL);
327
328 action = cras_ramp_get_current_action(ramp);
329
330 EXPECT_EQ(0, rc);
331 EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
332 EXPECT_FLOAT_EQ(scaler, action.scaler);
333 EXPECT_FLOAT_EQ(second_down_increment, action.increment);
334
335 cras_ramp_destroy(ramp);
336 }
337
ramp_callback(void * arg)338 void ramp_callback(void *arg) {
339 callback_called++;
340 callback_arg = arg;
341 }
342
TEST(RampTestSuite,RampUpPassedRampCallback)343 TEST(RampTestSuite, RampUpPassedRampCallback) {
344 int ramp_up = 1;
345 int duration_frames = 48000;
346 int rc;
347 int ramped_frames = 48000;
348 struct cras_ramp *ramp;
349 struct cras_ramp_action action;
350 void *cb_data = reinterpret_cast<void*>(0x123);
351
352 ResetStubData();
353
354 ramp = cras_ramp_create();
355 cras_ramp_start(ramp, ramp_up, duration_frames, ramp_callback, cb_data);
356
357 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
358
359 action = cras_ramp_get_current_action(ramp);
360
361 EXPECT_EQ(0, rc);
362 EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
363 EXPECT_FLOAT_EQ(1.0, action.scaler);
364 EXPECT_FLOAT_EQ(0.0, action.increment);
365 EXPECT_EQ(1, callback_called);
366 EXPECT_EQ(cb_data, callback_arg);
367
368 cras_ramp_destroy(ramp);
369 }
370
TEST(RampTestSuite,RampDownPassedRampCallback)371 TEST(RampTestSuite, RampDownPassedRampCallback) {
372 int ramp_up = 0;
373 int duration_frames = 48000;
374 int rc;
375 int ramped_frames = 48000;
376 struct cras_ramp *ramp;
377 struct cras_ramp_action action;
378 void *cb_data = reinterpret_cast<void*>(0x123);
379
380 ResetStubData();
381
382 ramp = cras_ramp_create();
383 cras_ramp_start(ramp, ramp_up, duration_frames, ramp_callback, cb_data);
384
385 rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
386
387 action = cras_ramp_get_current_action(ramp);
388
389 EXPECT_EQ(0, rc);
390 EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
391 EXPECT_FLOAT_EQ(1.0, action.scaler);
392 EXPECT_FLOAT_EQ(0.0, action.increment);
393 EXPECT_EQ(1, callback_called);
394 EXPECT_EQ(cb_data, callback_arg);
395
396 cras_ramp_destroy(ramp);
397 }
398
399 } // namespace
400
main(int argc,char ** argv)401 int main(int argc, char **argv) {
402 ::testing::InitGoogleTest(&argc, argv);
403 int rc = RUN_ALL_TESTS();
404
405 return rc;
406 }
407