#include "base/path_service.h"
#include "base/base_paths.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/scoped_environment_variable_override.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gtest_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 BUILDFLAG(IS_WIN)
#include <shlobj.h>
#include "base/win/windows_version.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "base/apple/bundle_locations.h"
#endif
namespace base {
namespace {
#if BUILDFLAG(IS_ANDROID)
constexpr char kExpectedChromiumTestsRoot[] =
"/storage/emulated/0/chromium_tests_root";
#endif
bool ReturnsValidPath(int key) { … }
bool ReturnsInvalidPath(int key) { … }
}
PathServiceTest;
TEST_F(PathServiceTest, Get) { … }
TEST_F(PathServiceTest, CheckedGet) { … }
#if defined(GTEST_HAS_DEATH_TEST)
TEST_F(PathServiceTest, CheckedGetFailure) { … }
#endif
TEST_F(PathServiceTest, Override) { … }
TEST_F(PathServiceTest, OverrideMultiple) { … }
TEST_F(PathServiceTest, RemoveOverride) { … }
#if BUILDFLAG(IS_WIN)
TEST_F(PathServiceTest, GetProgramFiles) {
FilePath programfiles_dir;
#if defined(_WIN64)
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files (x86)"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
#else
if (base::win::OSInfo::GetInstance()->IsWowX86OnAMD64() ||
base::win::OSInfo::GetInstance()->IsWowX86OnARM64()) {
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files (x86)"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files (x86)"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
} else {
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
}
#endif
}
TEST_F(PathServiceTest, GetSystemTemp) {
FilePath secure_system_temp;
EXPECT_EQ(PathService::Get(DIR_SYSTEM_TEMP, &secure_system_temp),
::IsUserAnAdmin());
if (!secure_system_temp.empty()) {
FilePath dir_windows;
ASSERT_TRUE(PathService::Get(DIR_WINDOWS, &dir_windows));
FilePath dir_program_files;
ASSERT_TRUE(PathService::Get(DIR_PROGRAM_FILES, &dir_program_files));
ASSERT_TRUE((dir_windows.AppendASCII("SystemTemp") == secure_system_temp) ||
(dir_program_files == secure_system_temp));
}
}
#endif
TEST_F(PathServiceTest, DIR_ASSETS) { … }
TEST_F(PathServiceTest, DIR_OUT_TEST_DATA_ROOT) { … }
TEST_F(PathServiceTest, DIR_GEN_TEST_DATA_ROOT) { … }
#if ((BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && \
!BUILDFLAG(IS_ANDROID)) || \
BUILDFLAG(IS_WIN))
TEST_F(PathServiceTest, SetTestDataRootAsAbsolutePath) { … }
TEST_F(PathServiceTest, SetTestDataRootAsRelativePath) { … }
#endif
#if BUILDFLAG(IS_FUCHSIA)
TEST_F(PathServiceTest, DIR_SRC_TEST_DATA_ROOT) {
FilePath test_binary_path;
EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(), "/pkg");
}
#elif BUILDFLAG(IS_ANDROID)
TEST_F(PathServiceTest, AndroidTestOverrides) {
EXPECT_EQ(PathService::CheckedGet(DIR_ANDROID_APP_DATA).value(),
kExpectedChromiumTestsRoot);
EXPECT_EQ(PathService::CheckedGet(DIR_ASSETS).value(),
kExpectedChromiumTestsRoot);
EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(),
kExpectedChromiumTestsRoot);
EXPECT_EQ(PathService::CheckedGet(DIR_OUT_TEST_DATA_ROOT).value(),
kExpectedChromiumTestsRoot);
}
#endif
}