OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_util.h" |
| 6 #include "base/scoped_temp_dir.h" |
| 7 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 8 #include "chrome/browser/policy/asynchronous_policy_test_base.h" |
5 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 9 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 10 #include "chrome/browser/policy/configuration_policy_store_interface.h" |
6 #include "chrome/browser/policy/file_based_policy_provider.h" | 11 #include "chrome/browser/policy/file_based_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 #include "chrome/browser/policy/mock_configuration_policy_store.h" |
| 14 #include "chrome/common/policy_constants.h" |
7 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
9 | 17 |
| 18 using testing::_; |
| 19 using testing::InSequence; |
10 using testing::Mock; | 20 using testing::Mock; |
| 21 using testing::Return; |
11 | 22 |
12 namespace policy { | 23 namespace policy { |
13 | 24 |
14 // Shorter reload intervals for testing FileBasedPolicyLoader. | 25 // Shorter reload intervals for testing FileBasedPolicyLoader. |
15 const int kSettleIntervalSecondsForTesting = 0; | 26 const int kSettleIntervalSecondsForTesting = 0; |
16 const int kReloadIntervalMinutesForTesting = 1; | |
17 | 27 |
18 // A delegate for testing that can feed arbitrary information to the loader. | 28 class FileBasedPolicyProviderDelegateMock |
19 class TestDelegate : public FileBasedPolicyProvider::Delegate { | 29 : public FileBasedPolicyProvider::ProviderDelegate { |
20 public: | 30 public: |
21 TestDelegate() | 31 FileBasedPolicyProviderDelegateMock() |
22 : FileBasedPolicyProvider::Delegate(FilePath(FILE_PATH_LITERAL("fake"))) { | 32 : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {} |
| 33 MOCK_METHOD0(Load, DictionaryValue*()); |
| 34 MOCK_METHOD0(GetLastModification, base::Time()); |
| 35 }; |
| 36 |
| 37 class FileBasedPolicyProviderTest : public AsynchronousPolicyTestBase { |
| 38 public: |
| 39 FileBasedPolicyProviderTest() : |
| 40 AsynchronousPolicyTestBase() {} |
| 41 virtual ~FileBasedPolicyProviderTest() {} |
| 42 |
| 43 virtual void SetUp() { |
| 44 AsynchronousPolicyTestBase::SetUp(); |
| 45 store_.reset(new MockConfigurationPolicyStore); |
| 46 provider_delegate_.reset(new FileBasedPolicyProviderDelegateMock()); |
| 47 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
23 } | 48 } |
24 | 49 |
25 // FileBasedPolicyProvider::Delegate implementation: | 50 virtual void TearDown() { |
26 virtual DictionaryValue* Load() { | 51 provider_.reset(NULL); |
27 return static_cast<DictionaryValue*>(dict_.DeepCopy()); | 52 AsynchronousPolicyTestBase::TearDown(); |
| 53 file_util::Delete(temp_directory_.path(), true); |
28 } | 54 } |
29 | 55 |
30 virtual base::Time GetLastModification() { | 56 protected: |
31 return last_modification_; | 57 ScopedTempDir temp_directory_; |
32 } | 58 scoped_ptr<FileBasedPolicyProviderDelegateMock> provider_delegate_; |
33 | |
34 DictionaryValue* dict() { return &dict_; } | |
35 void set_last_modification(const base::Time& last_modification) { | |
36 last_modification_ = last_modification; | |
37 } | |
38 | 59 |
39 private: | 60 private: |
40 DictionaryValue dict_; | 61 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyProviderTest); |
41 base::Time last_modification_; | |
42 }; | 62 }; |
43 | 63 |
44 // A mock provider that allows us to capture reload notifications. | 64 class PolicyChangeObserverMock |
45 class MockPolicyProvider : public ConfigurationPolicyProvider, | 65 : public AsynchronousPolicyProvider::PolicyChangeObserver { |
46 public base::SupportsWeakPtr<MockPolicyProvider> { | |
47 public: | 66 public: |
48 explicit MockPolicyProvider() | 67 MOCK_METHOD0(OnPolicyChange, void()); |
49 : ConfigurationPolicyProvider( | |
50 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()) { | |
51 } | |
52 | |
53 virtual bool Provide(ConfigurationPolicyStoreInterface* store) { | |
54 return true; | |
55 } | |
56 | |
57 MOCK_METHOD0(NotifyStoreOfPolicyChange, void()); | |
58 }; | 68 }; |
59 | 69 |
60 class FileBasedPolicyLoaderTest : public testing::Test { | 70 TEST_F(FileBasedPolicyProviderTest, WatcherDelegateInitialLoad) { |
61 protected: | 71 PolicyChangeObserverMock* reload_observer = new PolicyChangeObserverMock; |
62 FileBasedPolicyLoaderTest() | 72 EXPECT_CALL(*reload_observer, OnPolicyChange()).Times(1); |
63 : ui_thread_(BrowserThread::UI, &loop_), | 73 scoped_refptr<FileBasedPolicyProvider::WatcherDelegate> delegate = |
64 file_thread_(BrowserThread::FILE, &loop_) {} | 74 new FileBasedPolicyProvider::WatcherDelegate( |
65 | 75 temp_directory_.path(), |
66 virtual void TearDown() { | 76 reload_observer); |
67 loop_.RunAllPending(); | 77 delegate->Init(); |
68 } | 78 delegate->Stop(); |
69 | |
70 MessageLoop loop_; | |
71 | |
72 private: | |
73 BrowserThread ui_thread_; | |
74 BrowserThread file_thread_; | |
75 }; | |
76 | |
77 TEST_F(FileBasedPolicyLoaderTest, BasicLoad) { | |
78 TestDelegate* test_delegate = new TestDelegate; | |
79 test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com"); | |
80 | |
81 scoped_refptr<FileBasedPolicyLoader> loader( | |
82 new FileBasedPolicyLoader(base::WeakPtr<FileBasedPolicyProvider>(), | |
83 test_delegate, | |
84 kSettleIntervalSecondsForTesting, | |
85 kReloadIntervalMinutesForTesting)); | |
86 scoped_ptr<DictionaryValue> policy(loader->GetPolicy()); | |
87 EXPECT_TRUE(policy.get()); | |
88 EXPECT_EQ(1U, policy->size()); | |
89 | |
90 std::string str_value; | |
91 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value)); | |
92 EXPECT_EQ("http://www.google.com", str_value); | |
93 | |
94 loader->Stop(); | |
95 } | 79 } |
96 | 80 |
97 TEST_F(FileBasedPolicyLoaderTest, TestRefresh) { | 81 TEST_F(FileBasedPolicyProviderTest, WatcherDelegateFileChangeRefresh) { |
98 MockPolicyProvider provider; | 82 PolicyChangeObserverMock* reload_observer = new PolicyChangeObserverMock; |
99 TestDelegate* test_delegate = new TestDelegate; | 83 EXPECT_CALL(*reload_observer, OnPolicyChange()).Times(2); |
| 84 scoped_refptr<FileBasedPolicyProvider::WatcherDelegate> delegate = |
| 85 new FileBasedPolicyProvider::WatcherDelegate( |
| 86 temp_directory_.path(), |
| 87 reload_observer); |
| 88 delegate->Init(); |
| 89 delegate->OnFilePathChanged(temp_directory_.path()); |
| 90 delegate->Stop(); |
| 91 } |
100 | 92 |
101 scoped_refptr<FileBasedPolicyLoader> loader( | 93 TEST_F(FileBasedPolicyProviderTest, ProviderInit) { |
102 new FileBasedPolicyLoader(provider.AsWeakPtr(), | 94 base::Time last_modified; |
103 test_delegate, | 95 EXPECT_CALL(*provider_delegate_, GetLastModification()).WillRepeatedly( |
104 kSettleIntervalSecondsForTesting, | 96 Return(last_modified)); |
105 kReloadIntervalMinutesForTesting)); | 97 InSequence s; |
106 scoped_ptr<DictionaryValue> policy(loader->GetPolicy()); | 98 EXPECT_CALL(*provider_delegate_, Load()).WillOnce(Return( |
107 EXPECT_TRUE(policy.get()); | 99 new DictionaryValue)); |
108 EXPECT_EQ(0U, policy->size()); | 100 DictionaryValue* policies = new DictionaryValue(); |
109 | 101 policies->SetBoolean(policy::key::kSyncDisabled, true); |
110 test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com"); | 102 EXPECT_CALL(*provider_delegate_, Load()).WillOnce(Return(policies)); |
111 | 103 provider_.reset(new FileBasedPolicyProvider( |
112 EXPECT_CALL(provider, NotifyStoreOfPolicyChange()).Times(1); | 104 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), |
113 loader->OnFilePathChanged(FilePath(FILE_PATH_LITERAL("fake"))); | 105 provider_delegate_.release())); |
114 | 106 EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1); |
115 // Run the loop. The refresh should be handled immediately since the settle | |
116 // interval has been disabled. | |
117 loop_.RunAllPending(); | 107 loop_.RunAllPending(); |
118 Mock::VerifyAndClearExpectations(&provider); | 108 provider_->Provide(store_.get()); |
119 | |
120 policy.reset(loader->GetPolicy()); | |
121 EXPECT_TRUE(policy.get()); | |
122 EXPECT_EQ(1U, policy->size()); | |
123 | |
124 std::string str_value; | |
125 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value)); | |
126 EXPECT_EQ("http://www.google.com", str_value); | |
127 | |
128 loader->Stop(); | |
129 } | 109 } |
130 | 110 |
131 } // namespace policy | 111 } // namespace policy |
OLD | NEW |