Work on Windows GN component build.
[chromium-blink-merge.git] / components / metrics / daily_event_unittest.cc
blobc600ee9358e0e81eb8c2022e151e9b8e17c7164c
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.
5 #include "components/metrics/daily_event.h"
7 #include "base/prefs/testing_pref_service.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace metrics {
12 namespace {
14 const char kTestPrefName[] = "TestPref";
15 const char kTestMetricName[] = "TestMetric";
17 class TestDailyObserver : public DailyEvent::Observer {
18 public:
19 TestDailyObserver() : fired_(false) {}
21 bool fired() const { return fired_; }
23 void OnDailyEvent() override { fired_ = true; }
25 void Reset() {
26 fired_ = false;
29 private:
30 // True if this event has been observed.
31 bool fired_;
33 DISALLOW_COPY_AND_ASSIGN(TestDailyObserver);
36 class DailyEventTest : public testing::Test {
37 public:
38 DailyEventTest() : event_(&prefs_, kTestPrefName, kTestMetricName) {
39 DailyEvent::RegisterPref(prefs_.registry(), kTestPrefName);
40 observer_ = new TestDailyObserver();
41 event_.AddObserver(make_scoped_ptr(observer_));
44 protected:
45 TestingPrefServiceSimple prefs_;
46 TestDailyObserver* observer_;
47 DailyEvent event_;
49 private:
50 DISALLOW_COPY_AND_ASSIGN(DailyEventTest);
53 } // namespace
55 // The event should fire if the preference is not available.
56 TEST_F(DailyEventTest, TestNewFires) {
57 event_.CheckInterval();
58 EXPECT_TRUE(observer_->fired());
61 // The event should fire if the preference is more than a day old.
62 TEST_F(DailyEventTest, TestOldFires) {
63 base::Time last_time = base::Time::Now() - base::TimeDelta::FromHours(25);
64 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
65 event_.CheckInterval();
66 EXPECT_TRUE(observer_->fired());
69 // The event should fire if the preference is more than a day in the future.
70 TEST_F(DailyEventTest, TestFutureFires) {
71 base::Time last_time = base::Time::Now() + base::TimeDelta::FromHours(25);
72 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
73 event_.CheckInterval();
74 EXPECT_TRUE(observer_->fired());
77 // The event should not fire if the preference is more recent than a day.
78 TEST_F(DailyEventTest, TestRecentNotFired) {
79 base::Time last_time = base::Time::Now() - base::TimeDelta::FromMinutes(2);
80 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
81 event_.CheckInterval();
82 EXPECT_FALSE(observer_->fired());
85 // The event should not fire if the preference is less than a day in the future.
86 TEST_F(DailyEventTest, TestSoonNotFired) {
87 base::Time last_time = base::Time::Now() + base::TimeDelta::FromMinutes(2);
88 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
89 event_.CheckInterval();
90 EXPECT_FALSE(observer_->fired());
93 } // namespace metrics