1 // Copyright 2014 The Chromium Authors
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_event.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/memory/raw_ptr.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/abseil-cpp/absl/types/optional.h"
12
13 namespace metrics {
14
15 namespace {
16
17 const char kTestPrefName[] = "TestPref";
18 const char kTestMetricName[] = "TestMetric";
19
20 class TestDailyObserver : public DailyEvent::Observer {
21 public:
22 TestDailyObserver() = default;
23
24 TestDailyObserver(const TestDailyObserver&) = delete;
25 TestDailyObserver& operator=(const TestDailyObserver&) = delete;
26
fired() const27 bool fired() const { return type_.has_value(); }
type() const28 DailyEvent::IntervalType type() const { return type_.value(); }
29
OnDailyEvent(DailyEvent::IntervalType type)30 void OnDailyEvent(DailyEvent::IntervalType type) override { type_ = type; }
31
Reset()32 void Reset() { type_ = {}; }
33
34 private:
35 // Last-received type, or unset if OnDailyEvent() hasn't been called.
36 absl::optional<DailyEvent::IntervalType> type_;
37 };
38
39 class DailyEventTest : public testing::Test {
40 public:
DailyEventTest()41 DailyEventTest() : event_(&prefs_, kTestPrefName, kTestMetricName) {
42 DailyEvent::RegisterPref(prefs_.registry(), kTestPrefName);
43 auto observer = std::make_unique<TestDailyObserver>();
44 observer_ = observer.get();
45 event_.AddObserver(std::move(observer));
46 }
47
48 DailyEventTest(const DailyEventTest&) = delete;
49 DailyEventTest& operator=(const DailyEventTest&) = delete;
50
51 protected:
52 TestingPrefServiceSimple prefs_;
53 DailyEvent event_; // Owns and outlives `observer_`
54 raw_ptr<TestDailyObserver> observer_;
55 };
56
57 } // namespace
58
59 // The event should fire if the preference is not available.
TEST_F(DailyEventTest,TestNewFires)60 TEST_F(DailyEventTest, TestNewFires) {
61 event_.CheckInterval();
62 ASSERT_TRUE(observer_->fired());
63 EXPECT_EQ(DailyEvent::IntervalType::FIRST_RUN, observer_->type());
64 }
65
66 // The event should fire if the preference is more than a day old.
TEST_F(DailyEventTest,TestOldFires)67 TEST_F(DailyEventTest, TestOldFires) {
68 base::Time last_time = base::Time::Now() - base::Hours(25);
69 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
70 event_.CheckInterval();
71 ASSERT_TRUE(observer_->fired());
72 EXPECT_EQ(DailyEvent::IntervalType::DAY_ELAPSED, observer_->type());
73 }
74
75 // The event should fire if the preference is more than a day in the future.
TEST_F(DailyEventTest,TestFutureFires)76 TEST_F(DailyEventTest, TestFutureFires) {
77 base::Time last_time = base::Time::Now() + base::Hours(25);
78 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
79 event_.CheckInterval();
80 ASSERT_TRUE(observer_->fired());
81 EXPECT_EQ(DailyEvent::IntervalType::CLOCK_CHANGED, observer_->type());
82 }
83
84 // The event should not fire if the preference is more recent than a day.
TEST_F(DailyEventTest,TestRecentNotFired)85 TEST_F(DailyEventTest, TestRecentNotFired) {
86 base::Time last_time = base::Time::Now() - base::Minutes(2);
87 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
88 event_.CheckInterval();
89 EXPECT_FALSE(observer_->fired());
90 }
91
92 // The event should not fire if the preference is less than a day in the future.
TEST_F(DailyEventTest,TestSoonNotFired)93 TEST_F(DailyEventTest, TestSoonNotFired) {
94 base::Time last_time = base::Time::Now() + base::Minutes(2);
95 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
96 event_.CheckInterval();
97 EXPECT_FALSE(observer_->fired());
98 }
99
100 } // namespace metrics
101