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

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: Fixed compilation 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));
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 }
61
62 virtual void SetUp() OVERRIDE {
63 chrome::RegisterLocalState(&prefs_);
64
65 // Find ids for kTestPolicy1 and kTestPolicy2.
66 const policy::PolicyDefinitionList* policy_list =
67 policy::GetChromePolicyDefinitionList();
68 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin;
69 policy != policy_list->end; ++policy) {
70 if (strcmp(policy->name, kTestPolicy1) == 0)
71 test_policy_id1_ = policy->id;
72 else if (strcmp(policy->name, kTestPolicy2) == 0)
73 test_policy_id2_ = policy->id;
74 }
75 ASSERT_TRUE(test_policy_id1_ != -1);
76 ASSERT_TRUE(test_policy_id2_ != -1);
77
78 // Set up default function behaviour.
79 EXPECT_CALL(policy_service_, GetPolicies(POLICY_DOMAIN_CHROME,
80 std::string())).
81 WillRepeatedly(ReturnRef(policy_map_));
82
83 // Arbitrary negative value (so it'll be different from |update_delay_|).
84 last_delay_ = base::TimeDelta::FromDays(-1);
85 policy_map_.Clear();
86 policy_statistics_collector_.reset(new TestPolicyStatisticsCollector(
87 &policy_service_,
88 &prefs_,
89 task_runner_));
90 }
91
92 void SetPolicy(const std::string& name) {
93 policy_map_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
94 base::Value::CreateBooleanValue(true));
95 }
96
97 const base::TimeDelta update_delay_;
98
99 int test_policy_id1_;
100 int test_policy_id2_;
101
102 base::TimeDelta last_delay_;
103
104 TestingPrefService prefs_;
105 MockPolicyService policy_service_;
106 PolicyMap policy_map_;
107
108 scoped_refptr<TestTaskRunner> task_runner_;
109 scoped_ptr<TestPolicyStatisticsCollector> policy_statistics_collector_;
110 };
111
112 TEST_F(PolicyStatisticsCollectorTest, CollectPending) {
113 SetPolicy(kTestPolicy1);
114
115 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
116 (base::Time::Now() - update_delay_).ToInternalValue());
117
118 EXPECT_CALL(*policy_statistics_collector_.get(),
119 RecordPolicyUse(test_policy_id1_));
120 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, update_delay_)).
121 WillOnce(Return(true));
122
123 policy_statistics_collector_->Initialize();
124 }
125
126 TEST_F(PolicyStatisticsCollectorTest, CollectPendingVeryOld) {
127 SetPolicy(kTestPolicy1);
128
129 // Must not be 0.0 (read comment for Time::FromDoubleT).
130 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
131 base::Time::FromDoubleT(1.0).ToInternalValue());
132
133 EXPECT_CALL(*policy_statistics_collector_.get(),
134 RecordPolicyUse(test_policy_id1_));
135 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, update_delay_)).
136 WillOnce(Return(true));
137
138 policy_statistics_collector_->Initialize();
139 }
140
141 TEST_F(PolicyStatisticsCollectorTest, CollectLater) {
142 SetPolicy(kTestPolicy1);
143
144 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
145 (base::Time::Now() - update_delay_ / 2).ToInternalValue());
146
147 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, Lt(update_delay_))).
148 WillOnce(Return(true));
149
150 policy_statistics_collector_->Initialize();
151 }
152
153 TEST_F(PolicyStatisticsCollectorTest, MultiplePolicies) {
154 SetPolicy(kTestPolicy1);
155 SetPolicy(kTestPolicy2);
156
157 prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate,
158 (base::Time::Now() - update_delay_).ToInternalValue());
159
160 EXPECT_CALL(*policy_statistics_collector_.get(),
161 RecordPolicyUse(test_policy_id1_));
162 EXPECT_CALL(*policy_statistics_collector_.get(),
163 RecordPolicyUse(test_policy_id2_));
164 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).
165 WillOnce(Return(true));
166
167 policy_statistics_collector_->Initialize();
168 }
169
170 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/policy_statistics_collector.cc ('k') | chrome/browser/policy/test_task_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698