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

Side by Side Diff: chrome/browser/policy/policy_statistics_collector_unittest.cc

Issue 10916235: Record policy usage statistics every 24 hours. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Tests (+ minor changes to PolicyStatisticsCollector to support them) Created 8 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 (c) 2012 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 <cstring>
6 #include <string>
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/policy/mock_policy_service.h"
14 #include "chrome/browser/policy/policy_map.h"
15 #include "chrome/browser/policy/policy_statistics_collector.h"
16 #include "chrome/browser/policy/policy_types.h"
17 #include "chrome/browser/policy/test_task_runner.h"
18 #include "chrome/browser/prefs/browser_prefs.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_pref_service.h"
21 #include "policy/policy_constants.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace policy {
26
27 namespace {
28
29 using testing::_;
30 using testing::Lt;
31 using testing::Return;
32 using testing::ReturnRef;
33
34 // Arbitrary policy names used for testing.
35 const char* const kTestPolicy1 = key::kHomepageIsNewTabPage;
36 const char* const kTestPolicy2 = key::kInstantEnabled;
37
38 class TestPolicyStatisticsCollector : public PolicyStatisticsCollector {
39 public:
40 TestPolicyStatisticsCollector(
41 PolicyService* policy_service,
42 PrefService* prefs,
43 const scoped_refptr<base::TaskRunner>& task_runner)
44 : PolicyStatisticsCollector(policy_service, prefs, task_runner) {
45 }
46
47 MOCK_METHOD1(RecordPolicyUse, void(int));
Joao da Silva 2012/09/20 12:06:21 I'm cool with this test, I'll just leave this sugg
48 };
49
50 } // namespace
51
52 class PolicyStatisticsCollectorTest : public testing::Test {
53 protected:
54 PolicyStatisticsCollectorTest()
55 : update_delay_(base::TimeDelta::FromMilliseconds(
56 PolicyStatisticsCollector::kStatisticsUpdateRate)),
57 test_policy_id1_(-1),
58 test_policy_id2_(-1),
59 task_runner_(new TestTaskRunner) {
60 chrome::RegisterLocalState(&prefs_);
61
62 // Find ids for kTestPolicy1 and kTestPolicy2.
63 const policy::PolicyDefinitionList* policy_list =
64 policy::GetChromePolicyDefinitionList();
65 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin;
66 policy != policy_list->end; ++policy) {
67 if (strcmp(policy->name, kTestPolicy1) == 0)
68 test_policy_id1_ = policy->id;
69 else if (strcmp(policy->name, kTestPolicy2) == 0)
70 test_policy_id2_ = policy->id;
71 }
72 ASSERT_TRUE(test_policy_id1_ != -1);
73 ASSERT_TRUE(test_policy_id2_ != -1);
74
75 // Set up default function behaviour.
76 EXPECT_CALL(policy_service_, GetPolicies(POLICY_DOMAIN_CHROME,
77 std::string())).
78 WillRepeatedly(ReturnRef(policy_map_));
79 }
80
81 virtual void SetUp() OVERRIDE {
82 // Arbitrary negative value (so it'll be different from |update_delay_|).
83 last_delay_ = base::TimeDelta::FromDays(-1);
84 policy_map_.Clear();
85 policy_statistics_collector_.reset(new TestPolicyStatisticsCollector(
86 &policy_service_,
87 &prefs_,
88 task_runner_));
89 }
90
91 virtual void TearDown() OVERRIDE {
92 policy_statistics_collector_.reset();
Joao da Silva 2012/09/20 12:06:21 No need to do this explicitly.
qfel 2012/09/20 13:00:02 Just to have eventual problems in cleanup caught b
93 }
94
95 void SetPolicy(const std::string& name) {
96 policy_map_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
97 base::Value::CreateBooleanValue(true));
98 }
99
100 void UnsetPolicy(const std::string& name) {
101 policy_map_.Erase(name);
102 }
103
104 const base::TimeDelta update_delay_;
105
106 int test_policy_id1_;
107 int test_policy_id2_;
108
109 base::TimeDelta last_delay_;
110
111 TestingPrefService prefs_;
112 MockPolicyService policy_service_;
113 PolicyMap policy_map_;
114
115 scoped_refptr<TestTaskRunner> task_runner_;
116 scoped_ptr<TestPolicyStatisticsCollector> policy_statistics_collector_;
117 };
118
119 TEST_F(PolicyStatisticsCollectorTest, CollectPending) {
120 SetPolicy(kTestPolicy1);
121
122 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
123 (base::Time::Now() - update_delay_).ToInternalValue());
124
125 EXPECT_CALL(*policy_statistics_collector_.get(),
126 RecordPolicyUse(test_policy_id1_));
127 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, update_delay_)).
128 WillOnce(Return(true));
129
130 policy_statistics_collector_->Initialize();
131 }
132
133 TEST_F(PolicyStatisticsCollectorTest, CollectPendingVeryOld) {
134 SetPolicy(kTestPolicy1);
135
136 // Must not be 0.0 (read comment for Time::FromDoubleT).
137 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
138 base::Time::FromDoubleT(1.0).ToInternalValue());
139
140 EXPECT_CALL(*policy_statistics_collector_.get(),
141 RecordPolicyUse(test_policy_id1_));
142 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, update_delay_)).
143 WillOnce(Return(true));
144
145 policy_statistics_collector_->Initialize();
146 }
147
148 TEST_F(PolicyStatisticsCollectorTest, CollectLater) {
149 SetPolicy(kTestPolicy1);
150
151 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
152 (base::Time::Now() - update_delay_ / 2).ToInternalValue());
153
154 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, Lt(update_delay_))).
155 WillOnce(Return(true));
156
157 policy_statistics_collector_->Initialize();
158 }
159
160 TEST_F(PolicyStatisticsCollectorTest, MultiplePolicies) {
161 SetPolicy(kTestPolicy1);
162 SetPolicy(kTestPolicy2);
163
164 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
165 (base::Time::Now() - update_delay_).ToInternalValue());
166
167 EXPECT_CALL(*policy_statistics_collector_.get(),
168 RecordPolicyUse(test_policy_id1_));
169 EXPECT_CALL(*policy_statistics_collector_.get(),
170 RecordPolicyUse(test_policy_id2_));
171 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).
172 WillOnce(Return(true));
173
174 policy_statistics_collector_->Initialize();
175 }
176
177 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698