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/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/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) | |
18 #include "base/win/windows_version.h" | |
19 #endif | |
20 | |
21 namespace { | 17 namespace { |
22 | 18 |
23 // Returns true if PathService::Get returns true and sets the path parameter | 19 // Returns true if PathService::Get returns true and sets the path parameter |
24 // to non-empty for the given PathService::DirType enumeration value. | 20 // to non-empty for the given PathService::DirType enumeration value. |
25 bool ReturnsValidPath(int dir_type) { | 21 bool ReturnsValidPath(int dir_type) { |
26 base::FilePath path; | 22 base::FilePath path; |
27 bool result = PathService::Get(dir_type, &path); | 23 bool result = PathService::Get(dir_type, &path); |
28 | 24 |
29 // Some paths might not exist on some platforms in which case confirming | 25 // Some paths might not exist on some platforms in which case confirming |
30 // |result| is true and !path.empty() is the best we can do. | 26 // |result| is true and !path.empty() is the best we can do. |
31 bool check_path_exists = true; | 27 bool check_path_exists = true; |
32 #if defined(OS_POSIX) | 28 #if defined(OS_POSIX) |
33 // If chromium has never been started on this account, the cache path may not | 29 // If chromium has never been started on this account, the cache path may not |
34 // exist. | 30 // exist. |
35 if (dir_type == base::DIR_CACHE) | 31 if (dir_type == base::DIR_CACHE) |
36 check_path_exists = false; | 32 check_path_exists = false; |
37 #endif | 33 #endif |
38 #if defined(OS_LINUX) | 34 #if defined(OS_LINUX) |
39 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop), | 35 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop), |
40 // but it doesn't exist. | 36 // but it doesn't exist. |
41 if (dir_type == base::DIR_USER_DESKTOP) | 37 if (dir_type == base::DIR_USER_DESKTOP) |
42 check_path_exists = false; | 38 check_path_exists = false; |
43 #endif | 39 #endif |
44 #if defined(OS_WIN) | |
45 if (dir_type == base::DIR_TASKBAR_PINS) { | |
46 // There is no pinned-to-taskbar shortcuts prior to Win7. | |
47 if (base::win::GetVersion() < base::win::VERSION_WIN7) | |
48 check_path_exists = false; | |
49 } | |
50 #endif | |
51 #if defined(OS_MACOSX) | 40 #if defined(OS_MACOSX) |
52 if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE && | 41 if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE && |
53 dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) { | 42 dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) { |
54 if (path.ReferencesParent()) | 43 if (path.ReferencesParent()) |
55 return false; | 44 return false; |
56 } | 45 } |
57 #else | 46 #else |
58 if (path.ReferencesParent()) | 47 if (path.ReferencesParent()) |
59 return false; | 48 return false; |
60 #endif | 49 #endif |
61 return result && !path.empty() && (!check_path_exists || | 50 return result && !path.empty() && (!check_path_exists || |
62 base::PathExists(path)); | 51 base::PathExists(path)); |
63 } | 52 } |
64 | 53 |
65 #if defined(OS_WIN) | |
66 // Function to test any directory keys that are not supported on some versions | |
67 // of Windows. Checks that the function fails and that the returned path is | |
68 // empty. | |
69 bool ReturnsInvalidPath(int dir_type) { | |
70 base::FilePath path; | |
71 bool result = PathService::Get(dir_type, &path); | |
72 return !result && path.empty(); | |
73 } | |
74 #endif | |
75 | |
76 } // namespace | 54 } // namespace |
77 | 55 |
78 // On the Mac this winds up using some autoreleased objects, so we need to | 56 // On the Mac this winds up using some autoreleased objects, so we need to |
79 // be a PlatformTest. | 57 // be a PlatformTest. |
80 typedef PlatformTest PathServiceTest; | 58 typedef PlatformTest PathServiceTest; |
81 | 59 |
82 // Test that all PathService::Get calls return a value and a true result | 60 // Test that all PathService::Get calls return a value and a true result |
83 // in the development environment. (This test was created because a few | 61 // in the development environment. (This test was created because a few |
84 // later changes to Get broke the semantics of the function and yielded the | 62 // later changes to Get broke the semantics of the function and yielded the |
85 // correct value while returning false.) | 63 // correct value while returning false.) |
86 TEST_F(PathServiceTest, Get) { | 64 TEST_F(PathServiceTest, Get) { |
87 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { | 65 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { |
88 #if defined(OS_ANDROID) | 66 #if defined(OS_ANDROID) |
89 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP || | 67 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP || |
90 key == base::DIR_HOME) | 68 key == base::DIR_HOME) |
91 continue; // Android doesn't implement these. | 69 continue; // Android doesn't implement these. |
92 #elif defined(OS_IOS) | 70 #elif defined(OS_IOS) |
93 if (key == base::DIR_USER_DESKTOP) | 71 if (key == base::DIR_USER_DESKTOP) |
94 continue; // iOS doesn't implement DIR_USER_DESKTOP; | 72 continue; // iOS doesn't implement DIR_USER_DESKTOP; |
95 #endif | 73 #endif |
96 EXPECT_PRED1(ReturnsValidPath, key); | 74 EXPECT_PRED1(ReturnsValidPath, key); |
97 } | 75 } |
98 #if defined(OS_WIN) | 76 #if defined(OS_MACOSX) |
99 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { | |
100 bool valid = true; | |
101 if (key == base::DIR_APP_SHORTCUTS) | |
102 valid = base::win::GetVersion() >= base::win::VERSION_WIN8; | |
103 | |
104 if (valid) | |
105 EXPECT_TRUE(ReturnsValidPath(key)) << key; | |
106 else | |
107 EXPECT_TRUE(ReturnsInvalidPath(key)) << key; | |
108 } | |
109 #elif defined(OS_MACOSX) | |
110 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { | 77 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { |
111 EXPECT_PRED1(ReturnsValidPath, key); | 78 EXPECT_PRED1(ReturnsValidPath, key); |
112 } | 79 } |
113 #elif defined(OS_ANDROID) | 80 #elif defined(OS_ANDROID) |
114 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END; | 81 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END; |
115 ++key) { | 82 ++key) { |
116 EXPECT_PRED1(ReturnsValidPath, key); | 83 EXPECT_PRED1(ReturnsValidPath, key); |
117 } | 84 } |
118 #elif defined(OS_POSIX) | 85 #elif defined(OS_POSIX) |
119 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; | 86 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 180 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
214 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); | 181 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); |
215 base::FilePath new_user_data_dir; | 182 base::FilePath new_user_data_dir; |
216 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 183 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
217 EXPECT_NE(original_user_data_dir, new_user_data_dir); | 184 EXPECT_NE(original_user_data_dir, new_user_data_dir); |
218 | 185 |
219 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); | 186 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); |
220 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | 187 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
221 EXPECT_EQ(original_user_data_dir, new_user_data_dir); | 188 EXPECT_EQ(original_user_data_dir, new_user_data_dir); |
222 } | 189 } |
223 | |
224 #if defined(OS_WIN) | |
225 TEST_F(PathServiceTest, GetProgramFiles) { | |
226 base::FilePath programfiles_dir; | |
227 #if defined(_WIN64) | |
228 // 64-bit on 64-bit. | |
229 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES, | |
230 &programfiles_dir)); | |
231 EXPECT_EQ(programfiles_dir.value(), | |
232 FILE_PATH_LITERAL("C:\\Program Files")); | |
233 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILESX86, | |
234 &programfiles_dir)); | |
235 EXPECT_EQ(programfiles_dir.value(), | |
236 FILE_PATH_LITERAL("C:\\Program Files (x86)")); | |
237 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES6432, | |
238 &programfiles_dir)); | |
239 EXPECT_EQ(programfiles_dir.value(), | |
240 FILE_PATH_LITERAL("C:\\Program Files")); | |
241 #else | |
242 if (base::win::OSInfo::GetInstance()->wow64_status() == | |
243 base::win::OSInfo::WOW64_ENABLED) { | |
244 // 32-bit on 64-bit. | |
245 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES, | |
246 &programfiles_dir)); | |
247 EXPECT_EQ(programfiles_dir.value(), | |
248 FILE_PATH_LITERAL("C:\\Program Files (x86)")); | |
249 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILESX86, | |
250 &programfiles_dir)); | |
251 EXPECT_EQ(programfiles_dir.value(), | |
252 FILE_PATH_LITERAL("C:\\Program Files (x86)")); | |
253 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES6432, | |
254 &programfiles_dir)); | |
255 EXPECT_EQ(programfiles_dir.value(), | |
256 FILE_PATH_LITERAL("C:\\Program Files")); | |
257 } else { | |
258 // 32-bit on 32-bit. | |
259 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES, | |
260 &programfiles_dir)); | |
261 EXPECT_EQ(programfiles_dir.value(), | |
262 FILE_PATH_LITERAL("C:\\Program Files")); | |
263 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILESX86, | |
264 &programfiles_dir)); | |
265 EXPECT_EQ(programfiles_dir.value(), | |
266 FILE_PATH_LITERAL("C:\\Program Files")); | |
267 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES6432, | |
268 &programfiles_dir)); | |
269 EXPECT_EQ(programfiles_dir.value(), | |
270 FILE_PATH_LITERAL("C:\\Program Files")); | |
271 } | |
272 #endif | |
273 } | |
274 #endif | |
OLD | NEW |