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

Side by Side Diff: chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc

Issue 23592017: Fix policy invalidator lifecycle bugs for Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
11 #include "base/metrics/histogram_samples.h" 13 #include "base/metrics/histogram_samples.h"
12 #include "base/metrics/sample_map.h" 14 #include "base/metrics/sample_map.h"
13 #include "base/metrics/statistics_recorder.h" 15 #include "base/metrics/statistics_recorder.h"
14 #include "base/test/test_simple_task_runner.h" 16 #include "base/test/test_simple_task_runner.h"
15 #include "base/time/time.h" 17 #include "base/time/time.h"
16 #include "base/values.h" 18 #include "base/values.h"
17 #include "chrome/browser/invalidation/fake_invalidation_service.h" 19 #include "chrome/browser/invalidation/fake_invalidation_service.h"
20 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
18 #include "chrome/browser/policy/cloud/cloud_policy_core.h" 21 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
19 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" 22 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
20 #include "chrome/browser/policy/cloud/cloud_policy_service.h" 23 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h"
21 #include "chrome/browser/policy/cloud/enterprise_metrics.h" 24 #include "chrome/browser/policy/cloud/enterprise_metrics.h"
22 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" 25 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h"
23 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" 26 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h"
24 #include "chrome/browser/policy/policy_types.h" 27 #include "chrome/browser/policy/policy_types.h"
25 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" 28 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
26 #include "policy/policy_constants.h" 29 #include "policy/policy_constants.h"
27 #include "sync/notifier/invalidation_util.h" 30 #include "sync/notifier/invalidation_util.h"
28 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
30 33
31 namespace policy { 34 namespace policy {
32 35
33 class CloudPolicyInvalidatorTest : public testing::Test, 36 class CloudPolicyInvalidatorTest : public testing::Test {
34 public CloudPolicyInvalidationHandler {
35 protected: 37 protected:
36 // Policy objects which can be used in tests. 38 // Policy objects which can be used in tests.
37 enum PolicyObject { 39 enum PolicyObject {
38 POLICY_OBJECT_NONE, 40 POLICY_OBJECT_NONE,
39 POLICY_OBJECT_A, 41 POLICY_OBJECT_A,
40 POLICY_OBJECT_B 42 POLICY_OBJECT_B
41 }; 43 };
42 44
43 CloudPolicyInvalidatorTest(); 45 CloudPolicyInvalidatorTest();
44 46
45 virtual void SetUp() OVERRIDE; 47 virtual void SetUp() OVERRIDE;
46 48
47 virtual void TearDown() OVERRIDE; 49 virtual void TearDown() OVERRIDE;
48 50
49 // Starts the invalidator which will be tested. 51 // Starts the invalidator which will be tested.
50 void StartInvalidator(bool initialize); 52 // |initialize| determines if the invalidator should be initialized.
53 // |start_refresh_scheduler| determines if the refresh scheduler should start.
54 void StartInvalidator(bool initialize, bool start_refresh_scheduler);
51 void StartInvalidator() { 55 void StartInvalidator() {
52 StartInvalidator(true /* initialize */); 56 StartInvalidator(true /* initialize */, true /* start_refresh_scheduler */);
53 } 57 }
54 58
59 // Calls Initialize on the invalidator.
60 void InitializeInvalidator();
61
62 // Calls Shutdown on the invalidator. Test must call DestroyInvalidator
63 // afterwards to prevent Shutdown from being called twice.
64 void ShutdownInvalidator();
65
66 // Destroys the invalidator.
67 void DestroyInvalidator();
68
69 // Connects the cloud policy core.
70 void ConnectCore();
71
72 // Starts the refresh scheduler.
73 void StartRefreshScheduler();
74
75 // Disconnects the cloud policy core.
76 void DisconnectCore();
77
55 // Simulates storing a new policy to the policy store. 78 // Simulates storing a new policy to the policy store.
56 // |object| determines which policy object the store will report the 79 // |object| determines which policy object the store will report the
57 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. 80 // invalidator should register for. May be POLICY_OBJECT_NONE for no object.
58 // |invalidation_version| determines what invalidation the store will report. 81 // |invalidation_version| determines what invalidation the store will report.
59 // |policy_changed| determines whether the store will report that the 82 // |policy_changed| determines whether the store will report that the
60 // policy changed. 83 // policy changed.
61 // |timestamp| determines the response timestamp the store will report. 84 // |timestamp| determines the response timestamp the store will report.
62 void StorePolicy( 85 void StorePolicy(
63 PolicyObject object, 86 PolicyObject object,
64 int64 invalidation_version, 87 int64 invalidation_version,
(...skipping 26 matching lines...) Expand all
91 const std::string& payload); 114 const std::string& payload);
92 115
93 // Causes the invalidation service to fire an invalidation with unknown 116 // Causes the invalidation service to fire an invalidation with unknown
94 // version. Returns an ack handle which be used to verify that the 117 // version. Returns an ack handle which be used to verify that the
95 // invalidation was acknowledged. 118 // invalidation was acknowledged.
96 syncer::AckHandle FireInvalidation(PolicyObject object); 119 syncer::AckHandle FireInvalidation(PolicyObject object);
97 120
98 // Checks the expected value of the currently set invalidation info. 121 // Checks the expected value of the currently set invalidation info.
99 bool CheckInvalidationInfo(int64 version, const std::string& payload); 122 bool CheckInvalidationInfo(int64 version, const std::string& payload);
100 123
101 // Checks that the invalidate callback was not called. 124 // Checks that the policy was not refreshed due to an invalidation.
102 bool CheckInvalidateNotCalled(); 125 bool CheckPolicyNotRefreshed();
103 126
104 // Checks that the invalidate callback was called within an appropriate 127 // Checks that the policy was refreshed due to an invalidation within an
105 // timeframe depending on whether the invalidation had unknown version. 128 // appropriate timeframe depending on whether the invalidation had unknown
106 bool CheckInvalidateCalled(bool unknown_version); 129 // version.
107 bool CheckInvalidateCalled() { 130 bool CheckPolicyRefreshed();
108 return CheckInvalidateCalled(true); 131 bool CheckPolicyRefreshedWithUnknownVersion();
109 }
110 132
111 // Checks that the state changed callback of the invalidation handler was not 133 // Returns the invalidations enabled state set by the invalidator on the
112 // called. 134 // refresh scheduler.
113 bool CheckStateChangedNotCalled(); 135 bool InvalidationsEnabled();
114
115 // Checks that the state changed callback of the invalidation handler was
116 // called with the given state.
117 bool CheckStateChangedCalled(bool invalidations_enabled);
118 136
119 // Determines if the invalidation with the given ack handle has been 137 // Determines if the invalidation with the given ack handle has been
120 // acknowledged. 138 // acknowledged.
121 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle); 139 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle);
122 140
141 // Determines if the invalidator has registered for an object with the
142 // invalidation service.
143 bool IsInvalidatorRegistered();
144
123 // Get the current count for the given metric. 145 // Get the current count for the given metric.
124 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); 146 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
125 base::HistogramBase::Count GetInvalidationCount(bool with_payload); 147 base::HistogramBase::Count GetInvalidationCount(bool with_payload);
126 148
127 // CloudPolicyInvalidationHandler: 149 private:
128 virtual void SetInvalidationInfo( 150 // Checks that the policy was refreshed due to an invalidation with the given
129 int64 version, 151 // base delay.
130 const std::string& payload) OVERRIDE; 152 bool CheckPolicyRefreshed(base::TimeDelta delay);
131 virtual void InvalidatePolicy() OVERRIDE;
132 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE;
133 153
134 private:
135 // Returns the object id of the given policy object. 154 // Returns the object id of the given policy object.
136 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; 155 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
137 156
138 // Get histogram samples for the given histogram. 157 // Get histogram samples for the given histogram.
139 scoped_ptr<base::HistogramSamples> GetHistogramSamples( 158 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
140 const std::string& name) const; 159 const std::string& name) const;
141 160
142 // Objects the invalidator depends on. 161 // Objects the invalidator depends on.
143 invalidation::FakeInvalidationService invalidation_service_; 162 invalidation::FakeInvalidationService invalidation_service_;
144 MockCloudPolicyStore store_; 163 MockCloudPolicyStore store_;
164 CloudPolicyCore core_;
145 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 165 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
146 166
147 // The invalidator which will be tested. 167 // The invalidator which will be tested.
148 scoped_ptr<CloudPolicyInvalidator> invalidator_; 168 scoped_ptr<CloudPolicyInvalidator> invalidator_;
149 169
150 // The latest invalidation info set by the invalidator.
151 int64 invalidation_version_;
152 std::string invalidation_payload_;
153
154 // Object ids for the test policy objects. 170 // Object ids for the test policy objects.
155 invalidation::ObjectId object_id_a_; 171 invalidation::ObjectId object_id_a_;
156 invalidation::ObjectId object_id_b_; 172 invalidation::ObjectId object_id_b_;
157 173
158 // Increasing policy timestamp. 174 // Increasing policy timestamp.
159 int64 timestamp_; 175 int64 timestamp_;
160 176
161 // Fake policy values which are alternated to cause the store to report a 177 // Fake policy values which are alternated to cause the store to report a
162 // changed policy. 178 // changed policy.
163 const char* policy_value_a_; 179 const char* policy_value_a_;
164 const char* policy_value_b_; 180 const char* policy_value_b_;
165 181
166 // The currently used policy value. 182 // The currently used policy value.
167 const char* policy_value_cur_; 183 const char* policy_value_cur_;
168 184
169 // Stores how many times the invalidate callback was called.
170 int invalidate_callback_count_;
171
172 // Stores how many times the state change callback was called for each state.
173 int state_change_enabled_callback_count_;
174 int state_change_disabled_callback_count_;
175
176 // Stores starting histogram counts for kMetricPolicyRefresh. 185 // Stores starting histogram counts for kMetricPolicyRefresh.
177 scoped_ptr<base::HistogramSamples> refresh_samples_; 186 scoped_ptr<base::HistogramSamples> refresh_samples_;
178 187
179 // Stores starting histogram counts for kMetricPolicyInvalidations. 188 // Stores starting histogram counts for kMetricPolicyInvalidations.
180 scoped_ptr<base::HistogramSamples> invalidations_samples_; 189 scoped_ptr<base::HistogramSamples> invalidations_samples_;
190
191 // Initialize message loop.
192 base::MessageLoop loop_;
181 }; 193 };
182 194
183 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() 195 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
184 : task_runner_(new base::TestSimpleTaskRunner()), 196 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
185 invalidation_version_(0), 197 std::string()),
198 &store_),
199 task_runner_(new base::TestSimpleTaskRunner()),
186 object_id_a_(135, "asdf"), 200 object_id_a_(135, "asdf"),
187 object_id_b_(246, "zxcv"), 201 object_id_b_(246, "zxcv"),
188 timestamp_(123456), 202 timestamp_(123456),
189 policy_value_a_("asdf"), 203 policy_value_a_("asdf"),
190 policy_value_b_("zxcv"), 204 policy_value_b_("zxcv"),
191 policy_value_cur_(policy_value_a_), 205 policy_value_cur_(policy_value_a_) {}
192 invalidate_callback_count_(0),
193 state_change_enabled_callback_count_(0),
194 state_change_disabled_callback_count_(0) {}
195 206
196 void CloudPolicyInvalidatorTest::SetUp() { 207 void CloudPolicyInvalidatorTest::SetUp() {
197 base::StatisticsRecorder::Initialize(); 208 base::StatisticsRecorder::Initialize();
198 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); 209 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh);
199 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); 210 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations);
200 } 211 }
201 212
202 void CloudPolicyInvalidatorTest::TearDown() { 213 void CloudPolicyInvalidatorTest::TearDown() {
203 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement()); 214 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement());
204 if (invalidator_) 215 if (invalidator_)
205 invalidator_->Shutdown(); 216 invalidator_->Shutdown();
217 core_.Disconnect();
206 } 218 }
207 219
208 void CloudPolicyInvalidatorTest::StartInvalidator(bool initialize) { 220 void CloudPolicyInvalidatorTest::StartInvalidator(
209 invalidator_.reset(new CloudPolicyInvalidator( 221 bool initialize,
210 this /* invalidation_handler */, 222 bool start_refresh_scheduler) {
211 &store_, 223 invalidator_.reset(new CloudPolicyInvalidator(&core_, task_runner_));
212 task_runner_)); 224 if (start_refresh_scheduler) {
225 ConnectCore();
226 StartRefreshScheduler();
227 }
213 if (initialize) 228 if (initialize)
214 invalidator_->InitializeWithService(&invalidation_service_); 229 InitializeInvalidator();
230 }
231
232 void CloudPolicyInvalidatorTest::InitializeInvalidator() {
233 invalidator_->Initialize(&invalidation_service_);
234 }
235
236 void CloudPolicyInvalidatorTest::ShutdownInvalidator() {
237 invalidator_->Shutdown();
238 }
239
240 void CloudPolicyInvalidatorTest::DestroyInvalidator() {
241 invalidator_.reset();
242 }
243
244 void CloudPolicyInvalidatorTest::ConnectCore() {
245 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
246 }
247
248 void CloudPolicyInvalidatorTest::StartRefreshScheduler() {
249 core_.StartRefreshScheduler();
250 }
251
252 void CloudPolicyInvalidatorTest::DisconnectCore() {
253 core_.Disconnect();
215 } 254 }
216 255
217 void CloudPolicyInvalidatorTest::StorePolicy( 256 void CloudPolicyInvalidatorTest::StorePolicy(
218 PolicyObject object, 257 PolicyObject object,
219 int64 invalidation_version, 258 int64 invalidation_version,
220 bool policy_changed, 259 bool policy_changed,
221 int64 timestamp) { 260 int64 timestamp) {
222 enterprise_management::PolicyData* data = 261 enterprise_management::PolicyData* data =
223 new enterprise_management::PolicyData(); 262 new enterprise_management::PolicyData();
224 if (object != POLICY_OBJECT_NONE) { 263 if (object != POLICY_OBJECT_NONE) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 PolicyObject object) { 306 PolicyObject object) {
268 return invalidation_service_.EmitInvalidationForTest( 307 return invalidation_service_.EmitInvalidationForTest(
269 GetPolicyObjectId(object), 308 GetPolicyObjectId(object),
270 syncer::Invalidation::kUnknownVersion, 309 syncer::Invalidation::kUnknownVersion,
271 std::string()); 310 std::string());
272 } 311 }
273 312
274 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo( 313 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo(
275 int64 version, 314 int64 version,
276 const std::string& payload) { 315 const std::string& payload) {
277 return version == invalidation_version_ && payload == invalidation_payload_; 316 MockCloudPolicyClient* client =
317 static_cast<MockCloudPolicyClient*>(core_.client());
318 return version == client->invalidation_version_ &&
319 payload == client->invalidation_payload_;
278 } 320 }
279 321
280 bool CloudPolicyInvalidatorTest::CheckInvalidateNotCalled() { 322 bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() {
281 bool result = true; 323 const int start_count = invalidator_->policy_refresh_count();
282 if (invalidate_callback_count_ != 0)
283 result = false;
284 task_runner_->RunUntilIdle(); 324 task_runner_->RunUntilIdle();
285 if (invalidate_callback_count_ != 0) 325 return invalidator_->policy_refresh_count() == start_count;
286 result = false;
287 return result;
288 } 326 }
289 327
290 bool CloudPolicyInvalidatorTest::CheckInvalidateCalled(bool unknown_version) { 328 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed() {
291 base::TimeDelta min_delay; 329 return CheckPolicyRefreshed(base::TimeDelta());
292 base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds(
293 CloudPolicyInvalidator::kMaxFetchDelayMin);
294 if (unknown_version) {
295 base::TimeDelta additional_delay = base::TimeDelta::FromMinutes(
296 CloudPolicyInvalidator::kMissingPayloadDelay);
297 min_delay += additional_delay;
298 max_delay += additional_delay;
299 }
300
301 if (task_runner_->GetPendingTasks().empty())
302 return false;
303 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
304 EXPECT_GE(actual_delay, min_delay);
305 EXPECT_LE(actual_delay, max_delay);
306
307 bool result = true;
308 if (invalidate_callback_count_ != 0)
309 result = false;
310 task_runner_->RunUntilIdle();
311 if (invalidate_callback_count_ != 1)
312 result = false;
313 invalidate_callback_count_ = 0;
314 return result;
315 } 330 }
316 331
317 bool CloudPolicyInvalidatorTest::CheckStateChangedNotCalled() { 332 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshedWithUnknownVersion() {
318 return state_change_enabled_callback_count_ == 0 && 333 return CheckPolicyRefreshed(base::TimeDelta::FromMinutes(
319 state_change_disabled_callback_count_ == 0; 334 CloudPolicyInvalidator::kMissingPayloadDelay));
320 } 335 }
321 336
322 bool CloudPolicyInvalidatorTest::CheckStateChangedCalled( 337 bool CloudPolicyInvalidatorTest::InvalidationsEnabled() {
323 bool invalidations_enabled) { 338 return core_.refresh_scheduler()->invalidations_available();
324 int expected_enabled_count_ = invalidations_enabled ? 1 : 0;
325 int expected_disabled_count_ = invalidations_enabled ? 0 : 1;
326 bool result = state_change_enabled_callback_count_ == expected_enabled_count_
327 && state_change_disabled_callback_count_ == expected_disabled_count_;
328 state_change_enabled_callback_count_ = 0;
329 state_change_disabled_callback_count_ = 0;
330 return result;
331 } 339 }
332 340
333 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged( 341 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged(
334 const syncer::AckHandle& ack_handle) { 342 const syncer::AckHandle& ack_handle) {
335 return invalidation_service_.IsInvalidationAcknowledged(ack_handle); 343 return invalidation_service_.IsInvalidationAcknowledged(ack_handle);
336 } 344 }
337 345
346 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() {
347 return !invalidation_service_.invalidator_registrar()
348 .GetRegisteredIds(invalidator_.get()).empty();
349 }
350
338 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( 351 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount(
339 MetricPolicyRefresh metric) { 352 MetricPolicyRefresh metric) {
340 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - 353 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) -
341 refresh_samples_->GetCount(metric); 354 refresh_samples_->GetCount(metric);
342 } 355 }
343 356
344 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( 357 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount(
345 bool with_payload) { 358 bool with_payload) {
346 int metric = with_payload ? 1 : 0; 359 int metric = with_payload ? 1 : 0;
347 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - 360 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) -
348 invalidations_samples_->GetCount(metric); 361 invalidations_samples_->GetCount(metric);
349 } 362 }
350 363
351 void CloudPolicyInvalidatorTest::SetInvalidationInfo( 364 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) {
352 int64 version, 365 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds(
353 const std::string& payload) { 366 CloudPolicyInvalidator::kMaxFetchDelayMin);
354 invalidation_version_ = version;
355 invalidation_payload_ = payload;
356 }
357 367
358 void CloudPolicyInvalidatorTest::InvalidatePolicy() { 368 if (task_runner_->GetPendingTasks().empty())
359 ++invalidate_callback_count_; 369 return false;
360 } 370 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
371 EXPECT_GE(actual_delay, delay);
372 EXPECT_LE(actual_delay, max_delay);
361 373
362 void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged( 374 const int start_count = invalidator_->policy_refresh_count();
363 bool invalidations_enabled) { 375 task_runner_->RunUntilIdle();
364 if (invalidator_.get()) 376 return invalidator_->policy_refresh_count() == start_count + 1;
365 EXPECT_EQ(invalidations_enabled, invalidator_->invalidations_enabled());
366 if (invalidations_enabled)
367 ++state_change_enabled_callback_count_;
368 else
369 ++state_change_disabled_callback_count_;
370 } 377 }
371 378
372 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( 379 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
373 PolicyObject object) const { 380 PolicyObject object) const {
374 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); 381 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
375 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; 382 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
376 } 383 }
377 384
378 scoped_ptr<base::HistogramSamples> 385 scoped_ptr<base::HistogramSamples>
379 CloudPolicyInvalidatorTest::GetHistogramSamples( 386 CloudPolicyInvalidatorTest::GetHistogramSamples(
380 const std::string& name) const { 387 const std::string& name) const {
381 base::HistogramBase* histogram = 388 base::HistogramBase* histogram =
382 base::StatisticsRecorder::FindHistogram(name); 389 base::StatisticsRecorder::FindHistogram(name);
383 if (!histogram) 390 if (!histogram)
384 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); 391 return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
385 return histogram->SnapshotSamples(); 392 return histogram->SnapshotSamples();
386 } 393 }
387 394
388 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { 395 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) {
389 // No invalidations should be processed if the invalidator is not intialized. 396 // No invalidations should be processed if the invalidator is not initialized.
390 StartInvalidator(false /* initialize */); 397 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */);
391 StorePolicy(POLICY_OBJECT_A); 398 StorePolicy(POLICY_OBJECT_A);
399 EXPECT_FALSE(IsInvalidatorRegistered());
392 FireInvalidation(POLICY_OBJECT_A); 400 FireInvalidation(POLICY_OBJECT_A);
393 EXPECT_TRUE(CheckInvalidateNotCalled()); 401 EXPECT_TRUE(CheckPolicyNotRefreshed());
402 }
403
404 TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) {
405 // No invalidations should be processed if the refresh scheduler is not
406 // started.
407 StartInvalidator(true /* initialize */, false /* start_refresh_scheduler */);
408 StorePolicy(POLICY_OBJECT_A);
409 EXPECT_FALSE(IsInvalidatorRegistered());
410 FireInvalidation(POLICY_OBJECT_A);
411 EXPECT_TRUE(CheckPolicyNotRefreshed());
412 }
413
414 TEST_F(CloudPolicyInvalidatorTest, DisconnectCoreThenInitialize) {
415 // No invalidations should be processed if the core is disconnected before
416 // initialization.
417 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */);
418 DisconnectCore();
419 InitializeInvalidator();
420 StorePolicy(POLICY_OBJECT_A);
421 EXPECT_FALSE(IsInvalidatorRegistered());
422 FireInvalidation(POLICY_OBJECT_A);
423 EXPECT_TRUE(CheckPolicyNotRefreshed());
424 }
425
426 TEST_F(CloudPolicyInvalidatorTest, InitializeThenStartRefreshScheduler) {
427 // Make sure registration occurs and invalidations are processed when
428 // Initialize is called before starting the refresh scheduler.
429 // Note that the reverse case (start refresh scheduler then initialize) is
430 // the default behavior for the test fixture, so will be tested in most other
431 // tests.
432 StartInvalidator(true /* initialize */, false /* start_refresh_scheduler */);
433 ConnectCore();
434 StartRefreshScheduler();
435 StorePolicy(POLICY_OBJECT_A);
436 EXPECT_TRUE(IsInvalidatorRegistered());
437 FireInvalidation(POLICY_OBJECT_A);
438 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
394 } 439 }
395 440
396 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) { 441 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
397 // No registration when store is not loaded. 442 // No registration when store is not loaded.
398 StartInvalidator(); 443 StartInvalidator();
399 EXPECT_TRUE(CheckStateChangedNotCalled()); 444 EXPECT_FALSE(IsInvalidatorRegistered());
445 EXPECT_FALSE(InvalidationsEnabled());
400 FireInvalidation(POLICY_OBJECT_A); 446 FireInvalidation(POLICY_OBJECT_A);
401 FireInvalidation(POLICY_OBJECT_B); 447 FireInvalidation(POLICY_OBJECT_B);
402 EXPECT_TRUE(CheckInvalidateNotCalled()); 448 EXPECT_TRUE(CheckPolicyNotRefreshed());
403 449
404 // No registration when store is loaded with no invalidation object id. 450 // No registration when store is loaded with no invalidation object id.
405 StorePolicy(POLICY_OBJECT_NONE); 451 StorePolicy(POLICY_OBJECT_NONE);
406 EXPECT_TRUE(CheckStateChangedNotCalled()); 452 EXPECT_FALSE(IsInvalidatorRegistered());
453 EXPECT_FALSE(InvalidationsEnabled());
407 FireInvalidation(POLICY_OBJECT_A); 454 FireInvalidation(POLICY_OBJECT_A);
408 FireInvalidation(POLICY_OBJECT_B); 455 FireInvalidation(POLICY_OBJECT_B);
409 EXPECT_TRUE(CheckInvalidateNotCalled()); 456 EXPECT_TRUE(CheckPolicyNotRefreshed());
410 457
411 // Check registration when store is loaded for object A. 458 // Check registration when store is loaded for object A.
412 StorePolicy(POLICY_OBJECT_A); 459 StorePolicy(POLICY_OBJECT_A);
413 EXPECT_TRUE(CheckStateChangedCalled(true)); 460 EXPECT_TRUE(IsInvalidatorRegistered());
461 EXPECT_TRUE(InvalidationsEnabled());
414 FireInvalidation(POLICY_OBJECT_A); 462 FireInvalidation(POLICY_OBJECT_A);
415 EXPECT_TRUE(CheckInvalidateCalled()); 463 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
416 FireInvalidation(POLICY_OBJECT_B); 464 FireInvalidation(POLICY_OBJECT_B);
417 EXPECT_TRUE(CheckInvalidateNotCalled()); 465 EXPECT_TRUE(CheckPolicyNotRefreshed());
418 } 466 }
419 467
420 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) { 468 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) {
421 // Register for object A. 469 // Register for object A.
422 StartInvalidator(); 470 StartInvalidator();
423 StorePolicy(POLICY_OBJECT_A); 471 StorePolicy(POLICY_OBJECT_A);
424 EXPECT_TRUE(CheckStateChangedCalled(true)); 472 EXPECT_TRUE(IsInvalidatorRegistered());
473 EXPECT_TRUE(InvalidationsEnabled());
425 FireInvalidation(POLICY_OBJECT_A); 474 FireInvalidation(POLICY_OBJECT_A);
426 EXPECT_TRUE(CheckInvalidateCalled()); 475 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
427 FireInvalidation(POLICY_OBJECT_B); 476 FireInvalidation(POLICY_OBJECT_B);
428 EXPECT_TRUE(CheckInvalidateNotCalled()); 477 EXPECT_TRUE(CheckPolicyNotRefreshed());
429 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); 478 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
430 479
431 // Check re-registration for object B. Make sure the pending invalidation for 480 // Check re-registration for object B. Make sure the pending invalidation for
432 // object A is acknowledged without making the callback. 481 // object A is acknowledged without making the callback.
433 StorePolicy(POLICY_OBJECT_B); 482 StorePolicy(POLICY_OBJECT_B);
434 EXPECT_TRUE(CheckStateChangedNotCalled()); 483 EXPECT_TRUE(IsInvalidatorRegistered());
484 EXPECT_TRUE(InvalidationsEnabled());
435 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 485 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
436 EXPECT_TRUE(CheckInvalidateNotCalled()); 486 EXPECT_TRUE(CheckPolicyNotRefreshed());
437 487
438 // Make sure future invalidations for object A are ignored and for object B 488 // Make sure future invalidations for object A are ignored and for object B
439 // are processed. 489 // are processed.
440 FireInvalidation(POLICY_OBJECT_A); 490 FireInvalidation(POLICY_OBJECT_A);
441 EXPECT_TRUE(CheckInvalidateNotCalled()); 491 EXPECT_TRUE(CheckPolicyNotRefreshed());
442 FireInvalidation(POLICY_OBJECT_B); 492 FireInvalidation(POLICY_OBJECT_B);
443 EXPECT_TRUE(CheckInvalidateCalled()); 493 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
444 } 494 }
445 495
446 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) { 496 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) {
447 // Register for object A. 497 // Register for object A.
448 StartInvalidator(); 498 StartInvalidator();
449 StorePolicy(POLICY_OBJECT_A); 499 StorePolicy(POLICY_OBJECT_A);
450 EXPECT_TRUE(CheckStateChangedCalled(true)); 500 EXPECT_TRUE(IsInvalidatorRegistered());
501 EXPECT_TRUE(InvalidationsEnabled());
451 FireInvalidation(POLICY_OBJECT_A); 502 FireInvalidation(POLICY_OBJECT_A);
452 EXPECT_TRUE(CheckInvalidateCalled()); 503 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
453 504
454 // Check unregistration when store is loaded with no invalidation object id. 505 // Check unregistration when store is loaded with no invalidation object id.
455 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); 506 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
456 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); 507 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
457 StorePolicy(POLICY_OBJECT_NONE); 508 StorePolicy(POLICY_OBJECT_NONE);
509 EXPECT_FALSE(IsInvalidatorRegistered());
458 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 510 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
459 EXPECT_TRUE(CheckStateChangedCalled(false)); 511 EXPECT_FALSE(InvalidationsEnabled());
460 FireInvalidation(POLICY_OBJECT_A); 512 FireInvalidation(POLICY_OBJECT_A);
461 FireInvalidation(POLICY_OBJECT_B); 513 FireInvalidation(POLICY_OBJECT_B);
462 EXPECT_TRUE(CheckInvalidateNotCalled()); 514 EXPECT_TRUE(CheckPolicyNotRefreshed());
463 515
464 // Check re-registration for object B. 516 // Check re-registration for object B.
465 StorePolicy(POLICY_OBJECT_B); 517 StorePolicy(POLICY_OBJECT_B);
466 EXPECT_TRUE(CheckStateChangedCalled(true)); 518 EXPECT_TRUE(IsInvalidatorRegistered());
519 EXPECT_TRUE(InvalidationsEnabled());
467 FireInvalidation(POLICY_OBJECT_B); 520 FireInvalidation(POLICY_OBJECT_B);
468 EXPECT_TRUE(CheckInvalidateCalled()); 521 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
469 } 522 }
470 523
471 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) { 524 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) {
472 // Register and fire invalidation 525 // Register and fire invalidation
473 StorePolicy(POLICY_OBJECT_A); 526 StorePolicy(POLICY_OBJECT_A);
474 StartInvalidator(); 527 StartInvalidator();
475 EXPECT_TRUE(CheckStateChangedCalled(true)); 528 EXPECT_TRUE(InvalidationsEnabled());
476 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload"); 529 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload");
477 530
478 // Make sure client info is set as soon as the invalidation is received. 531 // Make sure client info is set as soon as the invalidation is received.
479 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); 532 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload"));
480 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); 533 EXPECT_TRUE(CheckPolicyRefreshed());
481 534
482 // Make sure invalidation is not acknowledged until the store is loaded. 535 // Make sure invalidation is not acknowledged until the store is loaded.
483 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); 536 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
484 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); 537 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload"));
485 StorePolicy(POLICY_OBJECT_A, 12); 538 StorePolicy(POLICY_OBJECT_A, 12);
486 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 539 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
487 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 540 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
488 } 541 }
489 542
490 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) { 543 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) {
491 // Register and fire invalidation with unknown version. 544 // Register and fire invalidation with unknown version.
492 StorePolicy(POLICY_OBJECT_A); 545 StorePolicy(POLICY_OBJECT_A);
493 StartInvalidator(); 546 StartInvalidator();
494 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); 547 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
495 548
496 // Make sure client info is not set until after the invalidation callback is 549 // Make sure client info is not set until after the invalidation callback is
497 // made. 550 // made.
498 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 551 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
499 EXPECT_TRUE(CheckInvalidateCalled()); 552 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
500 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); 553 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
501 554
502 // Make sure invalidation is not acknowledged until the store is loaded. 555 // Make sure invalidation is not acknowledged until the store is loaded.
503 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); 556 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
504 StorePolicy(POLICY_OBJECT_A, -1); 557 StorePolicy(POLICY_OBJECT_A, -1);
505 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 558 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
506 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 559 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
507 } 560 }
508 561
509 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) { 562 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) {
510 // Generate multiple invalidations. 563 // Generate multiple invalidations.
511 StorePolicy(POLICY_OBJECT_A); 564 StorePolicy(POLICY_OBJECT_A);
512 StartInvalidator(); 565 StartInvalidator();
513 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1"); 566 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1");
514 EXPECT_TRUE(CheckInvalidationInfo(1, "test1")); 567 EXPECT_TRUE(CheckInvalidationInfo(1, "test1"));
515 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2"); 568 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2");
516 EXPECT_TRUE(CheckInvalidationInfo(2, "test2")); 569 EXPECT_TRUE(CheckInvalidationInfo(2, "test2"));
517 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3"); 570 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3");
518 EXPECT_TRUE(CheckInvalidationInfo(3, "test3")); 571 EXPECT_TRUE(CheckInvalidationInfo(3, "test3"));
519 572
520 // Make sure the replaced invalidations are acknowledged. 573 // Make sure the replaced invalidations are acknowledged.
521 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); 574 EXPECT_TRUE(IsInvalidationAcknowledged(ack1));
522 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); 575 EXPECT_TRUE(IsInvalidationAcknowledged(ack2));
523 576
524 // Make sure the invalidate callback is called once. 577 // Make sure the policy is refreshed once.
525 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); 578 EXPECT_TRUE(CheckPolicyRefreshed());
526 579
527 // Make sure that the last invalidation is only acknowledged after the store 580 // Make sure that the last invalidation is only acknowledged after the store
528 // is loaded with the latest version. 581 // is loaded with the latest version.
529 StorePolicy(POLICY_OBJECT_A, 1); 582 StorePolicy(POLICY_OBJECT_A, 1);
530 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 583 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
531 StorePolicy(POLICY_OBJECT_A, 2); 584 StorePolicy(POLICY_OBJECT_A, 2);
532 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 585 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
533 StorePolicy(POLICY_OBJECT_A, 3); 586 StorePolicy(POLICY_OBJECT_A, 3);
534 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); 587 EXPECT_TRUE(IsInvalidationAcknowledged(ack3));
535 } 588 }
536 589
537 TEST_F(CloudPolicyInvalidatorTest, 590 TEST_F(CloudPolicyInvalidatorTest,
538 HandleMultipleInvalidationsWithUnknownVersion) { 591 HandleMultipleInvalidationsWithUnknownVersion) {
539 // Validate that multiple invalidations with unknown version each generate 592 // Validate that multiple invalidations with unknown version each generate
540 // unique invalidation version numbers. 593 // unique invalidation version numbers.
541 StorePolicy(POLICY_OBJECT_A); 594 StorePolicy(POLICY_OBJECT_A);
542 StartInvalidator(); 595 StartInvalidator();
543 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A); 596 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A);
544 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 597 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
545 EXPECT_TRUE(CheckInvalidateCalled()); 598 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
546 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); 599 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
547 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A); 600 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A);
548 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 601 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
549 EXPECT_TRUE(CheckInvalidateCalled()); 602 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
550 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string())); 603 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string()));
551 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A); 604 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A);
552 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 605 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
553 EXPECT_TRUE(CheckInvalidateCalled()); 606 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
554 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string())); 607 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string()));
555 608
556 // Make sure the replaced invalidations are acknowledged. 609 // Make sure the replaced invalidations are acknowledged.
557 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); 610 EXPECT_TRUE(IsInvalidationAcknowledged(ack1));
558 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); 611 EXPECT_TRUE(IsInvalidationAcknowledged(ack2));
559 612
560 // Make sure that the last invalidation is only acknowledged after the store 613 // Make sure that the last invalidation is only acknowledged after the store
561 // is loaded with the last unknown version. 614 // is loaded with the last unknown version.
562 StorePolicy(POLICY_OBJECT_A, -1); 615 StorePolicy(POLICY_OBJECT_A, -1);
563 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 616 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
564 StorePolicy(POLICY_OBJECT_A, -2); 617 StorePolicy(POLICY_OBJECT_A, -2);
565 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 618 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
566 StorePolicy(POLICY_OBJECT_A, -3); 619 StorePolicy(POLICY_OBJECT_A, -3);
567 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); 620 EXPECT_TRUE(IsInvalidationAcknowledged(ack3));
568 } 621 }
569 622
570 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeInvalidateCallback) { 623 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) {
571 // Generate an invalidation. 624 // Generate an invalidation.
572 StorePolicy(POLICY_OBJECT_A); 625 StorePolicy(POLICY_OBJECT_A);
573 StartInvalidator(); 626 StartInvalidator();
574 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); 627 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
575 628
576 // Ensure that the invalidate callback is not made and the invalidation is 629 // Ensure that the policy is not refreshed and the invalidation is
577 // acknowledged if the store is loaded with the latest version before the 630 // acknowledged if the store is loaded with the latest version before the
578 // callback is invoked. 631 // refresh can occur.
579 StorePolicy(POLICY_OBJECT_A, 3); 632 StorePolicy(POLICY_OBJECT_A, 3);
580 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 633 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
581 EXPECT_TRUE(CheckInvalidateNotCalled()); 634 EXPECT_TRUE(CheckPolicyNotRefreshed());
635 }
636
637 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) {
638 // Generate an invalidation.
639 StorePolicy(POLICY_OBJECT_A);
640 StartInvalidator();
641 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
642
643 // Ensure that the policy refresh is not made after the invalidator is shut
644 // down.
645 ShutdownInvalidator();
646 EXPECT_TRUE(CheckPolicyNotRefreshed());
647 DestroyInvalidator();
582 } 648 }
583 649
584 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { 650 TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
585 // Before registration, changes to the invalidation service state should not 651 // Test invalidation service state changes while not registered.
586 // generate change state notifications.
587 StartInvalidator(); 652 StartInvalidator();
588 DisableInvalidationService(); 653 DisableInvalidationService();
589 EnableInvalidationService(); 654 EnableInvalidationService();
590 EXPECT_TRUE(CheckStateChangedNotCalled()); 655 EXPECT_FALSE(InvalidationsEnabled());
591 656
592 // After registration, changes to the invalidation service state should 657 // Test invalidation service state changes while registered.
593 // generate notifications.
594 StorePolicy(POLICY_OBJECT_A); 658 StorePolicy(POLICY_OBJECT_A);
595 EXPECT_TRUE(CheckStateChangedCalled(true)); 659 EXPECT_TRUE(InvalidationsEnabled());
596 DisableInvalidationService(); 660 DisableInvalidationService();
597 EXPECT_TRUE(CheckStateChangedCalled(false)); 661 EXPECT_FALSE(InvalidationsEnabled());
598 DisableInvalidationService(); 662 DisableInvalidationService();
599 EXPECT_TRUE(CheckStateChangedNotCalled()); 663 EXPECT_FALSE(InvalidationsEnabled());
600 EnableInvalidationService(); 664 EnableInvalidationService();
601 EXPECT_TRUE(CheckStateChangedCalled(true)); 665 EXPECT_TRUE(InvalidationsEnabled());
602 EnableInvalidationService(); 666 EnableInvalidationService();
603 EXPECT_TRUE(CheckStateChangedNotCalled()); 667 EXPECT_TRUE(InvalidationsEnabled());
604 668
605 // When the invalidation service is enabled, changes to the registration 669 // Test registration changes with invalidation service enabled.
606 // state should generate notifications.
607 StorePolicy(POLICY_OBJECT_NONE); 670 StorePolicy(POLICY_OBJECT_NONE);
608 EXPECT_TRUE(CheckStateChangedCalled(false)); 671 EXPECT_FALSE(InvalidationsEnabled());
609 StorePolicy(POLICY_OBJECT_NONE); 672 StorePolicy(POLICY_OBJECT_NONE);
610 EXPECT_TRUE(CheckStateChangedNotCalled()); 673 EXPECT_FALSE(InvalidationsEnabled());
611 StorePolicy(POLICY_OBJECT_A); 674 StorePolicy(POLICY_OBJECT_A);
612 EXPECT_TRUE(CheckStateChangedCalled(true)); 675 EXPECT_TRUE(InvalidationsEnabled());
613 StorePolicy(POLICY_OBJECT_A); 676 StorePolicy(POLICY_OBJECT_A);
614 EXPECT_TRUE(CheckStateChangedNotCalled()); 677 EXPECT_TRUE(InvalidationsEnabled());
615 678
616 // When the invalidation service is disabled, changes to the registration 679 // Test registration changes with invalidation service disabled.
617 // state should not generate notifications.
618 DisableInvalidationService(); 680 DisableInvalidationService();
619 EXPECT_TRUE(CheckStateChangedCalled(false)); 681 EXPECT_FALSE(InvalidationsEnabled());
620 StorePolicy(POLICY_OBJECT_NONE); 682 StorePolicy(POLICY_OBJECT_NONE);
621 StorePolicy(POLICY_OBJECT_A); 683 StorePolicy(POLICY_OBJECT_A);
622 EXPECT_TRUE(CheckStateChangedNotCalled()); 684 EXPECT_FALSE(InvalidationsEnabled());
685 }
686
687 TEST_F(CloudPolicyInvalidatorTest, Disconnect) {
688 // Generate an invalidation.
689 StorePolicy(POLICY_OBJECT_A);
690 StartInvalidator();
691 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 1, "test");
692 EXPECT_TRUE(InvalidationsEnabled());
693
694 // Ensure that the policy is not refreshed after disconnecting the core, but
695 // a call to indicate that invalidations are disabled is made.
696 DisconnectCore();
697 EXPECT_TRUE(CheckPolicyNotRefreshed());
698
699 // Ensure that invalidation service events do not cause refreshes while the
700 // invalidator is stopped.
701 FireInvalidation(POLICY_OBJECT_A, 2, "test");
702 EXPECT_TRUE(CheckPolicyNotRefreshed());
703 DisableInvalidationService();
704 EnableInvalidationService();
705
706 // Connect and disconnect without starting the refresh scheduler.
707 ConnectCore();
708 FireInvalidation(POLICY_OBJECT_A, 3, "test");
709 EXPECT_TRUE(CheckPolicyNotRefreshed());
710 DisconnectCore();
711 FireInvalidation(POLICY_OBJECT_A, 4, "test");
712 EXPECT_TRUE(CheckPolicyNotRefreshed());
713
714 // Ensure that the invalidator returns to normal after reconnecting.
715 ConnectCore();
716 StartRefreshScheduler();
717 EXPECT_TRUE(CheckPolicyNotRefreshed());
718 EXPECT_TRUE(InvalidationsEnabled());
719 FireInvalidation(POLICY_OBJECT_A, 5, "test");
720 EXPECT_TRUE(CheckInvalidationInfo(5, "test"));
721 EXPECT_TRUE(CheckPolicyRefreshed());
722 DisableInvalidationService();
723 EXPECT_FALSE(InvalidationsEnabled());
623 } 724 }
624 725
625 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { 726 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) {
626 // Store loads occurring before invalidation registration are not counted. 727 // Store loads occurring before invalidation registration are not counted.
627 StartInvalidator(); 728 StartInvalidator();
628 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); 729 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */);
629 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); 730 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */);
630 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); 731 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED));
631 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); 732 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
632 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); 733 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 FireInvalidation(POLICY_OBJECT_A); 816 FireInvalidation(POLICY_OBJECT_A);
716 FireInvalidation(POLICY_OBJECT_A, 3, "test"); 817 FireInvalidation(POLICY_OBJECT_A, 3, "test");
717 FireInvalidation(POLICY_OBJECT_A, 4, "test"); 818 FireInvalidation(POLICY_OBJECT_A, 4, "test");
718 819
719 // Verify that received invalidations metrics are correct. 820 // Verify that received invalidations metrics are correct.
720 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); 821 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */));
721 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); 822 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */));
722 } 823 }
723 824
724 } // namespace policy 825 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud/cloud_policy_invalidator.cc ('k') | chrome/browser/policy/cloud/cloud_policy_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698