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" |
12 | 13 |
13 class ConfigDirPolicyProviderTest : public testing::Test { | 14 using testing::Mock; |
| 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 { |
14 protected: | 25 protected: |
| 26 ConfigDirPolicyProviderTestBase() |
| 27 : ui_thread_(ChromeThread::UI, &loop_), |
| 28 file_thread_(ChromeThread::FILE, &loop_) {} |
| 29 |
15 virtual void SetUp() { | 30 virtual void SetUp() { |
16 // Determine the directory to use for testing. | 31 // Determine the directory to use for testing. |
17 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 32 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
18 test_dir_ = | 33 test_dir_ = |
19 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest")); | 34 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest")); |
20 | 35 |
21 // Make sure the directory is fresh. | 36 // Make sure the directory is fresh. |
22 file_util::Delete(test_dir_, true); | 37 file_util::Delete(test_dir_, true); |
23 file_util::CreateDirectory(test_dir_); | 38 file_util::CreateDirectory(test_dir_); |
24 ASSERT_TRUE(file_util::DirectoryExists(test_dir_)); | 39 ASSERT_TRUE(file_util::DirectoryExists(test_dir_)); |
25 | |
26 // Create a fresh policy store mock. | |
27 policy_store_.reset(new MockConfigurationPolicyStore()); | |
28 } | 40 } |
29 | 41 |
30 virtual void TearDown() { | 42 virtual void TearDown() { |
| 43 loop_.RunAllPending(); |
31 // Clean up test directory. | 44 // Clean up test directory. |
32 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | 45 ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
33 ASSERT_FALSE(file_util::PathExists(test_dir_)); | 46 ASSERT_FALSE(file_util::PathExists(test_dir_)); |
34 } | 47 } |
35 | 48 |
36 // JSON-encode a dictionary and write it to a file. | 49 // JSON-encode a dictionary and write it to a file. |
37 void WriteConfigFile(const DictionaryValue& dict, | 50 void WriteConfigFile(const DictionaryValue& dict, |
38 const std::string& file_name) { | 51 const std::string& file_name) { |
39 std::string data; | 52 std::string data; |
40 JSONStringValueSerializer serializer(&data); | 53 JSONStringValueSerializer serializer(&data); |
41 serializer.Serialize(dict); | 54 serializer.Serialize(dict); |
42 FilePath file_path(test_dir_.AppendASCII(file_name)); | 55 FilePath file_path(test_dir_.AppendASCII(file_name)); |
43 file_util::WriteFile(file_path, data.c_str(), data.size()); | 56 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); |
44 } | 57 } |
45 | 58 |
46 FilePath test_dir_; | 59 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 |
47 scoped_ptr<MockConfigurationPolicyStore> policy_store_; | 151 scoped_ptr<MockConfigurationPolicyStore> policy_store_; |
48 }; | 152 }; |
49 | 153 |
50 // The preferences dictionary is expected to be empty when there are no files to | 154 // The preferences dictionary is expected to be empty when there are no files to |
51 // load. | 155 // load. |
52 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) { | 156 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) { |
53 ConfigDirPolicyProvider provider(test_dir_); | 157 ConfigDirPolicyProvider provider(test_dir_); |
54 EXPECT_TRUE(provider.Provide(policy_store_.get())); | 158 EXPECT_TRUE(provider.Provide(policy_store_.get())); |
55 EXPECT_TRUE(policy_store_->policy_map().empty()); | 159 EXPECT_TRUE(policy_store_->policy_map().empty()); |
56 } | 160 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 policy_store_->policy_map()); | 210 policy_store_->policy_map()); |
107 EXPECT_EQ(1U, policy_map.size()); | 211 EXPECT_EQ(1U, policy_map.size()); |
108 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = | 212 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = |
109 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); | 213 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); |
110 ASSERT_TRUE(entry != policy_map.end()); | 214 ASSERT_TRUE(entry != policy_map.end()); |
111 | 215 |
112 std::string str_value; | 216 std::string str_value; |
113 EXPECT_TRUE(entry->second->GetAsString(&str_value)); | 217 EXPECT_TRUE(entry->second->GetAsString(&str_value)); |
114 EXPECT_EQ("http://foo.com", str_value); | 218 EXPECT_EQ("http://foo.com", str_value); |
115 } | 219 } |
OLD | NEW |