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

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 "components/metrics/daily_observer.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace metrics {
12
13 namespace {
14
15 const char kTestPrefName[] = "TestPref";
16
17 class TestDailyObserver : public DailyObserver {
18 public:
19 TestDailyObserver() : fired_(false) {}
20
21 bool fired() const { return fired_; }
22
23 virtual void OnDailyInterval() OVERRIDE {
24 fired_ = true;
25 };
Alexei Svitkine (slow) 2014/09/08 15:34:20 Nit: Unnecessary semicolon. Also, below.
Steven Holte 2014/09/12 21:23:31 Done.
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) {
41 DailyInterval::RegisterPref(prefs_.registry(), kTestPrefName);
42 interval_.AddObserver(&observer_);
43 }
44
45 protected:
46 TestingPrefServiceSimple prefs_;
47 TestDailyObserver observer_;
48 DailyInterval interval_;
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(DailyIntervalTest);
52 };
53
54 } // namespace
55
56 // The event should fire if the preference is not available.
57 TEST_F(DailyIntervalTest, TestNewFires) {
58 interval_.CheckInterval();
59 EXPECT_TRUE(observer_.fired());
60 }
61
62 // The event should fire if the preference is more than a day old.
63 TEST_F(DailyIntervalTest, TestOldFires) {
64 base::Time last_time = base::Time::Now() - base::TimeDelta::FromDays(2);
65 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
66 interval_.CheckInterval();
67 EXPECT_TRUE(observer_.fired());
68 }
69
70 // The event should fire if the preference is more than a day in the future.
71 TEST_F(DailyIntervalTest, TestFutureFires) {
72 base::Time last_time = base::Time::Now() + base::TimeDelta::FromDays(2);
73 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
74 interval_.CheckInterval();
75 EXPECT_TRUE(observer_.fired());
76 }
77
78 // The event should not fire if the preference is more recent than a day.
79 TEST_F(DailyIntervalTest, TestRecentNotFired) {
80 base::Time last_time = base::Time::Now() - base::TimeDelta::FromMinutes(2);
81 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
82 interval_.CheckInterval();
83 EXPECT_FALSE(observer_.fired());
84 }
85
86 // The event should not fire if the preference is less than a day in the future.
87 TEST_F(DailyIntervalTest, TestSoonNotFired) {
88 base::Time last_time = base::Time::Now() + base::TimeDelta::FromMinutes(2);
89 prefs_.SetInt64(kTestPrefName, last_time.ToInternalValue());
90 interval_.CheckInterval();
91 EXPECT_FALSE(observer_.fired());
92 }
93
94 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698