1 // Copyright 2018 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_audio_thread_monitor.c"
10 #include "cras_main_message.h"
11 }
12
13 // Function call counters
14 static int cras_system_state_add_snapshot_called;
15 static int audio_thread_dump_thread_info_called;
16
17 // Stub data
18 static enum CRAS_MAIN_MESSAGE_TYPE type_set;
19 struct cras_audio_thread_event_message message;
20
ResetStubData()21 void ResetStubData() {
22 cras_system_state_add_snapshot_called = 0;
23 audio_thread_dump_thread_info_called = 0;
24 type_set = (enum CRAS_MAIN_MESSAGE_TYPE)999;
25 message.event_type = (enum CRAS_AUDIO_THREAD_EVENT_TYPE)999;
26 }
27
28 namespace {
29
30 class AudioThreadMonitorTestSuite : public testing::Test {
31 protected:
SetUp()32 virtual void SetUp() { ResetStubData(); }
33
TearDown()34 virtual void TearDown() {}
35 };
36
TEST_F(AudioThreadMonitorTestSuite,Init)37 TEST_F(AudioThreadMonitorTestSuite, Init) {
38 cras_audio_thread_monitor_init();
39 EXPECT_EQ(type_set, CRAS_MAIN_AUDIO_THREAD_EVENT);
40 }
41
TEST_F(AudioThreadMonitorTestSuite,Busyloop)42 TEST_F(AudioThreadMonitorTestSuite, Busyloop) {
43 cras_audio_thread_event_busyloop();
44 EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_BUSYLOOP);
45 }
46
TEST_F(AudioThreadMonitorTestSuite,Debug)47 TEST_F(AudioThreadMonitorTestSuite, Debug) {
48 cras_audio_thread_event_debug();
49 EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_DEBUG);
50 }
51
TEST_F(AudioThreadMonitorTestSuite,Underrun)52 TEST_F(AudioThreadMonitorTestSuite, Underrun) {
53 cras_audio_thread_event_underrun();
54 EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_UNDERRUN);
55 }
56
TEST_F(AudioThreadMonitorTestSuite,SevereUnderrun)57 TEST_F(AudioThreadMonitorTestSuite, SevereUnderrun) {
58 cras_audio_thread_event_severe_underrun();
59 EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_SEVERE_UNDERRUN);
60 }
61
TEST_F(AudioThreadMonitorTestSuite,DropSamples)62 TEST_F(AudioThreadMonitorTestSuite, DropSamples) {
63 cras_audio_thread_event_drop_samples();
64 EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_DROP_SAMPLES);
65 }
66
TEST_F(AudioThreadMonitorTestSuite,TakeSnapshot)67 TEST_F(AudioThreadMonitorTestSuite, TakeSnapshot) {
68 take_snapshot(AUDIO_THREAD_EVENT_DEBUG);
69 EXPECT_EQ(cras_system_state_add_snapshot_called, 1);
70 EXPECT_EQ(audio_thread_dump_thread_info_called, 1);
71 }
72
TEST_F(AudioThreadMonitorTestSuite,EventHandlerDoubleCall)73 TEST_F(AudioThreadMonitorTestSuite, EventHandlerDoubleCall) {
74 struct cras_audio_thread_event_message msg;
75 msg.event_type = AUDIO_THREAD_EVENT_DEBUG;
76 handle_audio_thread_event_message((struct cras_main_message*)&msg, NULL);
77 EXPECT_EQ(cras_system_state_add_snapshot_called, 1);
78 EXPECT_EQ(audio_thread_dump_thread_info_called, 1);
79
80 // take_snapshot shouldn't be called since the time interval is short
81 handle_audio_thread_event_message((struct cras_main_message*)&msg, NULL);
82 EXPECT_EQ(cras_system_state_add_snapshot_called, 1);
83 EXPECT_EQ(audio_thread_dump_thread_info_called, 1);
84 }
85
TEST_F(AudioThreadMonitorTestSuite,EventHandlerIgnoreInvalidEvent)86 TEST_F(AudioThreadMonitorTestSuite, EventHandlerIgnoreInvalidEvent) {
87 struct cras_audio_thread_event_message msg;
88 msg.event_type = (enum CRAS_AUDIO_THREAD_EVENT_TYPE)999;
89 handle_audio_thread_event_message((struct cras_main_message*)&msg, NULL);
90 EXPECT_EQ(cras_system_state_add_snapshot_called, 0);
91 EXPECT_EQ(audio_thread_dump_thread_info_called, 0);
92 }
93
94 extern "C" {
95
cras_system_state_add_snapshot(struct cras_audio_thread_snapshot * snapshot)96 void cras_system_state_add_snapshot(
97 struct cras_audio_thread_snapshot* snapshot) {
98 cras_system_state_add_snapshot_called++;
99 }
100
cras_iodev_list_get_audio_thread()101 struct audio_thread* cras_iodev_list_get_audio_thread() {
102 return reinterpret_cast<struct audio_thread*>(0xff);
103 }
104
audio_thread_dump_thread_info(struct audio_thread * thread,struct audio_debug_info * info)105 int audio_thread_dump_thread_info(struct audio_thread* thread,
106 struct audio_debug_info* info) {
107 audio_thread_dump_thread_info_called++;
108 return 0;
109 }
110
cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type,cras_message_callback callback,void * callback_data)111 int cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type,
112 cras_message_callback callback,
113 void* callback_data) {
114 type_set = type;
115 return 0;
116 }
117
cras_main_message_send(struct cras_main_message * msg)118 int cras_main_message_send(struct cras_main_message* msg) {
119 message = *(struct cras_audio_thread_event_message*)msg;
120 return 0;
121 }
122
123 } // extern "C"
124 } // namespace
125
main(int argc,char ** argv)126 int main(int argc, char** argv) {
127 ::testing::InitGoogleTest(&argc, argv);
128 int rc = RUN_ALL_TESTS();
129
130 return rc;
131 }
132