Index: chrome/browser/policy/asynchronous_policy_loader_unittest.cc |
diff --git a/chrome/browser/policy/asynchronous_policy_loader_unittest.cc b/chrome/browser/policy/asynchronous_policy_loader_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1c58fdb9be571fa0ace6982892392bab48e15406 |
--- /dev/null |
+++ b/chrome/browser/policy/asynchronous_policy_loader_unittest.cc |
@@ -0,0 +1,168 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/browser_thread.h" |
+#include "chrome/browser/policy/asynchronous_policy_loader.h" |
+#include "chrome/browser/policy/asynchronous_policy_test_base.h" |
+#include "chrome/browser/policy/mock_configuration_policy_provider.h" |
+ |
+using ::testing::_; |
+using ::testing::InSequence; |
+using ::testing::Return; |
+ |
+namespace policy { |
+ |
+const int kReloadIntervalMinutesForTesting = 1; |
+ |
+class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase { |
+ public: |
+ AsynchronousPolicyLoaderTest() {} |
+ virtual ~AsynchronousPolicyLoaderTest() {} |
+ |
+ virtual void SetUp() { |
+ AsynchronousPolicyTestBase::SetUp(); |
+ mock_provider_ = new MockConfigurationPolicyProvider(); |
+ provider_.reset(mock_provider_); |
+ } |
+ |
+ protected: |
+ MockConfigurationPolicyProvider* mock_provider_; // weak |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoaderTest); |
+}; |
+ |
+ACTION_P(CreateTestDictionary, quit) { |
+ if (quit) { |
+ MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( |
+ &MessageLoopQuitNow)); |
+ } |
+ return new DictionaryValue(); |
+} |
+ |
+ACTION_P2(CreateSequencedTestDictionary, number, quit) { |
+ DictionaryValue* test_dictionary = new DictionaryValue(); |
+ test_dictionary->SetInteger("id", ++(*number)); |
+ if (quit) { |
+ MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( |
+ &MessageLoopQuitNow)); |
+ } |
+ return test_dictionary; |
+} |
+ |
+ACTION(RescheduleImmediatePolicyReload) { |
+ *arg1 = base::TimeDelta(); |
+ return false; |
+} |
+ |
+TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) { |
+ DictionaryValue* template_dict(new DictionaryValue()); |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly( |
+ Return(true)); |
+ EXPECT_CALL(*delegate_, Load()).WillOnce(Return(template_dict)); |
+ scoped_refptr<AsynchronousPolicyLoader> loader = |
+ new AsynchronousPolicyLoader(mock_provider_->AsWeakPtr(), |
+ delegate_.release(), |
+ kReloadIntervalMinutesForTesting); |
+ loader->Init(); |
+ scoped_ptr<DictionaryValue> loaded_dict(loader->GetPolicy()); |
+ EXPECT_TRUE(loaded_dict->Equals(template_dict)); |
+} |
+ |
+// Verify that the fallback policy requests are made. |
+TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) { |
+ int dictionary_number = 0; |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly( |
+ Return(true)); |
+ InSequence s; |
+ EXPECT_CALL(*delegate_, Load()).WillOnce( |
+ CreateSequencedTestDictionary(&dictionary_number, false)); |
+ EXPECT_CALL(*delegate_, Load()).WillOnce( |
+ CreateSequencedTestDictionary(&dictionary_number, true)); |
+ scoped_refptr<AsynchronousPolicyLoader> loader = |
+ new AsynchronousPolicyLoader(mock_provider_->AsWeakPtr(), |
+ delegate_.release(), |
+ 0); |
+ loader->Init(); |
+ loop_.RunAllPending(); |
+ |
+ scoped_ptr<DictionaryValue> loaded_dict(loader->GetPolicy()); |
+ int loaded_number; |
+ EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number)); |
+ EXPECT_EQ(dictionary_number, loaded_number); |
+ |
+ DontRunAllPendingDuringTearDown(); |
+} |
+ |
+// Ensure that calling stop on the loader stops subsequent reloads from |
+// happening. |
+TEST_F(AsynchronousPolicyLoaderTest, Stop) { |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly( |
+ Return(true)); |
+ ON_CALL(*delegate_, Load()).WillByDefault(CreateTestDictionary(false)); |
+ EXPECT_CALL(*delegate_, Load()).Times(1); |
+ scoped_refptr<AsynchronousPolicyLoader> loader = |
+ new AsynchronousPolicyLoader(mock_provider_->AsWeakPtr(), |
+ delegate_.release(), |
+ 0); |
+ loader->Init(); |
+ loader->Stop(); |
+} |
+ |
+// Verifies that if it's not safe to reload a policy, subsequent |
+// attempts will be made. |
+TEST_F(AsynchronousPolicyLoaderTest, UnsafeForPolicyReload) { |
+ InSequence s; |
+ EXPECT_CALL(*delegate_, Load()).WillOnce(CreateTestDictionary(false)); |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce( |
+ RescheduleImmediatePolicyReload()); |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce( |
+ Return(true)); |
+ // Successful reload, but determined unsafe after the load, |
+ // should trigger another reload. |
+ EXPECT_CALL(*delegate_, Load()).WillOnce(CreateTestDictionary(false)); |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce( |
+ RescheduleImmediatePolicyReload()); |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce( |
+ Return(true)); |
+ // Successful reload. |
+ EXPECT_CALL(*delegate_, Load()).WillOnce(CreateTestDictionary(true)); |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce( |
+ Return(true)); |
+ scoped_refptr<AsynchronousPolicyLoader> loader = |
+ new AsynchronousPolicyLoader(mock_provider_->AsWeakPtr(), |
+ delegate_.release(), |
+ 0); |
+ loader->Init(); |
+} |
+ |
+// Verifies that the provider is notified upon policy reload, but only |
+// if the policy changed. |
+TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { |
+ EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly( |
+ Return(true)); |
+ InSequence s; |
+ int dictionary_number_1 = 0; |
+ int dictionary_number_2 = 0; |
+ EXPECT_CALL(*delegate_, Load()).WillOnce( |
+ CreateSequencedTestDictionary(&dictionary_number_1, false)); |
+ EXPECT_CALL(*delegate_, Load()).WillOnce( |
+ CreateSequencedTestDictionary(&dictionary_number_2, false)); |
+ EXPECT_CALL(*mock_provider_, NotifyStoreOfPolicyChange()).Times(0); |
+ EXPECT_CALL(*delegate_, Load()).WillOnce( |
+ CreateSequencedTestDictionary(&dictionary_number_2, false)); |
+ EXPECT_CALL(*mock_provider_, NotifyStoreOfPolicyChange()).Times(1); |
+ EXPECT_CALL(*delegate_, Load()).WillOnce( |
+ CreateSequencedTestDictionary(&dictionary_number_1, true)); |
+ scoped_refptr<AsynchronousPolicyLoader> loader = |
+ new AsynchronousPolicyLoader(mock_provider_->AsWeakPtr(), |
+ delegate_.release(), |
+ 0); |
+ loader->Init(); |
+ |
+ loop_.RunAllPending(); |
+ DontRunAllPendingDuringTearDown(); |
+} |
+ |
+} // namespace policy |