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

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

Powered by Google App Engine
This is Rietveld 408576698