Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: base/path_service_unittest.cc

Issue 12286020: Replace FilePath with base::FilePath. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/i18n/file_util_icu.cc ('k') | base/perftimer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/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-spi.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.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 {
25 25
26 // Returns true if PathService::Get returns true and sets the path parameter 26 // Returns true if PathService::Get returns true and sets the path parameter
27 // to non-empty for the given PathService::DirType enumeration value. 27 // to non-empty for the given PathService::DirType enumeration value.
28 bool ReturnsValidPath(int dir_type) { 28 bool ReturnsValidPath(int dir_type) {
29 FilePath path; 29 base::FilePath path;
30 bool result = PathService::Get(dir_type, &path); 30 bool result = PathService::Get(dir_type, &path);
31 31
32 // Some paths might not exist on some platforms in which case confirming 32 // Some paths might not exist on some platforms in which case confirming
33 // |result| is true and !path.empty() is the best we can do. 33 // |result| is true and !path.empty() is the best we can do.
34 bool check_path_exists = true; 34 bool check_path_exists = true;
35 #if defined(OS_POSIX) 35 #if defined(OS_POSIX)
36 // If chromium has never been started on this account, the cache path may not 36 // If chromium has never been started on this account, the cache path may not
37 // exist. 37 // exist.
38 if (dir_type == base::DIR_CACHE) 38 if (dir_type == base::DIR_CACHE)
39 check_path_exists = false; 39 check_path_exists = false;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 file_util::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 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();
87 } 87 }
88 #endif 88 #endif
89 89
90 } // namespace 90 } // namespace
91 91
92 // On the Mac this winds up using some autoreleased objects, so we need to 92 // On the Mac this winds up using some autoreleased objects, so we need to
93 // be a PlatformTest. 93 // be a PlatformTest.
94 typedef PlatformTest PathServiceTest; 94 typedef PlatformTest PathServiceTest;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } 145 }
146 #endif 146 #endif
147 } 147 }
148 148
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 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(file_util::PathExists(fake_cache_dir));
160 160
161 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(file_util::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(file_util::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 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(file_util::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 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(file_util::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 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(file_util::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(file_util::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 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 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 }
OLDNEW
« no previous file with comments | « base/i18n/file_util_icu.cc ('k') | base/perftimer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698