OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cstring> | 5 #include <cstring> |
6 | 6 |
7 #include <base/file_util.h> | 7 #include <base/file_util.h> |
8 #include <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
9 | 9 |
10 #include "c_metrics_library.h" | 10 #include "c_metrics_library.h" |
11 #include "metrics_library.h" | 11 #include "metrics_library.h" |
12 | 12 |
13 static const FilePath kTestUMAEventsFile("test-uma-events"); | 13 static const FilePath kTestUMAEventsFile("test-uma-events"); |
14 | 14 |
| 15 static const char kTestConsent[] = "test-consent"; |
| 16 |
| 17 static void SetMetricsEnabled(bool enabled) { |
| 18 if (enabled) |
| 19 ASSERT_EQ(1, file_util::WriteFile(FilePath(kTestConsent) , "0", 1)); |
| 20 else |
| 21 file_util::Delete(FilePath(kTestConsent), false); |
| 22 } |
| 23 |
15 class MetricsLibraryTest : public testing::Test { | 24 class MetricsLibraryTest : public testing::Test { |
16 protected: | 25 protected: |
17 virtual void SetUp() { | 26 virtual void SetUp() { |
18 EXPECT_EQ(NULL, lib_.uma_events_file_); | 27 EXPECT_EQ(NULL, lib_.uma_events_file_); |
19 lib_.Init(); | 28 lib_.Init(); |
20 EXPECT_TRUE(NULL != lib_.uma_events_file_); | 29 EXPECT_TRUE(NULL != lib_.uma_events_file_); |
21 lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); | 30 lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
| 31 SetMetricsEnabled(true); |
| 32 // Defeat metrics enabled caching between tests. |
| 33 lib_.cached_enabled_time_ = 0; |
| 34 lib_.consent_file_ = kTestConsent; |
22 } | 35 } |
23 | 36 |
24 virtual void TearDown() { | 37 virtual void TearDown() { |
25 file_util::Delete(kTestUMAEventsFile, false); | 38 file_util::Delete(kTestUMAEventsFile, false); |
26 } | 39 } |
27 | 40 |
| 41 void VerifyEnabledCacheHit(bool to_value); |
| 42 void VerifyEnabledCacheEviction(bool to_value); |
| 43 |
28 MetricsLibrary lib_; | 44 MetricsLibrary lib_; |
29 }; | 45 }; |
30 | 46 |
| 47 TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
| 48 SetMetricsEnabled(false); |
| 49 EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 50 } |
| 51 |
| 52 TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
| 53 EXPECT_TRUE(lib_.AreMetricsEnabled()); |
| 54 } |
| 55 |
| 56 void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { |
| 57 // We might step from one second to the next one time, but not 100 |
| 58 // times in a row. |
| 59 for (int i = 0; i < 100; ++i) { |
| 60 lib_.cached_enabled_time_ = 0; |
| 61 SetMetricsEnabled(!to_value); |
| 62 ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
| 63 SetMetricsEnabled(to_value); |
| 64 if (lib_.AreMetricsEnabled() == !to_value) |
| 65 return; |
| 66 } |
| 67 ADD_FAILURE() << "Did not see evidence of caching"; |
| 68 } |
| 69 |
| 70 void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) { |
| 71 lib_.cached_enabled_time_ = 0; |
| 72 SetMetricsEnabled(!to_value); |
| 73 ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
| 74 SetMetricsEnabled(to_value); |
| 75 ASSERT_LT(abs(time(NULL) - lib_.cached_enabled_time_), 5); |
| 76 // Sleep one second (or cheat to be faster). |
| 77 --lib_.cached_enabled_time_; |
| 78 ASSERT_EQ(to_value, lib_.AreMetricsEnabled()); |
| 79 } |
| 80 |
| 81 TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) { |
| 82 VerifyEnabledCacheHit(false); |
| 83 VerifyEnabledCacheHit(true); |
| 84 VerifyEnabledCacheEviction(false); |
| 85 VerifyEnabledCacheEviction(true); |
| 86 } |
| 87 |
31 TEST_F(MetricsLibraryTest, FormatChromeMessage) { | 88 TEST_F(MetricsLibraryTest, FormatChromeMessage) { |
32 char buf[7]; | 89 char buf[7]; |
33 const int kLen = 6; | 90 const int kLen = 6; |
34 EXPECT_EQ(kLen, lib_.FormatChromeMessage(7, buf, "%d", 1)); | 91 EXPECT_EQ(kLen, lib_.FormatChromeMessage(7, buf, "%d", 1)); |
35 | 92 |
36 char exp[kLen]; | 93 char exp[kLen]; |
37 sprintf(exp, "%c%c%c%c1", kLen, 0, 0, 0); | 94 sprintf(exp, "%c%c%c%c1", kLen, 0, 0, 0); |
38 EXPECT_EQ(0, memcmp(exp, buf, kLen)); | 95 EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
39 } | 96 } |
40 | 97 |
41 TEST_F(MetricsLibraryTest, FormatChromeMessageTooLong) { | 98 TEST_F(MetricsLibraryTest, FormatChromeMessageTooLong) { |
42 char buf[7]; | 99 char buf[7]; |
43 EXPECT_EQ(-1, lib_.FormatChromeMessage(7, buf, "test")); | 100 EXPECT_EQ(-1, lib_.FormatChromeMessage(7, buf, "test")); |
44 } | 101 } |
45 | 102 |
46 TEST_F(MetricsLibraryTest, SendEnumToUMA) { | 103 TEST_F(MetricsLibraryTest, SendEnumToUMA) { |
47 char buf[100]; | 104 char buf[100]; |
48 const int kLen = 40; | 105 const int kLen = 40; |
49 EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); | 106 EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); |
50 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); | 107 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
51 | 108 |
52 char exp[kLen]; | 109 char exp[kLen]; |
53 sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", | 110 sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
54 kLen, 0, 0, 0, 0); | 111 kLen, 0, 0, 0, 0); |
55 EXPECT_EQ(0, memcmp(exp, buf, kLen)); | 112 EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
56 } | 113 } |
57 | 114 |
| 115 TEST_F(MetricsLibraryTest, SendEnumToUMANotEnabled) { |
| 116 SetMetricsEnabled(false); |
| 117 EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); |
| 118 EXPECT_FALSE(file_util::PathExists(kTestUMAEventsFile)); |
| 119 } |
| 120 |
58 TEST_F(MetricsLibraryTest, SendMessageToChrome) { | 121 TEST_F(MetricsLibraryTest, SendMessageToChrome) { |
59 EXPECT_TRUE(lib_.SendMessageToChrome(4, "test")); | 122 EXPECT_TRUE(lib_.SendMessageToChrome(4, "test")); |
60 EXPECT_TRUE(lib_.SendMessageToChrome(7, "content")); | 123 EXPECT_TRUE(lib_.SendMessageToChrome(7, "content")); |
61 std::string uma_events; | 124 std::string uma_events; |
62 EXPECT_TRUE(file_util::ReadFileToString(kTestUMAEventsFile, &uma_events)); | 125 EXPECT_TRUE(file_util::ReadFileToString(kTestUMAEventsFile, &uma_events)); |
63 EXPECT_EQ("testcontent", uma_events); | 126 EXPECT_EQ("testcontent", uma_events); |
64 } | 127 } |
65 | 128 |
66 TEST_F(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation) { | 129 TEST_F(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation) { |
67 // Checks that the library doesn't die badly if the file can't be | 130 // Checks that the library doesn't die badly if the file can't be |
68 // created. | 131 // created. |
69 static const char kDoesNotExistFile[] = "/does/not/exist"; | 132 static const char kDoesNotExistFile[] = "/does/not/exist"; |
70 lib_.uma_events_file_ = kDoesNotExistFile; | 133 lib_.uma_events_file_ = kDoesNotExistFile; |
71 static const char kDummyMessage[] = "Dummy Message"; | 134 static const char kDummyMessage[] = "Dummy Message"; |
72 EXPECT_FALSE(lib_.SendMessageToChrome(strlen(kDummyMessage), kDummyMessage)); | 135 EXPECT_FALSE(lib_.SendMessageToChrome(strlen(kDummyMessage), kDummyMessage)); |
73 file_util::Delete(FilePath(kDoesNotExistFile), false); | 136 file_util::Delete(FilePath(kDoesNotExistFile), false); |
74 } | 137 } |
75 | 138 |
76 TEST_F(MetricsLibraryTest, SendToUMA) { | 139 TEST_F(MetricsLibraryTest, SendToUMA) { |
77 char buf[100]; | 140 char buf[100]; |
78 const int kLen = 37; | 141 const int kLen = 37; |
79 EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); | 142 EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); |
80 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); | 143 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
81 | 144 |
82 char exp[kLen]; | 145 char exp[kLen]; |
83 sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); | 146 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)); | 147 EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
85 } | 148 } |
86 | 149 |
| 150 TEST_F(MetricsLibraryTest, SendToUMANotEnabled) { |
| 151 SetMetricsEnabled(false); |
| 152 EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); |
| 153 EXPECT_FALSE(file_util::PathExists(kTestUMAEventsFile)); |
| 154 } |
| 155 |
87 class CMetricsLibraryTest : public testing::Test { | 156 class CMetricsLibraryTest : public testing::Test { |
88 protected: | 157 protected: |
89 virtual void SetUp() { | 158 virtual void SetUp() { |
90 lib_ = CMetricsLibraryNew(); | 159 lib_ = CMetricsLibraryNew(); |
91 MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_); | 160 MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_); |
92 EXPECT_EQ(NULL, ml.uma_events_file_); | 161 EXPECT_EQ(NULL, ml.uma_events_file_); |
93 CMetricsLibraryInit(lib_); | 162 CMetricsLibraryInit(lib_); |
94 EXPECT_TRUE(NULL != ml.uma_events_file_); | 163 EXPECT_TRUE(NULL != ml.uma_events_file_); |
95 ml.uma_events_file_ = kTestUMAEventsFile.value().c_str(); | 164 ml.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
| 165 SetMetricsEnabled(true); |
| 166 reinterpret_cast<MetricsLibrary*>(lib_)->cached_enabled_time_ = 0; |
| 167 reinterpret_cast<MetricsLibrary*>(lib_)->consent_file_ = kTestConsent; |
96 } | 168 } |
97 | 169 |
98 virtual void TearDown() { | 170 virtual void TearDown() { |
99 CMetricsLibraryDelete(lib_); | 171 CMetricsLibraryDelete(lib_); |
100 file_util::Delete(kTestUMAEventsFile, false); | 172 file_util::Delete(kTestUMAEventsFile, false); |
101 } | 173 } |
102 | 174 |
103 CMetricsLibrary lib_; | 175 CMetricsLibrary lib_; |
104 }; | 176 }; |
105 | 177 |
| 178 TEST_F(CMetricsLibraryTest, AreMetricsEnabledFalse) { |
| 179 SetMetricsEnabled(false); |
| 180 EXPECT_FALSE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 181 } |
| 182 |
| 183 TEST_F(CMetricsLibraryTest, AreMetricsEnabledTrue) { |
| 184 EXPECT_TRUE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 185 } |
| 186 |
106 TEST_F(CMetricsLibraryTest, SendEnumToUMA) { | 187 TEST_F(CMetricsLibraryTest, SendEnumToUMA) { |
107 char buf[100]; | 188 char buf[100]; |
108 const int kLen = 40; | 189 const int kLen = 40; |
109 EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3)); | 190 EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3)); |
110 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); | 191 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
111 | 192 |
112 char exp[kLen]; | 193 char exp[kLen]; |
113 sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", | 194 sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
114 kLen, 0, 0, 0, 0); | 195 kLen, 0, 0, 0, 0); |
115 EXPECT_EQ(0, memcmp(exp, buf, kLen)); | 196 EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
116 } | 197 } |
117 | 198 |
118 TEST_F(CMetricsLibraryTest, SendToUMA) { | 199 TEST_F(CMetricsLibraryTest, SendToUMA) { |
119 char buf[100]; | 200 char buf[100]; |
120 const int kLen = 37; | 201 const int kLen = 37; |
121 EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50)); | 202 EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50)); |
122 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); | 203 EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
123 | 204 |
124 char exp[kLen]; | 205 char exp[kLen]; |
125 sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); | 206 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)); | 207 EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
127 } | 208 } |
128 | 209 |
129 int main(int argc, char** argv) { | 210 int main(int argc, char** argv) { |
130 testing::InitGoogleTest(&argc, argv); | 211 testing::InitGoogleTest(&argc, argv); |
131 return RUN_ALL_TESTS(); | 212 return RUN_ALL_TESTS(); |
132 } | 213 } |
OLD | NEW |