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