[M108 Migration] Support standard build for armv7hl architecture
[platform/framework/web/chromium-efl.git] / components / metrics / daily_event_unittest.cc
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
27   bool fired() const { return type_.has_value(); }
28   DailyEvent::IntervalType type() const { return type_.value(); }
29
30   void OnDailyEvent(DailyEvent::IntervalType type) override { type_ = type; }
31
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:
41   DailyEventTest() : event_(&prefs_, kTestPrefName, kTestMetricName) {
42     DailyEvent::RegisterPref(prefs_.registry(), kTestPrefName);
43     observer_ = new TestDailyObserver();
44     event_.AddObserver(base::WrapUnique(observer_.get()));
45   }
46
47   DailyEventTest(const DailyEventTest&) = delete;
48   DailyEventTest& operator=(const DailyEventTest&) = delete;
49
50  protected:
51   TestingPrefServiceSimple prefs_;
52   raw_ptr<TestDailyObserver> observer_;
53   DailyEvent event_;
54 };
55
56 }  // namespace
57
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());
63 }
64
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());
72 }
73
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());
81 }
82
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());
89 }
90
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());
97 }
98
99 }  // namespace metrics