Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 5 | #include <cstring> |
| 6 | |
| 7 | #include <base/file_util.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame^] | 10 | #include "c_metrics_library.h" |
| 11 | #include "metrics_library.h" |
| 12 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 13 | static const FilePath kTestUMAEventsFile("test-uma-events"); |
| 14 | |
| 15 | class MetricsLibraryTest : public testing::Test { |
| 16 | protected: |
| 17 | virtual void SetUp() { |
| 18 | EXPECT_EQ(NULL, lib_.uma_events_file_); |
| 19 | lib_.Init(); |
| 20 | EXPECT_TRUE(NULL != lib_.uma_events_file_); |
| 21 | lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
| 22 | } |
| 23 | |
| 24 | virtual void TearDown() { |
| 25 | file_util::Delete(kTestUMAEventsFile, false); |
| 26 | } |
| 27 | |
| 28 | MetricsLibrary lib_; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(MetricsLibraryTest, FormatChromeMessage) { |
| 32 | char buf[7]; |
| 33 | const int kLen = 6; |
| 34 | EXPECT_EQ(kLen, lib_.FormatChromeMessage(7, buf, "%d", 1)); |
| 35 | |
| 36 | char exp[kLen]; |
| 37 | sprintf(exp, "%c%c%c%c1", kLen, 0, 0, 0); |
| 38 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 39 | } |
| 40 | |
| 41 | TEST_F(MetricsLibraryTest, FormatChromeMessageTooLong) { |
| 42 | char buf[7]; |
| 43 | EXPECT_EQ(-1, lib_.FormatChromeMessage(7, buf, "test")); |
| 44 | } |
| 45 | |
| 46 | TEST_F(MetricsLibraryTest, SendEnumToUMA) { |
| 47 | char buf[100]; |
| 48 | const int kLen = 40; |
| 49 | EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); |
| 50 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 51 | |
| 52 | char exp[kLen]; |
| 53 | sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
| 54 | kLen, 0, 0, 0, 0); |
| 55 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 56 | } |
| 57 | |
| 58 | TEST_F(MetricsLibraryTest, SendMessageToChrome) { |
| 59 | EXPECT_TRUE(lib_.SendMessageToChrome(4, "test")); |
| 60 | EXPECT_TRUE(lib_.SendMessageToChrome(7, "content")); |
| 61 | std::string uma_events; |
| 62 | EXPECT_TRUE(file_util::ReadFileToString(kTestUMAEventsFile, &uma_events)); |
| 63 | EXPECT_EQ("testcontent", uma_events); |
| 64 | } |
| 65 | |
| 66 | TEST_F(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation) { |
| 67 | // Checks that the library doesn't die badly if the file can't be |
| 68 | // created. |
| 69 | static const char kDoesNotExistFile[] = "/does/not/exist"; |
| 70 | lib_.uma_events_file_ = kDoesNotExistFile; |
| 71 | static const char kDummyMessage[] = "Dummy Message"; |
| 72 | EXPECT_FALSE(lib_.SendMessageToChrome(strlen(kDummyMessage), kDummyMessage)); |
| 73 | file_util::Delete(FilePath(kDoesNotExistFile), false); |
| 74 | } |
| 75 | |
| 76 | TEST_F(MetricsLibraryTest, SendToUMA) { |
| 77 | char buf[100]; |
| 78 | const int kLen = 37; |
| 79 | EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); |
| 80 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 81 | |
| 82 | char exp[kLen]; |
| 83 | sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 84 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 85 | } |
| 86 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame^] | 87 | class CMetricsLibraryTest : public testing::Test { |
| 88 | protected: |
| 89 | virtual void SetUp() { |
| 90 | lib_ = CMetricsLibraryNew(); |
| 91 | MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_); |
| 92 | EXPECT_EQ(NULL, ml.uma_events_file_); |
| 93 | CMetricsLibraryInit(lib_); |
| 94 | EXPECT_TRUE(NULL != ml.uma_events_file_); |
| 95 | ml.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
| 96 | } |
| 97 | |
| 98 | virtual void TearDown() { |
| 99 | CMetricsLibraryDelete(lib_); |
| 100 | file_util::Delete(kTestUMAEventsFile, false); |
| 101 | } |
| 102 | |
| 103 | CMetricsLibrary lib_; |
| 104 | }; |
| 105 | |
| 106 | TEST_F(CMetricsLibraryTest, SendEnumToUMA) { |
| 107 | char buf[100]; |
| 108 | const int kLen = 40; |
| 109 | EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3)); |
| 110 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 111 | |
| 112 | char exp[kLen]; |
| 113 | sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
| 114 | kLen, 0, 0, 0, 0); |
| 115 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 116 | } |
| 117 | |
| 118 | TEST_F(CMetricsLibraryTest, SendToUMA) { |
| 119 | char buf[100]; |
| 120 | const int kLen = 37; |
| 121 | EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50)); |
| 122 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 123 | |
| 124 | char exp[kLen]; |
| 125 | sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 126 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 127 | } |
| 128 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 129 | int main(int argc, char** argv) { |
| 130 | testing::InitGoogleTest(&argc, argv); |
| 131 | return RUN_ALL_TESTS(); |
| 132 | } |