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 observer_ = new TestDailyObserver();
44 event_.AddObserver(base::WrapUnique(observer_.get()));
47 DailyEventTest(const DailyEventTest&) = delete;
48 DailyEventTest& operator=(const DailyEventTest&) = delete;
51 TestingPrefServiceSimple prefs_;
52 raw_ptr<TestDailyObserver> observer_;
58 // The event should fire if the preference is not available.
59 TEST_F(DailyEventTest, TestNewFires) {
60 event_.CheckInterval();
61 ASSERT_TRUE(observer_->fired());
62 EXPECT_EQ(DailyEvent::IntervalType::FIRST_RUN, observer_->type());
65 // The event should fire if the preference is more than a day old.
66 TEST_F(DailyEventTest, TestOldFires) {
67 base::Time last_time = base::Time::Now() - base::Hours(25);
68 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
69 event_.CheckInterval();
70 ASSERT_TRUE(observer_->fired());
71 EXPECT_EQ(DailyEvent::IntervalType::DAY_ELAPSED, observer_->type());
74 // The event should fire if the preference is more than a day in the future.
75 TEST_F(DailyEventTest, TestFutureFires) {
76 base::Time last_time = base::Time::Now() + base::Hours(25);
77 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
78 event_.CheckInterval();
79 ASSERT_TRUE(observer_->fired());
80 EXPECT_EQ(DailyEvent::IntervalType::CLOCK_CHANGED, observer_->type());
83 // The event should not fire if the preference is more recent than a day.
84 TEST_F(DailyEventTest, TestRecentNotFired) {
85 base::Time last_time = base::Time::Now() - base::Minutes(2);
86 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
87 event_.CheckInterval();
88 EXPECT_FALSE(observer_->fired());
91 // The event should not fire if the preference is less than a day in the future.
92 TEST_F(DailyEventTest, TestSoonNotFired) {
93 base::Time last_time = base::Time::Now() + base::Minutes(2);
94 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
95 event_.CheckInterval();
96 EXPECT_FALSE(observer_->fired());
99 } // namespace metrics