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> |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 10 | #include <policy/mock_device_policy.h> |
| 11 | #include <policy/libpolicy.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 12 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 13 | #include "c_metrics_library.h" |
| 14 | #include "metrics_library.h" |
| 15 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 16 | using base::FilePath; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 17 | using ::testing::_; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::AnyNumber; |
| 20 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 21 | static const FilePath kTestUMAEventsFile("test-uma-events"); |
| 22 | static const char kTestMounts[] = "test-mounts"; |
| 23 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 24 | ACTION_P(SetMetricsPolicy, enabled) { |
| 25 | *arg0 = enabled; |
| 26 | return true; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 29 | class MetricsLibraryTest : public testing::Test { |
| 30 | protected: |
| 31 | virtual void SetUp() { |
| 32 | EXPECT_EQ(NULL, lib_.uma_events_file_); |
| 33 | lib_.Init(); |
| 34 | EXPECT_TRUE(NULL != lib_.uma_events_file_); |
| 35 | lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 36 | device_policy_ = new policy::MockDevicePolicy(); |
| 37 | EXPECT_CALL(*device_policy_, LoadPolicy()) |
| 38 | .Times(AnyNumber()) |
| 39 | .WillRepeatedly(Return(true)); |
| 40 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 41 | .Times(AnyNumber()) |
| 42 | .WillRepeatedly(SetMetricsPolicy(true)); |
| 43 | provider_ = new policy::PolicyProvider(device_policy_); |
| 44 | lib_.SetPolicyProvider(provider_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 45 | // Defeat metrics enabled caching between tests. |
| 46 | lib_.cached_enabled_time_ = 0; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | virtual void TearDown() { |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 50 | base::DeleteFile(FilePath(kTestMounts), false); |
| 51 | base::DeleteFile(kTestUMAEventsFile, false); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 54 | void VerifyEnabledCacheHit(bool to_value); |
| 55 | void VerifyEnabledCacheEviction(bool to_value); |
| 56 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 57 | MetricsLibrary lib_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 58 | policy::MockDevicePolicy* device_policy_; |
| 59 | policy::PolicyProvider* provider_; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 62 | TEST_F(MetricsLibraryTest, IsDeviceMounted) { |
| 63 | static const char kTestContents[] = |
| 64 | "0123456789abcde 0123456789abcde\nguestfs foo bar\n"; |
| 65 | char buffer[1024]; |
| 66 | int block_sizes[] = { 1, 2, 3, 4, 5, 6, 8, 12, 14, 16, 32, 1024 }; |
| 67 | bool result; |
| 68 | EXPECT_FALSE(lib_.IsDeviceMounted("guestfs", |
| 69 | "nonexistent", |
| 70 | buffer, |
| 71 | 1, |
| 72 | &result)); |
| 73 | ASSERT_TRUE(file_util::WriteFile(FilePath(kTestMounts), |
| 74 | kTestContents, |
| 75 | strlen(kTestContents))); |
| 76 | EXPECT_FALSE(lib_.IsDeviceMounted("guestfs", |
| 77 | kTestMounts, |
| 78 | buffer, |
| 79 | 0, |
| 80 | &result)); |
| 81 | for (size_t i = 0; i < arraysize(block_sizes); ++i) { |
| 82 | EXPECT_TRUE(lib_.IsDeviceMounted("0123456789abcde", |
| 83 | kTestMounts, |
| 84 | buffer, |
| 85 | block_sizes[i], |
| 86 | &result)); |
| 87 | EXPECT_TRUE(result); |
| 88 | EXPECT_TRUE(lib_.IsDeviceMounted("guestfs", |
| 89 | kTestMounts, |
| 90 | buffer, |
| 91 | block_sizes[i], |
| 92 | &result)); |
| 93 | EXPECT_TRUE(result); |
| 94 | EXPECT_TRUE(lib_.IsDeviceMounted("0123456", |
| 95 | kTestMounts, |
| 96 | buffer, |
| 97 | block_sizes[i], |
| 98 | &result)); |
| 99 | EXPECT_FALSE(result); |
| 100 | EXPECT_TRUE(lib_.IsDeviceMounted("9abcde", |
| 101 | kTestMounts, |
| 102 | buffer, |
| 103 | block_sizes[i], |
| 104 | &result)); |
| 105 | EXPECT_FALSE(result); |
| 106 | EXPECT_TRUE(lib_.IsDeviceMounted("foo", |
| 107 | kTestMounts, |
| 108 | buffer, |
| 109 | block_sizes[i], |
| 110 | &result)); |
| 111 | EXPECT_FALSE(result); |
| 112 | EXPECT_TRUE(lib_.IsDeviceMounted("bar", |
| 113 | kTestMounts, |
| 114 | buffer, |
| 115 | block_sizes[i], |
| 116 | &result)); |
| 117 | EXPECT_FALSE(result); |
| 118 | } |
| 119 | } |
| 120 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 121 | TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 122 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 123 | .WillOnce(SetMetricsPolicy(false)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 125 | } |
| 126 | |
| 127 | TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
| 128 | EXPECT_TRUE(lib_.AreMetricsEnabled()); |
| 129 | } |
| 130 | |
| 131 | void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { |
| 132 | // We might step from one second to the next one time, but not 100 |
| 133 | // times in a row. |
| 134 | for (int i = 0; i < 100; ++i) { |
| 135 | lib_.cached_enabled_time_ = 0; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 136 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 137 | .WillOnce(SetMetricsPolicy(!to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 138 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
Julian Pastarmov | 0e5debf | 2011-08-04 11:15:13 +0200 | [diff] [blame] | 139 | ON_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 140 | .WillByDefault(SetMetricsPolicy(to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 141 | if (lib_.AreMetricsEnabled() == !to_value) |
| 142 | return; |
| 143 | } |
| 144 | ADD_FAILURE() << "Did not see evidence of caching"; |
| 145 | } |
| 146 | |
| 147 | void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) { |
| 148 | lib_.cached_enabled_time_ = 0; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 149 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 150 | .WillOnce(SetMetricsPolicy(!to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 151 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 152 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 153 | .WillOnce(SetMetricsPolicy(to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 154 | ASSERT_LT(abs(time(NULL) - lib_.cached_enabled_time_), 5); |
| 155 | // Sleep one second (or cheat to be faster). |
| 156 | --lib_.cached_enabled_time_; |
| 157 | ASSERT_EQ(to_value, lib_.AreMetricsEnabled()); |
| 158 | } |
| 159 | |
| 160 | TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) { |
| 161 | VerifyEnabledCacheHit(false); |
| 162 | VerifyEnabledCacheHit(true); |
| 163 | VerifyEnabledCacheEviction(false); |
| 164 | VerifyEnabledCacheEviction(true); |
| 165 | } |
| 166 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 167 | TEST_F(MetricsLibraryTest, FormatChromeMessage) { |
| 168 | char buf[7]; |
| 169 | const int kLen = 6; |
| 170 | EXPECT_EQ(kLen, lib_.FormatChromeMessage(7, buf, "%d", 1)); |
| 171 | |
| 172 | char exp[kLen]; |
| 173 | sprintf(exp, "%c%c%c%c1", kLen, 0, 0, 0); |
| 174 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 175 | } |
| 176 | |
| 177 | TEST_F(MetricsLibraryTest, FormatChromeMessageTooLong) { |
| 178 | char buf[7]; |
| 179 | EXPECT_EQ(-1, lib_.FormatChromeMessage(7, buf, "test")); |
| 180 | } |
| 181 | |
| 182 | TEST_F(MetricsLibraryTest, SendEnumToUMA) { |
| 183 | char buf[100]; |
| 184 | const int kLen = 40; |
| 185 | EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 186 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 187 | |
| 188 | char exp[kLen]; |
| 189 | sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
| 190 | kLen, 0, 0, 0, 0); |
| 191 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 192 | } |
| 193 | |
| 194 | TEST_F(MetricsLibraryTest, SendMessageToChrome) { |
| 195 | EXPECT_TRUE(lib_.SendMessageToChrome(4, "test")); |
| 196 | EXPECT_TRUE(lib_.SendMessageToChrome(7, "content")); |
| 197 | std::string uma_events; |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 198 | EXPECT_TRUE(base::ReadFileToString(kTestUMAEventsFile, &uma_events)); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 199 | EXPECT_EQ("testcontent", uma_events); |
| 200 | } |
| 201 | |
| 202 | TEST_F(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation) { |
| 203 | // Checks that the library doesn't die badly if the file can't be |
| 204 | // created. |
| 205 | static const char kDoesNotExistFile[] = "/does/not/exist"; |
| 206 | lib_.uma_events_file_ = kDoesNotExistFile; |
| 207 | static const char kDummyMessage[] = "Dummy Message"; |
| 208 | EXPECT_FALSE(lib_.SendMessageToChrome(strlen(kDummyMessage), kDummyMessage)); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 209 | base::DeleteFile(FilePath(kDoesNotExistFile), false); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST_F(MetricsLibraryTest, SendToUMA) { |
| 213 | char buf[100]; |
| 214 | const int kLen = 37; |
| 215 | EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 216 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 217 | |
| 218 | char exp[kLen]; |
| 219 | sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 220 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 221 | } |
| 222 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 223 | TEST_F(MetricsLibraryTest, SendUserActionToUMA) { |
| 224 | char buf[100]; |
| 225 | const int kLen = 30; |
| 226 | EXPECT_TRUE(lib_.SendUserActionToUMA("SomeKeyPressed")); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 227 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 228 | |
| 229 | char exp[kLen]; |
| 230 | sprintf(exp, "%c%c%c%cuseraction%cSomeKeyPressed", kLen, 0, 0, 0, 0); |
| 231 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 232 | } |
| 233 | |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 234 | TEST_F(MetricsLibraryTest, SendSparseToUMA) { |
| 235 | char buf[100]; |
| 236 | const int kLen = 4 + sizeof("sparsehistogram") + sizeof("Test.Sparse 1234"); |
| 237 | EXPECT_TRUE(lib_.SendSparseToUMA("Test.Sparse", 1234)); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 238 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 239 | |
| 240 | char exp[kLen]; |
| 241 | sprintf(exp, "%c%c%c%csparsehistogram%cTest.Sparse 1234", kLen, 0, 0, 0, 0); |
| 242 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 243 | } |
| 244 | |
Luigi Semenzato | df3e452 | 2011-10-14 15:31:32 -0700 | [diff] [blame] | 245 | TEST_F(MetricsLibraryTest, SendCrashToUMA) { |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 246 | EXPECT_TRUE(lib_.SendCrashToUMA("kernel")); |
| 247 | char exp[100]; |
| 248 | int len = sprintf(exp, "%c%c%c%ccrash%ckernel", |
| 249 | 0, 0, 0, 0, 0) + 1; |
| 250 | exp[0] = len; |
| 251 | char buf[100]; |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 252 | EXPECT_EQ(len, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 253 | EXPECT_EQ(0, memcmp(exp, buf, len)); |
| 254 | } |
| 255 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 256 | class CMetricsLibraryTest : public testing::Test { |
| 257 | protected: |
| 258 | virtual void SetUp() { |
| 259 | lib_ = CMetricsLibraryNew(); |
| 260 | MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_); |
| 261 | EXPECT_EQ(NULL, ml.uma_events_file_); |
| 262 | CMetricsLibraryInit(lib_); |
| 263 | EXPECT_TRUE(NULL != ml.uma_events_file_); |
| 264 | ml.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 265 | device_policy_ = new policy::MockDevicePolicy(); |
| 266 | EXPECT_CALL(*device_policy_, LoadPolicy()) |
| 267 | .Times(AnyNumber()) |
| 268 | .WillRepeatedly(Return(true)); |
| 269 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 270 | .Times(AnyNumber()) |
| 271 | .WillRepeatedly(SetMetricsPolicy(true)); |
| 272 | provider_ = new policy::PolicyProvider(device_policy_); |
| 273 | ml.SetPolicyProvider(provider_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 274 | reinterpret_cast<MetricsLibrary*>(lib_)->cached_enabled_time_ = 0; |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | virtual void TearDown() { |
| 278 | CMetricsLibraryDelete(lib_); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 279 | base::DeleteFile(kTestUMAEventsFile, false); |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | CMetricsLibrary lib_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 283 | policy::MockDevicePolicy* device_policy_; |
| 284 | policy::PolicyProvider* provider_; |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 287 | TEST_F(CMetricsLibraryTest, AreMetricsEnabledFalse) { |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 288 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 289 | .WillOnce(SetMetricsPolicy(false)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 290 | EXPECT_FALSE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 291 | } |
| 292 | |
| 293 | TEST_F(CMetricsLibraryTest, AreMetricsEnabledTrue) { |
| 294 | EXPECT_TRUE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 295 | } |
| 296 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 297 | TEST_F(CMetricsLibraryTest, SendEnumToUMA) { |
| 298 | char buf[100]; |
| 299 | const int kLen = 40; |
| 300 | EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3)); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 301 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 302 | |
| 303 | char exp[kLen]; |
| 304 | sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
| 305 | kLen, 0, 0, 0, 0); |
| 306 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 307 | } |
| 308 | |
| 309 | TEST_F(CMetricsLibraryTest, SendToUMA) { |
| 310 | char buf[100]; |
| 311 | const int kLen = 37; |
| 312 | EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50)); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 313 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 314 | |
| 315 | char exp[kLen]; |
| 316 | sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 317 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 318 | } |
| 319 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 320 | TEST_F(CMetricsLibraryTest, SendUserActionToUMA) { |
| 321 | char buf[100]; |
| 322 | const int kLen = 30; |
| 323 | EXPECT_TRUE(CMetricsLibrarySendUserActionToUMA(lib_, "SomeKeyPressed")); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 324 | EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 325 | |
| 326 | char exp[kLen]; |
| 327 | sprintf(exp, "%c%c%c%cuseraction%cSomeKeyPressed", kLen, 0, 0, 0, 0); |
| 328 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 329 | } |
| 330 | |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 331 | TEST_F(CMetricsLibraryTest, SendCrashToUMA) { |
| 332 | char buf[100]; |
| 333 | char exp[100]; |
| 334 | int len = sprintf(exp, "%c%c%c%ccrash%cuser", 0, 0, 0, 0, 0) + 1; |
| 335 | exp[0] = len; |
| 336 | EXPECT_TRUE(CMetricsLibrarySendCrashToUMA(lib_, "user")); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 337 | EXPECT_EQ(len, base::ReadFile(kTestUMAEventsFile, buf, 100)); |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 338 | |
| 339 | EXPECT_EQ(0, memcmp(exp, buf, len)); |
| 340 | } |
| 341 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 342 | int main(int argc, char** argv) { |
| 343 | testing::InitGoogleTest(&argc, argv); |
| 344 | return RUN_ALL_TESTS(); |
| 345 | } |