pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 5 | #include "base/path_service.h" |
| 6 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | #include "base/basictypes.h" |
| 8 | #include "base/file_util.h" |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 9 | #include "base/file_path.h" |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 10 | #include "base/scoped_temp_dir.h" |
gab@chromium.org | 4011fea | 2012-09-20 09:13:40 +0900 | [diff] [blame] | 11 | #if defined(OS_WIN) |
| 12 | #include "base/win/windows_version.h" |
| 13 | #endif |
wjia@chromium.org | 9d594d1 | 2012-09-20 10:59:36 +0900 | [diff] [blame^] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | #include "testing/gtest/include/gtest/gtest-spi.h" |
| 16 | #include "testing/platform_test.h" |
gab@chromium.org | 4011fea | 2012-09-20 09:13:40 +0900 | [diff] [blame] | 17 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 18 | namespace { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | |
| 20 | // Returns true if PathService::Get returns true and sets the path parameter |
| 21 | // to non-empty for the given PathService::DirType enumeration value. |
| 22 | bool ReturnsValidPath(int dir_type) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 23 | FilePath path; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 24 | bool result = PathService::Get(dir_type, &path); |
evan@chromium.org | 9e3973b | 2010-11-24 07:39:30 +0900 | [diff] [blame] | 25 | #if defined(OS_POSIX) |
viettrungluu@chromium.org | 366266c | 2010-11-24 11:23:15 +0900 | [diff] [blame] | 26 | // If chromium has never been started on this account, the cache path may not |
evan@chromium.org | 9e3973b | 2010-11-24 07:39:30 +0900 | [diff] [blame] | 27 | // exist. |
viettrungluu@chromium.org | 366266c | 2010-11-24 11:23:15 +0900 | [diff] [blame] | 28 | if (dir_type == base::DIR_CACHE) |
wjia@chromium.org | 9d594d1 | 2012-09-20 10:59:36 +0900 | [diff] [blame^] | 29 | return result && !path.value().empty(); |
evan@chromium.org | 9e3973b | 2010-11-24 07:39:30 +0900 | [diff] [blame] | 30 | #endif |
wjia@chromium.org | 9d594d1 | 2012-09-20 10:59:36 +0900 | [diff] [blame^] | 31 | return result && !path.value().empty() && file_util::PathExists(path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 32 | } |
| 33 | |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 34 | #if defined(OS_WIN) |
benwells@chromium.org | bd09d67 | 2012-08-30 18:16:55 +0900 | [diff] [blame] | 35 | // 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 |
| 37 | // empty. |
maruel@google.com | 3117767 | 2008-08-08 08:59:04 +0900 | [diff] [blame] | 38 | bool ReturnsInvalidPath(int dir_type) { |
phajdan.jr@chromium.org | c800858 | 2009-09-15 04:36:31 +0900 | [diff] [blame] | 39 | FilePath path; |
benwells@chromium.org | bd09d67 | 2012-08-30 18:16:55 +0900 | [diff] [blame] | 40 | bool result = PathService::Get(dir_type, &path); |
maruel@google.com | 3117767 | 2008-08-08 08:59:04 +0900 | [diff] [blame] | 41 | return !result && path.empty(); |
maruel@google.com | 15cfc7f | 2008-08-08 05:23:09 +0900 | [diff] [blame] | 42 | } |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 43 | #endif |
maruel@google.com | 15cfc7f | 2008-08-08 05:23:09 +0900 | [diff] [blame] | 44 | |
| 45 | } // namespace |
| 46 | |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 47 | // On the Mac this winds up using some autoreleased objects, so we need to |
| 48 | // be a PlatformTest. |
| 49 | typedef PlatformTest PathServiceTest; |
| 50 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 51 | // 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 |
| 53 | // later changes to Get broke the semantics of the function and yielded the |
| 54 | // correct value while returning false.) |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 55 | TEST_F(PathServiceTest, Get) { |
wjia@chromium.org | 9d594d1 | 2012-09-20 10:59:36 +0900 | [diff] [blame^] | 56 | for (int key = base::DIR_CURRENT; key < base::PATH_END; ++key) { |
michaelbai@google.com | 4464bef | 2011-08-27 01:43:59 +0900 | [diff] [blame] | 57 | #if defined(OS_ANDROID) |
wjia@chromium.org | 9d594d1 | 2012-09-20 10:59:36 +0900 | [diff] [blame^] | 58 | if (key == base::FILE_MODULE) |
| 59 | continue; // Android doesn't implement FILE_MODULE; |
michaelbai@google.com | 4464bef | 2011-08-27 01:43:59 +0900 | [diff] [blame] | 60 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 61 | EXPECT_PRED1(ReturnsValidPath, key); |
| 62 | } |
mark@chromium.org | af9f62a | 2009-09-17 06:03:44 +0900 | [diff] [blame] | 63 | #if defined(OS_WIN) |
erikkay@google.com | 1d4507f | 2008-08-07 01:29:44 +0900 | [diff] [blame] | 64 | for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { |
benwells@chromium.org | bd09d67 | 2012-08-30 18:16:55 +0900 | [diff] [blame] | 65 | bool valid = true; |
| 66 | switch(key) { |
| 67 | case base::DIR_LOCAL_APP_DATA_LOW: |
| 68 | // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected |
| 69 | // to fail. |
| 70 | valid = base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 71 | break; |
| 72 | case base::DIR_APP_SHORTCUTS: |
| 73 | // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to |
| 74 | // fail. |
| 75 | valid = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 76 | break; |
maruel@google.com | 15cfc7f | 2008-08-08 05:23:09 +0900 | [diff] [blame] | 77 | } |
benwells@chromium.org | bd09d67 | 2012-08-30 18:16:55 +0900 | [diff] [blame] | 78 | |
| 79 | if (valid) |
| 80 | EXPECT_TRUE(ReturnsValidPath(key)) << key; |
| 81 | else |
| 82 | EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
erikkay@google.com | 1d4507f | 2008-08-07 01:29:44 +0900 | [diff] [blame] | 83 | } |
mark@chromium.org | af9f62a | 2009-09-17 06:03:44 +0900 | [diff] [blame] | 84 | #elif defined(OS_MACOSX) |
| 85 | for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { |
wjia@chromium.org | 9d594d1 | 2012-09-20 10:59:36 +0900 | [diff] [blame^] | 86 | EXPECT_PRED1(ReturnsValidPath, key); |
mark@chromium.org | af9f62a | 2009-09-17 06:03:44 +0900 | [diff] [blame] | 87 | } |
erikkay@google.com | 1d4507f | 2008-08-07 01:29:44 +0900 | [diff] [blame] | 88 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 89 | } |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 90 | |
| 91 | // test that all versions of the Override function of PathService do what they |
| 92 | // are supposed to do. |
| 93 | TEST_F(PathServiceTest, Override) { |
| 94 | int my_special_key = 666; |
| 95 | ScopedTempDir temp_dir; |
| 96 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 97 | FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
| 98 | // PathService::Override should always create the path provided if it doesn't |
| 99 | // exist. |
| 100 | EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
| 101 | EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); |
| 102 | |
| 103 | FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
| 104 | // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
| 105 | PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 106 | fake_cache_dir2, |
| 107 | false); |
| 108 | EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); |
| 109 | EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 110 | fake_cache_dir2, |
| 111 | true)); |
| 112 | EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); |
| 113 | } |