blob: 81d1fef0b96c61379712f4c9ee2a87e720b2a37a [file] [log] [blame]
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
evanm@google.com874d1672008-10-31 08:54:04 +09005#include "base/path_service.h"
6
initial.commit3f4a7322008-07-27 06:49:38 +09007#include "base/basictypes.h"
8#include "base/file_util.h"
evanm@google.com874d1672008-10-31 08:54:04 +09009#include "base/file_path.h"
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +090010#include "base/scoped_temp_dir.h"
gab@chromium.org4011fea2012-09-20 09:13:40 +090011#if defined(OS_WIN)
12#include "base/win/windows_version.h"
13#endif
wjia@chromium.org9d594d12012-09-20 10:59:36 +090014#include "testing/gtest/include/gtest/gtest.h"
15#include "testing/gtest/include/gtest/gtest-spi.h"
16#include "testing/platform_test.h"
gab@chromium.org4011fea2012-09-20 09:13:40 +090017
initial.commit3f4a7322008-07-27 06:49:38 +090018namespace {
initial.commit3f4a7322008-07-27 06:49:38 +090019
20// Returns true if PathService::Get returns true and sets the path parameter
21// to non-empty for the given PathService::DirType enumeration value.
22bool ReturnsValidPath(int dir_type) {
evanm@google.com874d1672008-10-31 08:54:04 +090023 FilePath path;
initial.commit3f4a7322008-07-27 06:49:38 +090024 bool result = PathService::Get(dir_type, &path);
evan@chromium.org9e3973b2010-11-24 07:39:30 +090025#if defined(OS_POSIX)
viettrungluu@chromium.org366266c2010-11-24 11:23:15 +090026 // If chromium has never been started on this account, the cache path may not
evan@chromium.org9e3973b2010-11-24 07:39:30 +090027 // exist.
viettrungluu@chromium.org366266c2010-11-24 11:23:15 +090028 if (dir_type == base::DIR_CACHE)
wjia@chromium.org9d594d12012-09-20 10:59:36 +090029 return result && !path.value().empty();
evan@chromium.org9e3973b2010-11-24 07:39:30 +090030#endif
wjia@chromium.org9d594d12012-09-20 10:59:36 +090031 return result && !path.value().empty() && file_util::PathExists(path);
initial.commit3f4a7322008-07-27 06:49:38 +090032}
33
erikkay@google.com3620e4c2008-08-12 00:38:27 +090034#if defined(OS_WIN)
benwells@chromium.orgbd09d672012-08-30 18:16:55 +090035// 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.com31177672008-08-08 08:59:04 +090038bool ReturnsInvalidPath(int dir_type) {
phajdan.jr@chromium.orgc8008582009-09-15 04:36:31 +090039 FilePath path;
benwells@chromium.orgbd09d672012-08-30 18:16:55 +090040 bool result = PathService::Get(dir_type, &path);
maruel@google.com31177672008-08-08 08:59:04 +090041 return !result && path.empty();
maruel@google.com15cfc7f2008-08-08 05:23:09 +090042}
erikkay@google.com3620e4c2008-08-12 00:38:27 +090043#endif
maruel@google.com15cfc7f2008-08-08 05:23:09 +090044
45} // namespace
46
erikkay@google.comf2406842008-08-21 00:59:49 +090047// On the Mac this winds up using some autoreleased objects, so we need to
48// be a PlatformTest.
49typedef PlatformTest PathServiceTest;
50
initial.commit3f4a7322008-07-27 06:49:38 +090051// 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.comf2406842008-08-21 00:59:49 +090055TEST_F(PathServiceTest, Get) {
wjia@chromium.org9d594d12012-09-20 10:59:36 +090056 for (int key = base::DIR_CURRENT; key < base::PATH_END; ++key) {
michaelbai@google.com4464bef2011-08-27 01:43:59 +090057#if defined(OS_ANDROID)
wjia@chromium.org9d594d12012-09-20 10:59:36 +090058 if (key == base::FILE_MODULE)
59 continue; // Android doesn't implement FILE_MODULE;
michaelbai@google.com4464bef2011-08-27 01:43:59 +090060#endif
initial.commit3f4a7322008-07-27 06:49:38 +090061 EXPECT_PRED1(ReturnsValidPath, key);
62 }
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +090063#if defined(OS_WIN)
erikkay@google.com1d4507f2008-08-07 01:29:44 +090064 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
benwells@chromium.orgbd09d672012-08-30 18:16:55 +090065 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.com15cfc7f2008-08-08 05:23:09 +090077 }
benwells@chromium.orgbd09d672012-08-30 18:16:55 +090078
79 if (valid)
80 EXPECT_TRUE(ReturnsValidPath(key)) << key;
81 else
82 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
erikkay@google.com1d4507f2008-08-07 01:29:44 +090083 }
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +090084#elif defined(OS_MACOSX)
85 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
wjia@chromium.org9d594d12012-09-20 10:59:36 +090086 EXPECT_PRED1(ReturnsValidPath, key);
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +090087 }
erikkay@google.com1d4507f2008-08-07 01:29:44 +090088#endif
initial.commit3f4a7322008-07-27 06:49:38 +090089}
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +090090
91// test that all versions of the Override function of PathService do what they
92// are supposed to do.
93TEST_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}