Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: components/metrics/daily_interval_unittest.cc

Issue 511623002: Add a mechanism for collecting Rappor samples on a daily interval. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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_interval.h"
6
7 #include "base/prefs/testing_pref_service.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace metrics {
11
12 namespace {
13
14 const char kTestPrefName[] = "TestPref";
15 const char kTestMetricName[] = "TestMetric";
16
17 class TestDailyObserver : public DailyInterval::Observer {
18 public:
19 TestDailyObserver() : fired_(false) {}
20
21 bool fired() const { return fired_; }
22
23 virtual void OnDailyInterval() OVERRIDE {
24 fired_ = true;
25 }
26
27 void Reset() {
28 fired_ = false;
29 }
30
31 private:
32 // True if this event has been observed.
33 bool fired_;
34
35 DISALLOW_COPY_AND_ASSIGN(TestDailyObserver);
36 };
37
38 class DailyIntervalTest : public testing::Test {
39 public:
40 DailyIntervalTest() : interval_(&prefs_, kTestPrefName, kTestMetricName) {
41 DailyInterval::RegisterPref(prefs_.registry(), kTestPrefName);
42 observer_ = new TestDailyObserver();
43 scoped_ptr<metrics::DailyInterval::Observer> p(observer_);
44 interval_.AddObserver(p.Pass());
45 }
46
47 protected:
48 TestingPrefServiceSimple prefs_;
49 TestDailyObserver* observer_;
50 DailyInterval interval_;
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(DailyIntervalTest);
54 };
55
56 } // namespace
57
58 // The event should fire if the preference is not available.
59 TEST_F(DailyIntervalTest, TestNewFires) {
60 interval_.CheckInterval();
61 EXPECT_TRUE(observer_->fired());
62 }
63
64 // The event should fire if the preference is more than a day old.
65 TEST_F(DailyIntervalTest, TestOldFires) {
66 base::Time last_time = base::Time::Now() - base::TimeDelta::FromHours(25);
67 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
68 interval_.CheckInterval();
69 EXPECT_TRUE(observer_->fired());
70 }
71
72 // The event should fire if the preference is more than a day in the future.
73 TEST_F(DailyIntervalTest, TestFutureFires) {
74 base::Time last_time = base::Time::Now() + base::TimeDelta::FromHours(25);
75 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
76 interval_.CheckInterval();
77 EXPECT_TRUE(observer_->fired());
78 }
79
80 // The event should not fire if the preference is more recent than a day.
81 TEST_F(DailyIntervalTest, TestRecentNotFired) {
82 base::Time last_time = base::Time::Now() - base::TimeDelta::FromMinutes(2);
83 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
84 interval_.CheckInterval();
85 EXPECT_FALSE(observer_->fired());
86 }
87
88 // The event should not fire if the preference is less than a day in the future.
89 TEST_F(DailyIntervalTest, TestSoonNotFired) {
90 base::Time last_time = base::Time::Now() + base::TimeDelta::FromMinutes(2);
91 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
92 interval_.CheckInterval();
93 EXPECT_FALSE(observer_->fired());
94 }
95
96 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698