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

Side by Side Diff: components/metrics/daily_event_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
« no previous file with comments | « components/metrics/daily_event.cc ('k') | components/rappor/rappor_pref_names.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_event.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 DailyEvent::Observer {
18 public:
19 TestDailyObserver() : fired_(false) {}
20
21 bool fired() const { return fired_; }
22
23 virtual void OnDailyEvent() 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 DailyEventTest : public testing::Test {
39 public:
40 DailyEventTest() : event_(&prefs_, kTestPrefName, kTestMetricName) {
41 DailyEvent::RegisterPref(prefs_.registry(), kTestPrefName);
42 observer_ = new TestDailyObserver();
43 scoped_ptr<metrics::DailyEvent::Observer> p(observer_);
44 event_.AddObserver(p.Pass());
45 }
46
47 protected:
48 TestingPrefServiceSimple prefs_;
49 TestDailyObserver* observer_;
50 DailyEvent event_;
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(DailyEventTest);
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 EXPECT_TRUE(observer_->fired());
62 }
63
64 // The event should fire if the preference is more than a day old.
65 TEST_F(DailyEventTest, TestOldFires) {
66 base::Time last_time = base::Time::Now() - base::TimeDelta::FromHours(25);
67 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
68 event_.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(DailyEventTest, TestFutureFires) {
74 base::Time last_time = base::Time::Now() + base::TimeDelta::FromHours(25);
75 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
76 event_.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(DailyEventTest, TestRecentNotFired) {
82 base::Time last_time = base::Time::Now() - base::TimeDelta::FromMinutes(2);
83 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
84 event_.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(DailyEventTest, TestSoonNotFired) {
90 base::Time last_time = base::Time::Now() + base::TimeDelta::FromMinutes(2);
91 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
92 event_.CheckInterval();
93 EXPECT_FALSE(observer_->fired());
94 }
95
96 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/daily_event.cc ('k') | components/rappor/rappor_pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698