Index: chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc |
diff --git a/chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc b/chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc |
index eed8bfc9f1f6012c0baa09e7fbfe0696a9bf87f9..b8d1de03aa4316399975b40a29d12bb370dbc4db 100644 |
--- a/chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc |
+++ b/chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc |
@@ -5,6 +5,7 @@ |
#include <string> |
#include "base/basictypes.h" |
+#include "base/bind.h" |
#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/metrics/histogram.h" |
@@ -47,11 +48,23 @@ class CloudPolicyInvalidatorTest : public testing::Test, |
virtual void TearDown() OVERRIDE; |
// Starts the invalidator which will be tested. |
- void StartInvalidator(bool initialize); |
+ // |instantiate| determines if the invalidator should be instantiated. |
+ // |start| determines if the invalidator should be started. |
+ void StartInvalidator(bool instantiate, bool start); |
void StartInvalidator() { |
- StartInvalidator(true /* initialize */); |
+ StartInvalidator(true /* instantiate */, true /* start */); |
} |
+ // Calls Stop on the invalidator. |
+ void StopInvalidator(); |
+ |
+ // Calls Shutdown on the invalidator. Test must call DestroyInvalidator |
+ // afterwards to prevent Shutdown from being called twice. |
+ void ShutdownInvalidator(); |
+ |
+ // Destroys the invalidator. |
+ void DestroyInvalidator(); |
+ |
// Simulates storing a new policy to the policy store. |
// |object| determines which policy object the store will report the |
// invalidator should register for. May be POLICY_OBJECT_NONE for no object. |
@@ -132,6 +145,9 @@ class CloudPolicyInvalidatorTest : public testing::Test, |
virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE; |
private: |
+ // Get a pointer to the invalidation service. |
+ invalidation::InvalidationService* GetInvalidationService(); |
+ |
// Returns the object id of the given policy object. |
const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; |
@@ -205,13 +221,31 @@ void CloudPolicyInvalidatorTest::TearDown() { |
invalidator_->Shutdown(); |
} |
-void CloudPolicyInvalidatorTest::StartInvalidator(bool initialize) { |
- invalidator_.reset(new CloudPolicyInvalidator( |
- this /* invalidation_handler */, |
- &store_, |
- task_runner_)); |
- if (initialize) |
- invalidator_->InitializeWithService(&invalidation_service_); |
+void CloudPolicyInvalidatorTest::StartInvalidator(bool instantiate, |
+ bool start) { |
+ if (instantiate) { |
+ invalidator_.reset(new CloudPolicyInvalidator( |
+ base::Bind( |
+ &CloudPolicyInvalidatorTest::GetInvalidationService, |
+ base::Unretained(this)), |
+ this /* invalidation_handler */, |
+ &store_, |
+ task_runner_)); |
+ } |
+ if (start) |
+ invalidator_->Start(); |
+} |
+ |
+void CloudPolicyInvalidatorTest::StopInvalidator() { |
+ invalidator_->Stop(); |
+} |
+ |
+void CloudPolicyInvalidatorTest::ShutdownInvalidator() { |
+ invalidator_->Shutdown(); |
+} |
+ |
+void CloudPolicyInvalidatorTest::DestroyInvalidator() { |
+ invalidator_.reset(); |
} |
void CloudPolicyInvalidatorTest::StorePolicy( |
@@ -369,6 +403,11 @@ void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged( |
++state_change_disabled_callback_count_; |
} |
+invalidation::InvalidationService* |
+CloudPolicyInvalidatorTest::GetInvalidationService() { |
+ return &invalidation_service_; |
+} |
+ |
const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( |
PolicyObject object) const { |
EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); |
@@ -385,9 +424,9 @@ scoped_ptr<base::HistogramSamples> |
return histogram->SnapshotSamples(); |
} |
-TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { |
- // No invalidations should be processed if the invalidator is not intialized. |
- StartInvalidator(false /* initialize */); |
+TEST_F(CloudPolicyInvalidatorTest, Unstarted) { |
+ // No invalidations should be processed if the invalidator is not started. |
+ StartInvalidator(true /* instantiate */, false /* start */); |
StorePolicy(POLICY_OBJECT_A); |
FireInvalidation(POLICY_OBJECT_A); |
EXPECT_TRUE(CheckInvalidateNotCalled()); |
@@ -581,6 +620,19 @@ TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeInvalidateCallback) { |
EXPECT_TRUE(CheckInvalidateNotCalled()); |
} |
+TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) { |
+ // Generate an invalidation. |
+ StorePolicy(POLICY_OBJECT_A); |
+ StartInvalidator(); |
+ syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); |
+ |
+ // Ensure that the invalidate callback is not made after the invalidator is |
+ // shut down. |
+ ShutdownInvalidator(); |
+ EXPECT_TRUE(CheckInvalidateNotCalled()); |
+ DestroyInvalidator(); |
+} |
+ |
TEST_F(CloudPolicyInvalidatorTest, StateChanged) { |
// Before registration, changes to the invalidation service state should not |
// generate change state notifications. |
@@ -622,6 +674,40 @@ TEST_F(CloudPolicyInvalidatorTest, StateChanged) { |
EXPECT_TRUE(CheckStateChangedNotCalled()); |
} |
+TEST_F(CloudPolicyInvalidatorTest, Stop) { |
+ // Generate an invalidation. |
+ StorePolicy(POLICY_OBJECT_A); |
+ StartInvalidator(); |
+ syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); |
+ EXPECT_TRUE(CheckStateChangedCalled(true)); |
+ |
+ // Ensure that the invalidate callback is not made after stopping the |
+ // invalidator, but a call to indicate that invalidations are disabled is |
+ // made. |
+ StopInvalidator(); |
+ EXPECT_TRUE(CheckInvalidateNotCalled()); |
+ EXPECT_TRUE(CheckStateChangedCalled(false)); |
+ |
+ // Ensure that invalidation service events do not cause callbacks to be |
+ // invoked while the invalidator is stopped. |
+ FireInvalidation(POLICY_OBJECT_A, 4, "test"); |
+ EXPECT_TRUE(CheckInvalidateNotCalled()); |
+ DisableInvalidationService(); |
+ EXPECT_TRUE(CheckStateChangedNotCalled()); |
+ EnableInvalidationService(); |
+ EXPECT_TRUE(CheckStateChangedNotCalled()); |
+ |
+ // Ensure that the invalidator returns to normal after restarting. |
+ StartInvalidator(false /* instantiate */, true /* start */); |
+ EXPECT_TRUE(CheckInvalidateNotCalled()); |
+ EXPECT_TRUE(CheckStateChangedCalled(true)); |
+ FireInvalidation(POLICY_OBJECT_A, 4, "test"); |
+ EXPECT_TRUE(CheckInvalidationInfo(4, "test")); |
+ EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); |
+ DisableInvalidationService(); |
+ EXPECT_TRUE(CheckStateChangedCalled(false)); |
+} |
+ |
TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { |
// Store loads occurring before invalidation registration are not counted. |
StartInvalidator(); |