Revert 171679
[chromium-blink-merge.git] / base / path_service_unittest.cc
blob0b96c123e97abbdca9490c1e3edf593f120651ba
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
3 // found in the LICENSE file.
5 #include "base/path_service.h"
7 #include "base/basictypes.h"
8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/string_util.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest-spi.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
17 #if defined(OS_WIN)
18 #include <userenv.h>
19 #include "base/win/windows_version.h"
20 // userenv.dll is required for GetDefaultUserProfileDirectory().
21 #pragma comment(lib, "userenv.lib")
22 #endif
24 namespace {
26 // Returns true if PathService::Get returns true and sets the path parameter
27 // to non-empty for the given PathService::DirType enumeration value.
28 bool ReturnsValidPath(int dir_type) {
29 FilePath path;
30 bool result = PathService::Get(dir_type, &path);
31 // Some paths might not exist on some platforms in which case confirming
32 // |result| is true and !path.empty() is the best we can do.
33 bool check_path_exists = true;
34 #if defined(OS_POSIX)
35 // If chromium has never been started on this account, the cache path may not
36 // exist.
37 if (dir_type == base::DIR_CACHE)
38 check_path_exists = false;
39 #endif
40 #if defined(OS_LINUX)
41 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
42 // but it doesn't exist.
43 if (dir_type == base::DIR_USER_DESKTOP)
44 check_path_exists = false;
45 #endif
46 #if defined(OS_WIN)
47 if (dir_type == base::DIR_DEFAULT_USER_QUICK_LAUNCH) {
48 // On Windows XP, the Quick Launch folder for the "Default User" doesn't
49 // exist by default. At least confirm that the path returned begins with the
50 // Default User's profile path.
51 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
52 wchar_t default_profile_path[MAX_PATH];
53 DWORD size = arraysize(default_profile_path);
54 return (result &&
55 ::GetDefaultUserProfileDirectory(default_profile_path, &size) &&
56 StartsWith(path.value(), default_profile_path, false));
58 } else if (dir_type == base::DIR_TASKBAR_PINS) {
59 // There is no pinned-to-taskbar shortcuts prior to Win7.
60 if (base::win::GetVersion() < base::win::VERSION_WIN7)
61 check_path_exists = false;
63 #endif
64 return result && !path.empty() && (!check_path_exists ||
65 file_util::PathExists(path));
68 #if defined(OS_WIN)
69 // Function to test any directory keys that are not supported on some versions
70 // of Windows. Checks that the function fails and that the returned path is
71 // empty.
72 bool ReturnsInvalidPath(int dir_type) {
73 FilePath path;
74 bool result = PathService::Get(dir_type, &path);
75 return !result && path.empty();
77 #endif
79 } // namespace
81 // On the Mac this winds up using some autoreleased objects, so we need to
82 // be a PlatformTest.
83 typedef PlatformTest PathServiceTest;
85 // Test that all PathService::Get calls return a value and a true result
86 // in the development environment. (This test was created because a few
87 // later changes to Get broke the semantics of the function and yielded the
88 // correct value while returning false.)
89 TEST_F(PathServiceTest, Get) {
90 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
91 #if defined(OS_ANDROID)
92 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP)
93 continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP;
94 #elif defined(OS_IOS)
95 if (key == base::DIR_USER_DESKTOP)
96 continue; // iOS doesn't implement DIR_USER_DESKTOP;
97 #endif
98 EXPECT_PRED1(ReturnsValidPath, key);
100 #if defined(OS_WIN)
101 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
102 bool valid = true;
103 switch(key) {
104 case base::DIR_LOCAL_APP_DATA_LOW:
105 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected
106 // to fail.
107 valid = base::win::GetVersion() >= base::win::VERSION_VISTA;
108 break;
109 case base::DIR_APP_SHORTCUTS:
110 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to
111 // fail.
112 valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
113 break;
116 if (valid)
117 EXPECT_TRUE(ReturnsValidPath(key)) << key;
118 else
119 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
121 #elif defined(OS_MACOSX)
122 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
123 EXPECT_PRED1(ReturnsValidPath, key);
125 #elif defined(OS_ANDROID)
126 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END;
127 ++key) {
128 EXPECT_PRED1(ReturnsValidPath, key);
130 #elif defined(OS_POSIX)
131 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END;
132 ++key) {
133 EXPECT_PRED1(ReturnsValidPath, key);
135 #endif
138 // test that all versions of the Override function of PathService do what they
139 // are supposed to do.
140 TEST_F(PathServiceTest, Override) {
141 int my_special_key = 666;
142 base::ScopedTempDir temp_dir;
143 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
144 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
145 // PathService::Override should always create the path provided if it doesn't
146 // exist.
147 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
148 EXPECT_TRUE(file_util::PathExists(fake_cache_dir));
150 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
151 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
152 PathService::OverrideAndCreateIfNeeded(my_special_key,
153 fake_cache_dir2,
154 false);
155 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2));
156 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
157 fake_cache_dir2,
158 true));
159 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2));
162 // Check if multiple overrides can co-exist.
163 TEST_F(PathServiceTest, OverrideMultiple) {
164 int my_special_key = 666;
165 base::ScopedTempDir temp_dir;
166 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
167 FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
168 EXPECT_TRUE(PathService::Override(my_special_key, 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));
172 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
173 EXPECT_TRUE(PathService::Override(my_special_key + 1, 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));
177 FilePath result;
178 EXPECT_TRUE(PathService::Get(my_special_key, &result));
179 // Override might have changed the path representation but our test file
180 // should be still there.
181 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t1")));
182 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
183 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t2")));
186 TEST_F(PathServiceTest, RemoveOverride) {
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.
189 PathService::RemoveOverride(base::DIR_TEMP);
191 FilePath original_user_data_dir;
192 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir));
193 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP));
195 base::ScopedTempDir temp_dir;
196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
197 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path()));
198 FilePath 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);
202 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP));
203 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
204 EXPECT_EQ(original_user_data_dir, new_user_data_dir);