| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 EXPECT_PRED1(ReturnsValidPath, key); | 140 EXPECT_PRED1(ReturnsValidPath, key); |
| 141 } | 141 } |
| 142 #elif defined(OS_POSIX) | 142 #elif defined(OS_POSIX) |
| 143 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; | 143 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; |
| 144 ++key) { | 144 ++key) { |
| 145 EXPECT_PRED1(ReturnsValidPath, key); | 145 EXPECT_PRED1(ReturnsValidPath, key); |
| 146 } | 146 } |
| 147 #endif | 147 #endif |
| 148 } | 148 } |
| 149 | 149 |
| 150 // test that all versions of the Override function of PathService do what they | 150 // Test that all versions of the Override function of PathService do what they |
| 151 // are supposed to do. | 151 // are supposed to do. |
| 152 TEST_F(PathServiceTest, Override) { | 152 TEST_F(PathServiceTest, Override) { |
| 153 int my_special_key = 666; | 153 int my_special_key = 666; |
| 154 base::ScopedTempDir temp_dir; | 154 base::ScopedTempDir temp_dir; |
| 155 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 155 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 156 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); | 156 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
| 157 // PathService::Override should always create the path provided if it doesn't | 157 // PathService::Override should always create the path provided if it doesn't |
| 158 // exist. | 158 // exist. |
| 159 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); | 159 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
| 160 EXPECT_TRUE(base::PathExists(fake_cache_dir)); | 160 EXPECT_TRUE(base::PathExists(fake_cache_dir)); |
| 161 | 161 |
| 162 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); | 162 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
| 163 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. | 163 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
| 164 PathService::OverrideAndCreateIfNeeded(my_special_key, | 164 PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 165 fake_cache_dir2, | 165 fake_cache_dir2, |
| 166 false, |
| 166 false); | 167 false); |
| 167 EXPECT_FALSE(base::PathExists(fake_cache_dir2)); | 168 EXPECT_FALSE(base::PathExists(fake_cache_dir2)); |
| 168 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | 169 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 169 fake_cache_dir2, | 170 fake_cache_dir2, |
| 171 false, |
| 170 true)); | 172 true)); |
| 171 EXPECT_TRUE(base::PathExists(fake_cache_dir2)); | 173 EXPECT_TRUE(base::PathExists(fake_cache_dir2)); |
| 174 |
| 175 #if defined(OS_POSIX) |
| 176 base::FilePath non_existent( |
| 177 base::MakeAbsoluteFilePath(temp_dir.path()).AppendASCII("non_existent")); |
| 178 EXPECT_TRUE(non_existent.IsAbsolute()); |
| 179 EXPECT_FALSE(base::PathExists(non_existent)); |
| 180 // This fails because MakeAbsoluteFilePath fails for non-existent files. |
| 181 EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 182 non_existent, |
| 183 false, |
| 184 false)); |
| 185 // This works because indicating that |non_existent| is absolute skips the |
| 186 // internal MakeAbsoluteFilePath call. |
| 187 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 188 non_existent, |
| 189 true, |
| 190 false)); |
| 191 // Check that the path has been overridden and no directory was created. |
| 192 EXPECT_FALSE(base::PathExists(non_existent)); |
| 193 base::FilePath path; |
| 194 EXPECT_TRUE(PathService::Get(my_special_key, &path)); |
| 195 EXPECT_EQ(non_existent, path); |
| 196 #endif |
| 172 } | 197 } |
| 173 | 198 |
| 174 // Check if multiple overrides can co-exist. | 199 // Check if multiple overrides can co-exist. |
| 175 TEST_F(PathServiceTest, OverrideMultiple) { | 200 TEST_F(PathServiceTest, OverrideMultiple) { |
| 176 int my_special_key = 666; | 201 int my_special_key = 666; |
| 177 base::ScopedTempDir temp_dir; | 202 base::ScopedTempDir temp_dir; |
| 178 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 179 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); | 204 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); |
| 180 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); | 205 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); |
| 181 EXPECT_TRUE(base::PathExists(fake_cache_dir1)); | 206 EXPECT_TRUE(base::PathExists(fake_cache_dir1)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 208 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 233 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 209 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); | 234 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); |
| 210 base::FilePath new_user_data_dir; | 235 base::FilePath new_user_data_dir; |
| 211 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 236 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
| 212 EXPECT_NE(original_user_data_dir, new_user_data_dir); | 237 EXPECT_NE(original_user_data_dir, new_user_data_dir); |
| 213 | 238 |
| 214 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); | 239 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); |
| 215 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 240 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
| 216 EXPECT_EQ(original_user_data_dir, new_user_data_dir); | 241 EXPECT_EQ(original_user_data_dir, new_user_data_dir); |
| 217 } | 242 } |
| OLD | NEW |