WebKit merge 94748:94770
[chromium-blink-merge.git] / base / path_service_unittest.cc
blobec9cf281a94ce69ebce01effd2d7f73cb908d5c8
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/path_service.h"
7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/file_path.h"
10 #if defined(OS_WIN)
11 #include "base/win/windows_version.h"
12 #endif
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/gtest/include/gtest/gtest-spi.h"
15 #include "testing/platform_test.h"
17 namespace {
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) {
22 FilePath path;
23 bool result = PathService::Get(dir_type, &path);
24 #if defined(OS_POSIX)
25 // If chromium has never been started on this account, the cache path may not
26 // exist.
27 if (dir_type == base::DIR_CACHE)
28 return result && !path.value().empty();
29 #endif
30 return result && !path.value().empty() && file_util::PathExists(path);
33 #if defined(OS_WIN)
34 // Function to test DIR_LOCAL_APP_DATA_LOW on Windows XP. Make sure it fails.
35 bool ReturnsInvalidPath(int dir_type) {
36 FilePath path;
37 bool result = PathService::Get(base::DIR_LOCAL_APP_DATA_LOW, &path);
38 return !result && path.empty();
40 #endif
42 } // namespace
44 // On the Mac this winds up using some autoreleased objects, so we need to
45 // be a PlatformTest.
46 typedef PlatformTest PathServiceTest;
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.)
52 TEST_F(PathServiceTest, Get) {
53 for (int key = base::DIR_CURRENT; key < base::PATH_END; ++key) {
54 #if defined(OS_ANDROID)
55 if (key == base::FILE_MODULE)
56 continue; // Android doesn't implement FILE_MODULE;
57 #endif
58 EXPECT_PRED1(ReturnsValidPath, key);
60 #if defined(OS_WIN)
61 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
62 if (key == base::DIR_LOCAL_APP_DATA_LOW &&
63 base::win::GetVersion() < base::win::VERSION_VISTA) {
64 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected to
65 // fail.
66 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
67 } else {
68 EXPECT_TRUE(ReturnsValidPath(key)) << key;
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);
75 #endif