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 } 12 13 static enum CRAS_MAIN_MESSAGE_TYPE type_set; 14 static struct cras_server_metrics_message *sent_msg; 15 16 void ResetStubData() { 17 type_set = (enum CRAS_MAIN_MESSAGE_TYPE)0; 18 } 19 20 namespace { 21 22 TEST(ServerMetricsTestSuite, Init) { 23 ResetStubData(); 24 25 cras_server_metrics_init(); 26 27 EXPECT_EQ(type_set, CRAS_MAIN_METRICS); 28 } 29 30 TEST(ServerMetricsTestSuite, SetMetrics) { 31 ResetStubData(); 32 unsigned int delay = 100; 33 sent_msg = (struct cras_server_metrics_message *)calloc(1, sizeof(*sent_msg)); 34 35 cras_server_metrics_longest_fetch_delay(delay); 36 37 EXPECT_EQ(sent_msg->header.type, CRAS_MAIN_METRICS); 38 EXPECT_EQ(sent_msg->header.length, sizeof(*sent_msg)); 39 EXPECT_EQ(sent_msg->metrics_type, LONGEST_FETCH_DELAY); 40 EXPECT_EQ(sent_msg->data, delay); 41 42 free(sent_msg); 43 } 44 45 extern "C" { 46 47 int cras_main_message_add_handler(enum CRAS_MAIN_MESSAGE_TYPE type, 48 cras_message_callback callback, 49 void *callback_data) { 50 type_set = type; 51 return 0; 52 } 53 54 void cras_metrics_log_histogram(const char *name, int sample, int min, 55 int max, int nbuckets) { 56 } 57 58 int cras_main_message_send(struct cras_main_message *msg) { 59 // Copy the sent message so we can examine it in the test later. 60 memcpy(sent_msg, msg, sizeof(*sent_msg)); 61 return 0; 62 }; 63 64 } // extern "C" 65 } // namespace 66 67 int main(int argc, char **argv) { 68 ::testing::InitGoogleTest(&argc, argv); 69 int rc = RUN_ALL_TESTS(); 70 71 return rc; 72 } 73