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.
5 #include "components/metrics/daily_event.h"
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"
17 const char kTestPrefName[] = "TestPref";
18 const char kTestMetricName[] = "TestMetric";
20 class TestDailyObserver : public DailyEvent::Observer {
22 TestDailyObserver() = default;
24 TestDailyObserver(const TestDailyObserver&) = delete;
25 TestDailyObserver& operator=(const TestDailyObserver&) = delete;
27 bool fired() const { return type_.has_value(); }
28 DailyEvent::IntervalType type() const { return type_.value(); }
30 void OnDailyEvent(DailyEvent::IntervalType type) override { type_ = type; }
32 void Reset() { type_ = {}; }
35 // Last-received type, or unset if OnDailyEvent() hasn't been called.
36 absl::optional<DailyEvent::IntervalType> type_;
39 class DailyEventTest : public testing::Test {
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));
48 DailyEventTest(const DailyEventTest&) = delete;
49 DailyEventTest& operator=(const DailyEventTest&) = delete;
52 TestingPrefServiceSimple prefs_;
53 DailyEvent event_; // Owns and outlives `observer_`
54 raw_ptr<TestDailyObserver> observer_;
59 // The event should fire if the preference is not available.
60 TEST_F(DailyEventTest, TestNewFires) {
61 event_.CheckInterval();
62 ASSERT_TRUE(observer_->fired());
63 EXPECT_EQ(DailyEvent::IntervalType::FIRST_RUN, observer_->type());
66 // The event should fire if the preference is more than a day old.
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());
75 // The event should fire if the preference is more than a day in the future.
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());
84 // The event should not fire if the preference is more recent than a day.
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());
92 // The event should not fire if the preference is less than a day in the future.
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());
100 } // namespace metrics