• 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_server_metrics.c"
10 #include "cras_main_message.h"
11 #include "cras_rstream.h"
12 }
13 
14 static enum CRAS_MAIN_MESSAGE_TYPE type_set;
15 static struct cras_server_metrics_message *sent_msg;
16 
ResetStubData()17 void ResetStubData() {
18   type_set = (enum CRAS_MAIN_MESSAGE_TYPE)0;
19 }
20 
21 namespace {
22 
TEST(ServerMetricsTestSuite,Init)23 TEST(ServerMetricsTestSuite, Init) {
24   ResetStubData();
25 
26   cras_server_metrics_init();
27 
28   EXPECT_EQ(type_set, CRAS_MAIN_METRICS);
29 }
30 
TEST(ServerMetricsTestSuite,SetMetricHighestHardwareLevel)31 TEST(ServerMetricsTestSuite, SetMetricHighestHardwareLevel) {
32   ResetStubData();
33   unsigned int hw_level = 1000;
34   sent_msg = (struct cras_server_metrics_message *)calloc(1, sizeof(*sent_msg));
35 
36   cras_server_metrics_highest_hw_level(hw_level, CRAS_STREAM_INPUT);
37 
38   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_METRICS);
39   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
40   EXPECT_EQ(sent_msg->metrics_type, HIGHEST_INPUT_HW_LEVEL);
41   EXPECT_EQ(sent_msg->data.value, hw_level);
42 
43   free(sent_msg);
44 
45   sent_msg = (struct cras_server_metrics_message *)calloc(1, sizeof(*sent_msg));
46 
47   cras_server_metrics_highest_hw_level(hw_level, CRAS_STREAM_OUTPUT);
48 
49   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_METRICS);
50   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
51   EXPECT_EQ(sent_msg->metrics_type, HIGHEST_OUTPUT_HW_LEVEL);
52   EXPECT_EQ(sent_msg->data.value, hw_level);
53 
54   free(sent_msg);
55 }
56 
TEST(ServerMetricsTestSuite,SetMetricsLongestFetchDelay)57 TEST(ServerMetricsTestSuite, SetMetricsLongestFetchDelay) {
58   ResetStubData();
59   unsigned int delay = 100;
60   sent_msg = (struct cras_server_metrics_message *)calloc(1, sizeof(*sent_msg));
61 
62   cras_server_metrics_longest_fetch_delay(delay);
63 
64   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_METRICS);
65   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
66   EXPECT_EQ(sent_msg->metrics_type, LONGEST_FETCH_DELAY);
67   EXPECT_EQ(sent_msg->data.value, delay);
68 
69   free(sent_msg);
70 }
71 
TEST(ServerMetricsTestSuite,SetMetricsNumUnderruns)72 TEST(ServerMetricsTestSuite, SetMetricsNumUnderruns) {
73   ResetStubData();
74   unsigned int underrun = 10;
75   sent_msg = (struct cras_server_metrics_message *)calloc(1, sizeof(*sent_msg));
76 
77   cras_server_metrics_num_underruns(underrun);
78 
79   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_METRICS);
80   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
81   EXPECT_EQ(sent_msg->metrics_type, NUM_UNDERRUNS);
82   EXPECT_EQ(sent_msg->data.value, underrun);
83 
84   free(sent_msg);
85 }
86 
TEST(ServerMetricsTestSuite,SetMetricsStreamConfig)87 TEST(ServerMetricsTestSuite, SetMetricsStreamConfig) {
88   ResetStubData();
89   struct cras_rstream_config config;
90   struct cras_audio_format format;
91   sent_msg = (struct cras_server_metrics_message *)calloc(1, sizeof(*sent_msg));
92 
93   config.cb_threshold = 1024;
94   config.flags = BULK_AUDIO_OK;
95   format.format = SND_PCM_FORMAT_S16_LE;
96   format.frame_rate = 48000;
97 
98   config.format = &format;
99   cras_server_metrics_stream_config(&config);
100 
101   EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_METRICS);
102   EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg));
103   EXPECT_EQ(sent_msg->metrics_type, STREAM_CONFIG);
104   EXPECT_EQ(sent_msg->data.stream_config.cb_threshold, 1024);
105   EXPECT_EQ(sent_msg->data.stream_config.flags, BULK_AUDIO_OK);
106   EXPECT_EQ(sent_msg->data.stream_config.format, SND_PCM_FORMAT_S16_LE);
107   EXPECT_EQ(sent_msg->data.stream_config.rate, 48000);
108 
109   free(sent_msg);
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_metrics_log_histogram(const char * name,int sample,int min,int max,int nbuckets)121 void cras_metrics_log_histogram(const char *name, int sample, int min,
122                                 int max, int nbuckets) {
123 }
124 
cras_metrics_log_sparse_histogram(const char * name,int sample)125 void cras_metrics_log_sparse_histogram(const char *name, int sample)
126 {
127 }
128 
cras_main_message_send(struct cras_main_message * msg)129 int cras_main_message_send(struct cras_main_message *msg) {
130   // Copy the sent message so we can examine it in the test later.
131   memcpy(sent_msg, msg, sizeof(*sent_msg));
132   return 0;
133 };
134 
135 }  // extern "C"
136 }  // namespace
137 
main(int argc,char ** argv)138 int main(int argc, char **argv) {
139   ::testing::InitGoogleTest(&argc, argv);
140   int rc = RUN_ALL_TESTS();
141 
142   return rc;
143 }
144