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