• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
6 #include <gtest/gtest.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() {
33       ResetStubData();
34     }
35 
TearDown()36     virtual void TearDown() {
37     }
38 };
39 
TEST_F(AudioThreadMonitorTestSuite,Init)40 TEST_F(AudioThreadMonitorTestSuite, Init) {
41   cras_audio_thread_monitor_init();
42   EXPECT_EQ(type_set, CRAS_MAIN_AUDIO_THREAD_EVENT);
43 }
44 
TEST_F(AudioThreadMonitorTestSuite,Busyloop)45 TEST_F(AudioThreadMonitorTestSuite, Busyloop) {
46   cras_audio_thread_busyloop();
47   EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_BUSYLOOP);
48 }
49 
TEST_F(AudioThreadMonitorTestSuite,Debug)50 TEST_F(AudioThreadMonitorTestSuite, Debug) {
51   cras_audio_thread_debug();
52   EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_DEBUG);
53 }
54 
TEST_F(AudioThreadMonitorTestSuite,Underrun)55 TEST_F(AudioThreadMonitorTestSuite, Underrun) {
56   cras_audio_thread_underrun();
57   EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_UNDERRUN);
58 }
59 
TEST_F(AudioThreadMonitorTestSuite,SevereUnderrun)60 TEST_F(AudioThreadMonitorTestSuite, SevereUnderrun) {
61   cras_audio_thread_severe_underrun();
62   EXPECT_EQ(message.event_type, AUDIO_THREAD_EVENT_SEVERE_UNDERRUN);
63 }
64 
TEST_F(AudioThreadMonitorTestSuite,TakeSnapshot)65 TEST_F(AudioThreadMonitorTestSuite, TakeSnapshot) {
66   take_snapshot(AUDIO_THREAD_EVENT_DEBUG);
67   EXPECT_EQ(cras_system_state_add_snapshot_called, 1);
68   EXPECT_EQ(audio_thread_dump_thread_info_called, 1);
69 }
70 
TEST_F(AudioThreadMonitorTestSuite,EventHandlerDoubleCall)71 TEST_F(AudioThreadMonitorTestSuite, EventHandlerDoubleCall) {
72   struct cras_audio_thread_event_message msg;
73   msg.event_type = AUDIO_THREAD_EVENT_DEBUG;
74   handle_audio_thread_event_message((struct cras_main_message *)&msg, NULL);
75   EXPECT_EQ(cras_system_state_add_snapshot_called, 1);
76   EXPECT_EQ(audio_thread_dump_thread_info_called, 1);
77 
78   // take_snapshot shouldn't be called since the time interval is short
79   handle_audio_thread_event_message((struct cras_main_message *)&msg, NULL);
80   EXPECT_EQ(cras_system_state_add_snapshot_called, 1);
81   EXPECT_EQ(audio_thread_dump_thread_info_called, 1);
82 }
83 
TEST_F(AudioThreadMonitorTestSuite,EventHandlerIgnoreInvalidEvent)84 TEST_F(AudioThreadMonitorTestSuite, EventHandlerIgnoreInvalidEvent) {
85   struct cras_audio_thread_event_message msg;
86   msg.event_type = (enum CRAS_AUDIO_THREAD_EVENT_TYPE)999;
87   handle_audio_thread_event_message((struct cras_main_message *)&msg, NULL);
88   EXPECT_EQ(cras_system_state_add_snapshot_called, 0);
89   EXPECT_EQ(audio_thread_dump_thread_info_called, 0);
90 }
91 
92 extern "C" {
93 
cras_system_state_add_snapshot(struct cras_audio_thread_snapshot * snapshot)94 void cras_system_state_add_snapshot(
95   struct cras_audio_thread_snapshot *snapshot) {
96   cras_system_state_add_snapshot_called ++;
97 }
98 
cras_iodev_list_get_audio_thread()99 struct audio_thread* cras_iodev_list_get_audio_thread() {
100   return reinterpret_cast <struct audio_thread*>(0xff);
101 }
102 
audio_thread_dump_thread_info(struct audio_thread * thread,struct audio_debug_info * info)103 int audio_thread_dump_thread_info(struct audio_thread *thread,
104                                   struct audio_debug_info *info) {
105   audio_thread_dump_thread_info_called ++;
106   return 0;
107 }
108 
cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type,cras_message_callback callback,void * callback_data)109 int cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type,
110                                   cras_message_callback callback,
111                                   void *callback_data) {
112   type_set = type;
113   return 0;
114 }
115 
cras_main_message_send(struct cras_main_message * msg)116 int cras_main_message_send(struct cras_main_message *msg) {
117   message = *(struct cras_audio_thread_event_message*)msg;
118   return 0;
119 }
120 
121 }  // extern "C"
122 }  // namespace
123 
main(int argc,char ** argv)124 int main(int argc, char **argv) {
125   ::testing::InitGoogleTest(&argc, argv);
126   int rc = RUN_ALL_TESTS();
127 
128   return rc;
129 }
130