OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium 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 |
| 5 #include "components/metrics/daily_interval.h" |
| 6 |
| 7 #include "base/prefs/testing_pref_service.h" |
| 8 #include "components/metrics/daily_observer.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace metrics { |
| 12 |
| 13 namespace { |
| 14 |
| 15 const char kTestPrefName[] = "TestPref"; |
| 16 const char kTestMetricName[] = "TestMetric"; |
| 17 |
| 18 class TestDailyObserver : public DailyObserver { |
| 19 public: |
| 20 TestDailyObserver() : fired_(false) {} |
| 21 |
| 22 bool fired() const { return fired_; } |
| 23 |
| 24 virtual void OnDailyInterval() OVERRIDE { |
| 25 fired_ = true; |
| 26 } |
| 27 |
| 28 void Reset() { |
| 29 fired_ = false; |
| 30 } |
| 31 |
| 32 private: |
| 33 // True if this event has been observed. |
| 34 bool fired_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(TestDailyObserver); |
| 37 }; |
| 38 |
| 39 class DailyIntervalTest : public testing::Test { |
| 40 public: |
| 41 DailyIntervalTest() : interval_(&prefs_, kTestPrefName, kTestMetricName) { |
| 42 DailyInterval::RegisterPref(prefs_.registry(), kTestPrefName); |
| 43 observer_ = new TestDailyObserver(); |
| 44 interval_.AddObserver(observer_); |
| 45 } |
| 46 |
| 47 protected: |
| 48 TestingPrefServiceSimple prefs_; |
| 49 TestDailyObserver* observer_; |
| 50 DailyInterval interval_; |
| 51 |
| 52 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(DailyIntervalTest); |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 // The event should fire if the preference is not available. |
| 59 TEST_F(DailyIntervalTest, TestNewFires) { |
| 60 interval_.CheckInterval(); |
| 61 EXPECT_TRUE(observer_->fired()); |
| 62 } |
| 63 |
| 64 // The event should fire if the preference is more than a day old. |
| 65 TEST_F(DailyIntervalTest, TestOldFires) { |
| 66 base::Time last_time = base::Time::Now() - base::TimeDelta::FromHours(25); |
| 67 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue()); |
| 68 interval_.CheckInterval(); |
| 69 EXPECT_TRUE(observer_->fired()); |
| 70 } |
| 71 |
| 72 // The event should fire if the preference is more than a day in the future. |
| 73 TEST_F(DailyIntervalTest, TestFutureFires) { |
| 74 base::Time last_time = base::Time::Now() + base::TimeDelta::FromHours(25); |
| 75 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue()); |
| 76 interval_.CheckInterval(); |
| 77 EXPECT_TRUE(observer_->fired()); |
| 78 } |
| 79 |
| 80 // The event should not fire if the preference is more recent than a day. |
| 81 TEST_F(DailyIntervalTest, TestRecentNotFired) { |
| 82 base::Time last_time = base::Time::Now() - base::TimeDelta::FromMinutes(2); |
| 83 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue()); |
| 84 interval_.CheckInterval(); |
| 85 EXPECT_FALSE(observer_->fired()); |
| 86 } |
| 87 |
| 88 // The event should not fire if the preference is less than a day in the future. |
| 89 TEST_F(DailyIntervalTest, TestSoonNotFired) { |
| 90 base::Time last_time = base::Time::Now() + base::TimeDelta::FromMinutes(2); |
| 91 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue()); |
| 92 interval_.CheckInterval(); |
| 93 EXPECT_FALSE(observer_->fired()); |
| 94 } |
| 95 |
| 96 } // namespace metrics |
OLD | NEW |