OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/path_service.h" | 5 #include "base/path_service.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE && | 66 if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE && |
67 dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) { | 67 dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) { |
68 if (path.ReferencesParent()) | 68 if (path.ReferencesParent()) |
69 return false; | 69 return false; |
70 } | 70 } |
71 #else | 71 #else |
72 if (path.ReferencesParent()) | 72 if (path.ReferencesParent()) |
73 return false; | 73 return false; |
74 #endif | 74 #endif |
75 return result && !path.empty() && (!check_path_exists || | 75 return result && !path.empty() && (!check_path_exists || |
76 file_util::PathExists(path)); | 76 base::PathExists(path)); |
77 } | 77 } |
78 | 78 |
79 #if defined(OS_WIN) | 79 #if defined(OS_WIN) |
80 // Function to test any directory keys that are not supported on some versions | 80 // Function to test any directory keys that are not supported on some versions |
81 // of Windows. Checks that the function fails and that the returned path is | 81 // of Windows. Checks that the function fails and that the returned path is |
82 // empty. | 82 // empty. |
83 bool ReturnsInvalidPath(int dir_type) { | 83 bool ReturnsInvalidPath(int dir_type) { |
84 base::FilePath path; | 84 base::FilePath path; |
85 bool result = PathService::Get(dir_type, &path); | 85 bool result = PathService::Get(dir_type, &path); |
86 return !result && path.empty(); | 86 return !result && path.empty(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // test that all versions of the Override function of PathService do what they | 149 // test that all versions of the Override function of PathService do what they |
150 // are supposed to do. | 150 // are supposed to do. |
151 TEST_F(PathServiceTest, Override) { | 151 TEST_F(PathServiceTest, Override) { |
152 int my_special_key = 666; | 152 int my_special_key = 666; |
153 base::ScopedTempDir temp_dir; | 153 base::ScopedTempDir temp_dir; |
154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
155 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); | 155 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
156 // PathService::Override should always create the path provided if it doesn't | 156 // PathService::Override should always create the path provided if it doesn't |
157 // exist. | 157 // exist. |
158 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); | 158 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
159 EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); | 159 EXPECT_TRUE(base::PathExists(fake_cache_dir)); |
160 | 160 |
161 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); | 161 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
162 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. | 162 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
163 PathService::OverrideAndCreateIfNeeded(my_special_key, | 163 PathService::OverrideAndCreateIfNeeded(my_special_key, |
164 fake_cache_dir2, | 164 fake_cache_dir2, |
165 false); | 165 false); |
166 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); | 166 EXPECT_FALSE(base::PathExists(fake_cache_dir2)); |
167 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | 167 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
168 fake_cache_dir2, | 168 fake_cache_dir2, |
169 true)); | 169 true)); |
170 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); | 170 EXPECT_TRUE(base::PathExists(fake_cache_dir2)); |
171 } | 171 } |
172 | 172 |
173 // Check if multiple overrides can co-exist. | 173 // Check if multiple overrides can co-exist. |
174 TEST_F(PathServiceTest, OverrideMultiple) { | 174 TEST_F(PathServiceTest, OverrideMultiple) { |
175 int my_special_key = 666; | 175 int my_special_key = 666; |
176 base::ScopedTempDir temp_dir; | 176 base::ScopedTempDir temp_dir; |
177 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 177 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
178 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); | 178 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); |
179 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); | 179 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); |
180 EXPECT_TRUE(file_util::PathExists(fake_cache_dir1)); | 180 EXPECT_TRUE(base::PathExists(fake_cache_dir1)); |
181 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); | 181 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); |
182 | 182 |
183 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); | 183 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); |
184 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); | 184 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); |
185 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); | 185 EXPECT_TRUE(base::PathExists(fake_cache_dir2)); |
186 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); | 186 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); |
187 | 187 |
188 base::FilePath result; | 188 base::FilePath result; |
189 EXPECT_TRUE(PathService::Get(my_special_key, &result)); | 189 EXPECT_TRUE(PathService::Get(my_special_key, &result)); |
190 // Override might have changed the path representation but our test file | 190 // Override might have changed the path representation but our test file |
191 // should be still there. | 191 // should be still there. |
192 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t1"))); | 192 EXPECT_TRUE(base::PathExists(result.AppendASCII("t1"))); |
193 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); | 193 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); |
194 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t2"))); | 194 EXPECT_TRUE(base::PathExists(result.AppendASCII("t2"))); |
195 } | 195 } |
196 | 196 |
197 TEST_F(PathServiceTest, RemoveOverride) { | 197 TEST_F(PathServiceTest, RemoveOverride) { |
198 // Before we start the test we have to call RemoveOverride at least once to | 198 // Before we start the test we have to call RemoveOverride at least once to |
199 // clear any overrides that might have been left from other tests. | 199 // clear any overrides that might have been left from other tests. |
200 PathService::RemoveOverride(base::DIR_TEMP); | 200 PathService::RemoveOverride(base::DIR_TEMP); |
201 | 201 |
202 base::FilePath original_user_data_dir; | 202 base::FilePath original_user_data_dir; |
203 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); | 203 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); |
204 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); | 204 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); |
205 | 205 |
206 base::ScopedTempDir temp_dir; | 206 base::ScopedTempDir temp_dir; |
207 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 207 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
208 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); | 208 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); |
209 base::FilePath new_user_data_dir; | 209 base::FilePath new_user_data_dir; |
210 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 210 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
211 EXPECT_NE(original_user_data_dir, new_user_data_dir); | 211 EXPECT_NE(original_user_data_dir, new_user_data_dir); |
212 | 212 |
213 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); | 213 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); |
214 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 214 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
215 EXPECT_EQ(original_user_data_dir, new_user_data_dir); | 215 EXPECT_EQ(original_user_data_dir, new_user_data_dir); |
216 } | 216 } |
OLD | NEW |