blob: 158b7457003d011a72e6a74088ef3fb2bf8a63ac [file] [log] [blame]
[email protected]cb571e752012-05-09 10:50:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]640517f2008-10-30 23:54:045#include "base/path_service.h"
6
initial.commitd7cae122008-07-26 21:49:387#include "base/basictypes.h"
8#include "base/file_util.h"
[email protected]640517f2008-10-30 23:54:049#include "base/file_path.h"
[email protected]cb571e752012-05-09 10:50:1010#include "base/scoped_temp_dir.h"
[email protected]dea1d7d2012-09-20 16:24:5211#include "base/string_util.h"
12#include "build/build_config.h"
[email protected]1f4ae162012-09-20 01:59:3613#include "testing/gtest/include/gtest/gtest.h"
14#include "testing/gtest/include/gtest/gtest-spi.h"
15#include "testing/platform_test.h"
[email protected]e5738a02012-09-20 00:13:4016
[email protected]dea1d7d2012-09-20 16:24:5217#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
23
initial.commitd7cae122008-07-26 21:49:3824namespace {
initial.commitd7cae122008-07-26 21:49:3825
26// Returns true if PathService::Get returns true and sets the path parameter
27// to non-empty for the given PathService::DirType enumeration value.
28bool ReturnsValidPath(int dir_type) {
[email protected]640517f2008-10-30 23:54:0429 FilePath path;
initial.commitd7cae122008-07-26 21:49:3830 bool result = PathService::Get(dir_type, &path);
[email protected]d8a80d62010-11-23 22:39:3031#if defined(OS_POSIX)
[email protected]b411da32010-11-24 02:23:1532 // If chromium has never been started on this account, the cache path may not
[email protected]d8a80d62010-11-23 22:39:3033 // exist.
[email protected]b411da32010-11-24 02:23:1534 if (dir_type == base::DIR_CACHE)
[email protected]dea1d7d2012-09-20 16:24:5235 return result && !path.empty();
[email protected]d8a80d62010-11-23 22:39:3036#endif
[email protected]dea1d7d2012-09-20 16:24:5237#if defined(OS_LINUX)
38 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
39 // but it doesn't exist.
40 if (dir_type == base::DIR_USER_DESKTOP)
41 return result && !path.empty();
42#endif
43#if defined(OS_WIN)
44 // On Windows XP, the Quick Launch folder for the "Default User" doesn't exist
45 // by default. At least confirm that the path returned begins with the
46 // Default User's profile path.
47 if (dir_type == base::DIR_DEFAULT_USER_QUICK_LAUNCH &&
48 base::win::GetVersion() < base::win::VERSION_VISTA) {
49 wchar_t default_profile_path[MAX_PATH];
50 DWORD size = arraysize(default_profile_path);
51 return (result &&
52 ::GetDefaultUserProfileDirectory(default_profile_path, &size) &&
53 StartsWith(path.value(), default_profile_path, false));
54 }
55#endif
56 return result && !path.empty() && file_util::PathExists(path);
initial.commitd7cae122008-07-26 21:49:3857}
58
[email protected]6723f832008-08-11 15:38:2759#if defined(OS_WIN)
[email protected]b2721b02012-08-30 09:16:5560// Function to test any directory keys that are not supported on some versions
61// of Windows. Checks that the function fails and that the returned path is
62// empty.
[email protected]0cfda1e2008-08-07 23:59:0463bool ReturnsInvalidPath(int dir_type) {
[email protected]b65de8b92009-09-14 19:36:3164 FilePath path;
[email protected]b2721b02012-08-30 09:16:5565 bool result = PathService::Get(dir_type, &path);
[email protected]0cfda1e2008-08-07 23:59:0466 return !result && path.empty();
[email protected]09ad1e622008-08-07 20:23:0967}
[email protected]6723f832008-08-11 15:38:2768#endif
[email protected]09ad1e622008-08-07 20:23:0969
70} // namespace
71
[email protected]ed2f2332008-08-20 15:59:4972// On the Mac this winds up using some autoreleased objects, so we need to
73// be a PlatformTest.
74typedef PlatformTest PathServiceTest;
75
initial.commitd7cae122008-07-26 21:49:3876// Test that all PathService::Get calls return a value and a true result
77// in the development environment. (This test was created because a few
78// later changes to Get broke the semantics of the function and yielded the
79// correct value while returning false.)
[email protected]ed2f2332008-08-20 15:59:4980TEST_F(PathServiceTest, Get) {
[email protected]dea1d7d2012-09-20 16:24:5281 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
[email protected]aa91eb92011-08-26 16:43:5982#if defined(OS_ANDROID)
[email protected]dea1d7d2012-09-20 16:24:5283 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP)
84 continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP;
[email protected]aa91eb92011-08-26 16:43:5985#endif
initial.commitd7cae122008-07-26 21:49:3886 EXPECT_PRED1(ReturnsValidPath, key);
87 }
[email protected]405a64b2009-09-16 21:03:4488#if defined(OS_WIN)
[email protected]1010f7d2008-08-06 16:29:4489 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
[email protected]b2721b02012-08-30 09:16:5590 bool valid = true;
91 switch(key) {
92 case base::DIR_LOCAL_APP_DATA_LOW:
93 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected
94 // to fail.
95 valid = base::win::GetVersion() >= base::win::VERSION_VISTA;
96 break;
97 case base::DIR_APP_SHORTCUTS:
98 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to
99 // fail.
100 valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
101 break;
[email protected]09ad1e622008-08-07 20:23:09102 }
[email protected]b2721b02012-08-30 09:16:55103
104 if (valid)
105 EXPECT_TRUE(ReturnsValidPath(key)) << key;
106 else
107 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
[email protected]1010f7d2008-08-06 16:29:44108 }
[email protected]405a64b2009-09-16 21:03:44109#elif defined(OS_MACOSX)
110 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
[email protected]dea1d7d2012-09-20 16:24:52111 EXPECT_PRED1(ReturnsValidPath, key);
112 }
113#elif defined(OS_ANDROID)
114 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END;
115 ++key) {
116 EXPECT_PRED1(ReturnsValidPath, key);
117 }
118#elif defined(OS_POSIX)
119 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END;
120 ++key) {
121 EXPECT_PRED1(ReturnsValidPath, key);
[email protected]405a64b2009-09-16 21:03:44122 }
[email protected]1010f7d2008-08-06 16:29:44123#endif
initial.commitd7cae122008-07-26 21:49:38124}
[email protected]cb571e752012-05-09 10:50:10125
126// test that all versions of the Override function of PathService do what they
127// are supposed to do.
128TEST_F(PathServiceTest, Override) {
129 int my_special_key = 666;
130 ScopedTempDir temp_dir;
131 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
132 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
133 // PathService::Override should always create the path provided if it doesn't
134 // exist.
135 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
136 EXPECT_TRUE(file_util::PathExists(fake_cache_dir));
137
138 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
139 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
140 PathService::OverrideAndCreateIfNeeded(my_special_key,
141 fake_cache_dir2,
142 false);
143 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2));
144 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
145 fake_cache_dir2,
146 true));
147 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2));
148}