| 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/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 122 } |
| 123 #endif | 123 #endif |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Test that all versions of the Override function of PathService do what they | 126 // Test that all versions of the Override function of PathService do what they |
| 127 // are supposed to do. | 127 // are supposed to do. |
| 128 TEST_F(PathServiceTest, Override) { | 128 TEST_F(PathServiceTest, Override) { |
| 129 int my_special_key = 666; | 129 int my_special_key = 666; |
| 130 ScopedTempDir temp_dir; | 130 ScopedTempDir temp_dir; |
| 131 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 131 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 132 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); | 132 FilePath fake_cache_dir(temp_dir.GetPath().AppendASCII("cache")); |
| 133 // PathService::Override should always create the path provided if it doesn't | 133 // PathService::Override should always create the path provided if it doesn't |
| 134 // exist. | 134 // exist. |
| 135 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); | 135 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
| 136 EXPECT_TRUE(PathExists(fake_cache_dir)); | 136 EXPECT_TRUE(PathExists(fake_cache_dir)); |
| 137 | 137 |
| 138 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); | 138 FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("cache2")); |
| 139 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. | 139 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
| 140 PathService::OverrideAndCreateIfNeeded(my_special_key, | 140 PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 141 fake_cache_dir2, | 141 fake_cache_dir2, |
| 142 false, | 142 false, |
| 143 false); | 143 false); |
| 144 EXPECT_FALSE(PathExists(fake_cache_dir2)); | 144 EXPECT_FALSE(PathExists(fake_cache_dir2)); |
| 145 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | 145 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 146 fake_cache_dir2, | 146 fake_cache_dir2, |
| 147 false, | 147 false, |
| 148 true)); | 148 true)); |
| 149 EXPECT_TRUE(PathExists(fake_cache_dir2)); | 149 EXPECT_TRUE(PathExists(fake_cache_dir2)); |
| 150 | 150 |
| 151 #if defined(OS_POSIX) | 151 #if defined(OS_POSIX) |
| 152 FilePath non_existent( | 152 FilePath non_existent( |
| 153 MakeAbsoluteFilePath(temp_dir.path()).AppendASCII("non_existent")); | 153 MakeAbsoluteFilePath(temp_dir.GetPath()).AppendASCII("non_existent")); |
| 154 EXPECT_TRUE(non_existent.IsAbsolute()); | 154 EXPECT_TRUE(non_existent.IsAbsolute()); |
| 155 EXPECT_FALSE(PathExists(non_existent)); | 155 EXPECT_FALSE(PathExists(non_existent)); |
| 156 #if !defined(OS_ANDROID) | 156 #if !defined(OS_ANDROID) |
| 157 // This fails because MakeAbsoluteFilePath fails for non-existent files. | 157 // This fails because MakeAbsoluteFilePath fails for non-existent files. |
| 158 // Earlier versions of Bionic libc don't fail for non-existent files, so | 158 // Earlier versions of Bionic libc don't fail for non-existent files, so |
| 159 // skip this check on Android. | 159 // skip this check on Android. |
| 160 EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key, | 160 EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 161 non_existent, | 161 non_existent, |
| 162 false, | 162 false, |
| 163 false)); | 163 false)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 EXPECT_TRUE(PathService::Get(my_special_key, &path)); | 174 EXPECT_TRUE(PathService::Get(my_special_key, &path)); |
| 175 EXPECT_EQ(non_existent, path); | 175 EXPECT_EQ(non_existent, path); |
| 176 #endif | 176 #endif |
| 177 } | 177 } |
| 178 | 178 |
| 179 // Check if multiple overrides can co-exist. | 179 // Check if multiple overrides can co-exist. |
| 180 TEST_F(PathServiceTest, OverrideMultiple) { | 180 TEST_F(PathServiceTest, OverrideMultiple) { |
| 181 int my_special_key = 666; | 181 int my_special_key = 666; |
| 182 ScopedTempDir temp_dir; | 182 ScopedTempDir temp_dir; |
| 183 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 183 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 184 FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); | 184 FilePath fake_cache_dir1(temp_dir.GetPath().AppendASCII("1")); |
| 185 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); | 185 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); |
| 186 EXPECT_TRUE(PathExists(fake_cache_dir1)); | 186 EXPECT_TRUE(PathExists(fake_cache_dir1)); |
| 187 ASSERT_EQ(1, WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); | 187 ASSERT_EQ(1, WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); |
| 188 | 188 |
| 189 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); | 189 FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("2")); |
| 190 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); | 190 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); |
| 191 EXPECT_TRUE(PathExists(fake_cache_dir2)); | 191 EXPECT_TRUE(PathExists(fake_cache_dir2)); |
| 192 ASSERT_EQ(1, WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); | 192 ASSERT_EQ(1, WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); |
| 193 | 193 |
| 194 FilePath result; | 194 FilePath result; |
| 195 EXPECT_TRUE(PathService::Get(my_special_key, &result)); | 195 EXPECT_TRUE(PathService::Get(my_special_key, &result)); |
| 196 // Override might have changed the path representation but our test file | 196 // Override might have changed the path representation but our test file |
| 197 // should be still there. | 197 // should be still there. |
| 198 EXPECT_TRUE(PathExists(result.AppendASCII("t1"))); | 198 EXPECT_TRUE(PathExists(result.AppendASCII("t1"))); |
| 199 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); | 199 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); |
| 200 EXPECT_TRUE(PathExists(result.AppendASCII("t2"))); | 200 EXPECT_TRUE(PathExists(result.AppendASCII("t2"))); |
| 201 } | 201 } |
| 202 | 202 |
| 203 TEST_F(PathServiceTest, RemoveOverride) { | 203 TEST_F(PathServiceTest, RemoveOverride) { |
| 204 // Before we start the test we have to call RemoveOverride at least once to | 204 // Before we start the test we have to call RemoveOverride at least once to |
| 205 // clear any overrides that might have been left from other tests. | 205 // clear any overrides that might have been left from other tests. |
| 206 PathService::RemoveOverride(DIR_TEMP); | 206 PathService::RemoveOverride(DIR_TEMP); |
| 207 | 207 |
| 208 FilePath original_user_data_dir; | 208 FilePath original_user_data_dir; |
| 209 EXPECT_TRUE(PathService::Get(DIR_TEMP, &original_user_data_dir)); | 209 EXPECT_TRUE(PathService::Get(DIR_TEMP, &original_user_data_dir)); |
| 210 EXPECT_FALSE(PathService::RemoveOverride(DIR_TEMP)); | 210 EXPECT_FALSE(PathService::RemoveOverride(DIR_TEMP)); |
| 211 | 211 |
| 212 ScopedTempDir temp_dir; | 212 ScopedTempDir temp_dir; |
| 213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 214 EXPECT_TRUE(PathService::Override(DIR_TEMP, temp_dir.path())); | 214 EXPECT_TRUE(PathService::Override(DIR_TEMP, temp_dir.GetPath())); |
| 215 FilePath new_user_data_dir; | 215 FilePath new_user_data_dir; |
| 216 EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir)); | 216 EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir)); |
| 217 EXPECT_NE(original_user_data_dir, new_user_data_dir); | 217 EXPECT_NE(original_user_data_dir, new_user_data_dir); |
| 218 | 218 |
| 219 EXPECT_TRUE(PathService::RemoveOverride(DIR_TEMP)); | 219 EXPECT_TRUE(PathService::RemoveOverride(DIR_TEMP)); |
| 220 EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir)); | 220 EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir)); |
| 221 EXPECT_EQ(original_user_data_dir, new_user_data_dir); | 221 EXPECT_EQ(original_user_data_dir, new_user_data_dir); |
| 222 } | 222 } |
| 223 | 223 |
| 224 #if defined(OS_WIN) | 224 #if defined(OS_WIN) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432, | 267 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432, |
| 268 &programfiles_dir)); | 268 &programfiles_dir)); |
| 269 EXPECT_EQ(programfiles_dir.value(), | 269 EXPECT_EQ(programfiles_dir.value(), |
| 270 FILE_PATH_LITERAL("C:\\Program Files")); | 270 FILE_PATH_LITERAL("C:\\Program Files")); |
| 271 } | 271 } |
| 272 #endif | 272 #endif |
| 273 } | 273 } |
| 274 #endif | 274 #endif |
| 275 | 275 |
| 276 } // namespace base | 276 } // namespace base |
| OLD | NEW |