Chromium Code Reviews| 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 <userenv.h> | |
| 8 | |
| 7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 9 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 10 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 11 #if defined(OS_WIN) | 13 #include "base/string_util.h" |
| 12 #include "base/win/windows_version.h" | 14 #include "build/build_config.h" |
| 13 #endif | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/gtest/include/gtest/gtest-spi.h" | 16 #include "testing/gtest/include/gtest/gtest-spi.h" |
| 16 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 17 | 18 |
| 19 #if defined(OS_WIN) | |
| 20 #include "base/win/windows_version.h" | |
| 21 #endif | |
| 22 | |
| 23 // userenv.dll is required for GetDefaultUserProfileDirectory(). | |
| 24 #pragma comment(lib, "userenv.lib") | |
| 25 | |
| 18 namespace { | 26 namespace { |
| 19 | 27 |
| 20 // Returns true if PathService::Get returns true and sets the path parameter | 28 // Returns true if PathService::Get returns true and sets the path parameter |
| 21 // to non-empty for the given PathService::DirType enumeration value. | 29 // to non-empty for the given PathService::DirType enumeration value. |
| 22 bool ReturnsValidPath(int dir_type) { | 30 bool ReturnsValidPath(int dir_type) { |
| 23 FilePath path; | 31 FilePath path; |
| 24 bool result = PathService::Get(dir_type, &path); | 32 bool result = PathService::Get(dir_type, &path); |
| 25 #if defined(OS_POSIX) | 33 #if defined(OS_POSIX) |
| 26 // If chromium has never been started on this account, the cache path may not | 34 // If chromium has never been started on this account, the cache path may not |
| 27 // exist. | 35 // exist. |
| 28 if (dir_type == base::DIR_CACHE) | 36 if (dir_type == base::DIR_CACHE) |
| 29 return result && !path.value().empty(); | 37 return result && !path.empty(); |
| 30 #endif | 38 #endif |
| 31 return result && !path.value().empty() && file_util::PathExists(path); | 39 #if defined(OS_LINUX) |
| 40 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop), | |
| 41 // but it doesn't exist. | |
| 42 if (dir_type == base::DIR_USER_DESKTOP) | |
| 43 return result && !path.empty(); | |
| 44 #endif | |
| 45 #if defined(OS_WIN) | |
| 46 // On Windows XP, the Quick Launch folder for the "Default User" doesn't exist | |
| 47 // by default. At least confirm that the path returned begins with the | |
| 48 // Default User's profile path. | |
| 49 if (dir_type == base::DIR_DEFAULT_USER_QUICK_LAUNCH && | |
| 50 base::win::GetVersion() < base::win::VERSION_VISTA) { | |
| 51 wchar_t default_profile_path[MAX_PATH]; | |
| 52 DWORD size = MAX_PATH; | |
|
grt (UTC plus 2)
2012/09/20 04:33:42
MAX_PATH -> arraysize(default_profile_path)
gab
2012/09/20 12:30:32
Done.
| |
| 53 return (result && | |
| 54 ::GetDefaultUserProfileDirectory(default_profile_path, &size) && | |
| 55 StartsWith(path.value(), default_profile_path, false)); | |
| 56 } | |
| 57 #endif | |
| 58 return result && !path.empty() && file_util::PathExists(path); | |
| 32 } | 59 } |
| 33 | 60 |
| 34 #if defined(OS_WIN) | 61 #if defined(OS_WIN) |
| 35 // Function to test any directory keys that are not supported on some versions | 62 // Function to test any directory keys that are not supported on some versions |
| 36 // of Windows. Checks that the function fails and that the returned path is | 63 // of Windows. Checks that the function fails and that the returned path is |
| 37 // empty. | 64 // empty. |
| 38 bool ReturnsInvalidPath(int dir_type) { | 65 bool ReturnsInvalidPath(int dir_type) { |
| 39 FilePath path; | 66 FilePath path; |
| 40 bool result = PathService::Get(dir_type, &path); | 67 bool result = PathService::Get(dir_type, &path); |
| 41 return !result && path.empty(); | 68 return !result && path.empty(); |
| 42 } | 69 } |
| 43 #endif | 70 #endif |
| 44 | 71 |
| 45 } // namespace | 72 } // namespace |
| 46 | 73 |
| 47 // On the Mac this winds up using some autoreleased objects, so we need to | 74 // On the Mac this winds up using some autoreleased objects, so we need to |
| 48 // be a PlatformTest. | 75 // be a PlatformTest. |
| 49 typedef PlatformTest PathServiceTest; | 76 typedef PlatformTest PathServiceTest; |
| 50 | 77 |
| 51 // Test that all PathService::Get calls return a value and a true result | 78 // Test that all PathService::Get calls return a value and a true result |
| 52 // in the development environment. (This test was created because a few | 79 // in the development environment. (This test was created because a few |
| 53 // later changes to Get broke the semantics of the function and yielded the | 80 // later changes to Get broke the semantics of the function and yielded the |
| 54 // correct value while returning false.) | 81 // correct value while returning false.) |
| 55 TEST_F(PathServiceTest, Get) { | 82 TEST_F(PathServiceTest, Get) { |
| 56 for (int key = base::DIR_CURRENT; key < base::PATH_END; ++key) { | 83 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { |
| 57 #if defined(OS_ANDROID) | 84 #if defined(OS_ANDROID) |
| 58 if (key == base::FILE_MODULE) | 85 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP) |
| 59 continue; // Android doesn't implement FILE_MODULE; | 86 continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP; |
| 60 #endif | 87 #endif |
| 61 EXPECT_PRED1(ReturnsValidPath, key); | 88 EXPECT_PRED1(ReturnsValidPath, key); |
| 62 } | 89 } |
| 63 #if defined(OS_WIN) | 90 #if defined(OS_WIN) |
| 64 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { | 91 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { |
| 65 bool valid = true; | 92 bool valid = true; |
| 66 switch(key) { | 93 switch(key) { |
| 67 case base::DIR_LOCAL_APP_DATA_LOW: | 94 case base::DIR_LOCAL_APP_DATA_LOW: |
| 68 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected | 95 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected |
| 69 // to fail. | 96 // to fail. |
| 70 valid = base::win::GetVersion() >= base::win::VERSION_VISTA; | 97 valid = base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 71 break; | 98 break; |
| 72 case base::DIR_APP_SHORTCUTS: | 99 case base::DIR_APP_SHORTCUTS: |
| 73 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to | 100 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to |
| 74 // fail. | 101 // fail. |
| 75 valid = base::win::GetVersion() >= base::win::VERSION_WIN8; | 102 valid = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 76 break; | 103 break; |
| 77 } | 104 } |
| 78 | 105 |
| 79 if (valid) | 106 if (valid) |
| 80 EXPECT_TRUE(ReturnsValidPath(key)) << key; | 107 EXPECT_TRUE(ReturnsValidPath(key)) << key; |
| 81 else | 108 else |
| 82 EXPECT_TRUE(ReturnsInvalidPath(key)) << key; | 109 EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
| 83 } | 110 } |
| 84 #elif defined(OS_MACOSX) | 111 #elif defined(OS_MACOSX) |
| 85 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { | 112 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { |
| 86 EXPECT_PRED1(ReturnsValidPath, key); | 113 EXPECT_PRED1(ReturnsValidPath, key); |
| 114 } | |
| 115 #elif defined(OS_ANDROID) | |
| 116 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END; | |
| 117 ++key) { | |
| 118 EXPECT_PRED1(ReturnsValidPath, key); | |
| 119 } | |
| 120 #elif defined(OS_POSIX) | |
| 121 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; | |
| 122 ++key) { | |
| 123 EXPECT_PRED1(ReturnsValidPath, key); | |
| 87 } | 124 } |
| 88 #endif | 125 #endif |
| 89 } | 126 } |
| 90 | 127 |
| 91 // test that all versions of the Override function of PathService do what they | 128 // test that all versions of the Override function of PathService do what they |
| 92 // are supposed to do. | 129 // are supposed to do. |
| 93 TEST_F(PathServiceTest, Override) { | 130 TEST_F(PathServiceTest, Override) { |
| 94 int my_special_key = 666; | 131 int my_special_key = 666; |
| 95 ScopedTempDir temp_dir; | 132 ScopedTempDir temp_dir; |
| 96 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 97 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); | 134 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
| 98 // PathService::Override should always create the path provided if it doesn't | 135 // PathService::Override should always create the path provided if it doesn't |
| 99 // exist. | 136 // exist. |
| 100 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); | 137 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
| 101 EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); | 138 EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); |
| 102 | 139 |
| 103 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); | 140 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
| 104 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. | 141 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
| 105 PathService::OverrideAndCreateIfNeeded(my_special_key, | 142 PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 106 fake_cache_dir2, | 143 fake_cache_dir2, |
| 107 false); | 144 false); |
| 108 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); | 145 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); |
| 109 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | 146 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 110 fake_cache_dir2, | 147 fake_cache_dir2, |
| 111 true)); | 148 true)); |
| 112 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); | 149 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); |
| 113 } | 150 } |
| OLD | NEW |