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 <sys/file.h> | 5 #include <sys/file.h> |
6 | 6 |
7 #include <base/eintr_wrapper.h> | 7 #include <base/eintr_wrapper.h> |
8 #include <base/file_util.h> | 8 #include <base/file_util.h> |
9 #include <base/logging.h> | 9 #include <base/logging.h> |
10 #include <base/string_util.h> | 10 #include <base/string_util.h> |
11 #include <gmock/gmock.h> | 11 #include <gmock/gmock.h> |
12 #include <gtest/gtest.h> | 12 #include <gtest/gtest.h> |
13 | 13 |
14 #include "counter.h" | 14 #include "counter.h" |
15 #include "counter_mock.h" // For TaggedCounterMock. | 15 #include "counter_mock.h" // For TaggedCounterMock. |
| 16 #include "metrics_library_mock.h" |
16 | 17 |
17 using ::testing::_; | 18 using ::testing::_; |
18 using ::testing::MockFunction; | 19 using ::testing::MockFunction; |
19 using ::testing::StrictMock; | 20 using ::testing::StrictMock; |
20 | 21 |
21 namespace chromeos_metrics { | 22 namespace chromeos_metrics { |
22 | 23 |
23 static const char kTestRecordFile[] = "record-file"; | 24 static const char kTestRecordFile[] = "record-file"; |
24 static const char kDoesNotExistFile[] = "/does/not/exist"; | 25 static const char kDoesNotExistFile[] = "/does/not/exist"; |
25 | 26 |
26 class RecordTest : public testing::Test { | 27 class RecordTest : public testing::Test { |
27 protected: | 28 protected: |
28 virtual void SetUp() { | 29 virtual void SetUp() { |
29 EXPECT_EQ(0, record_.tag()); | 30 EXPECT_EQ(0, record_.tag()); |
30 EXPECT_EQ(0, record_.count()); | 31 EXPECT_EQ(0, record_.count()); |
31 } | 32 } |
32 | 33 |
33 // The record under test. | 34 // The record under test. |
34 TaggedCounter::Record record_; | 35 TaggedCounter::Record record_; |
35 }; | 36 }; |
36 | 37 |
37 class TaggedCounterTest : public testing::Test { | 38 class TaggedCounterTest : public testing::Test { |
38 protected: | 39 protected: |
39 virtual void SetUp() { | 40 virtual void SetUp() { |
40 EXPECT_EQ(NULL, counter_.filename_); | 41 EXPECT_TRUE(counter_.filename_.empty()); |
41 EXPECT_TRUE(NULL == counter_.reporter_); | 42 EXPECT_TRUE(NULL == counter_.reporter_); |
42 EXPECT_EQ(NULL, counter_.reporter_handle_); | 43 EXPECT_EQ(NULL, counter_.reporter_handle_); |
43 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); | 44 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
44 | 45 |
45 counter_.Init(kTestRecordFile, &Reporter, this); | 46 counter_.Init(kTestRecordFile, &Reporter, this); |
46 EXPECT_TRUE(AssertNoOrEmptyRecordFile()); | 47 EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
47 EXPECT_EQ(kTestRecordFile, counter_.filename_); | 48 EXPECT_EQ(kTestRecordFile, counter_.filename_); |
48 EXPECT_TRUE(Reporter == counter_.reporter_); | 49 EXPECT_TRUE(Reporter == counter_.reporter_); |
49 EXPECT_EQ(this, counter_.reporter_handle_); | 50 EXPECT_EQ(this, counter_.reporter_handle_); |
50 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); | 51 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 counter_.Update(/* tag */ 21, /* count */ 15); | 250 counter_.Update(/* tag */ 21, /* count */ 15); |
250 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15); | 251 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15); |
251 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); | 252 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
252 | 253 |
253 ExpectReporterCall(/* tag */ 21, /* count */ 15); | 254 ExpectReporterCall(/* tag */ 21, /* count */ 15); |
254 counter_.Update(/* tag */ 22, /* count */ 0); | 255 counter_.Update(/* tag */ 22, /* count */ 0); |
255 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 22, /* seconds */ 0); | 256 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 22, /* seconds */ 0); |
256 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); | 257 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
257 } | 258 } |
258 | 259 |
| 260 static const char kTestFilename[] = "test_filename"; |
| 261 static const char kTestHistogram[] = "test_histogram"; |
| 262 const int kHistogramMin = 15; |
| 263 const int kHistogramMax = 1024; |
| 264 const int kHistogramBuckets = 23; |
| 265 |
| 266 class TaggedCounterReporterTest : public testing::Test { |
| 267 protected: |
| 268 virtual void SetUp() { |
| 269 tagged_counter_ = new StrictMock<TaggedCounterMock>(); |
| 270 reporter_.tagged_counter_.reset(tagged_counter_); |
| 271 metrics_lib_.reset(new StrictMock<MetricsLibraryMock>); |
| 272 reporter_.SetMetricsLibraryInterface(metrics_lib_.get()); |
| 273 ASSERT_TRUE(metrics_lib_.get() == reporter_.metrics_lib_); |
| 274 } |
| 275 virtual void TearDown() { |
| 276 reporter_.SetMetricsLibraryInterface(NULL); |
| 277 } |
| 278 |
| 279 void DoInit(); |
| 280 StrictMock<TaggedCounterMock>* tagged_counter_; |
| 281 TaggedCounterReporter reporter_; |
| 282 scoped_ptr<MetricsLibraryMock> metrics_lib_; |
| 283 }; |
| 284 |
| 285 void TaggedCounterReporterTest::DoInit() { |
| 286 EXPECT_CALL(*tagged_counter_, |
| 287 Init(kTestFilename, |
| 288 TaggedCounterReporter::Report, |
| 289 &reporter_)) |
| 290 .Times(1) |
| 291 .RetiresOnSaturation(); |
| 292 reporter_.Init(kTestFilename, |
| 293 kTestHistogram, |
| 294 kHistogramMin, |
| 295 kHistogramMax, |
| 296 kHistogramBuckets); |
| 297 EXPECT_EQ(kTestHistogram, reporter_.histogram_name_); |
| 298 EXPECT_EQ(kHistogramBuckets, reporter_.buckets_); |
| 299 EXPECT_EQ(kHistogramMax, reporter_.max_); |
| 300 EXPECT_EQ(kHistogramMin, reporter_.min_); |
| 301 } |
| 302 |
| 303 TEST_F(TaggedCounterReporterTest, Init) { |
| 304 DoInit(); |
| 305 } |
| 306 |
| 307 TEST_F(TaggedCounterReporterTest, Update) { |
| 308 DoInit(); |
| 309 EXPECT_CALL(*tagged_counter_, Update(1, 2)) |
| 310 .Times(1) |
| 311 .RetiresOnSaturation(); |
| 312 reporter_.Update(1, 2); |
| 313 } |
| 314 |
| 315 TEST_F(TaggedCounterReporterTest, Flush) { |
| 316 DoInit(); |
| 317 EXPECT_CALL(*tagged_counter_, Flush()) |
| 318 .Times(1) |
| 319 .RetiresOnSaturation(); |
| 320 reporter_.Flush(); |
| 321 } |
| 322 |
| 323 TEST_F(TaggedCounterReporterTest, Report) { |
| 324 DoInit(); |
| 325 EXPECT_CALL(*metrics_lib_, SendToUMA(kTestHistogram, |
| 326 301, |
| 327 kHistogramMin, |
| 328 kHistogramMax, |
| 329 kHistogramBuckets)) |
| 330 .Times(1) |
| 331 .RetiresOnSaturation(); |
| 332 reporter_.Report(&reporter_, 127, 301); |
| 333 } |
| 334 |
259 class FrequencyCounterTest : public testing::Test { | 335 class FrequencyCounterTest : public testing::Test { |
260 protected: | 336 protected: |
261 virtual void SetUp() { | 337 virtual void SetUp() { |
262 tagged_counter_ = new StrictMock<TaggedCounterMock>; | 338 tagged_counter_ = NULL; |
263 frequency_counter_.tagged_counter_.reset(tagged_counter_); | |
264 } | |
265 | |
266 static void FakeReporter(void *, int32, int32) { | |
267 } | 339 } |
268 | 340 |
269 void CheckInit(int32 cycle_duration); | 341 void CheckInit(int32 cycle_duration); |
270 void CheckCycleNumber(int32 cycle_duration); | 342 void CheckCycleNumber(int32 cycle_duration); |
271 | 343 |
272 FrequencyCounter frequency_counter_; | 344 FrequencyCounter frequency_counter_; |
273 StrictMock<TaggedCounterMock>* tagged_counter_; | 345 StrictMock<TaggedCounterMock>* tagged_counter_; |
274 | 346 |
275 TaggedCounter::Reporter reporter_; | 347 TaggedCounter::Reporter reporter_; |
276 }; | 348 }; |
277 | 349 |
278 void FrequencyCounterTest::CheckInit(int32 cycle_duration) { | 350 void FrequencyCounterTest::CheckInit(int32 cycle_duration) { |
279 EXPECT_CALL(*tagged_counter_, Init(kTestRecordFile, FakeReporter, this)) | 351 tagged_counter_ = new StrictMock<TaggedCounterMock>; |
280 .Times(1) | 352 frequency_counter_.Init(tagged_counter_, cycle_duration); |
281 .RetiresOnSaturation(); | |
282 frequency_counter_.Init(kTestRecordFile, | |
283 FakeReporter, | |
284 this, | |
285 cycle_duration); | |
286 EXPECT_EQ(cycle_duration, frequency_counter_.cycle_duration_); | 353 EXPECT_EQ(cycle_duration, frequency_counter_.cycle_duration_); |
| 354 EXPECT_EQ(tagged_counter_, frequency_counter_.tagged_counter_.get()); |
287 } | 355 } |
288 | 356 |
289 TEST_F(FrequencyCounterTest, Init) { | 357 TEST_F(FrequencyCounterTest, Init) { |
290 CheckInit(100); | 358 CheckInit(100); |
291 } | 359 } |
292 | 360 |
293 void FrequencyCounterTest::CheckCycleNumber(int32 cycle_duration) { | 361 void FrequencyCounterTest::CheckCycleNumber(int32 cycle_duration) { |
294 CheckInit(cycle_duration); | 362 CheckInit(cycle_duration); |
295 EXPECT_EQ(150, frequency_counter_.GetCycleNumber(cycle_duration * 150)); | 363 EXPECT_EQ(150, frequency_counter_.GetCycleNumber( |
296 EXPECT_EQ(150, frequency_counter_.GetCycleNumber(cycle_duration * 150 + | 364 cycle_duration * 150)); |
297 cycle_duration - 1)); | 365 EXPECT_EQ(150, frequency_counter_.GetCycleNumber( |
298 EXPECT_EQ(151, frequency_counter_.GetCycleNumber(cycle_duration * 151 + 1)); | 366 cycle_duration * 150 + cycle_duration - 1)); |
| 367 EXPECT_EQ(151, frequency_counter_.GetCycleNumber( |
| 368 cycle_duration * 151 + 1)); |
299 EXPECT_EQ(0, frequency_counter_.GetCycleNumber(0)); | 369 EXPECT_EQ(0, frequency_counter_.GetCycleNumber(0)); |
300 } | 370 } |
301 | 371 |
302 | 372 |
303 TEST_F(FrequencyCounterTest, GetCycleNumberForWeek) { | 373 TEST_F(FrequencyCounterTest, GetCycleNumberForWeek) { |
304 CheckCycleNumber(kSecondsPerWeek); | 374 CheckCycleNumber(kSecondsPerWeek); |
305 } | 375 } |
306 | 376 |
307 TEST_F(FrequencyCounterTest, GetCycleNumberForDay) { | 377 TEST_F(FrequencyCounterTest, GetCycleNumberForDay) { |
308 CheckCycleNumber(kSecondsPerDay); | 378 CheckCycleNumber(kSecondsPerDay); |
309 } | 379 } |
310 | 380 |
311 TEST_F(FrequencyCounterTest, UpdateInternal) { | 381 TEST_F(FrequencyCounterTest, UpdateInternal) { |
312 CheckInit(kSecondsPerWeek); | 382 CheckInit(kSecondsPerWeek); |
313 EXPECT_CALL(*tagged_counter_, Update(150, 2)); | 383 EXPECT_CALL(*tagged_counter_, Update(150, 2)) |
| 384 .Times(1) |
| 385 .RetiresOnSaturation(); |
314 frequency_counter_.UpdateInternal(2, kSecondsPerWeek * 150); | 386 frequency_counter_.UpdateInternal(2, kSecondsPerWeek * 150); |
315 } | 387 } |
316 | 388 |
317 } // namespace chromeos_metrics | 389 } // namespace chromeos_metrics |
318 | 390 |
319 int main(int argc, char** argv) { | 391 int main(int argc, char** argv) { |
320 testing::InitGoogleTest(&argc, argv); | 392 testing::InitGoogleTest(&argc, argv); |
321 return RUN_ALL_TESTS(); | 393 return RUN_ALL_TESTS(); |
322 } | 394 } |
OLD | NEW |