| Index: chrome/browser/policy/file_based_policy_provider_unittest.cc
|
| diff --git a/chrome/browser/policy/file_based_policy_provider_unittest.cc b/chrome/browser/policy/file_based_policy_provider_unittest.cc
|
| index c78c5869675229064759713b11efaff799860227..8ae43396884439118bb89095baec0ff39d0f0142 100644
|
| --- a/chrome/browser/policy/file_based_policy_provider_unittest.cc
|
| +++ b/chrome/browser/policy/file_based_policy_provider_unittest.cc
|
| @@ -2,130 +2,110 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| +#include "base/file_util.h"
|
| +#include "base/scoped_temp_dir.h"
|
| +#include "chrome/browser/policy/asynchronous_policy_provider.h"
|
| +#include "chrome/browser/policy/asynchronous_policy_test_base.h"
|
| #include "chrome/browser/policy/configuration_policy_pref_store.h"
|
| +#include "chrome/browser/policy/configuration_policy_store_interface.h"
|
| #include "chrome/browser/policy/file_based_policy_provider.h"
|
| +#include "chrome/browser/policy/mock_configuration_policy_provider.h"
|
| +#include "chrome/browser/policy/mock_configuration_policy_store.h"
|
| +#include "chrome/common/policy_constants.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| +using testing::_;
|
| +using testing::InSequence;
|
| using testing::Mock;
|
| +using testing::Return;
|
|
|
| namespace policy {
|
|
|
| // Shorter reload intervals for testing FileBasedPolicyLoader.
|
| const int kSettleIntervalSecondsForTesting = 0;
|
| -const int kReloadIntervalMinutesForTesting = 1;
|
|
|
| -// A delegate for testing that can feed arbitrary information to the loader.
|
| -class TestDelegate : public FileBasedPolicyProvider::Delegate {
|
| +class FileBasedPolicyProviderDelegateMock
|
| + : public FileBasedPolicyProvider::ProviderDelegate {
|
| public:
|
| - TestDelegate()
|
| - : FileBasedPolicyProvider::Delegate(FilePath(FILE_PATH_LITERAL("fake"))) {
|
| - }
|
| -
|
| - // FileBasedPolicyProvider::Delegate implementation:
|
| - virtual DictionaryValue* Load() {
|
| - return static_cast<DictionaryValue*>(dict_.DeepCopy());
|
| - }
|
| -
|
| - virtual base::Time GetLastModification() {
|
| - return last_modification_;
|
| - }
|
| -
|
| - DictionaryValue* dict() { return &dict_; }
|
| - void set_last_modification(const base::Time& last_modification) {
|
| - last_modification_ = last_modification;
|
| - }
|
| -
|
| - private:
|
| - DictionaryValue dict_;
|
| - base::Time last_modification_;
|
| + FileBasedPolicyProviderDelegateMock()
|
| + : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {}
|
| + MOCK_METHOD0(Load, DictionaryValue*());
|
| + MOCK_METHOD0(GetLastModification, base::Time());
|
| };
|
|
|
| -// A mock provider that allows us to capture reload notifications.
|
| -class MockPolicyProvider : public ConfigurationPolicyProvider,
|
| - public base::SupportsWeakPtr<MockPolicyProvider> {
|
| +class FileBasedPolicyProviderTest : public AsynchronousPolicyTestBase {
|
| public:
|
| - explicit MockPolicyProvider()
|
| - : ConfigurationPolicyProvider(
|
| - ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()) {
|
| - }
|
| -
|
| - virtual bool Provide(ConfigurationPolicyStoreInterface* store) {
|
| - return true;
|
| + FileBasedPolicyProviderTest() :
|
| + AsynchronousPolicyTestBase() {}
|
| + virtual ~FileBasedPolicyProviderTest() {}
|
| +
|
| + virtual void SetUp() {
|
| + AsynchronousPolicyTestBase::SetUp();
|
| + store_.reset(new MockConfigurationPolicyStore);
|
| + provider_delegate_.reset(new FileBasedPolicyProviderDelegateMock());
|
| + ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
|
| }
|
|
|
| - MOCK_METHOD0(NotifyStoreOfPolicyChange, void());
|
| -};
|
| -
|
| -class FileBasedPolicyLoaderTest : public testing::Test {
|
| - protected:
|
| - FileBasedPolicyLoaderTest()
|
| - : ui_thread_(BrowserThread::UI, &loop_),
|
| - file_thread_(BrowserThread::FILE, &loop_) {}
|
| -
|
| virtual void TearDown() {
|
| - loop_.RunAllPending();
|
| + provider_.reset(NULL);
|
| + AsynchronousPolicyTestBase::TearDown();
|
| + file_util::Delete(temp_directory_.path(), true);
|
| }
|
|
|
| - MessageLoop loop_;
|
| + protected:
|
| + ScopedTempDir temp_directory_;
|
| + scoped_ptr<FileBasedPolicyProviderDelegateMock> provider_delegate_;
|
|
|
| private:
|
| - BrowserThread ui_thread_;
|
| - BrowserThread file_thread_;
|
| + DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyProviderTest);
|
| };
|
|
|
| -TEST_F(FileBasedPolicyLoaderTest, BasicLoad) {
|
| - TestDelegate* test_delegate = new TestDelegate;
|
| - test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com");
|
| -
|
| - scoped_refptr<FileBasedPolicyLoader> loader(
|
| - new FileBasedPolicyLoader(base::WeakPtr<FileBasedPolicyProvider>(),
|
| - test_delegate,
|
| - kSettleIntervalSecondsForTesting,
|
| - kReloadIntervalMinutesForTesting));
|
| - scoped_ptr<DictionaryValue> policy(loader->GetPolicy());
|
| - EXPECT_TRUE(policy.get());
|
| - EXPECT_EQ(1U, policy->size());
|
| -
|
| - std::string str_value;
|
| - EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
|
| - EXPECT_EQ("http://www.google.com", str_value);
|
| +class PolicyChangeObserverMock
|
| + : public AsynchronousPolicyProvider::PolicyChangeObserver {
|
| + public:
|
| + MOCK_METHOD0(OnPolicyChange, void());
|
| +};
|
|
|
| - loader->Stop();
|
| +TEST_F(FileBasedPolicyProviderTest, WatcherDelegateInitialLoad) {
|
| + PolicyChangeObserverMock* reload_observer = new PolicyChangeObserverMock;
|
| + EXPECT_CALL(*reload_observer, OnPolicyChange()).Times(1);
|
| + scoped_refptr<FileBasedPolicyProvider::WatcherDelegate> delegate =
|
| + new FileBasedPolicyProvider::WatcherDelegate(
|
| + temp_directory_.path(),
|
| + reload_observer);
|
| + delegate->Init();
|
| + delegate->Stop();
|
| }
|
|
|
| -TEST_F(FileBasedPolicyLoaderTest, TestRefresh) {
|
| - MockPolicyProvider provider;
|
| - TestDelegate* test_delegate = new TestDelegate;
|
| -
|
| - scoped_refptr<FileBasedPolicyLoader> loader(
|
| - new FileBasedPolicyLoader(provider.AsWeakPtr(),
|
| - test_delegate,
|
| - kSettleIntervalSecondsForTesting,
|
| - kReloadIntervalMinutesForTesting));
|
| - scoped_ptr<DictionaryValue> policy(loader->GetPolicy());
|
| - EXPECT_TRUE(policy.get());
|
| - EXPECT_EQ(0U, policy->size());
|
| -
|
| - test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com");
|
| -
|
| - EXPECT_CALL(provider, NotifyStoreOfPolicyChange()).Times(1);
|
| - loader->OnFilePathChanged(FilePath(FILE_PATH_LITERAL("fake")));
|
| +TEST_F(FileBasedPolicyProviderTest, WatcherDelegateFileChangeRefresh) {
|
| + PolicyChangeObserverMock* reload_observer = new PolicyChangeObserverMock;
|
| + EXPECT_CALL(*reload_observer, OnPolicyChange()).Times(2);
|
| + scoped_refptr<FileBasedPolicyProvider::WatcherDelegate> delegate =
|
| + new FileBasedPolicyProvider::WatcherDelegate(
|
| + temp_directory_.path(),
|
| + reload_observer);
|
| + delegate->Init();
|
| + delegate->OnFilePathChanged(temp_directory_.path());
|
| + delegate->Stop();
|
| +}
|
|
|
| - // Run the loop. The refresh should be handled immediately since the settle
|
| - // interval has been disabled.
|
| +TEST_F(FileBasedPolicyProviderTest, ProviderInit) {
|
| + base::Time last_modified;
|
| + EXPECT_CALL(*provider_delegate_, GetLastModification()).WillRepeatedly(
|
| + Return(last_modified));
|
| + InSequence s;
|
| + EXPECT_CALL(*provider_delegate_, Load()).WillOnce(Return(
|
| + new DictionaryValue));
|
| + DictionaryValue* policies = new DictionaryValue();
|
| + policies->SetBoolean(policy::key::kSyncDisabled, true);
|
| + EXPECT_CALL(*provider_delegate_, Load()).WillOnce(Return(policies));
|
| + provider_.reset(new FileBasedPolicyProvider(
|
| + ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
|
| + provider_delegate_.release()));
|
| + EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
|
| loop_.RunAllPending();
|
| - Mock::VerifyAndClearExpectations(&provider);
|
| -
|
| - policy.reset(loader->GetPolicy());
|
| - EXPECT_TRUE(policy.get());
|
| - EXPECT_EQ(1U, policy->size());
|
| -
|
| - std::string str_value;
|
| - EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
|
| - EXPECT_EQ("http://www.google.com", str_value);
|
| -
|
| - loader->Stop();
|
| + provider_->Provide(store_.get());
|
| }
|
|
|
| } // namespace policy
|
|
|