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 | 16 |
16 using ::testing::_; | 17 using ::testing::_; |
17 using ::testing::MockFunction; | 18 using ::testing::MockFunction; |
18 using ::testing::StrictMock; | 19 using ::testing::StrictMock; |
19 | 20 |
20 namespace chromeos_metrics { | 21 namespace chromeos_metrics { |
21 | 22 |
22 static const char kTestRecordFile[] = "record-file"; | 23 static const char kTestRecordFile[] = "record-file"; |
23 static const char kDoesNotExistFile[] = "/does/not/exist"; | 24 static const char kDoesNotExistFile[] = "/does/not/exist"; |
24 | 25 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 counter_.Update(/* tag */ 21, /* count */ 15); | 249 counter_.Update(/* tag */ 21, /* count */ 15); |
249 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15); | 250 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15); |
250 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); | 251 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
251 | 252 |
252 ExpectReporterCall(/* tag */ 21, /* count */ 15); | 253 ExpectReporterCall(/* tag */ 21, /* count */ 15); |
253 counter_.Update(/* tag */ 22, /* count */ 0); | 254 counter_.Update(/* tag */ 22, /* count */ 0); |
254 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 22, /* seconds */ 0); | 255 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 22, /* seconds */ 0); |
255 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); | 256 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
256 } | 257 } |
257 | 258 |
| 259 class FrequencyCounterTest : public testing::Test { |
| 260 protected: |
| 261 virtual void SetUp() { |
| 262 tagged_counter_ = new StrictMock<TaggedCounterMock>; |
| 263 frequency_counter_.tagged_counter_.reset(tagged_counter_); |
| 264 } |
| 265 |
| 266 static void FakeReporter(void *, int32, int32) { |
| 267 } |
| 268 |
| 269 void CheckInit(int32 cycle_duration); |
| 270 void CheckCycleNumber(int32 cycle_duration); |
| 271 |
| 272 FrequencyCounter frequency_counter_; |
| 273 StrictMock<TaggedCounterMock>* tagged_counter_; |
| 274 |
| 275 TaggedCounter::Reporter reporter_; |
| 276 }; |
| 277 |
| 278 void FrequencyCounterTest::CheckInit(int32 cycle_duration) { |
| 279 EXPECT_CALL(*tagged_counter_, Init(kTestRecordFile, FakeReporter, this)) |
| 280 .Times(1) |
| 281 .RetiresOnSaturation(); |
| 282 frequency_counter_.Init(kTestRecordFile, |
| 283 FakeReporter, |
| 284 this, |
| 285 cycle_duration); |
| 286 EXPECT_EQ(cycle_duration, frequency_counter_.cycle_duration_); |
| 287 } |
| 288 |
| 289 TEST_F(FrequencyCounterTest, Init) { |
| 290 CheckInit(100); |
| 291 } |
| 292 |
| 293 void FrequencyCounterTest::CheckCycleNumber(int32 cycle_duration) { |
| 294 CheckInit(cycle_duration); |
| 295 EXPECT_EQ(150, frequency_counter_.GetCycleNumber(cycle_duration * 150)); |
| 296 EXPECT_EQ(150, frequency_counter_.GetCycleNumber(cycle_duration * 150 + |
| 297 cycle_duration - 1)); |
| 298 EXPECT_EQ(151, frequency_counter_.GetCycleNumber(cycle_duration * 151 + 1)); |
| 299 EXPECT_EQ(0, frequency_counter_.GetCycleNumber(0)); |
| 300 } |
| 301 |
| 302 |
| 303 TEST_F(FrequencyCounterTest, GetCycleNumberForWeek) { |
| 304 CheckCycleNumber(kSecondsPerWeek); |
| 305 } |
| 306 |
| 307 TEST_F(FrequencyCounterTest, GetCycleNumberForDay) { |
| 308 CheckCycleNumber(kSecondsPerDay); |
| 309 } |
| 310 |
| 311 TEST_F(FrequencyCounterTest, UpdateInternal) { |
| 312 CheckInit(kSecondsPerWeek); |
| 313 EXPECT_CALL(*tagged_counter_, Update(150, 2)); |
| 314 frequency_counter_.UpdateInternal(2, kSecondsPerWeek * 150); |
| 315 } |
| 316 |
258 } // namespace chromeos_metrics | 317 } // namespace chromeos_metrics |
259 | 318 |
260 int main(int argc, char** argv) { | 319 int main(int argc, char** argv) { |
261 testing::InitGoogleTest(&argc, argv); | 320 testing::InitGoogleTest(&argc, argv); |
262 return RUN_ALL_TESTS(); | 321 return RUN_ALL_TESTS(); |
263 } | 322 } |
OLD | NEW |