OLD | NEW |
| (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/callback.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/prefs/pref_registry_simple.h" | |
12 #include "base/prefs/testing_pref_service.h" | |
13 #include "base/test/test_simple_task_runner.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/policy/mock_policy_service.h" | |
16 #include "chrome/browser/policy/policy_statistics_collector.h" | |
17 #include "chrome/browser/policy/test/policy_test_utils.h" | |
18 #include "components/policy/core/common/external_data_fetcher.h" | |
19 #include "components/policy/core/common/policy_map.h" | |
20 #include "components/policy/core/common/policy_pref_names.h" | |
21 #include "components/policy/core/common/policy_types.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::ReturnRef; | |
30 | |
31 // Arbitrary policy names used for testing. | |
32 const char kTestPolicy1[] = "Test Policy 1"; | |
33 const char kTestPolicy2[] = "Test Policy 2"; | |
34 | |
35 const int kTestPolicy1Id = 42; | |
36 const int kTestPolicy2Id = 123; | |
37 | |
38 const char kTestChromeSchema[] = | |
39 "{" | |
40 " \"type\": \"object\"," | |
41 " \"properties\": {" | |
42 " \"Test Policy 1\": { \"type\": \"string\" }," | |
43 " \"Test Policy 2\": { \"type\": \"string\" }" | |
44 " }" | |
45 "}"; | |
46 | |
47 const PolicyDetails kTestPolicyDetails[] = { | |
48 // is_deprecated is_device_policy id max_external_data_size | |
49 { false, false, kTestPolicy1Id, 0 }, | |
50 { false, false, kTestPolicy2Id, 0 }, | |
51 }; | |
52 | |
53 class TestPolicyStatisticsCollector : public PolicyStatisticsCollector { | |
54 public: | |
55 TestPolicyStatisticsCollector( | |
56 const GetChromePolicyDetailsCallback& get_details, | |
57 const Schema& chrome_schema, | |
58 PolicyService* policy_service, | |
59 PrefService* prefs, | |
60 const scoped_refptr<base::TaskRunner>& task_runner) | |
61 : PolicyStatisticsCollector(get_details, | |
62 chrome_schema, | |
63 policy_service, | |
64 prefs, | |
65 task_runner) {} | |
66 | |
67 MOCK_METHOD1(RecordPolicyUse, void(int)); | |
68 }; | |
69 | |
70 } // namespace | |
71 | |
72 class PolicyStatisticsCollectorTest : public testing::Test { | |
73 protected: | |
74 PolicyStatisticsCollectorTest() | |
75 : update_delay_(base::TimeDelta::FromMilliseconds( | |
76 PolicyStatisticsCollector::kStatisticsUpdateRate)), | |
77 task_runner_(new base::TestSimpleTaskRunner()) { | |
78 } | |
79 | |
80 virtual void SetUp() OVERRIDE { | |
81 std::string error; | |
82 chrome_schema_ = Schema::Parse(kTestChromeSchema, &error); | |
83 ASSERT_TRUE(chrome_schema_.valid()) << error; | |
84 | |
85 policy_details_.SetDetails(kTestPolicy1, &kTestPolicyDetails[0]); | |
86 policy_details_.SetDetails(kTestPolicy2, &kTestPolicyDetails[1]); | |
87 | |
88 prefs_.registry()->RegisterInt64Pref( | |
89 policy_prefs::kLastPolicyStatisticsUpdate, 0); | |
90 | |
91 // Set up default function behaviour. | |
92 EXPECT_CALL(policy_service_, | |
93 GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, | |
94 std::string()))) | |
95 .WillRepeatedly(ReturnRef(policy_map_)); | |
96 | |
97 // Arbitrary negative value (so it'll be different from |update_delay_|). | |
98 last_delay_ = base::TimeDelta::FromDays(-1); | |
99 policy_map_.Clear(); | |
100 policy_statistics_collector_.reset(new TestPolicyStatisticsCollector( | |
101 policy_details_.GetCallback(), | |
102 chrome_schema_, | |
103 &policy_service_, | |
104 &prefs_, | |
105 task_runner_)); | |
106 } | |
107 | |
108 void SetPolicy(const std::string& name) { | |
109 policy_map_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
110 base::Value::CreateBooleanValue(true), NULL); | |
111 } | |
112 | |
113 base::TimeDelta GetFirstDelay() const { | |
114 if (task_runner_->GetPendingTasks().empty()) { | |
115 ADD_FAILURE(); | |
116 return base::TimeDelta(); | |
117 } | |
118 return task_runner_->GetPendingTasks().front().delay; | |
119 } | |
120 | |
121 const base::TimeDelta update_delay_; | |
122 | |
123 base::TimeDelta last_delay_; | |
124 | |
125 PolicyDetailsMap policy_details_; | |
126 Schema chrome_schema_; | |
127 TestingPrefServiceSimple prefs_; | |
128 MockPolicyService policy_service_; | |
129 PolicyMap policy_map_; | |
130 | |
131 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
132 scoped_ptr<TestPolicyStatisticsCollector> policy_statistics_collector_; | |
133 }; | |
134 | |
135 TEST_F(PolicyStatisticsCollectorTest, CollectPending) { | |
136 SetPolicy(kTestPolicy1); | |
137 | |
138 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | |
139 (base::Time::Now() - update_delay_).ToInternalValue()); | |
140 | |
141 EXPECT_CALL(*policy_statistics_collector_.get(), | |
142 RecordPolicyUse(kTestPolicy1Id)); | |
143 | |
144 policy_statistics_collector_->Initialize(); | |
145 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
146 EXPECT_EQ(update_delay_, GetFirstDelay()); | |
147 } | |
148 | |
149 TEST_F(PolicyStatisticsCollectorTest, CollectPendingVeryOld) { | |
150 SetPolicy(kTestPolicy1); | |
151 | |
152 // Must not be 0.0 (read comment for Time::FromDoubleT). | |
153 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | |
154 base::Time::FromDoubleT(1.0).ToInternalValue()); | |
155 | |
156 EXPECT_CALL(*policy_statistics_collector_.get(), | |
157 RecordPolicyUse(kTestPolicy1Id)); | |
158 | |
159 policy_statistics_collector_->Initialize(); | |
160 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
161 EXPECT_EQ(update_delay_, GetFirstDelay()); | |
162 } | |
163 | |
164 TEST_F(PolicyStatisticsCollectorTest, CollectLater) { | |
165 SetPolicy(kTestPolicy1); | |
166 | |
167 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | |
168 (base::Time::Now() - update_delay_ / 2).ToInternalValue()); | |
169 | |
170 policy_statistics_collector_->Initialize(); | |
171 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
172 EXPECT_LT(GetFirstDelay(), update_delay_); | |
173 } | |
174 | |
175 TEST_F(PolicyStatisticsCollectorTest, MultiplePolicies) { | |
176 SetPolicy(kTestPolicy1); | |
177 SetPolicy(kTestPolicy2); | |
178 | |
179 prefs_.SetInt64(policy_prefs::kLastPolicyStatisticsUpdate, | |
180 (base::Time::Now() - update_delay_).ToInternalValue()); | |
181 | |
182 EXPECT_CALL(*policy_statistics_collector_.get(), | |
183 RecordPolicyUse(kTestPolicy1Id)); | |
184 EXPECT_CALL(*policy_statistics_collector_.get(), | |
185 RecordPolicyUse(kTestPolicy2Id)); | |
186 | |
187 policy_statistics_collector_->Initialize(); | |
188 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
189 } | |
190 | |
191 } // namespace policy | |
OLD | NEW |