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" | 5 #include "base/file_util.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "chrome/browser/policy/config_dir_policy_provider.h" | 8 #include "chrome/browser/policy/config_dir_policy_provider.h" |
9 #include "chrome/browser/policy/mock_configuration_policy_store.h" | 9 #include "chrome/browser/policy/mock_configuration_policy_store.h" |
10 #include "chrome/common/json_value_serializer.h" | 10 #include "chrome/common/json_value_serializer.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 | 12 |
14 using testing::Mock; | 13 class ConfigDirPolicyProviderTest : public testing::Test { |
15 | |
16 namespace { | |
17 | |
18 // Shorter reload intervals for testing PolicyDirWatcher. | |
19 const int kSettleIntervalSecondsForTesting = 0; | |
20 const int kReloadIntervalMinutesForTesting = 1; | |
21 | |
22 } // namespace | |
23 | |
24 class ConfigDirPolicyProviderTestBase : public testing::Test { | |
25 protected: | 14 protected: |
26 ConfigDirPolicyProviderTestBase() | |
27 : ui_thread_(ChromeThread::UI, &loop_), | |
28 file_thread_(ChromeThread::FILE, &loop_) {} | |
29 | |
30 virtual void SetUp() { | 15 virtual void SetUp() { |
31 // Determine the directory to use for testing. | 16 // Determine the directory to use for testing. |
32 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 17 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
33 test_dir_ = | 18 test_dir_ = |
34 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest")); | 19 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest")); |
35 | 20 |
36 // Make sure the directory is fresh. | 21 // Make sure the directory is fresh. |
37 file_util::Delete(test_dir_, true); | 22 file_util::Delete(test_dir_, true); |
38 file_util::CreateDirectory(test_dir_); | 23 file_util::CreateDirectory(test_dir_); |
39 ASSERT_TRUE(file_util::DirectoryExists(test_dir_)); | 24 ASSERT_TRUE(file_util::DirectoryExists(test_dir_)); |
| 25 |
| 26 // Create a fresh policy store mock. |
| 27 policy_store_.reset(new MockConfigurationPolicyStore()); |
40 } | 28 } |
41 | 29 |
42 virtual void TearDown() { | 30 virtual void TearDown() { |
43 loop_.RunAllPending(); | |
44 // Clean up test directory. | 31 // Clean up test directory. |
45 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | 32 ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
46 ASSERT_FALSE(file_util::PathExists(test_dir_)); | 33 ASSERT_FALSE(file_util::PathExists(test_dir_)); |
47 } | 34 } |
48 | 35 |
49 // JSON-encode a dictionary and write it to a file. | 36 // JSON-encode a dictionary and write it to a file. |
50 void WriteConfigFile(const DictionaryValue& dict, | 37 void WriteConfigFile(const DictionaryValue& dict, |
51 const std::string& file_name) { | 38 const std::string& file_name) { |
52 std::string data; | 39 std::string data; |
53 JSONStringValueSerializer serializer(&data); | 40 JSONStringValueSerializer serializer(&data); |
54 serializer.Serialize(dict); | 41 serializer.Serialize(dict); |
55 FilePath file_path(test_dir_.AppendASCII(file_name)); | 42 FilePath file_path(test_dir_.AppendASCII(file_name)); |
56 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); | 43 file_util::WriteFile(file_path, data.c_str(), data.size()); |
57 } | 44 } |
58 | 45 |
59 FilePath test_dir_; | 46 FilePath test_dir_; |
60 MessageLoop loop_; | |
61 ChromeThread ui_thread_; | |
62 ChromeThread file_thread_; | |
63 }; | |
64 | |
65 // A mock provider that allows us to capture to reload notifications. | |
66 class MockConfigDirPolicyProvider : public ConfigDirPolicyProvider { | |
67 public: | |
68 explicit MockConfigDirPolicyProvider(const FilePath& config_dir_) | |
69 : ConfigDirPolicyProvider(config_dir_) {} | |
70 | |
71 MOCK_METHOD0(NotifyStoreOfPolicyChange, void()); | |
72 }; | |
73 | |
74 class PolicyDirLoaderTest : public ConfigDirPolicyProviderTestBase { | |
75 protected: | |
76 PolicyDirLoaderTest() {} | |
77 | |
78 virtual void SetUp() { | |
79 ConfigDirPolicyProviderTestBase::SetUp(); | |
80 provider_.reset(new MockConfigDirPolicyProvider(test_dir_)); | |
81 } | |
82 | |
83 virtual void TearDown() { | |
84 provider_.reset(NULL); | |
85 ConfigDirPolicyProviderTestBase::TearDown(); | |
86 } | |
87 | |
88 scoped_ptr<MockConfigDirPolicyProvider> provider_; | |
89 }; | |
90 | |
91 TEST_F(PolicyDirLoaderTest, BasicLoad) { | |
92 DictionaryValue test_dict; | |
93 test_dict.SetString("HomepageLocation", "http://www.google.com"); | |
94 WriteConfigFile(test_dict, "config_file"); | |
95 | |
96 scoped_refptr<PolicyDirLoader> loader_( | |
97 new PolicyDirLoader(provider_->AsWeakPtr(), test_dir_, | |
98 kSettleIntervalSecondsForTesting, | |
99 kReloadIntervalMinutesForTesting)); | |
100 scoped_ptr<DictionaryValue> policy(loader_->GetPolicy()); | |
101 EXPECT_TRUE(policy.get()); | |
102 EXPECT_EQ(1U, policy->size()); | |
103 | |
104 std::string str_value; | |
105 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value)); | |
106 EXPECT_EQ("http://www.google.com", str_value); | |
107 | |
108 loader_->Stop(); | |
109 } | |
110 | |
111 TEST_F(PolicyDirLoaderTest, TestRefresh) { | |
112 scoped_refptr<PolicyDirLoader> loader_( | |
113 new PolicyDirLoader(provider_->AsWeakPtr(), test_dir_, | |
114 kSettleIntervalSecondsForTesting, | |
115 kReloadIntervalMinutesForTesting)); | |
116 scoped_ptr<DictionaryValue> policy(loader_->GetPolicy()); | |
117 EXPECT_TRUE(policy.get()); | |
118 EXPECT_EQ(0U, policy->size()); | |
119 | |
120 DictionaryValue test_dict; | |
121 test_dict.SetString("HomepageLocation", "http://www.google.com"); | |
122 WriteConfigFile(test_dict, "config_file"); | |
123 | |
124 EXPECT_CALL(*provider_, NotifyStoreOfPolicyChange()).Times(1); | |
125 loader_->OnFilePathChanged(test_dir_.AppendASCII("config_file")); | |
126 | |
127 // Run the loop. The refresh should be handled immediately since the settle | |
128 // interval has been disabled. | |
129 loop_.RunAllPending(); | |
130 Mock::VerifyAndClearExpectations(provider_.get()); | |
131 | |
132 policy.reset(loader_->GetPolicy()); | |
133 EXPECT_TRUE(policy.get()); | |
134 EXPECT_EQ(1U, policy->size()); | |
135 | |
136 std::string str_value; | |
137 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value)); | |
138 EXPECT_EQ("http://www.google.com", str_value); | |
139 | |
140 loader_->Stop(); | |
141 } | |
142 | |
143 class ConfigDirPolicyProviderTest : public ConfigDirPolicyProviderTestBase { | |
144 protected: | |
145 virtual void SetUp() { | |
146 ConfigDirPolicyProviderTestBase::SetUp(); | |
147 // Create a fresh policy store mock. | |
148 policy_store_.reset(new MockConfigurationPolicyStore()); | |
149 } | |
150 | |
151 scoped_ptr<MockConfigurationPolicyStore> policy_store_; | 47 scoped_ptr<MockConfigurationPolicyStore> policy_store_; |
152 }; | 48 }; |
153 | 49 |
154 // The preferences dictionary is expected to be empty when there are no files to | 50 // The preferences dictionary is expected to be empty when there are no files to |
155 // load. | 51 // load. |
156 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) { | 52 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) { |
157 ConfigDirPolicyProvider provider(test_dir_); | 53 ConfigDirPolicyProvider provider(test_dir_); |
158 EXPECT_TRUE(provider.Provide(policy_store_.get())); | 54 EXPECT_TRUE(provider.Provide(policy_store_.get())); |
159 EXPECT_TRUE(policy_store_->policy_map().empty()); | 55 EXPECT_TRUE(policy_store_->policy_map().empty()); |
160 } | 56 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 policy_store_->policy_map()); | 106 policy_store_->policy_map()); |
211 EXPECT_EQ(1U, policy_map.size()); | 107 EXPECT_EQ(1U, policy_map.size()); |
212 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = | 108 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = |
213 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); | 109 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); |
214 ASSERT_TRUE(entry != policy_map.end()); | 110 ASSERT_TRUE(entry != policy_map.end()); |
215 | 111 |
216 std::string str_value; | 112 std::string str_value; |
217 EXPECT_TRUE(entry->second->GetAsString(&str_value)); | 113 EXPECT_TRUE(entry->second->GetAsString(&str_value)); |
218 EXPECT_EQ("http://foo.com", str_value); | 114 EXPECT_EQ("http://foo.com", str_value); |
219 } | 115 } |
OLD | NEW |