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

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"
10 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
11 #include "base/metrics/histogram_samples.h" 12 #include "base/metrics/histogram_samples.h"
12 #include "base/metrics/sample_map.h" 13 #include "base/metrics/sample_map.h"
13 #include "base/metrics/statistics_recorder.h" 14 #include "base/metrics/statistics_recorder.h"
14 #include "base/test/test_simple_task_runner.h" 15 #include "base/test/test_simple_task_runner.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "base/values.h" 17 #include "base/values.h"
17 #include "chrome/browser/invalidation/fake_invalidation_service.h" 18 #include "chrome/browser/invalidation/fake_invalidation_service.h"
(...skipping 22 matching lines...) Expand all
40 POLICY_OBJECT_B 41 POLICY_OBJECT_B
41 }; 42 };
42 43
43 CloudPolicyInvalidatorTest(); 44 CloudPolicyInvalidatorTest();
44 45
45 virtual void SetUp() OVERRIDE; 46 virtual void SetUp() OVERRIDE;
46 47
47 virtual void TearDown() OVERRIDE; 48 virtual void TearDown() OVERRIDE;
48 49
49 // Starts the invalidator which will be tested. 50 // Starts the invalidator which will be tested.
50 void StartInvalidator(bool initialize); 51 // |instantiate| determines if the invalidator should be instantiated.
52 // |start| determines if the invalidator should be started.
53 void StartInvalidator(bool instantiate, bool start);
51 void StartInvalidator() { 54 void StartInvalidator() {
52 StartInvalidator(true /* initialize */); 55 StartInvalidator(true /* instantiate */, true /* start */);
53 } 56 }
54 57
58 // Calls Stop on the invalidator.
59 void StopInvalidator();
60
61 // Calls Shutdown on the invalidator. Test must call DestroyInvalidator
62 // afterwards to prevent Shutdown from being called twice.
63 void ShutdownInvalidator();
64
65 // Destroys the invalidator.
66 void DestroyInvalidator();
67
55 // Simulates storing a new policy to the policy store. 68 // Simulates storing a new policy to the policy store.
56 // |object| determines which policy object the store will report the 69 // |object| determines which policy object the store will report the
57 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. 70 // invalidator should register for. May be POLICY_OBJECT_NONE for no object.
58 // |invalidation_version| determines what invalidation the store will report. 71 // |invalidation_version| determines what invalidation the store will report.
59 // |policy_changed| determines whether the store will report that the 72 // |policy_changed| determines whether the store will report that the
60 // policy changed. 73 // policy changed.
61 // |timestamp| determines the response timestamp the store will report. 74 // |timestamp| determines the response timestamp the store will report.
62 void StorePolicy( 75 void StorePolicy(
63 PolicyObject object, 76 PolicyObject object,
64 int64 invalidation_version, 77 int64 invalidation_version,
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 base::HistogramBase::Count GetInvalidationCount(bool with_payload); 138 base::HistogramBase::Count GetInvalidationCount(bool with_payload);
126 139
127 // CloudPolicyInvalidationHandler: 140 // CloudPolicyInvalidationHandler:
128 virtual void SetInvalidationInfo( 141 virtual void SetInvalidationInfo(
129 int64 version, 142 int64 version,
130 const std::string& payload) OVERRIDE; 143 const std::string& payload) OVERRIDE;
131 virtual void InvalidatePolicy() OVERRIDE; 144 virtual void InvalidatePolicy() OVERRIDE;
132 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE; 145 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE;
133 146
134 private: 147 private:
148 // Get a pointer to the invalidation service.
149 invalidation::InvalidationService* GetInvalidationService();
150
135 // Returns the object id of the given policy object. 151 // Returns the object id of the given policy object.
136 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; 152 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
137 153
138 // Get histogram samples for the given histogram. 154 // Get histogram samples for the given histogram.
139 scoped_ptr<base::HistogramSamples> GetHistogramSamples( 155 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
140 const std::string& name) const; 156 const std::string& name) const;
141 157
142 // Objects the invalidator depends on. 158 // Objects the invalidator depends on.
143 invalidation::FakeInvalidationService invalidation_service_; 159 invalidation::FakeInvalidationService invalidation_service_;
144 MockCloudPolicyStore store_; 160 MockCloudPolicyStore store_;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); 214 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh);
199 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); 215 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations);
200 } 216 }
201 217
202 void CloudPolicyInvalidatorTest::TearDown() { 218 void CloudPolicyInvalidatorTest::TearDown() {
203 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement()); 219 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement());
204 if (invalidator_) 220 if (invalidator_)
205 invalidator_->Shutdown(); 221 invalidator_->Shutdown();
206 } 222 }
207 223
208 void CloudPolicyInvalidatorTest::StartInvalidator(bool initialize) { 224 void CloudPolicyInvalidatorTest::StartInvalidator(bool instantiate,
209 invalidator_.reset(new CloudPolicyInvalidator( 225 bool start) {
210 this /* invalidation_handler */, 226 if (instantiate) {
211 &store_, 227 invalidator_.reset(new CloudPolicyInvalidator(
212 task_runner_)); 228 base::Bind(
213 if (initialize) 229 &CloudPolicyInvalidatorTest::GetInvalidationService,
214 invalidator_->InitializeWithService(&invalidation_service_); 230 base::Unretained(this)),
231 this /* invalidation_handler */,
232 &store_,
233 task_runner_));
234 }
235 if (start)
236 invalidator_->Start();
237 }
238
239 void CloudPolicyInvalidatorTest::StopInvalidator() {
240 invalidator_->Stop();
241 }
242
243 void CloudPolicyInvalidatorTest::ShutdownInvalidator() {
244 invalidator_->Shutdown();
245 }
246
247 void CloudPolicyInvalidatorTest::DestroyInvalidator() {
248 invalidator_.reset();
215 } 249 }
216 250
217 void CloudPolicyInvalidatorTest::StorePolicy( 251 void CloudPolicyInvalidatorTest::StorePolicy(
218 PolicyObject object, 252 PolicyObject object,
219 int64 invalidation_version, 253 int64 invalidation_version,
220 bool policy_changed, 254 bool policy_changed,
221 int64 timestamp) { 255 int64 timestamp) {
222 enterprise_management::PolicyData* data = 256 enterprise_management::PolicyData* data =
223 new enterprise_management::PolicyData(); 257 new enterprise_management::PolicyData();
224 if (object != POLICY_OBJECT_NONE) { 258 if (object != POLICY_OBJECT_NONE) {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged( 396 void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged(
363 bool invalidations_enabled) { 397 bool invalidations_enabled) {
364 if (invalidator_.get()) 398 if (invalidator_.get())
365 EXPECT_EQ(invalidations_enabled, invalidator_->invalidations_enabled()); 399 EXPECT_EQ(invalidations_enabled, invalidator_->invalidations_enabled());
366 if (invalidations_enabled) 400 if (invalidations_enabled)
367 ++state_change_enabled_callback_count_; 401 ++state_change_enabled_callback_count_;
368 else 402 else
369 ++state_change_disabled_callback_count_; 403 ++state_change_disabled_callback_count_;
370 } 404 }
371 405
406 invalidation::InvalidationService*
407 CloudPolicyInvalidatorTest::GetInvalidationService() {
408 return &invalidation_service_;
409 }
410
372 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( 411 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
373 PolicyObject object) const { 412 PolicyObject object) const {
374 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); 413 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
375 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; 414 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
376 } 415 }
377 416
378 scoped_ptr<base::HistogramSamples> 417 scoped_ptr<base::HistogramSamples>
379 CloudPolicyInvalidatorTest::GetHistogramSamples( 418 CloudPolicyInvalidatorTest::GetHistogramSamples(
380 const std::string& name) const { 419 const std::string& name) const {
381 base::HistogramBase* histogram = 420 base::HistogramBase* histogram =
382 base::StatisticsRecorder::FindHistogram(name); 421 base::StatisticsRecorder::FindHistogram(name);
383 if (!histogram) 422 if (!histogram)
384 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); 423 return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
385 return histogram->SnapshotSamples(); 424 return histogram->SnapshotSamples();
386 } 425 }
387 426
388 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { 427 TEST_F(CloudPolicyInvalidatorTest, Unstarted) {
389 // No invalidations should be processed if the invalidator is not intialized. 428 // No invalidations should be processed if the invalidator is not started.
390 StartInvalidator(false /* initialize */); 429 StartInvalidator(true /* instantiate */, false /* start */);
391 StorePolicy(POLICY_OBJECT_A); 430 StorePolicy(POLICY_OBJECT_A);
392 FireInvalidation(POLICY_OBJECT_A); 431 FireInvalidation(POLICY_OBJECT_A);
393 EXPECT_TRUE(CheckInvalidateNotCalled()); 432 EXPECT_TRUE(CheckInvalidateNotCalled());
394 } 433 }
395 434
396 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) { 435 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
397 // No registration when store is not loaded. 436 // No registration when store is not loaded.
398 StartInvalidator(); 437 StartInvalidator();
399 EXPECT_TRUE(CheckStateChangedNotCalled()); 438 EXPECT_TRUE(CheckStateChangedNotCalled());
400 FireInvalidation(POLICY_OBJECT_A); 439 FireInvalidation(POLICY_OBJECT_A);
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); 613 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
575 614
576 // Ensure that the invalidate callback is not made and the invalidation is 615 // Ensure that the invalidate callback is not made and the invalidation is
577 // acknowledged if the store is loaded with the latest version before the 616 // acknowledged if the store is loaded with the latest version before the
578 // callback is invoked. 617 // callback is invoked.
579 StorePolicy(POLICY_OBJECT_A, 3); 618 StorePolicy(POLICY_OBJECT_A, 3);
580 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); 619 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
581 EXPECT_TRUE(CheckInvalidateNotCalled()); 620 EXPECT_TRUE(CheckInvalidateNotCalled());
582 } 621 }
583 622
623 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) {
624 // Generate an invalidation.
625 StorePolicy(POLICY_OBJECT_A);
626 StartInvalidator();
627 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
628
629 // Ensure that the invalidate callback is not made after the invalidator is
630 // shut down.
631 ShutdownInvalidator();
632 EXPECT_TRUE(CheckInvalidateNotCalled());
633 DestroyInvalidator();
634 }
635
584 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { 636 TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
585 // Before registration, changes to the invalidation service state should not 637 // Before registration, changes to the invalidation service state should not
586 // generate change state notifications. 638 // generate change state notifications.
587 StartInvalidator(); 639 StartInvalidator();
588 DisableInvalidationService(); 640 DisableInvalidationService();
589 EnableInvalidationService(); 641 EnableInvalidationService();
590 EXPECT_TRUE(CheckStateChangedNotCalled()); 642 EXPECT_TRUE(CheckStateChangedNotCalled());
591 643
592 // After registration, changes to the invalidation service state should 644 // After registration, changes to the invalidation service state should
593 // generate notifications. 645 // generate notifications.
(...skipping 21 matching lines...) Expand all
615 667
616 // When the invalidation service is disabled, changes to the registration 668 // When the invalidation service is disabled, changes to the registration
617 // state should not generate notifications. 669 // state should not generate notifications.
618 DisableInvalidationService(); 670 DisableInvalidationService();
619 EXPECT_TRUE(CheckStateChangedCalled(false)); 671 EXPECT_TRUE(CheckStateChangedCalled(false));
620 StorePolicy(POLICY_OBJECT_NONE); 672 StorePolicy(POLICY_OBJECT_NONE);
621 StorePolicy(POLICY_OBJECT_A); 673 StorePolicy(POLICY_OBJECT_A);
622 EXPECT_TRUE(CheckStateChangedNotCalled()); 674 EXPECT_TRUE(CheckStateChangedNotCalled());
623 } 675 }
624 676
677 TEST_F(CloudPolicyInvalidatorTest, Stop) {
678 // Generate an invalidation.
679 StorePolicy(POLICY_OBJECT_A);
680 StartInvalidator();
681 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
682 EXPECT_TRUE(CheckStateChangedCalled(true));
683
684 // Ensure that the invalidate callback is not made after stopping the
685 // invalidator, but a call to indicate that invalidations are disabled is
686 // made.
687 StopInvalidator();
688 EXPECT_TRUE(CheckInvalidateNotCalled());
689 EXPECT_TRUE(CheckStateChangedCalled(false));
690
691 // Ensure that invalidation service events do not cause callbacks to be
692 // invoked while the invalidator is stopped.
693 FireInvalidation(POLICY_OBJECT_A, 4, "test");
694 EXPECT_TRUE(CheckInvalidateNotCalled());
695 DisableInvalidationService();
696 EXPECT_TRUE(CheckStateChangedNotCalled());
697 EnableInvalidationService();
698 EXPECT_TRUE(CheckStateChangedNotCalled());
699
700 // Ensure that the invalidator returns to normal after restarting.
701 StartInvalidator(false /* instantiate */, true /* start */);
702 EXPECT_TRUE(CheckInvalidateNotCalled());
703 EXPECT_TRUE(CheckStateChangedCalled(true));
704 FireInvalidation(POLICY_OBJECT_A, 4, "test");
705 EXPECT_TRUE(CheckInvalidationInfo(4, "test"));
706 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */));
707 DisableInvalidationService();
708 EXPECT_TRUE(CheckStateChangedCalled(false));
709 }
710
625 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { 711 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) {
626 // Store loads occurring before invalidation registration are not counted. 712 // Store loads occurring before invalidation registration are not counted.
627 StartInvalidator(); 713 StartInvalidator();
628 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); 714 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */);
629 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); 715 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */);
630 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); 716 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED));
631 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); 717 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
632 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); 718 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
633 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); 719 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
634 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); 720 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 FireInvalidation(POLICY_OBJECT_A); 801 FireInvalidation(POLICY_OBJECT_A);
716 FireInvalidation(POLICY_OBJECT_A, 3, "test"); 802 FireInvalidation(POLICY_OBJECT_A, 3, "test");
717 FireInvalidation(POLICY_OBJECT_A, 4, "test"); 803 FireInvalidation(POLICY_OBJECT_A, 4, "test");
718 804
719 // Verify that received invalidations metrics are correct. 805 // Verify that received invalidations metrics are correct.
720 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); 806 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */));
721 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); 807 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */));
722 } 808 }
723 809
724 } // namespace policy 810 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698