[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame^] | 1 | // Copyright (c) 2011 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] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 10 | #if defined(OS_WIN) |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 11 | #include "base/win/windows_version.h" |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 12 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest-spi.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 15 | #include "testing/platform_test.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
17 | namespace { | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | |
19 | // Returns true if PathService::Get returns true and sets the path parameter | ||||
20 | // to non-empty for the given PathService::DirType enumeration value. | ||||
21 | bool ReturnsValidPath(int dir_type) { | ||||
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 22 | FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | bool result = PathService::Get(dir_type, &path); |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 24 | #if defined(OS_POSIX) |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 25 | // 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] | 26 | // exist. |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 27 | if (dir_type == base::DIR_CACHE) |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 28 | return result && !path.value().empty(); |
29 | #endif | ||||
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 30 | return result && !path.value().empty() && file_util::PathExists(path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | } |
32 | |||||
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 33 | #if defined(OS_WIN) |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 34 | // Function to test DIR_LOCAL_APP_DATA_LOW on Windows XP. Make sure it fails. |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 35 | bool ReturnsInvalidPath(int dir_type) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 36 | FilePath path; |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 37 | bool result = PathService::Get(base::DIR_LOCAL_APP_DATA_LOW, &path); |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 38 | return !result && path.empty(); |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 39 | } |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 40 | #endif |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 41 | |
42 | } // namespace | ||||
43 | |||||
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 44 | // On the Mac this winds up using some autoreleased objects, so we need to |
45 | // be a PlatformTest. | ||||
46 | typedef PlatformTest PathServiceTest; | ||||
47 | |||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | // Test that all PathService::Get calls return a value and a true result |
49 | // in the development environment. (This test was created because a few | ||||
50 | // later changes to Get broke the semantics of the function and yielded the | ||||
51 | // correct value while returning false.) | ||||
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 52 | TEST_F(PathServiceTest, Get) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | for (int key = base::DIR_CURRENT; key < base::PATH_END; ++key) { |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame^] | 54 | #if defined(OS_ANDROID) |
55 | if (key == base::FILE_MODULE) | ||||
56 | continue; // Android doesn't implement FILE_MODULE; | ||||
57 | #endif | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | EXPECT_PRED1(ReturnsValidPath, key); |
59 | } | ||||
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 60 | #if defined(OS_WIN) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 61 | for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 62 | if (key == base::DIR_LOCAL_APP_DATA_LOW && |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 63 | base::win::GetVersion() < base::win::VERSION_VISTA) { |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 64 | // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected to |
65 | // fail. | ||||
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 66 | EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 67 | } else { |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 68 | EXPECT_TRUE(ReturnsValidPath(key)) << key; |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 69 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 70 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 71 | #elif defined(OS_MACOSX) |
72 | for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { | ||||
73 | EXPECT_PRED1(ReturnsValidPath, key); | ||||
74 | } | ||||
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 75 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | } |