[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 5 | #include "base/path_service.h" |
| 6 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | #include "base/basictypes.h" |
| 8 | #include "base/file_util.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 9 | #include "base/file_path.h" |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 10 | #include "base/scoped_temp_dir.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 11 | #include "base/string_util.h" |
| 12 | #include "build/build_config.h" |
[email protected] | 1f4ae16 | 2012-09-20 01:59:36 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "testing/gtest/include/gtest/gtest-spi.h" |
| 15 | #include "testing/platform_test.h" |
[email protected] | e5738a0 | 2012-09-20 00:13:40 | [diff] [blame] | 16 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 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 |
| 23 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | |
| 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) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 29 | FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | bool result = PathService::Get(dir_type, &path); |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 31 | #if defined(OS_POSIX) |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 32 | // If chromium has never been started on this account, the cache path may not |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 33 | // exist. |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 34 | if (dir_type == base::DIR_CACHE) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 35 | return result && !path.empty(); |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 36 | #endif |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 37 | #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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 59 | #if defined(OS_WIN) |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 60 | // 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] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 63 | bool ReturnsInvalidPath(int dir_type) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 64 | FilePath path; |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 65 | bool result = PathService::Get(dir_type, &path); |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 66 | return !result && path.empty(); |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 67 | } |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 68 | #endif |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 69 | |
| 70 | } // namespace |
| 71 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 72 | // On the Mac this winds up using some autoreleased objects, so we need to |
| 73 | // be a PlatformTest. |
| 74 | typedef PlatformTest PathServiceTest; |
| 75 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | // 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] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 80 | TEST_F(PathServiceTest, Get) { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 81 | for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 82 | #if defined(OS_ANDROID) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 83 | if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP) |
| 84 | continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP; |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 85 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | EXPECT_PRED1(ReturnsValidPath, key); |
| 87 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 88 | #if defined(OS_WIN) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 89 | for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 90 | 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] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 102 | } |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 103 | |
| 104 | if (valid) |
| 105 | EXPECT_TRUE(ReturnsValidPath(key)) << key; |
| 106 | else |
| 107 | EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 108 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 109 | #elif defined(OS_MACOSX) |
| 110 | for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame^] | 111 | 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] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 122 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 123 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | } |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 125 | |
| 126 | // test that all versions of the Override function of PathService do what they |
| 127 | // are supposed to do. |
| 128 | TEST_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 | } |