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_path.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/file_path.h" | 10 #include "base/files/scoped_temp_dir.h" |
10 #include "base/scoped_temp_dir.h" | |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "testing/gtest/include/gtest/gtest-spi.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/gtest/include/gtest/gtest-spi.h" | |
15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
16 | 16 |
17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
18 #include <userenv.h> | 18 #include <userenv.h> |
19 #include "base/win/windows_version.h" | 19 #include "base/win/windows_version.h" |
20 // userenv.dll is required for GetDefaultUserProfileDirectory(). | 20 // userenv.dll is required for GetDefaultUserProfileDirectory(). |
21 #pragma comment(lib, "userenv.lib") | 21 #pragma comment(lib, "userenv.lib") |
22 #endif | 22 #endif |
23 | 23 |
24 namespace { | 24 namespace { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 ++key) { | 132 ++key) { |
133 EXPECT_PRED1(ReturnsValidPath, key); | 133 EXPECT_PRED1(ReturnsValidPath, key); |
134 } | 134 } |
135 #endif | 135 #endif |
136 } | 136 } |
137 | 137 |
138 // test that all versions of the Override function of PathService do what they | 138 // test that all versions of the Override function of PathService do what they |
139 // are supposed to do. | 139 // are supposed to do. |
140 TEST_F(PathServiceTest, Override) { | 140 TEST_F(PathServiceTest, Override) { |
141 int my_special_key = 666; | 141 int my_special_key = 666; |
142 ScopedTempDir temp_dir; | 142 base::ScopedTempDir temp_dir; |
143 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 143 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
144 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); | 144 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
145 // PathService::Override should always create the path provided if it doesn't | 145 // PathService::Override should always create the path provided if it doesn't |
146 // exist. | 146 // exist. |
147 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); | 147 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
148 EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); | 148 EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); |
149 | 149 |
150 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); | 150 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
151 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. | 151 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
152 PathService::OverrideAndCreateIfNeeded(my_special_key, | 152 PathService::OverrideAndCreateIfNeeded(my_special_key, |
153 fake_cache_dir2, | 153 fake_cache_dir2, |
154 false); | 154 false); |
155 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); | 155 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); |
156 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | 156 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
157 fake_cache_dir2, | 157 fake_cache_dir2, |
158 true)); | 158 true)); |
159 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); | 159 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); |
160 } | 160 } |
161 | 161 |
162 // Check if multiple overrides can co-exist. | 162 // Check if multiple overrides can co-exist. |
163 TEST_F(PathServiceTest, OverrideMultiple) { | 163 TEST_F(PathServiceTest, OverrideMultiple) { |
164 int my_special_key = 666; | 164 int my_special_key = 666; |
165 ScopedTempDir temp_dir; | 165 base::ScopedTempDir temp_dir; |
166 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 166 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
167 FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); | 167 FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); |
168 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); | 168 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); |
169 EXPECT_TRUE(file_util::PathExists(fake_cache_dir1)); | 169 EXPECT_TRUE(file_util::PathExists(fake_cache_dir1)); |
170 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); | 170 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); |
171 | 171 |
172 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); | 172 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); |
173 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); | 173 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); |
174 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); | 174 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); |
175 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); | 175 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); |
176 | 176 |
177 FilePath result; | 177 FilePath result; |
178 EXPECT_TRUE(PathService::Get(my_special_key, &result)); | 178 EXPECT_TRUE(PathService::Get(my_special_key, &result)); |
179 // Override might have changed the path representation but our test file | 179 // Override might have changed the path representation but our test file |
180 // should be still there. | 180 // should be still there. |
181 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t1"))); | 181 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t1"))); |
182 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); | 182 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); |
183 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t2"))); | 183 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t2"))); |
184 } | 184 } |
185 | 185 |
186 TEST_F(PathServiceTest, RemoveOverride) { | 186 TEST_F(PathServiceTest, RemoveOverride) { |
187 // Before we start the test we have to call RemoveOverride at least once to | 187 // Before we start the test we have to call RemoveOverride at least once to |
188 // clear any overrides that might have been left from other tests. | 188 // clear any overrides that might have been left from other tests. |
189 PathService::RemoveOverride(base::DIR_TEMP); | 189 PathService::RemoveOverride(base::DIR_TEMP); |
190 | 190 |
191 FilePath original_user_data_dir; | 191 FilePath original_user_data_dir; |
192 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); | 192 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); |
193 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); | 193 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); |
194 | 194 |
195 ScopedTempDir temp_dir; | 195 base::ScopedTempDir temp_dir; |
196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
197 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); | 197 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); |
198 FilePath new_user_data_dir; | 198 FilePath new_user_data_dir; |
199 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 199 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
200 EXPECT_NE(original_user_data_dir, new_user_data_dir); | 200 EXPECT_NE(original_user_data_dir, new_user_data_dir); |
201 | 201 |
202 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); | 202 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); |
203 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 203 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
204 EXPECT_EQ(original_user_data_dir, new_user_data_dir); | 204 EXPECT_EQ(original_user_data_dir, new_user_data_dir); |
205 } | 205 } |
OLD | NEW |