• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
6 #include <stdio.h>
7 
8 extern "C" {
9 #include "cras_device_monitor.c"
10 #include "cras_iodev.h"
11 #include "cras_main_message.h"
12 }
13 
14 static enum CRAS_MAIN_MESSAGE_TYPE type_set;
15 static struct cras_device_monitor_message* sent_msg;
16 static int resume_dev_called;
17 unsigned int resume_dev_idx;
18 static int suspend_dev_called;
19 unsigned int suspend_dev_idx;
20 static int set_mute_called;
21 unsigned int mute_dev_idx;
22 unsigned int fake_dev_idx = 123;
23 
ResetStubData()24 void ResetStubData() {
25   type_set = (enum CRAS_MAIN_MESSAGE_TYPE)0;
26   resume_dev_called = 0;
27   resume_dev_idx = 0;
28   suspend_dev_called = 0;
29   suspend_dev_idx = 0;
30   set_mute_called = 0;
31   mute_dev_idx = 0;
32 }
33 
34 namespace {
35 
TEST(DeviceMonitorTestSuite,Init)36 TEST(DeviceMonitorTestSuite, Init) {
37   ResetStubData();
38 
39   cras_device_monitor_init();
40 
41   EXPECT_EQ(type_set, CRAS_MAIN_MONITOR_DEVICE);
42 }
43 
TEST(DeviceMonitorTestSuite,ResetDevice)44 TEST(DeviceMonitorTestSuite, ResetDevice) {
45   ResetStubData();
46   // sent_msg will be filled with message content in cras_main_message_send.
47   sent_msg = (struct cras_device_monitor_message*)calloc(1, sizeof(*sent_msg));
48 
49   cras_device_monitor_reset_device(fake_dev_idx);
50 
51   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_MONITOR_DEVICE);
52   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
53   EXPECT_EQ(sent_msg->message_type, RESET_DEVICE);
54   EXPECT_EQ(sent_msg->dev_idx, fake_dev_idx);
55 
56   free(sent_msg);
57 }
58 
TEST(DeviceMonitorTestSuite,HandleResetDevice)59 TEST(DeviceMonitorTestSuite, HandleResetDevice) {
60   struct cras_device_monitor_message msg;
61   struct cras_main_message* main_message =
62       reinterpret_cast<struct cras_main_message*>(&msg);
63 
64   ResetStubData();
65 
66   // Filled msg with message content for resetting device.
67   init_device_msg(&msg, RESET_DEVICE, fake_dev_idx);
68   // Assume the pipe works fine and main message handler receives the same
69   // message.
70   handle_device_message(main_message, NULL);
71 
72   // Verify that disable/enable functions are called with correct device.
73   EXPECT_EQ(resume_dev_called, 1);
74   EXPECT_EQ(resume_dev_idx, fake_dev_idx);
75   EXPECT_EQ(suspend_dev_called, 1);
76   EXPECT_EQ(suspend_dev_idx, fake_dev_idx);
77 }
78 
TEST(DeviceMonitorTestSuite,MuteDevice)79 TEST(DeviceMonitorTestSuite, MuteDevice) {
80   ResetStubData();
81   // sent_msg will be filled with message content in cras_main_message_send.
82   sent_msg = (struct cras_device_monitor_message*)calloc(1, sizeof(*sent_msg));
83 
84   cras_device_monitor_set_device_mute_state(fake_dev_idx);
85 
86   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_MONITOR_DEVICE);
87   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
88   EXPECT_EQ(sent_msg->message_type, SET_MUTE_STATE);
89   EXPECT_EQ(sent_msg->dev_idx, fake_dev_idx);
90 
91   free(sent_msg);
92 }
93 
TEST(DeviceMonitorTestSuite,HandleMuteDevice)94 TEST(DeviceMonitorTestSuite, HandleMuteDevice) {
95   struct cras_device_monitor_message msg;
96   struct cras_main_message* main_message =
97       reinterpret_cast<struct cras_main_message*>(&msg);
98 
99   ResetStubData();
100 
101   // Filled msg with message content for device mute/unmute.
102   init_device_msg(&msg, SET_MUTE_STATE, fake_dev_idx);
103   // Assume the pipe works fine and main message handler receives the same
104   // message.
105   handle_device_message(main_message, NULL);
106 
107   // Verify that cras_iodev_set_mute is called with correct device.
108   EXPECT_EQ(set_mute_called, 1);
109   EXPECT_EQ(mute_dev_idx, fake_dev_idx);
110 }
111 
112 extern "C" {
113 
cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type,cras_message_callback callback,void * callback_data)114 int cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type,
115                                   cras_message_callback callback,
116                                   void* callback_data) {
117   type_set = type;
118   return 0;
119 }
120 
cras_main_message_send(struct cras_main_message * msg)121 int cras_main_message_send(struct cras_main_message* msg) {
122   // Copy the sent message so we can examine it in the test later.
123   memcpy(sent_msg, msg, sizeof(*sent_msg));
124   return 0;
125 };
126 
cras_iodev_list_resume_dev(unsigned int dev_idx)127 void cras_iodev_list_resume_dev(unsigned int dev_idx) {
128   resume_dev_called++;
129   resume_dev_idx = dev_idx;
130 }
131 
cras_iodev_list_suspend_dev(unsigned int dev_idx)132 void cras_iodev_list_suspend_dev(unsigned int dev_idx) {
133   suspend_dev_called++;
134   suspend_dev_idx = dev_idx;
135 }
136 
cras_iodev_list_set_dev_mute(unsigned int dev_idx)137 void cras_iodev_list_set_dev_mute(unsigned int dev_idx) {
138   set_mute_called++;
139   mute_dev_idx = dev_idx;
140 }
141 
142 }  // extern "C"
143 }  // namespace
144 
main(int argc,char ** argv)145 int main(int argc, char** argv) {
146   ::testing::InitGoogleTest(&argc, argv);
147   int rc = RUN_ALL_TESTS();
148 
149   return rc;
150 }
151