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

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(bool unknown_version);
108 return CheckInvalidateCalled(true); 131 bool CheckPolicyRefreshed() {
Mattias Nissler (ping if slow) 2013/09/05 14:54:04 nit: style guide advises against non-obvious overl
Steve Condie 2013/09/06 06:23:05 Done.
132 return CheckPolicyRefreshed(true);
109 } 133 }
110 134
111 // Checks that the state changed callback of the invalidation handler was not 135 // Returns the invalidations enabled state set by the invalidator on the
112 // called. 136 // refresh scheduler.
113 bool CheckStateChangedNotCalled(); 137 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 138
119 // Determines if the invalidation with the given ack handle has been 139 // Determines if the invalidation with the given ack handle has been
120 // acknowledged. 140 // acknowledged.
121 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle); 141 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle);
122 142
143 // Determines if the invalidator has registered for an object with the
144 // invalidation service.
145 bool IsInvalidatorRegistered();
146
123 // Get the current count for the given metric. 147 // Get the current count for the given metric.
124 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); 148 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
125 base::HistogramBase::Count GetInvalidationCount(bool with_payload); 149 base::HistogramBase::Count GetInvalidationCount(bool with_payload);
126 150
127 // CloudPolicyInvalidationHandler: 151 private:
128 virtual void SetInvalidationInfo( 152 // Get a pointer to the invalidation service.
129 int64 version, 153 invalidation::InvalidationService* GetInvalidationService();
130 const std::string& payload) OVERRIDE;
131 virtual void InvalidatePolicy() OVERRIDE;
132 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE;
133 154
134 private:
135 // Returns the object id of the given policy object. 155 // Returns the object id of the given policy object.
136 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; 156 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
137 157
138 // Get histogram samples for the given histogram. 158 // Get histogram samples for the given histogram.
139 scoped_ptr<base::HistogramSamples> GetHistogramSamples( 159 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
140 const std::string& name) const; 160 const std::string& name) const;
141 161
142 // Objects the invalidator depends on. 162 // Objects the invalidator depends on.
143 invalidation::FakeInvalidationService invalidation_service_; 163 invalidation::FakeInvalidationService invalidation_service_;
144 MockCloudPolicyStore store_; 164 MockCloudPolicyStore store_;
165 CloudPolicyCore core_;
145 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 166 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
146 167
147 // The invalidator which will be tested. 168 // The invalidator which will be tested.
148 scoped_ptr<CloudPolicyInvalidator> invalidator_; 169 scoped_ptr<CloudPolicyInvalidator> invalidator_;
149 170
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. 171 // Object ids for the test policy objects.
155 invalidation::ObjectId object_id_a_; 172 invalidation::ObjectId object_id_a_;
156 invalidation::ObjectId object_id_b_; 173 invalidation::ObjectId object_id_b_;
157 174
158 // Increasing policy timestamp. 175 // Increasing policy timestamp.
159 int64 timestamp_; 176 int64 timestamp_;
160 177
161 // Fake policy values which are alternated to cause the store to report a 178 // Fake policy values which are alternated to cause the store to report a
162 // changed policy. 179 // changed policy.
163 const char* policy_value_a_; 180 const char* policy_value_a_;
164 const char* policy_value_b_; 181 const char* policy_value_b_;
165 182
166 // The currently used policy value. 183 // The currently used policy value.
167 const char* policy_value_cur_; 184 const char* policy_value_cur_;
168 185
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. 186 // Stores starting histogram counts for kMetricPolicyRefresh.
177 scoped_ptr<base::HistogramSamples> refresh_samples_; 187 scoped_ptr<base::HistogramSamples> refresh_samples_;
178 188
179 // Stores starting histogram counts for kMetricPolicyInvalidations. 189 // Stores starting histogram counts for kMetricPolicyInvalidations.
180 scoped_ptr<base::HistogramSamples> invalidations_samples_; 190 scoped_ptr<base::HistogramSamples> invalidations_samples_;
191
192 // Initialize message loop.
193 base::MessageLoop loop_;
181 }; 194 };
182 195
183 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() 196 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
184 : task_runner_(new base::TestSimpleTaskRunner()), 197 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
185 invalidation_version_(0), 198 std::string()),
199 &store_),
200 task_runner_(new base::TestSimpleTaskRunner()),
186 object_id_a_(135, "asdf"), 201 object_id_a_(135, "asdf"),
187 object_id_b_(246, "zxcv"), 202 object_id_b_(246, "zxcv"),
188 timestamp_(123456), 203 timestamp_(123456),
189 policy_value_a_("asdf"), 204 policy_value_a_("asdf"),
190 policy_value_b_("zxcv"), 205 policy_value_b_("zxcv"),
191 policy_value_cur_(policy_value_a_), 206 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 207
196 void CloudPolicyInvalidatorTest::SetUp() { 208 void CloudPolicyInvalidatorTest::SetUp() {
197 base::StatisticsRecorder::Initialize(); 209 base::StatisticsRecorder::Initialize();
198 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); 210 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh);
199 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); 211 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations);
200 } 212 }
201 213
202 void CloudPolicyInvalidatorTest::TearDown() { 214 void CloudPolicyInvalidatorTest::TearDown() {
203 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement()); 215 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement());
204 if (invalidator_) 216 if (invalidator_)
205 invalidator_->Shutdown(); 217 invalidator_->Shutdown();
218 core_.Disconnect();
206 } 219 }
207 220
208 void CloudPolicyInvalidatorTest::StartInvalidator(bool initialize) { 221 void CloudPolicyInvalidatorTest::StartInvalidator(
209 invalidator_.reset(new CloudPolicyInvalidator( 222 bool initialize,
210 this /* invalidation_handler */, 223 bool start_refresh_scheduler) {
211 &store_, 224 invalidator_.reset(new CloudPolicyInvalidator(&core_, task_runner_));
212 task_runner_)); 225 if (start_refresh_scheduler) {
226 ConnectCore();
227 StartRefreshScheduler();
228 }
213 if (initialize) 229 if (initialize)
214 invalidator_->InitializeWithService(&invalidation_service_); 230 InitializeInvalidator();
231 }
232
233 void CloudPolicyInvalidatorTest::InitializeInvalidator() {
234 invalidator_->Initialize(base::Bind(
235 &CloudPolicyInvalidatorTest::GetInvalidationService,
236 base::Unretained(this)));
237 }
238
239 void CloudPolicyInvalidatorTest::ShutdownInvalidator() {
240 invalidator_->Shutdown();
241 }
242
243 void CloudPolicyInvalidatorTest::DestroyInvalidator() {
244 invalidator_.reset();
245 }
246
247 void CloudPolicyInvalidatorTest::ConnectCore() {
248 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
249 }
250
251 void CloudPolicyInvalidatorTest::StartRefreshScheduler() {
252 core_.StartRefreshScheduler();
253 }
254
255 void CloudPolicyInvalidatorTest::DisconnectCore() {
256 core_.Disconnect();
215 } 257 }
216 258
217 void CloudPolicyInvalidatorTest::StorePolicy( 259 void CloudPolicyInvalidatorTest::StorePolicy(
218 PolicyObject object, 260 PolicyObject object,
219 int64 invalidation_version, 261 int64 invalidation_version,
220 bool policy_changed, 262 bool policy_changed,
221 int64 timestamp) { 263 int64 timestamp) {
222 enterprise_management::PolicyData* data = 264 enterprise_management::PolicyData* data =
223 new enterprise_management::PolicyData(); 265 new enterprise_management::PolicyData();
224 if (object != POLICY_OBJECT_NONE) { 266 if (object != POLICY_OBJECT_NONE) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 PolicyObject object) { 309 PolicyObject object) {
268 return invalidation_service_.EmitInvalidationForTest( 310 return invalidation_service_.EmitInvalidationForTest(
269 GetPolicyObjectId(object), 311 GetPolicyObjectId(object),
270 syncer::Invalidation::kUnknownVersion, 312 syncer::Invalidation::kUnknownVersion,
271 std::string()); 313 std::string());
272 } 314 }
273 315
274 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo( 316 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo(
275 int64 version, 317 int64 version,
276 const std::string& payload) { 318 const std::string& payload) {
277 return version == invalidation_version_ && payload == invalidation_payload_; 319 MockCloudPolicyClient* client =
320 static_cast<MockCloudPolicyClient*>(core_.client());
321 return version == client->invalidation_version_ &&
322 payload == client->invalidation_payload_;
278 } 323 }
279 324
280 bool CloudPolicyInvalidatorTest::CheckInvalidateNotCalled() { 325 bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() {
281 bool result = true; 326 const int start_count = invalidator_->policy_refresh_count();
282 if (invalidate_callback_count_ != 0)
283 result = false;
284 task_runner_->RunUntilIdle(); 327 task_runner_->RunUntilIdle();
285 if (invalidate_callback_count_ != 0) 328 return invalidator_->policy_refresh_count() == start_count;
286 result = false;
287 return result;
288 } 329 }
289 330
290 bool CloudPolicyInvalidatorTest::CheckInvalidateCalled(bool unknown_version) { 331 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(bool unknown_version) {
291 base::TimeDelta min_delay; 332 base::TimeDelta min_delay;
292 base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds( 333 base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds(
293 CloudPolicyInvalidator::kMaxFetchDelayMin); 334 CloudPolicyInvalidator::kMaxFetchDelayMin);
294 if (unknown_version) { 335 if (unknown_version) {
295 base::TimeDelta additional_delay = base::TimeDelta::FromMinutes( 336 base::TimeDelta additional_delay = base::TimeDelta::FromMinutes(
296 CloudPolicyInvalidator::kMissingPayloadDelay); 337 CloudPolicyInvalidator::kMissingPayloadDelay);
297 min_delay += additional_delay; 338 min_delay += additional_delay;
298 max_delay += additional_delay; 339 max_delay += additional_delay;
299 } 340 }
300 341
301 if (task_runner_->GetPendingTasks().empty()) 342 if (task_runner_->GetPendingTasks().empty())
302 return false; 343 return false;
303 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; 344 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
304 EXPECT_GE(actual_delay, min_delay); 345 EXPECT_GE(actual_delay, min_delay);
305 EXPECT_LE(actual_delay, max_delay); 346 EXPECT_LE(actual_delay, max_delay);
306 347
307 bool result = true; 348 const int start_count = invalidator_->policy_refresh_count();
308 if (invalidate_callback_count_ != 0)
309 result = false;
310 task_runner_->RunUntilIdle(); 349 task_runner_->RunUntilIdle();
311 if (invalidate_callback_count_ != 1) 350 return invalidator_->policy_refresh_count() == start_count + 1;
312 result = false;
313 invalidate_callback_count_ = 0;
314 return result;
315 } 351 }
316 352
317 bool CloudPolicyInvalidatorTest::CheckStateChangedNotCalled() { 353 bool CloudPolicyInvalidatorTest::InvalidationsEnabled() {
318 return state_change_enabled_callback_count_ == 0 && 354 return core_.refresh_scheduler()->invalidations_available();
319 state_change_disabled_callback_count_ == 0;
320 }
321
322 bool CloudPolicyInvalidatorTest::CheckStateChangedCalled(
323 bool invalidations_enabled) {
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 } 355 }
332 356
333 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged( 357 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged(
334 const syncer::AckHandle& ack_handle) { 358 const syncer::AckHandle& ack_handle) {
335 return invalidation_service_.IsInvalidationAcknowledged(ack_handle); 359 return invalidation_service_.IsInvalidationAcknowledged(ack_handle);
336 } 360 }
337 361
362 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() {
363 return !invalidation_service_.invalidator_registrar()
364 .GetRegisteredIds(invalidator_.get()).empty();
365 }
366
338 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( 367 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount(
339 MetricPolicyRefresh metric) { 368 MetricPolicyRefresh metric) {
340 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - 369 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) -
341 refresh_samples_->GetCount(metric); 370 refresh_samples_->GetCount(metric);
342 } 371 }
343 372
344 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( 373 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount(
345 bool with_payload) { 374 bool with_payload) {
346 int metric = with_payload ? 1 : 0; 375 int metric = with_payload ? 1 : 0;
347 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - 376 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) -
348 invalidations_samples_->GetCount(metric); 377 invalidations_samples_->GetCount(metric);
349 } 378 }
350 379
351 void CloudPolicyInvalidatorTest::SetInvalidationInfo( 380 invalidation::InvalidationService*
352 int64 version, 381 CloudPolicyInvalidatorTest::GetInvalidationService() {
353 const std::string& payload) { 382 return &invalidation_service_;
354 invalidation_version_ = version;
355 invalidation_payload_ = payload;
356 }
357
358 void CloudPolicyInvalidatorTest::InvalidatePolicy() {
359 ++invalidate_callback_count_;
360 }
361
362 void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged(
363 bool invalidations_enabled) {
364 if (invalidator_.get())
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 } 383 }
371 384
372 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( 385 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
373 PolicyObject object) const { 386 PolicyObject object) const {
374 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); 387 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
375 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; 388 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
376 } 389 }
377 390
378 scoped_ptr<base::HistogramSamples> 391 scoped_ptr<base::HistogramSamples>
379 CloudPolicyInvalidatorTest::GetHistogramSamples( 392 CloudPolicyInvalidatorTest::GetHistogramSamples(
380 const std::string& name) const { 393 const std::string& name) const {
381 base::HistogramBase* histogram = 394 base::HistogramBase* histogram =
382 base::StatisticsRecorder::FindHistogram(name); 395 base::StatisticsRecorder::FindHistogram(name);
383 if (!histogram) 396 if (!histogram)
384 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); 397 return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
385 return histogram->SnapshotSamples(); 398 return histogram->SnapshotSamples();
386 } 399 }
387 400
388 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { 401 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) {
389 // No invalidations should be processed if the invalidator is not intialized. 402 // No invalidations should be processed if the invalidator is not initialized.
390 StartInvalidator(false /* initialize */); 403 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */);
391 StorePolicy(POLICY_OBJECT_A); 404 StorePolicy(POLICY_OBJECT_A);
405 EXPECT_FALSE(IsInvalidatorRegistered());
392 FireInvalidation(POLICY_OBJECT_A); 406 FireInvalidation(POLICY_OBJECT_A);
393 EXPECT_TRUE(CheckInvalidateNotCalled()); 407 EXPECT_TRUE(CheckPolicyNotRefreshed());
408 }
409
410 TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) {
411 // No invalidations should be processed if the refresh scheduler is not
412 // started.
413 StartInvalidator(true /* initialize */, false /* start_refresh_scheduler */);
414 StorePolicy(POLICY_OBJECT_A);
415 EXPECT_FALSE(IsInvalidatorRegistered());
416 FireInvalidation(POLICY_OBJECT_A);
417 EXPECT_TRUE(CheckPolicyNotRefreshed());
418 }
419
420 TEST_F(CloudPolicyInvalidatorTest, DisconnectCoreThenInitialize) {
421 // No invalidations should be processed if the core is disconnected before
422 // initialization.
423 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */);
424 DisconnectCore();
425 InitializeInvalidator();
426 StorePolicy(POLICY_OBJECT_A);
427 EXPECT_FALSE(IsInvalidatorRegistered());
428 FireInvalidation(POLICY_OBJECT_A);
429 EXPECT_TRUE(CheckPolicyNotRefreshed());
430 }
431
432 TEST_F(CloudPolicyInvalidatorTest, InitializeThenStartRefreshScheduler) {
433 // Make sure registration occurs and invalidations are processed when
434 // Initialize is called before starting the refresh scheduler.
435 // Note that the reverse case (start refresh scheduler then initialize) is
436 // the default behavior for the test fixture, so will be tested in most other
437 // tests.
438 StartInvalidator(true /* initialize */, false /* start_refresh_scheduler */);
439 ConnectCore();
440 StartRefreshScheduler();
441 StorePolicy(POLICY_OBJECT_A);
442 EXPECT_TRUE(IsInvalidatorRegistered());
443 FireInvalidation(POLICY_OBJECT_A);
444 EXPECT_TRUE(CheckPolicyRefreshed());
394 } 445 }
395 446
396 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) { 447 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
397 // No registration when store is not loaded. 448 // No registration when store is not loaded.
398 StartInvalidator(); 449 StartInvalidator();
399 EXPECT_TRUE(CheckStateChangedNotCalled()); 450 EXPECT_FALSE(IsInvalidatorRegistered());
451 EXPECT_FALSE(InvalidationsEnabled());
400 FireInvalidation(POLICY_OBJECT_A); 452 FireInvalidation(POLICY_OBJECT_A);
401 FireInvalidation(POLICY_OBJECT_B); 453 FireInvalidation(POLICY_OBJECT_B);
402 EXPECT_TRUE(CheckInvalidateNotCalled()); 454 EXPECT_TRUE(CheckPolicyNotRefreshed());
403 455
404 // No registration when store is loaded with no invalidation object id. 456 // No registration when store is loaded with no invalidation object id.
405 StorePolicy(POLICY_OBJECT_NONE); 457 StorePolicy(POLICY_OBJECT_NONE);
406 EXPECT_TRUE(CheckStateChangedNotCalled()); 458 EXPECT_FALSE(IsInvalidatorRegistered());
459 EXPECT_FALSE(InvalidationsEnabled());
407 FireInvalidation(POLICY_OBJECT_A); 460 FireInvalidation(POLICY_OBJECT_A);
408 FireInvalidation(POLICY_OBJECT_B); 461 FireInvalidation(POLICY_OBJECT_B);
409 EXPECT_TRUE(CheckInvalidateNotCalled()); 462 EXPECT_TRUE(CheckPolicyNotRefreshed());
410 463
411 // Check registration when store is loaded for object A. 464 // Check registration when store is loaded for object A.
412 StorePolicy(POLICY_OBJECT_A); 465 StorePolicy(POLICY_OBJECT_A);
413 EXPECT_TRUE(CheckStateChangedCalled(true)); 466 EXPECT_TRUE(IsInvalidatorRegistered());
467 EXPECT_TRUE(InvalidationsEnabled());
414 FireInvalidation(POLICY_OBJECT_A); 468 FireInvalidation(POLICY_OBJECT_A);
415 EXPECT_TRUE(CheckInvalidateCalled()); 469 EXPECT_TRUE(CheckPolicyRefreshed());
416 FireInvalidation(POLICY_OBJECT_B); 470 FireInvalidation(POLICY_OBJECT_B);
417 EXPECT_TRUE(CheckInvalidateNotCalled()); 471 EXPECT_TRUE(CheckPolicyNotRefreshed());
418 } 472 }
419 473
420 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) { 474 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) {
421 // Register for object A. 475 // Register for object A.
422 StartInvalidator(); 476 StartInvalidator();
423 StorePolicy(POLICY_OBJECT_A); 477 StorePolicy(POLICY_OBJECT_A);
424 EXPECT_TRUE(CheckStateChangedCalled(true)); 478 EXPECT_TRUE(IsInvalidatorRegistered());
479 EXPECT_TRUE(InvalidationsEnabled());
425 FireInvalidation(POLICY_OBJECT_A); 480 FireInvalidation(POLICY_OBJECT_A);
426 EXPECT_TRUE(CheckInvalidateCalled()); 481 EXPECT_TRUE(CheckPolicyRefreshed());
427 FireInvalidation(POLICY_OBJECT_B); 482 FireInvalidation(POLICY_OBJECT_B);
428 EXPECT_TRUE(CheckInvalidateNotCalled()); 483 EXPECT_TRUE(CheckPolicyNotRefreshed());
429 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); 484 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
430 485
431 // Check re-registration for object B. Make sure the pending invalidation for 486 // Check re-registration for object B. Make sure the pending invalidation for
432 // object A is acknowledged without making the callback. 487 // object A is acknowledged without making the callback.
433 StorePolicy(POLICY_OBJECT_B); 488 StorePolicy(POLICY_OBJECT_B);
434 EXPECT_TRUE(CheckStateChangedNotCalled()); 489 EXPECT_TRUE(IsInvalidatorRegistered());
490 EXPECT_TRUE(InvalidationsEnabled());
435 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 491 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
436 EXPECT_TRUE(CheckInvalidateNotCalled()); 492 EXPECT_TRUE(CheckPolicyNotRefreshed());
437 493
438 // Make sure future invalidations for object A are ignored and for object B 494 // Make sure future invalidations for object A are ignored and for object B
439 // are processed. 495 // are processed.
440 FireInvalidation(POLICY_OBJECT_A); 496 FireInvalidation(POLICY_OBJECT_A);
441 EXPECT_TRUE(CheckInvalidateNotCalled()); 497 EXPECT_TRUE(CheckPolicyNotRefreshed());
442 FireInvalidation(POLICY_OBJECT_B); 498 FireInvalidation(POLICY_OBJECT_B);
443 EXPECT_TRUE(CheckInvalidateCalled()); 499 EXPECT_TRUE(CheckPolicyRefreshed());
444 } 500 }
445 501
446 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) { 502 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) {
447 // Register for object A. 503 // Register for object A.
448 StartInvalidator(); 504 StartInvalidator();
449 StorePolicy(POLICY_OBJECT_A); 505 StorePolicy(POLICY_OBJECT_A);
450 EXPECT_TRUE(CheckStateChangedCalled(true)); 506 EXPECT_TRUE(IsInvalidatorRegistered());
507 EXPECT_TRUE(InvalidationsEnabled());
451 FireInvalidation(POLICY_OBJECT_A); 508 FireInvalidation(POLICY_OBJECT_A);
452 EXPECT_TRUE(CheckInvalidateCalled()); 509 EXPECT_TRUE(CheckPolicyRefreshed());
453 510
454 // Check unregistration when store is loaded with no invalidation object id. 511 // Check unregistration when store is loaded with no invalidation object id.
455 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); 512 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
456 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); 513 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
457 StorePolicy(POLICY_OBJECT_NONE); 514 StorePolicy(POLICY_OBJECT_NONE);
515 EXPECT_FALSE(IsInvalidatorRegistered());
458 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 516 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
459 EXPECT_TRUE(CheckStateChangedCalled(false)); 517 EXPECT_FALSE(InvalidationsEnabled());
460 FireInvalidation(POLICY_OBJECT_A); 518 FireInvalidation(POLICY_OBJECT_A);
461 FireInvalidation(POLICY_OBJECT_B); 519 FireInvalidation(POLICY_OBJECT_B);
462 EXPECT_TRUE(CheckInvalidateNotCalled()); 520 EXPECT_TRUE(CheckPolicyNotRefreshed());
463 521
464 // Check re-registration for object B. 522 // Check re-registration for object B.
465 StorePolicy(POLICY_OBJECT_B); 523 StorePolicy(POLICY_OBJECT_B);
466 EXPECT_TRUE(CheckStateChangedCalled(true)); 524 EXPECT_TRUE(IsInvalidatorRegistered());
525 EXPECT_TRUE(InvalidationsEnabled());
467 FireInvalidation(POLICY_OBJECT_B); 526 FireInvalidation(POLICY_OBJECT_B);
468 EXPECT_TRUE(CheckInvalidateCalled()); 527 EXPECT_TRUE(CheckPolicyRefreshed());
469 } 528 }
470 529
471 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) { 530 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) {
472 // Register and fire invalidation 531 // Register and fire invalidation
473 StorePolicy(POLICY_OBJECT_A); 532 StorePolicy(POLICY_OBJECT_A);
474 StartInvalidator(); 533 StartInvalidator();
475 EXPECT_TRUE(CheckStateChangedCalled(true)); 534 EXPECT_TRUE(InvalidationsEnabled());
476 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload"); 535 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload");
477 536
478 // Make sure client info is set as soon as the invalidation is received. 537 // Make sure client info is set as soon as the invalidation is received.
479 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); 538 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload"));
480 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); 539 EXPECT_TRUE(CheckPolicyRefreshed(false /* unknown_version */));
481 540
482 // Make sure invalidation is not acknowledged until the store is loaded. 541 // Make sure invalidation is not acknowledged until the store is loaded.
483 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); 542 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
484 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); 543 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload"));
485 StorePolicy(POLICY_OBJECT_A, 12); 544 StorePolicy(POLICY_OBJECT_A, 12);
486 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 545 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
487 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 546 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
488 } 547 }
489 548
490 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) { 549 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) {
491 // Register and fire invalidation with unknown version. 550 // Register and fire invalidation with unknown version.
492 StorePolicy(POLICY_OBJECT_A); 551 StorePolicy(POLICY_OBJECT_A);
493 StartInvalidator(); 552 StartInvalidator();
494 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); 553 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
495 554
496 // Make sure client info is not set until after the invalidation callback is 555 // Make sure client info is not set until after the invalidation callback is
497 // made. 556 // made.
498 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 557 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
499 EXPECT_TRUE(CheckInvalidateCalled()); 558 EXPECT_TRUE(CheckPolicyRefreshed());
500 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); 559 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
501 560
502 // Make sure invalidation is not acknowledged until the store is loaded. 561 // Make sure invalidation is not acknowledged until the store is loaded.
503 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); 562 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
504 StorePolicy(POLICY_OBJECT_A, -1); 563 StorePolicy(POLICY_OBJECT_A, -1);
505 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 564 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
506 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 565 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
507 } 566 }
508 567
509 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) { 568 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) {
510 // Generate multiple invalidations. 569 // Generate multiple invalidations.
511 StorePolicy(POLICY_OBJECT_A); 570 StorePolicy(POLICY_OBJECT_A);
512 StartInvalidator(); 571 StartInvalidator();
513 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1"); 572 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1");
514 EXPECT_TRUE(CheckInvalidationInfo(1, "test1")); 573 EXPECT_TRUE(CheckInvalidationInfo(1, "test1"));
515 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2"); 574 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2");
516 EXPECT_TRUE(CheckInvalidationInfo(2, "test2")); 575 EXPECT_TRUE(CheckInvalidationInfo(2, "test2"));
517 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3"); 576 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3");
518 EXPECT_TRUE(CheckInvalidationInfo(3, "test3")); 577 EXPECT_TRUE(CheckInvalidationInfo(3, "test3"));
519 578
520 // Make sure the replaced invalidations are acknowledged. 579 // Make sure the replaced invalidations are acknowledged.
521 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); 580 EXPECT_TRUE(IsInvalidationAcknowledged(ack1));
522 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); 581 EXPECT_TRUE(IsInvalidationAcknowledged(ack2));
523 582
524 // Make sure the invalidate callback is called once. 583 // Make sure the policy is refreshed once.
525 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); 584 EXPECT_TRUE(CheckPolicyRefreshed(false /* unknown_version */));
526 585
527 // Make sure that the last invalidation is only acknowledged after the store 586 // Make sure that the last invalidation is only acknowledged after the store
528 // is loaded with the latest version. 587 // is loaded with the latest version.
529 StorePolicy(POLICY_OBJECT_A, 1); 588 StorePolicy(POLICY_OBJECT_A, 1);
530 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 589 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
531 StorePolicy(POLICY_OBJECT_A, 2); 590 StorePolicy(POLICY_OBJECT_A, 2);
532 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 591 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
533 StorePolicy(POLICY_OBJECT_A, 3); 592 StorePolicy(POLICY_OBJECT_A, 3);
534 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); 593 EXPECT_TRUE(IsInvalidationAcknowledged(ack3));
535 } 594 }
536 595
537 TEST_F(CloudPolicyInvalidatorTest, 596 TEST_F(CloudPolicyInvalidatorTest,
538 HandleMultipleInvalidationsWithUnknownVersion) { 597 HandleMultipleInvalidationsWithUnknownVersion) {
539 // Validate that multiple invalidations with unknown version each generate 598 // Validate that multiple invalidations with unknown version each generate
540 // unique invalidation version numbers. 599 // unique invalidation version numbers.
541 StorePolicy(POLICY_OBJECT_A); 600 StorePolicy(POLICY_OBJECT_A);
542 StartInvalidator(); 601 StartInvalidator();
543 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A); 602 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A);
544 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 603 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
545 EXPECT_TRUE(CheckInvalidateCalled()); 604 EXPECT_TRUE(CheckPolicyRefreshed());
546 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); 605 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
547 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A); 606 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A);
548 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 607 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
549 EXPECT_TRUE(CheckInvalidateCalled()); 608 EXPECT_TRUE(CheckPolicyRefreshed());
550 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string())); 609 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string()));
551 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A); 610 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A);
552 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); 611 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
553 EXPECT_TRUE(CheckInvalidateCalled()); 612 EXPECT_TRUE(CheckPolicyRefreshed());
554 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string())); 613 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string()));
555 614
556 // Make sure the replaced invalidations are acknowledged. 615 // Make sure the replaced invalidations are acknowledged.
557 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); 616 EXPECT_TRUE(IsInvalidationAcknowledged(ack1));
558 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); 617 EXPECT_TRUE(IsInvalidationAcknowledged(ack2));
559 618
560 // Make sure that the last invalidation is only acknowledged after the store 619 // Make sure that the last invalidation is only acknowledged after the store
561 // is loaded with the last unknown version. 620 // is loaded with the last unknown version.
562 StorePolicy(POLICY_OBJECT_A, -1); 621 StorePolicy(POLICY_OBJECT_A, -1);
563 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 622 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
564 StorePolicy(POLICY_OBJECT_A, -2); 623 StorePolicy(POLICY_OBJECT_A, -2);
565 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); 624 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
566 StorePolicy(POLICY_OBJECT_A, -3); 625 StorePolicy(POLICY_OBJECT_A, -3);
567 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); 626 EXPECT_TRUE(IsInvalidationAcknowledged(ack3));
568 } 627 }
569 628
570 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeInvalidateCallback) { 629 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) {
571 // Generate an invalidation. 630 // Generate an invalidation.
572 StorePolicy(POLICY_OBJECT_A); 631 StorePolicy(POLICY_OBJECT_A);
573 StartInvalidator(); 632 StartInvalidator();
574 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); 633 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
575 634
576 // Ensure that the invalidate callback is not made and the invalidation is 635 // Ensure that the policy is not refreshed and the invalidation is
577 // acknowledged if the store is loaded with the latest version before the 636 // acknowledged if the store is loaded with the latest version before the
578 // callback is invoked. 637 // refresh can occur.
579 StorePolicy(POLICY_OBJECT_A, 3); 638 StorePolicy(POLICY_OBJECT_A, 3);
580 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 639 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
581 EXPECT_TRUE(CheckInvalidateNotCalled()); 640 EXPECT_TRUE(CheckPolicyNotRefreshed());
641 }
642
643 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) {
644 // Generate an invalidation.
645 StorePolicy(POLICY_OBJECT_A);
646 StartInvalidator();
647 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
648
649 // Ensure that the policy refresh is not made after the invalidator is shut
650 // down.
651 ShutdownInvalidator();
652 EXPECT_TRUE(CheckPolicyNotRefreshed());
653 DestroyInvalidator();
582 } 654 }
583 655
584 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { 656 TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
585 // Before registration, changes to the invalidation service state should not 657 // Test invalidation service state changes while not registered.
586 // generate change state notifications.
587 StartInvalidator(); 658 StartInvalidator();
588 DisableInvalidationService(); 659 DisableInvalidationService();
589 EnableInvalidationService(); 660 EnableInvalidationService();
590 EXPECT_TRUE(CheckStateChangedNotCalled()); 661 EXPECT_FALSE(InvalidationsEnabled());
591 662
592 // After registration, changes to the invalidation service state should 663 // Test invalidation service state changes while registered.
593 // generate notifications.
594 StorePolicy(POLICY_OBJECT_A); 664 StorePolicy(POLICY_OBJECT_A);
595 EXPECT_TRUE(CheckStateChangedCalled(true)); 665 EXPECT_TRUE(InvalidationsEnabled());
596 DisableInvalidationService(); 666 DisableInvalidationService();
597 EXPECT_TRUE(CheckStateChangedCalled(false)); 667 EXPECT_FALSE(InvalidationsEnabled());
598 DisableInvalidationService(); 668 DisableInvalidationService();
599 EXPECT_TRUE(CheckStateChangedNotCalled()); 669 EXPECT_FALSE(InvalidationsEnabled());
600 EnableInvalidationService(); 670 EnableInvalidationService();
601 EXPECT_TRUE(CheckStateChangedCalled(true)); 671 EXPECT_TRUE(InvalidationsEnabled());
602 EnableInvalidationService(); 672 EnableInvalidationService();
603 EXPECT_TRUE(CheckStateChangedNotCalled()); 673 EXPECT_TRUE(InvalidationsEnabled());
604 674
605 // When the invalidation service is enabled, changes to the registration 675 // Test registration changes with invalidation service enabled.
606 // state should generate notifications.
607 StorePolicy(POLICY_OBJECT_NONE); 676 StorePolicy(POLICY_OBJECT_NONE);
608 EXPECT_TRUE(CheckStateChangedCalled(false)); 677 EXPECT_FALSE(InvalidationsEnabled());
609 StorePolicy(POLICY_OBJECT_NONE); 678 StorePolicy(POLICY_OBJECT_NONE);
610 EXPECT_TRUE(CheckStateChangedNotCalled()); 679 EXPECT_FALSE(InvalidationsEnabled());
611 StorePolicy(POLICY_OBJECT_A); 680 StorePolicy(POLICY_OBJECT_A);
612 EXPECT_TRUE(CheckStateChangedCalled(true)); 681 EXPECT_TRUE(InvalidationsEnabled());
613 StorePolicy(POLICY_OBJECT_A); 682 StorePolicy(POLICY_OBJECT_A);
614 EXPECT_TRUE(CheckStateChangedNotCalled()); 683 EXPECT_TRUE(InvalidationsEnabled());
615 684
616 // When the invalidation service is disabled, changes to the registration 685 // Test registration changes with invalidation service disabled.
617 // state should not generate notifications.
618 DisableInvalidationService(); 686 DisableInvalidationService();
619 EXPECT_TRUE(CheckStateChangedCalled(false)); 687 EXPECT_FALSE(InvalidationsEnabled());
620 StorePolicy(POLICY_OBJECT_NONE); 688 StorePolicy(POLICY_OBJECT_NONE);
621 StorePolicy(POLICY_OBJECT_A); 689 StorePolicy(POLICY_OBJECT_A);
622 EXPECT_TRUE(CheckStateChangedNotCalled()); 690 EXPECT_FALSE(InvalidationsEnabled());
691 }
692
693 TEST_F(CloudPolicyInvalidatorTest, Disconnect) {
694 // Generate an invalidation.
695 StorePolicy(POLICY_OBJECT_A);
696 StartInvalidator();
697 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 1, "test");
698 EXPECT_TRUE(InvalidationsEnabled());
699
700 // Ensure that the policy is not refreshed after disconnecting the core, but
701 // a call to indicate that invalidations are disabled is made.
702 DisconnectCore();
703 EXPECT_TRUE(CheckPolicyNotRefreshed());
704
705 // Ensure that invalidation service events do not cause refreshes while the
706 // invalidator is stopped.
707 FireInvalidation(POLICY_OBJECT_A, 2, "test");
708 EXPECT_TRUE(CheckPolicyNotRefreshed());
709 DisableInvalidationService();
710 EnableInvalidationService();
711
712 // Connect and disconnect without starting the refresh scheduler.
713 ConnectCore();
714 FireInvalidation(POLICY_OBJECT_A, 3, "test");
715 EXPECT_TRUE(CheckPolicyNotRefreshed());
716 DisconnectCore();
717 FireInvalidation(POLICY_OBJECT_A, 4, "test");
718 EXPECT_TRUE(CheckPolicyNotRefreshed());
719
720 // Ensure that the invalidator returns to normal after reconnecting.
721 ConnectCore();
722 StartRefreshScheduler();
723 EXPECT_TRUE(CheckPolicyNotRefreshed());
724 EXPECT_TRUE(InvalidationsEnabled());
725 FireInvalidation(POLICY_OBJECT_A, 5, "test");
726 EXPECT_TRUE(CheckInvalidationInfo(5, "test"));
727 EXPECT_TRUE(CheckPolicyRefreshed(false /* unknown_version */));
728 DisableInvalidationService();
729 EXPECT_FALSE(InvalidationsEnabled());
623 } 730 }
624 731
625 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { 732 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) {
626 // Store loads occurring before invalidation registration are not counted. 733 // Store loads occurring before invalidation registration are not counted.
627 StartInvalidator(); 734 StartInvalidator();
628 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); 735 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */);
629 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); 736 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */);
630 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); 737 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED));
631 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); 738 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
632 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); 739 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); 822 FireInvalidation(POLICY_OBJECT_A);
716 FireInvalidation(POLICY_OBJECT_A, 3, "test"); 823 FireInvalidation(POLICY_OBJECT_A, 3, "test");
717 FireInvalidation(POLICY_OBJECT_A, 4, "test"); 824 FireInvalidation(POLICY_OBJECT_A, 4, "test");
718 825
719 // Verify that received invalidations metrics are correct. 826 // Verify that received invalidations metrics are correct.
720 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); 827 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */));
721 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); 828 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */));
722 } 829 }
723 830
724 } // namespace policy 831 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698