This source file includes following definitions.
- ReturnsValidPath
- ReturnsInvalidPath
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/path_service.h"
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if defined(OS_WIN)
#include <userenv.h>
#include "base/win/windows_version.h"
#pragma comment(lib, "userenv.lib")
#endif
namespace {
bool ReturnsValidPath(int dir_type) {
base::FilePath path;
bool result = PathService::Get(dir_type, &path);
bool check_path_exists = true;
#if defined(OS_POSIX)
if (dir_type == base::DIR_CACHE)
check_path_exists = false;
#endif
#if defined(OS_LINUX)
if (dir_type == base::DIR_USER_DESKTOP)
check_path_exists = false;
#endif
#if defined(OS_WIN)
if (dir_type == base::DIR_DEFAULT_USER_QUICK_LAUNCH) {
if (base::win::GetVersion() < base::win::VERSION_VISTA) {
wchar_t default_profile_path[MAX_PATH];
DWORD size = arraysize(default_profile_path);
return (result &&
::GetDefaultUserProfileDirectory(default_profile_path, &size) &&
StartsWith(path.value(), default_profile_path, false));
}
} else if (dir_type == base::DIR_TASKBAR_PINS) {
if (base::win::GetVersion() < base::win::VERSION_WIN7)
check_path_exists = false;
}
#endif
#if defined(OS_MACOSX)
if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE &&
dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) {
if (path.ReferencesParent())
return false;
}
#else
if (path.ReferencesParent())
return false;
#endif
return result && !path.empty() && (!check_path_exists ||
base::PathExists(path));
}
#if defined(OS_WIN)
bool ReturnsInvalidPath(int dir_type) {
base::FilePath path;
bool result = PathService::Get(dir_type, &path);
return !result && path.empty();
}
#endif
}
typedef PlatformTest PathServiceTest;
TEST_F(PathServiceTest, Get) {
for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
#if defined(OS_ANDROID)
if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP ||
key == base::DIR_HOME)
continue;
#elif defined(OS_IOS)
if (key == base::DIR_USER_DESKTOP)
continue;
#endif
EXPECT_PRED1(ReturnsValidPath, key);
}
#if defined(OS_WIN)
for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
bool valid = true;
switch(key) {
case base::DIR_LOCAL_APP_DATA_LOW:
valid = base::win::GetVersion() >= base::win::VERSION_VISTA;
break;
case base::DIR_APP_SHORTCUTS:
valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
break;
}
if (valid)
EXPECT_TRUE(ReturnsValidPath(key)) << key;
else
EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
}
#elif defined(OS_MACOSX)
for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#elif defined(OS_ANDROID)
for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END;
++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#elif defined(OS_POSIX)
for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END;
++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#endif
}
TEST_F(PathServiceTest, Override) {
int my_special_key = 666;
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
EXPECT_TRUE(base::PathExists(fake_cache_dir));
base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
PathService::OverrideAndCreateIfNeeded(my_special_key,
fake_cache_dir2,
false);
EXPECT_FALSE(base::PathExists(fake_cache_dir2));
EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
fake_cache_dir2,
true));
EXPECT_TRUE(base::PathExists(fake_cache_dir2));
}
TEST_F(PathServiceTest, OverrideMultiple) {
int my_special_key = 666;
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
EXPECT_TRUE(base::PathExists(fake_cache_dir1));
ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
EXPECT_TRUE(base::PathExists(fake_cache_dir2));
ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
base::FilePath result;
EXPECT_TRUE(PathService::Get(my_special_key, &result));
EXPECT_TRUE(base::PathExists(result.AppendASCII("t1")));
EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
EXPECT_TRUE(base::PathExists(result.AppendASCII("t2")));
}
TEST_F(PathServiceTest, RemoveOverride) {
PathService::RemoveOverride(base::DIR_TEMP);
base::FilePath original_user_data_dir;
EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir));
EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP));
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path()));
base::FilePath new_user_data_dir;
EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
EXPECT_NE(original_user_data_dir, new_user_data_dir);
EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP));
EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
EXPECT_EQ(original_user_data_dir, new_user_data_dir);
}