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 #include <unistd.h>
8
9 extern "C" {
10 #include "audio_thread.h"
11 #include "cras_messages.h"
12 #include "cras_rclient.h"
13 #include "cras_rstream.h"
14 #include "cras_system_state.h"
15
16 // Access to data structures and static functions.
17 #include "cras_rclient.c"
18 }
19
20 // Stub data.
21 static int cras_rstream_create_return;
22 static struct cras_rstream *cras_rstream_create_stream_out;
23 static int cras_iodev_attach_stream_retval;
24 static size_t cras_system_set_volume_value;
25 static int cras_system_set_volume_called;
26 static size_t cras_system_set_capture_gain_value;
27 static int cras_system_set_capture_gain_called;
28 static size_t cras_system_set_mute_value;
29 static int cras_system_set_mute_called;
30 static size_t cras_system_set_user_mute_value;
31 static int cras_system_set_user_mute_called;
32 static size_t cras_system_set_mute_locked_value;
33 static int cras_system_set_mute_locked_called;
34 static size_t cras_system_set_capture_mute_value;
35 static int cras_system_set_capture_mute_called;
36 static size_t cras_system_set_capture_mute_locked_value;
37 static int cras_system_set_capture_mute_locked_called;
38 static int cras_system_state_dump_snapshots_called;
39 static size_t cras_make_fd_nonblocking_called;
40 static audio_thread* iodev_get_thread_return;
41 static int stream_list_add_stream_return;
42 static unsigned int stream_list_add_stream_called;
43 static unsigned int stream_list_disconnect_stream_called;
44 static unsigned int cras_iodev_list_rm_input_called;
45 static unsigned int cras_iodev_list_rm_output_called;
46 static unsigned int cras_server_metrics_stream_config_called;
47 static struct cras_rstream dummy_rstream;
48 static size_t cras_observer_num_ops_registered;
49 static size_t cras_observer_register_notify_called;
50 static size_t cras_observer_add_called;
51 static void *cras_observer_add_context_value;
52 static struct cras_observer_client *cras_observer_add_return_value;
53 static size_t cras_observer_get_ops_called;
54 static struct cras_observer_ops cras_observer_ops_value;
55 static size_t cras_observer_set_ops_called;
56 static size_t cras_observer_ops_are_empty_called;
57 static struct cras_observer_ops cras_observer_ops_are_empty_empty_ops;
58 static size_t cras_observer_remove_called;
59
ResetStubData()60 void ResetStubData() {
61 cras_rstream_create_return = 0;
62 cras_rstream_create_stream_out = (struct cras_rstream *)NULL;
63 cras_iodev_attach_stream_retval = 0;
64 cras_server_metrics_stream_config_called = 0;
65 cras_system_set_volume_value = 0;
66 cras_system_set_volume_called = 0;
67 cras_system_set_capture_gain_value = 0;
68 cras_system_set_capture_gain_called = 0;
69 cras_system_set_mute_value = 0;
70 cras_system_set_mute_called = 0;
71 cras_system_set_user_mute_value = 0;
72 cras_system_set_user_mute_called = 0;
73 cras_system_set_mute_locked_value = 0;
74 cras_system_set_mute_locked_called = 0;
75 cras_system_set_capture_mute_value = 0;
76 cras_system_set_capture_mute_called = 0;
77 cras_system_set_capture_mute_locked_value = 0;
78 cras_system_set_capture_mute_locked_called = 0;
79 cras_system_state_dump_snapshots_called = 0;
80 cras_make_fd_nonblocking_called = 0;
81 iodev_get_thread_return = reinterpret_cast<audio_thread*>(0xad);
82 stream_list_add_stream_return = 0;
83 stream_list_add_stream_called = 0;
84 stream_list_disconnect_stream_called = 0;
85 cras_iodev_list_rm_output_called = 0;
86 cras_iodev_list_rm_input_called = 0;
87 cras_observer_num_ops_registered = 0;
88 cras_observer_register_notify_called = 0;
89 cras_observer_add_called = 0;
90 cras_observer_add_return_value =
91 reinterpret_cast<struct cras_observer_client *>(1);
92 cras_observer_add_context_value = NULL;
93 cras_observer_get_ops_called = 0;
94 memset(&cras_observer_ops_value, 0, sizeof(cras_observer_ops_value));
95 cras_observer_set_ops_called = 0;
96 cras_observer_ops_are_empty_called = 0;
97 memset(&cras_observer_ops_are_empty_empty_ops, 0,
98 sizeof(cras_observer_ops_are_empty_empty_ops));
99 cras_observer_remove_called = 0;
100 }
101
102 namespace {
103
TEST(RClientSuite,CreateSendMessage)104 TEST(RClientSuite, CreateSendMessage) {
105 struct cras_rclient *rclient;
106 int rc;
107 struct cras_client_connected msg;
108 int pipe_fds[2];
109
110 ResetStubData();
111
112 rc = pipe(pipe_fds);
113 ASSERT_EQ(0, rc);
114
115 rclient = cras_rclient_create(pipe_fds[1], 800);
116 ASSERT_NE((void *)NULL, rclient);
117
118 rc = read(pipe_fds[0], &msg, sizeof(msg));
119 EXPECT_EQ(sizeof(msg), rc);
120 EXPECT_EQ(CRAS_CLIENT_CONNECTED, msg.header.id);
121
122 cras_rclient_destroy(rclient);
123 close(pipe_fds[0]);
124 close(pipe_fds[1]);
125 }
126
127 class RClientMessagesSuite : public testing::Test {
128 protected:
SetUp()129 virtual void SetUp() {
130 int rc;
131 struct cras_client_connected msg;
132
133 rc = pipe(pipe_fds_);
134 if (rc < 0)
135 return;
136 rclient_ = cras_rclient_create(pipe_fds_[1], 800);
137 rc = read(pipe_fds_[0], &msg, sizeof(msg));
138 if (rc < 0)
139 return;
140
141 rstream_ = (struct cras_rstream *)calloc(1, sizeof(*rstream_));
142
143 stream_id_ = 0x10002;
144 connect_msg_.header.id = CRAS_SERVER_CONNECT_STREAM;
145 connect_msg_.header.length = sizeof(connect_msg_);
146 connect_msg_.proto_version = CRAS_PROTO_VER;
147 connect_msg_.stream_type = CRAS_STREAM_TYPE_DEFAULT;
148 connect_msg_.direction = CRAS_STREAM_OUTPUT;
149 connect_msg_.stream_id = stream_id_;
150 connect_msg_.buffer_frames = 480;
151 connect_msg_.cb_threshold = 240;
152 connect_msg_.flags = 0;
153 connect_msg_.format.num_channels = 2;
154 connect_msg_.format.frame_rate = 48000;
155 connect_msg_.format.format = SND_PCM_FORMAT_S16_LE;
156 connect_msg_.dev_idx = NO_DEVICE;
157
158 ResetStubData();
159 }
160
TearDown()161 virtual void TearDown() {
162 cras_rclient_destroy(rclient_);
163 free(rstream_);
164 close(pipe_fds_[0]);
165 close(pipe_fds_[1]);
166 }
167
168 void RegisterNotification(enum CRAS_CLIENT_MESSAGE_ID msg_id,
169 void *callback, void **ops_address);
170
171 struct cras_connect_message connect_msg_;
172 struct cras_rclient *rclient_;
173 struct cras_rstream *rstream_;
174 size_t stream_id_;
175 int pipe_fds_[2];
176 };
177
TEST_F(RClientMessagesSuite,AudThreadAttachFail)178 TEST_F(RClientMessagesSuite, AudThreadAttachFail) {
179 struct cras_client_stream_connected out_msg;
180 int rc;
181
182 cras_rstream_create_stream_out = rstream_;
183 stream_list_add_stream_return = -EINVAL;
184
185 rc = cras_rclient_message_from_client(rclient_, &connect_msg_.header, 100);
186 EXPECT_EQ(0, rc);
187
188 rc = read(pipe_fds_[0], &out_msg, sizeof(out_msg));
189 EXPECT_EQ(sizeof(out_msg), rc);
190 EXPECT_EQ(stream_id_, out_msg.stream_id);
191 EXPECT_NE(0, out_msg.err);
192 EXPECT_EQ(0, cras_iodev_list_rm_output_called);
193 EXPECT_EQ(1, stream_list_add_stream_called);
194 EXPECT_EQ(0, stream_list_disconnect_stream_called);
195 EXPECT_EQ(0, cras_server_metrics_stream_config_called);
196 }
197
TEST_F(RClientMessagesSuite,ConnectMsgWithBadFd)198 TEST_F(RClientMessagesSuite, ConnectMsgWithBadFd) {
199 struct cras_client_stream_connected out_msg;
200 int rc;
201
202 rc = cras_rclient_message_from_client(rclient_, &connect_msg_.header, -1);
203 EXPECT_EQ(0, rc);
204
205 rc = read(pipe_fds_[0], &out_msg, sizeof(out_msg));
206 EXPECT_EQ(sizeof(out_msg), rc);
207 EXPECT_EQ(stream_id_, out_msg.stream_id);
208 EXPECT_NE(0, out_msg.err);
209 EXPECT_EQ(stream_list_add_stream_called,
210 stream_list_disconnect_stream_called);
211 EXPECT_EQ(0, cras_server_metrics_stream_config_called);
212 }
213
TEST_F(RClientMessagesSuite,ConnectMsgFromOldClient)214 TEST_F(RClientMessagesSuite, ConnectMsgFromOldClient) {
215 struct cras_client_stream_connected_old out_msg;
216 int rc;
217
218 cras_rstream_create_stream_out = rstream_;
219 cras_iodev_attach_stream_retval = 0;
220
221 connect_msg_.header.length = sizeof(struct cras_connect_message_old);
222 connect_msg_.proto_version = CRAS_PROTO_VER - 1;
223
224 rc = cras_rclient_message_from_client(rclient_, &connect_msg_.header, 100);
225 EXPECT_EQ(0, rc);
226 EXPECT_EQ(1, cras_make_fd_nonblocking_called);
227
228 rc = read(pipe_fds_[0], &out_msg, sizeof(out_msg));
229 EXPECT_EQ(sizeof(out_msg), rc);
230 EXPECT_EQ(stream_id_, out_msg.stream_id);
231 EXPECT_EQ(0, out_msg.err);
232 EXPECT_EQ(1, stream_list_add_stream_called);
233 EXPECT_EQ(0, stream_list_disconnect_stream_called);
234 EXPECT_EQ(1, cras_server_metrics_stream_config_called);
235 }
236
TEST_F(RClientMessagesSuite,SuccessReply)237 TEST_F(RClientMessagesSuite, SuccessReply) {
238 struct cras_client_stream_connected out_msg;
239 int rc;
240
241 cras_rstream_create_stream_out = rstream_;
242 cras_iodev_attach_stream_retval = 0;
243
244 rc = cras_rclient_message_from_client(rclient_, &connect_msg_.header, 100);
245 EXPECT_EQ(0, rc);
246 EXPECT_EQ(1, cras_make_fd_nonblocking_called);
247
248 rc = read(pipe_fds_[0], &out_msg, sizeof(out_msg));
249 EXPECT_EQ(sizeof(out_msg), rc);
250 EXPECT_EQ(stream_id_, out_msg.stream_id);
251 EXPECT_EQ(0, out_msg.err);
252 EXPECT_EQ(1, stream_list_add_stream_called);
253 EXPECT_EQ(0, stream_list_disconnect_stream_called);
254 EXPECT_EQ(1, cras_server_metrics_stream_config_called);
255 }
256
TEST_F(RClientMessagesSuite,SuccessCreateThreadReply)257 TEST_F(RClientMessagesSuite, SuccessCreateThreadReply) {
258 struct cras_client_stream_connected out_msg;
259 int rc;
260
261 cras_rstream_create_stream_out = rstream_;
262 cras_iodev_attach_stream_retval = 0;
263
264 rc = cras_rclient_message_from_client(rclient_, &connect_msg_.header, 100);
265 EXPECT_EQ(0, rc);
266 EXPECT_EQ(1, cras_make_fd_nonblocking_called);
267
268 rc = read(pipe_fds_[0], &out_msg, sizeof(out_msg));
269 EXPECT_EQ(sizeof(out_msg), rc);
270 EXPECT_EQ(stream_id_, out_msg.stream_id);
271 EXPECT_EQ(0, out_msg.err);
272 EXPECT_EQ(1, stream_list_add_stream_called);
273 EXPECT_EQ(0, stream_list_disconnect_stream_called);
274 EXPECT_EQ(1, cras_server_metrics_stream_config_called);
275 }
276
TEST_F(RClientMessagesSuite,SetVolume)277 TEST_F(RClientMessagesSuite, SetVolume) {
278 struct cras_set_system_volume msg;
279 int rc;
280
281 msg.header.id = CRAS_SERVER_SET_SYSTEM_VOLUME;
282 msg.header.length = sizeof(msg);
283 msg.volume = 66;
284
285 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
286 EXPECT_EQ(0, rc);
287 EXPECT_EQ(1, cras_system_set_volume_called);
288 EXPECT_EQ(66, cras_system_set_volume_value);
289 }
290
TEST_F(RClientMessagesSuite,SetCaptureVolume)291 TEST_F(RClientMessagesSuite, SetCaptureVolume) {
292 struct cras_set_system_volume msg;
293 int rc;
294
295 msg.header.id = CRAS_SERVER_SET_SYSTEM_CAPTURE_GAIN;
296 msg.header.length = sizeof(msg);
297 msg.volume = 66;
298
299 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
300 EXPECT_EQ(0, rc);
301 EXPECT_EQ(1, cras_system_set_capture_gain_called);
302 EXPECT_EQ(66, cras_system_set_capture_gain_value);
303 }
304
TEST_F(RClientMessagesSuite,SetMute)305 TEST_F(RClientMessagesSuite, SetMute) {
306 struct cras_set_system_mute msg;
307 int rc;
308
309 msg.header.id = CRAS_SERVER_SET_SYSTEM_MUTE;
310 msg.header.length = sizeof(msg);
311 msg.mute = 1;
312
313 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
314 EXPECT_EQ(0, rc);
315 EXPECT_EQ(1, cras_system_set_mute_called);
316 EXPECT_EQ(1, cras_system_set_mute_value);
317
318 msg.header.id = CRAS_SERVER_SET_SYSTEM_MUTE_LOCKED;
319 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
320 EXPECT_EQ(0, rc);
321 EXPECT_EQ(1, cras_system_set_mute_locked_called);
322 EXPECT_EQ(1, cras_system_set_mute_locked_value);
323 }
324
TEST_F(RClientMessagesSuite,SetUserMute)325 TEST_F(RClientMessagesSuite, SetUserMute) {
326 struct cras_set_system_mute msg;
327 int rc;
328
329 msg.header.id = CRAS_SERVER_SET_USER_MUTE;
330 msg.header.length = sizeof(msg);
331 msg.mute = 1;
332
333 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
334 EXPECT_EQ(0, rc);
335 EXPECT_EQ(1, cras_system_set_user_mute_called);
336 EXPECT_EQ(1, cras_system_set_user_mute_value);
337 }
338
TEST_F(RClientMessagesSuite,SetCaptureMute)339 TEST_F(RClientMessagesSuite, SetCaptureMute) {
340 struct cras_set_system_mute msg;
341 int rc;
342
343 msg.header.id = CRAS_SERVER_SET_SYSTEM_CAPTURE_MUTE;
344 msg.header.length = sizeof(msg);
345 msg.mute = 1;
346
347 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
348 EXPECT_EQ(0, rc);
349 EXPECT_EQ(1, cras_system_set_capture_mute_called);
350 EXPECT_EQ(1, cras_system_set_capture_mute_value);
351
352 msg.header.id = CRAS_SERVER_SET_SYSTEM_CAPTURE_MUTE_LOCKED;
353 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
354 EXPECT_EQ(0, rc);
355 EXPECT_EQ(1, cras_system_set_capture_mute_locked_called);
356 EXPECT_EQ(1, cras_system_set_capture_mute_locked_value);
357 }
358
TEST_F(RClientMessagesSuite,DumpSnapshots)359 TEST_F(RClientMessagesSuite, DumpSnapshots) {
360 struct cras_dump_snapshots msg;
361 int rc;
362 cras_fill_dump_snapshots(&msg);
363 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
364 EXPECT_EQ(0, rc);
365 EXPECT_EQ(1, cras_system_state_dump_snapshots_called);
366 }
367
RegisterNotification(enum CRAS_CLIENT_MESSAGE_ID msg_id,void * callback,void ** ops_address)368 void RClientMessagesSuite::RegisterNotification(
369 enum CRAS_CLIENT_MESSAGE_ID msg_id,
370 void *callback, void **ops_address) {
371 struct cras_register_notification msg;
372 int do_register = callback != NULL ? 1 : 0;
373 int rc;
374
375 cras_observer_register_notify_called++;
376
377 cras_fill_register_notification_message(&msg, msg_id, do_register);
378 EXPECT_EQ(msg.header.length, sizeof(msg));
379 EXPECT_EQ(msg.header.id, CRAS_SERVER_REGISTER_NOTIFICATION);
380 EXPECT_EQ(msg.do_register, do_register);
381 EXPECT_EQ(msg.msg_id, msg_id);
382
383 rc = cras_rclient_message_from_client(rclient_, &msg.header, -1);
384 EXPECT_EQ(0, rc);
385 EXPECT_EQ(cras_observer_register_notify_called, cras_observer_get_ops_called);
386 EXPECT_EQ(cras_observer_register_notify_called,\
387 cras_observer_ops_are_empty_called);
388 if (msg.do_register)
389 cras_observer_num_ops_registered++;
390 if (cras_observer_num_ops_registered == 1) {
391 if (msg.do_register) {
392 EXPECT_EQ(1, cras_observer_add_called);
393 EXPECT_EQ(rclient_, cras_observer_add_context_value);
394 EXPECT_EQ(rclient_->observer, cras_observer_add_return_value);
395 } else {
396 EXPECT_EQ(1, cras_observer_remove_called);
397 EXPECT_EQ(rclient_->observer, (struct cras_observer_client *)NULL);
398 }
399 } else {
400 EXPECT_EQ(cras_observer_register_notify_called - 1,\
401 cras_observer_set_ops_called);
402 }
403 if (!msg.do_register)
404 cras_observer_num_ops_registered--;
405 if (cras_observer_num_ops_registered) {
406 EXPECT_EQ(callback, *ops_address);
407 }
408 }
409
TEST_F(RClientMessagesSuite,RegisterStatusNotification)410 TEST_F(RClientMessagesSuite, RegisterStatusNotification) {
411 /* First registration for this client. */
412 RegisterNotification(
413 CRAS_CLIENT_OUTPUT_VOLUME_CHANGED,
414 (void *)send_output_volume_changed,
415 (void **)&cras_observer_ops_value.output_volume_changed);
416
417 /* Second registration for this client. */
418 RegisterNotification(
419 CRAS_CLIENT_CAPTURE_GAIN_CHANGED,
420 (void *)send_capture_gain_changed,
421 (void **)&cras_observer_ops_value.capture_gain_changed);
422
423 /* Deregister output_volume. */
424 RegisterNotification(
425 CRAS_CLIENT_OUTPUT_VOLUME_CHANGED, NULL,
426 (void **)&cras_observer_ops_value.output_volume_changed);
427
428 /* Register/deregister all msg_ids. */
429
430 RegisterNotification(
431 CRAS_CLIENT_OUTPUT_MUTE_CHANGED,
432 (void *)send_output_mute_changed,
433 (void **)&cras_observer_ops_value.output_mute_changed);
434 RegisterNotification(
435 CRAS_CLIENT_OUTPUT_MUTE_CHANGED, NULL,
436 (void **)&cras_observer_ops_value.output_mute_changed);
437
438 RegisterNotification(
439 CRAS_CLIENT_CAPTURE_MUTE_CHANGED,
440 (void *)send_capture_mute_changed,
441 (void **)&cras_observer_ops_value.capture_mute_changed);
442 RegisterNotification(
443 CRAS_CLIENT_CAPTURE_MUTE_CHANGED, NULL,
444 (void **)&cras_observer_ops_value.capture_mute_changed);
445
446 RegisterNotification(
447 CRAS_CLIENT_NODES_CHANGED,
448 (void *)send_nodes_changed,
449 (void **)&cras_observer_ops_value.nodes_changed);
450 RegisterNotification(
451 CRAS_CLIENT_NODES_CHANGED, NULL,
452 (void **)&cras_observer_ops_value.nodes_changed);
453
454 RegisterNotification(
455 CRAS_CLIENT_ACTIVE_NODE_CHANGED,
456 (void *)send_active_node_changed,
457 (void **)&cras_observer_ops_value.active_node_changed);
458 RegisterNotification(
459 CRAS_CLIENT_ACTIVE_NODE_CHANGED, NULL,
460 (void **)&cras_observer_ops_value.active_node_changed);
461
462 RegisterNotification(
463 CRAS_CLIENT_OUTPUT_NODE_VOLUME_CHANGED,
464 (void *)send_output_node_volume_changed,
465 (void **)&cras_observer_ops_value.output_node_volume_changed);
466 RegisterNotification(
467 CRAS_CLIENT_OUTPUT_NODE_VOLUME_CHANGED, NULL,
468 (void **)&cras_observer_ops_value.output_node_volume_changed);
469
470 RegisterNotification(
471 CRAS_CLIENT_NODE_LEFT_RIGHT_SWAPPED_CHANGED,
472 (void *)send_node_left_right_swapped_changed,
473 (void **)&cras_observer_ops_value.node_left_right_swapped_changed);
474 RegisterNotification(
475 CRAS_CLIENT_NODE_LEFT_RIGHT_SWAPPED_CHANGED, NULL,
476 (void **)&cras_observer_ops_value.node_left_right_swapped_changed);
477
478 RegisterNotification(
479 CRAS_CLIENT_INPUT_NODE_GAIN_CHANGED,
480 (void *)send_input_node_gain_changed,
481 (void **)&cras_observer_ops_value.input_node_gain_changed);
482 RegisterNotification(
483 CRAS_CLIENT_INPUT_NODE_GAIN_CHANGED, NULL,
484 (void **)&cras_observer_ops_value.input_node_gain_changed);
485
486 RegisterNotification(
487 CRAS_CLIENT_NUM_ACTIVE_STREAMS_CHANGED,
488 (void *)send_num_active_streams_changed,
489 (void **)&cras_observer_ops_value.num_active_streams_changed);
490 RegisterNotification(
491 CRAS_CLIENT_NUM_ACTIVE_STREAMS_CHANGED, NULL,
492 (void **)&cras_observer_ops_value.num_active_streams_changed);
493
494 /* Deregister last. */
495 RegisterNotification(
496 CRAS_CLIENT_CAPTURE_GAIN_CHANGED, NULL,
497 (void **)&cras_observer_ops_value.capture_gain_changed);
498 }
499
TEST_F(RClientMessagesSuite,SendOutputVolumeChanged)500 TEST_F(RClientMessagesSuite, SendOutputVolumeChanged) {
501 void *void_client = reinterpret_cast<void *>(rclient_);
502 char buf[1024];
503 ssize_t rc;
504 struct cras_client_volume_changed *msg =
505 (struct cras_client_volume_changed *)buf;
506 const int32_t volume = 90;
507
508 send_output_volume_changed(void_client, volume);
509 rc = read(pipe_fds_[0], buf, sizeof(buf));
510 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
511 EXPECT_EQ(msg->header.id, CRAS_CLIENT_OUTPUT_VOLUME_CHANGED);
512 EXPECT_EQ(msg->volume, volume);
513 }
514
TEST_F(RClientMessagesSuite,SendOutputMuteChanged)515 TEST_F(RClientMessagesSuite, SendOutputMuteChanged) {
516 void *void_client = reinterpret_cast<void *>(rclient_);
517 char buf[1024];
518 ssize_t rc;
519 struct cras_client_mute_changed *msg =
520 (struct cras_client_mute_changed *)buf;
521 const int muted = 1;
522 const int user_muted = 0;
523 const int mute_locked = 1;
524
525 send_output_mute_changed(void_client, muted, user_muted, mute_locked);
526 rc = read(pipe_fds_[0], buf, sizeof(buf));
527 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
528 EXPECT_EQ(msg->header.id, CRAS_CLIENT_OUTPUT_MUTE_CHANGED);
529 EXPECT_EQ(msg->muted, muted);
530 EXPECT_EQ(msg->user_muted, user_muted);
531 EXPECT_EQ(msg->mute_locked, mute_locked);
532 }
533
TEST_F(RClientMessagesSuite,SendCaptureGainChanged)534 TEST_F(RClientMessagesSuite, SendCaptureGainChanged) {
535 void *void_client = reinterpret_cast<void *>(rclient_);
536 char buf[1024];
537 ssize_t rc;
538 struct cras_client_volume_changed *msg =
539 (struct cras_client_volume_changed *)buf;
540 const int32_t gain = 90;
541
542 send_capture_gain_changed(void_client, gain);
543 rc = read(pipe_fds_[0], buf, sizeof(buf));
544 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
545 EXPECT_EQ(msg->header.id, CRAS_CLIENT_CAPTURE_GAIN_CHANGED);
546 EXPECT_EQ(msg->volume, gain);
547 }
548
TEST_F(RClientMessagesSuite,SendCaptureMuteChanged)549 TEST_F(RClientMessagesSuite, SendCaptureMuteChanged) {
550 void *void_client = reinterpret_cast<void *>(rclient_);
551 char buf[1024];
552 ssize_t rc;
553 struct cras_client_mute_changed *msg =
554 (struct cras_client_mute_changed *)buf;
555 const int muted = 1;
556 const int mute_locked = 0;
557
558 send_capture_mute_changed(void_client, muted, mute_locked);
559 rc = read(pipe_fds_[0], buf, sizeof(buf));
560 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
561 EXPECT_EQ(msg->header.id, CRAS_CLIENT_CAPTURE_MUTE_CHANGED);
562 EXPECT_EQ(msg->muted, muted);
563 EXPECT_EQ(msg->mute_locked, mute_locked);
564 }
565
TEST_F(RClientMessagesSuite,SendNodesChanged)566 TEST_F(RClientMessagesSuite, SendNodesChanged) {
567 void *void_client = reinterpret_cast<void *>(rclient_);
568 char buf[1024];
569 ssize_t rc;
570 struct cras_client_nodes_changed *msg =
571 (struct cras_client_nodes_changed *)buf;
572
573 send_nodes_changed(void_client);
574 rc = read(pipe_fds_[0], buf, sizeof(buf));
575 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
576 EXPECT_EQ(msg->header.id, CRAS_CLIENT_NODES_CHANGED);
577 }
578
TEST_F(RClientMessagesSuite,SendActiveNodeChanged)579 TEST_F(RClientMessagesSuite, SendActiveNodeChanged) {
580 void *void_client = reinterpret_cast<void *>(rclient_);
581 char buf[1024];
582 ssize_t rc;
583 struct cras_client_active_node_changed *msg =
584 (struct cras_client_active_node_changed *)buf;
585 const enum CRAS_STREAM_DIRECTION dir = CRAS_STREAM_INPUT;
586 const cras_node_id_t node_id = 0x0001000200030004;
587
588 send_active_node_changed(void_client, dir, node_id);
589 rc = read(pipe_fds_[0], buf, sizeof(buf));
590 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
591 EXPECT_EQ(msg->header.id, CRAS_CLIENT_ACTIVE_NODE_CHANGED);
592 EXPECT_EQ(msg->direction, (int32_t)dir);
593 EXPECT_EQ((uint64_t)msg->node_id, node_id);
594 }
595
TEST_F(RClientMessagesSuite,SendOutputNodeVolumeChanged)596 TEST_F(RClientMessagesSuite, SendOutputNodeVolumeChanged) {
597 void *void_client = reinterpret_cast<void *>(rclient_);
598 char buf[1024];
599 ssize_t rc;
600 struct cras_client_node_value_changed *msg =
601 (struct cras_client_node_value_changed *)buf;
602 const cras_node_id_t node_id = 0x0001000200030004;
603 const int32_t value = 90;
604
605 send_output_node_volume_changed(void_client, node_id, value);
606 rc = read(pipe_fds_[0], buf, sizeof(buf));
607 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
608 EXPECT_EQ(msg->header.id, CRAS_CLIENT_OUTPUT_NODE_VOLUME_CHANGED);
609 EXPECT_EQ(msg->node_id, node_id);
610 EXPECT_EQ(msg->value, value);
611 }
612
TEST_F(RClientMessagesSuite,SendNodeLeftRightSwappedChanged)613 TEST_F(RClientMessagesSuite, SendNodeLeftRightSwappedChanged) {
614 void *void_client = reinterpret_cast<void *>(rclient_);
615 char buf[1024];
616 ssize_t rc;
617 struct cras_client_node_value_changed *msg =
618 (struct cras_client_node_value_changed *)buf;
619 const cras_node_id_t node_id = 0x0001000200030004;
620 const int32_t value = 0;
621
622 send_node_left_right_swapped_changed(void_client, node_id, value);
623 rc = read(pipe_fds_[0], buf, sizeof(buf));
624 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
625 EXPECT_EQ(msg->header.id, CRAS_CLIENT_NODE_LEFT_RIGHT_SWAPPED_CHANGED);
626 EXPECT_EQ(msg->node_id, node_id);
627 EXPECT_EQ(msg->value, value);
628 }
629
TEST_F(RClientMessagesSuite,SendNodeInputNodeGainChanged)630 TEST_F(RClientMessagesSuite, SendNodeInputNodeGainChanged) {
631 void *void_client = reinterpret_cast<void *>(rclient_);
632 char buf[1024];
633 ssize_t rc;
634 struct cras_client_node_value_changed *msg =
635 (struct cras_client_node_value_changed *)buf;
636 const cras_node_id_t node_id = 0x0001000200030004;
637 const int32_t value = -19;
638
639 send_input_node_gain_changed(void_client, node_id, value);
640 rc = read(pipe_fds_[0], buf, sizeof(buf));
641 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
642 EXPECT_EQ(msg->header.id, CRAS_CLIENT_INPUT_NODE_GAIN_CHANGED);
643 EXPECT_EQ(msg->node_id, node_id);
644 EXPECT_EQ(msg->value, value);
645 }
646
TEST_F(RClientMessagesSuite,SendNumActiveStreamsChanged)647 TEST_F(RClientMessagesSuite, SendNumActiveStreamsChanged) {
648 void *void_client = reinterpret_cast<void *>(rclient_);
649 char buf[1024];
650 ssize_t rc;
651 struct cras_client_num_active_streams_changed *msg =
652 (struct cras_client_num_active_streams_changed *)buf;
653 const enum CRAS_STREAM_DIRECTION dir = CRAS_STREAM_INPUT;
654 const uint32_t num_active_streams = 3;
655
656 send_num_active_streams_changed(void_client, dir, num_active_streams);
657 rc = read(pipe_fds_[0], buf, sizeof(buf));
658 ASSERT_EQ(rc, (ssize_t)sizeof(*msg));
659 EXPECT_EQ(msg->header.id, CRAS_CLIENT_NUM_ACTIVE_STREAMS_CHANGED);
660 EXPECT_EQ(msg->direction, (int32_t)dir);
661 EXPECT_EQ(msg->num_active_streams, num_active_streams);
662 }
663
664 } // namespace
665
main(int argc,char ** argv)666 int main(int argc, char **argv) {
667 ::testing::InitGoogleTest(&argc, argv);
668 return RUN_ALL_TESTS();
669 }
670
671 /* stubs */
672 extern "C" {
673
cras_iodev_list_get_audio_thread()674 struct audio_thread* cras_iodev_list_get_audio_thread() {
675 return iodev_get_thread_return;
676 }
677
cras_iodev_list_add_active_node(enum CRAS_STREAM_DIRECTION dir,cras_node_id_t node_id)678 void cras_iodev_list_add_active_node(enum CRAS_STREAM_DIRECTION dir,
679 cras_node_id_t node_id)
680 {
681 }
682
cras_iodev_list_rm_active_node(enum CRAS_STREAM_DIRECTION dir,cras_node_id_t node_id)683 void cras_iodev_list_rm_active_node(enum CRAS_STREAM_DIRECTION dir,
684 cras_node_id_t node_id)
685 {
686 }
687
audio_thread_rm_stream(audio_thread * thread,cras_rstream * stream)688 int audio_thread_rm_stream(audio_thread* thread,
689 cras_rstream* stream) {
690 return 0;
691 }
692
audio_thread_add_output_dev(struct audio_thread * thread,struct cras_iodev * odev)693 void audio_thread_add_output_dev(struct audio_thread *thread,
694 struct cras_iodev *odev)
695 {
696 }
697
audio_thread_dump_thread_info(struct audio_thread * thread,struct audio_debug_info * info)698 int audio_thread_dump_thread_info(struct audio_thread *thread,
699 struct audio_debug_info *info)
700 {
701 return 0;
702 }
703
audio_thread_suspend(struct audio_thread * thread)704 int audio_thread_suspend(struct audio_thread *thread)
705 {
706 return 0;
707 }
708
audio_thread_resume(struct audio_thread * thread)709 int audio_thread_resume(struct audio_thread *thread)
710 {
711 return 0;
712 }
713
audio_thread_config_global_remix(struct audio_thread * thread,unsigned int num_channels,const float * coefficient)714 int audio_thread_config_global_remix(struct audio_thread *thread,
715 unsigned int num_channels,
716 const float *coefficient)
717 {
718 return 0;
719 }
720
audio_thread_set_aec_dump(struct audio_thread * thread,cras_stream_id_t stream_id,unsigned int start,int fd)721 int audio_thread_set_aec_dump(struct audio_thread *thread,
722 cras_stream_id_t stream_id,
723 unsigned int start,
724 int fd)
725 {
726 return 0;
727 }
728
729 #ifdef HAVE_WEBRTC_APM
cras_apm_list_reload_aec_config()730 void cras_apm_list_reload_aec_config()
731 {
732 }
733 #endif
734
cras_config_get_socket_file_dir()735 const char *cras_config_get_socket_file_dir()
736 {
737 return CRAS_UT_TMPDIR;
738 }
739
cras_rstream_create(struct cras_rstream_config * stream_config,struct cras_rstream ** stream_out)740 int cras_rstream_create(struct cras_rstream_config *stream_config,
741 struct cras_rstream **stream_out)
742 {
743 *stream_out = cras_rstream_create_stream_out;
744 return cras_rstream_create_return;
745 }
746
cras_rstream_get_effects(const struct cras_rstream * stream)747 unsigned int cras_rstream_get_effects(const struct cras_rstream *stream)
748 {
749 return 0;
750 }
751
cras_iodev_move_stream_type(uint32_t type,uint32_t index)752 int cras_iodev_move_stream_type(uint32_t type, uint32_t index)
753 {
754 return 0;
755 }
756
cras_iodev_list_rm_output(struct cras_iodev * output)757 int cras_iodev_list_rm_output(struct cras_iodev *output) {
758 cras_iodev_list_rm_output_called++;
759 return 0;
760 }
761
cras_iodev_list_rm_input(struct cras_iodev * input)762 int cras_iodev_list_rm_input(struct cras_iodev *input) {
763 cras_iodev_list_rm_input_called++;
764 return 0;
765 }
766
cras_server_disconnect_from_client_socket(int socket_fd)767 int cras_server_disconnect_from_client_socket(int socket_fd) {
768 return 0;
769 }
770
cras_make_fd_nonblocking(int fd)771 int cras_make_fd_nonblocking(int fd)
772 {
773 cras_make_fd_nonblocking_called++;
774 return 0;
775 }
776
cras_system_state_dump_snapshots()777 void cras_system_state_dump_snapshots()
778 {
779 cras_system_state_dump_snapshots_called ++;
780 }
781
cras_system_set_volume(size_t volume)782 void cras_system_set_volume(size_t volume)
783 {
784 cras_system_set_volume_value = volume;
785 cras_system_set_volume_called++;
786 }
787
cras_system_set_capture_gain(long gain)788 void cras_system_set_capture_gain(long gain)
789 {
790 cras_system_set_capture_gain_value = gain;
791 cras_system_set_capture_gain_called++;
792 }
793
794 // From system_state.
cras_system_set_mute(int mute)795 void cras_system_set_mute(int mute)
796 {
797 cras_system_set_mute_value = mute;
798 cras_system_set_mute_called++;
799 }
cras_system_set_user_mute(int mute)800 void cras_system_set_user_mute(int mute)
801 {
802 cras_system_set_user_mute_value = mute;
803 cras_system_set_user_mute_called++;
804 }
cras_system_set_mute_locked(int mute)805 void cras_system_set_mute_locked(int mute)
806 {
807 cras_system_set_mute_locked_value = mute;
808 cras_system_set_mute_locked_called++;
809 }
cras_system_set_capture_mute(int mute)810 void cras_system_set_capture_mute(int mute)
811 {
812 cras_system_set_capture_mute_value = mute;
813 cras_system_set_capture_mute_called++;
814 }
cras_system_set_capture_mute_locked(int mute)815 void cras_system_set_capture_mute_locked(int mute)
816 {
817 cras_system_set_capture_mute_locked_value = mute;
818 cras_system_set_capture_mute_locked_called++;
819 }
820
cras_system_remove_alsa_card(size_t alsa_card_index)821 int cras_system_remove_alsa_card(size_t alsa_card_index)
822 {
823 return -1;
824 }
825
cras_system_set_suspended(int suspended)826 void cras_system_set_suspended(int suspended) {
827 }
828
cras_system_state_get_no_lock()829 struct cras_server_state *cras_system_state_get_no_lock()
830 {
831 return NULL;
832 }
833
cras_sys_state_shm_fd()834 key_t cras_sys_state_shm_fd()
835 {
836 return 1;
837 }
838
cras_dsp_reload_ini()839 void cras_dsp_reload_ini()
840 {
841 }
842
cras_dsp_dump_info()843 void cras_dsp_dump_info()
844 {
845 }
846
cras_iodev_list_set_node_attr(cras_node_id_t id,enum ionode_attr attr,int value)847 int cras_iodev_list_set_node_attr(cras_node_id_t id,
848 enum ionode_attr attr, int value)
849 {
850 return 0;
851 }
852
cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,cras_node_id_t node_id)853 void cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,
854 cras_node_id_t node_id)
855 {
856 }
857
cras_iodev_list_add_test_dev(enum TEST_IODEV_TYPE type)858 void cras_iodev_list_add_test_dev(enum TEST_IODEV_TYPE type) {
859 }
860
cras_iodev_list_get_stream_list()861 struct stream_list *cras_iodev_list_get_stream_list()
862 {
863 return NULL;
864 }
865
866 /* Handles sending a command to a test iodev. */
cras_iodev_list_test_dev_command(unsigned int iodev_idx,enum CRAS_TEST_IODEV_CMD command,unsigned int data_len,const uint8_t * data)867 void cras_iodev_list_test_dev_command(unsigned int iodev_idx,
868 enum CRAS_TEST_IODEV_CMD command,
869 unsigned int data_len,
870 const uint8_t *data) {
871 }
872
cras_iodev_list_configure_global_remix_converter(unsigned int num_channels,const float * coefficient)873 void cras_iodev_list_configure_global_remix_converter(
874 unsigned int num_channels,
875 const float *coefficient)
876 {
877 }
878
stream_list_add(struct stream_list * list,struct cras_rstream_config * config,struct cras_rstream ** stream)879 int stream_list_add(struct stream_list *list,
880 struct cras_rstream_config *config,
881 struct cras_rstream **stream)
882 {
883 int ret;
884
885 *stream = &dummy_rstream;
886
887 stream_list_add_stream_called++;
888 ret = stream_list_add_stream_return;
889 if (ret)
890 stream_list_add_stream_return = -EINVAL;
891
892 dummy_rstream.direction = config->direction;
893 dummy_rstream.stream_id = config->stream_id;
894
895 return ret;
896 }
897
stream_list_rm(struct stream_list * list,cras_stream_id_t id)898 int stream_list_rm(struct stream_list *list, cras_stream_id_t id)
899 {
900 stream_list_disconnect_stream_called++;
901 return 0;
902 }
903
stream_list_rm_all_client_streams(struct stream_list * list,struct cras_rclient * rclient)904 int stream_list_rm_all_client_streams(struct stream_list *list,
905 struct cras_rclient *rclient)
906 {
907 return 0;
908 }
909
cras_send_with_fds(int sockfd,const void * buf,size_t len,int * fd,unsigned int num_fds)910 int cras_send_with_fds(int sockfd, const void *buf, size_t len, int *fd,
911 unsigned int num_fds)
912 {
913 return write(sockfd, buf, len);
914 }
915
cras_iodev_list_get_hotword_models(cras_node_id_t node_id)916 char *cras_iodev_list_get_hotword_models(cras_node_id_t node_id)
917 {
918 return NULL;
919 }
920
cras_iodev_list_set_hotword_model(cras_node_id_t id,const char * model_name)921 int cras_iodev_list_set_hotword_model(cras_node_id_t id,
922 const char *model_name)
923 {
924 return 0;
925 }
926
cras_observer_add(const struct cras_observer_ops * ops,void * context)927 struct cras_observer_client *cras_observer_add(
928 const struct cras_observer_ops *ops,
929 void *context)
930 {
931 cras_observer_add_called++;
932 cras_observer_add_context_value = context;
933 memcpy(&cras_observer_ops_value, ops, sizeof(cras_observer_ops_value));
934 return cras_observer_add_return_value;
935 }
936
cras_observer_get_ops(const struct cras_observer_client * client,struct cras_observer_ops * ops)937 void cras_observer_get_ops(const struct cras_observer_client *client,
938 struct cras_observer_ops *ops)
939 {
940 cras_observer_get_ops_called++;
941 memcpy(ops, &cras_observer_ops_value, sizeof(*ops));
942 }
943
cras_observer_set_ops(struct cras_observer_client * client,const struct cras_observer_ops * ops)944 void cras_observer_set_ops(struct cras_observer_client *client,
945 const struct cras_observer_ops *ops)
946 {
947 cras_observer_set_ops_called++;
948 memcpy(&cras_observer_ops_value, ops, sizeof(cras_observer_ops_value));
949 }
950
cras_observer_ops_are_empty(const struct cras_observer_ops * ops)951 int cras_observer_ops_are_empty(const struct cras_observer_ops *ops)
952 {
953 cras_observer_ops_are_empty_called++;
954 return memcmp(&cras_observer_ops_are_empty_empty_ops, ops,
955 sizeof(cras_observer_ops_are_empty_empty_ops)) == 0;
956 }
957
cras_observer_remove(struct cras_observer_client * client)958 void cras_observer_remove(struct cras_observer_client *client)
959 {
960 cras_observer_remove_called++;
961 }
962
cras_server_metrics_stream_config(struct cras_rstream_config * config)963 int cras_server_metrics_stream_config(struct cras_rstream_config *config)
964 {
965 cras_server_metrics_stream_config_called++;
966 return 0;
967 }
968
969 } // extern "C"
970