blob: f9a6f52c7f0bfe851935df2394b7455d1aee436a [file] [log] [blame]
holte07637b012014-09-22 19:15:021// 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.
4
5#include "components/metrics/daily_event.h"
6
avi26062922015-12-26 00:14:187#include "base/macros.h"
dchengd99c42a2016-04-21 21:54:138#include "base/memory/ptr_util.h"
Daniel Erateb06a792017-11-14 22:30:519#include "base/optional.h"
brettw066508682016-02-03 08:22:0210#include "components/prefs/testing_pref_service.h"
holte07637b012014-09-22 19:15:0211#include "testing/gtest/include/gtest/gtest.h"
12
13namespace metrics {
14
15namespace {
16
17const char kTestPrefName[] = "TestPref";
18const char kTestMetricName[] = "TestMetric";
19
20class TestDailyObserver : public DailyEvent::Observer {
21 public:
Daniel Erateb06a792017-11-14 22:30:5122 TestDailyObserver() = default;
holte07637b012014-09-22 19:15:0223
Daniel Erateb06a792017-11-14 22:30:5124 bool fired() const { return type_.has_value(); }
25 DailyEvent::IntervalType type() const { return type_.value(); }
holte07637b012014-09-22 19:15:0226
Daniel Erateb06a792017-11-14 22:30:5127 void OnDailyEvent(DailyEvent::IntervalType type) override { type_ = type; }
holte07637b012014-09-22 19:15:0228
Daniel Erateb06a792017-11-14 22:30:5129 void Reset() { type_ = {}; }
holte07637b012014-09-22 19:15:0230
31 private:
Daniel Erateb06a792017-11-14 22:30:5132 // Last-received type, or unset if OnDailyEvent() hasn't been called.
33 base::Optional<DailyEvent::IntervalType> type_;
holte07637b012014-09-22 19:15:0234
35 DISALLOW_COPY_AND_ASSIGN(TestDailyObserver);
36};
37
38class DailyEventTest : public testing::Test {
39 public:
40 DailyEventTest() : event_(&prefs_, kTestPrefName, kTestMetricName) {
41 DailyEvent::RegisterPref(prefs_.registry(), kTestPrefName);
42 observer_ = new TestDailyObserver();
dchengd99c42a2016-04-21 21:54:1343 event_.AddObserver(base::WrapUnique(observer_));
holte07637b012014-09-22 19:15:0244 }
45
46 protected:
47 TestingPrefServiceSimple prefs_;
48 TestDailyObserver* observer_;
49 DailyEvent event_;
50
51 private:
52 DISALLOW_COPY_AND_ASSIGN(DailyEventTest);
53};
54
55} // namespace
56
57// The event should fire if the preference is not available.
58TEST_F(DailyEventTest, TestNewFires) {
59 event_.CheckInterval();
Daniel Erateb06a792017-11-14 22:30:5160 ASSERT_TRUE(observer_->fired());
61 EXPECT_EQ(DailyEvent::IntervalType::FIRST_RUN, observer_->type());
holte07637b012014-09-22 19:15:0262}
63
64// The event should fire if the preference is more than a day old.
65TEST_F(DailyEventTest, TestOldFires) {
66 base::Time last_time = base::Time::Now() - base::TimeDelta::FromHours(25);
Sebastien Marchand8c8f90632017-08-21 20:27:1267 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
holte07637b012014-09-22 19:15:0268 event_.CheckInterval();
Daniel Erateb06a792017-11-14 22:30:5169 ASSERT_TRUE(observer_->fired());
70 EXPECT_EQ(DailyEvent::IntervalType::DAY_ELAPSED, observer_->type());
holte07637b012014-09-22 19:15:0271}
72
73// The event should fire if the preference is more than a day in the future.
74TEST_F(DailyEventTest, TestFutureFires) {
75 base::Time last_time = base::Time::Now() + base::TimeDelta::FromHours(25);
Sebastien Marchand8c8f90632017-08-21 20:27:1276 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
holte07637b012014-09-22 19:15:0277 event_.CheckInterval();
Daniel Erateb06a792017-11-14 22:30:5178 ASSERT_TRUE(observer_->fired());
79 EXPECT_EQ(DailyEvent::IntervalType::CLOCK_CHANGED, observer_->type());
holte07637b012014-09-22 19:15:0280}
81
82// The event should not fire if the preference is more recent than a day.
83TEST_F(DailyEventTest, TestRecentNotFired) {
84 base::Time last_time = base::Time::Now() - base::TimeDelta::FromMinutes(2);
Sebastien Marchand8c8f90632017-08-21 20:27:1285 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
holte07637b012014-09-22 19:15:0286 event_.CheckInterval();
87 EXPECT_FALSE(observer_->fired());
88}
89
90// The event should not fire if the preference is less than a day in the future.
91TEST_F(DailyEventTest, TestSoonNotFired) {
92 base::Time last_time = base::Time::Now() + base::TimeDelta::FromMinutes(2);
Sebastien Marchand8c8f90632017-08-21 20:27:1293 prefs_.SetInt64(kTestPrefName, last_time.since_origin().InMicroseconds());
holte07637b012014-09-22 19:15:0294 event_.CheckInterval();
95 EXPECT_FALSE(observer_->fired());
96}
97
98} // namespace metrics