#include "chrome/test/base/test_launcher_utils.h"
#include <memory>
#include "base/command_line.h"
#include "base/environment.h"
#include "base/feature_list.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "components/os_crypt/common/os_crypt_switches.h"
#include "components/password_manager/core/browser/password_manager_switches.h"
#include "content/public/common/content_switches.h"
#include "ui/display/display_switches.h"
#if defined(USE_AURA)
#include "ui/wm/core/wm_core_switches.h"
#endif
namespace test_launcher_utils {
void PrepareBrowserCommandLineForTests(base::CommandLine* command_line) {
command_line->AppendSwitch(switches::kNoFirstRun);
if (!command_line->HasSwitch(switches::kEnableLogging))
command_line->AppendSwitchASCII(switches::kEnableLogging, "stderr");
command_line->AppendSwitch(switches::kDisableDefaultApps);
#if defined(USE_AURA)
command_line->AppendSwitch(
wm::switches::kWindowAnimationsDisabled);
#endif
#if BUILDFLAG(IS_LINUX)
if (!command_line->HasSwitch(password_manager::kPasswordStore)) {
command_line->AppendSwitchASCII(password_manager::kPasswordStore, "basic");
}
#endif
#if BUILDFLAG(IS_MAC)
command_line->AppendSwitch(os_crypt::switches::kUseMockKeychain);
#endif
command_line->AppendSwitch(switches::kDisableComponentUpdate);
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
command_line->AppendSwitchASCII(switches::kChangeStackGuardOnFork,
switches::kChangeStackGuardOnForkDisabled);
#endif
}
void PrepareBrowserCommandLineForBrowserTests(base::CommandLine* command_line,
bool open_about_blank_on_launch) {
command_line->AppendSwitchASCII(switches::kTestType, "browser");
if (open_about_blank_on_launch && command_line->GetArgs().empty())
command_line->AppendArg(url::kAboutBlankURL);
}
bool CreateUserDataDir(base::ScopedTempDir* temp_dir) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
base::FilePath user_data_dir =
command_line->GetSwitchValuePath(switches::kUserDataDir);
if (user_data_dir.empty()) {
DCHECK(temp_dir);
if (temp_dir->CreateUniqueTempDir() && temp_dir->IsValid()) {
user_data_dir = temp_dir->GetPath();
} else {
LOG(ERROR) << "Could not create temporary user data directory \""
<< temp_dir->GetPath().value() << "\".";
return false;
}
}
return OverrideUserDataDir(user_data_dir);
}
bool OverrideUserDataDir(const base::FilePath& user_data_dir) {
bool success = true;
success = base::PathService::Override(chrome::DIR_USER_DATA, user_data_dir);
VLOG(1) << "chrome::DIR_USER_DATA is overridden to: "
<< user_data_dir.value();
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) && !BUILDFLAG(ENABLE_CEF)
std::unique_ptr<base::Environment> env(base::Environment::Create());
success = success && env->SetVar("XDG_CACHE_HOME", user_data_dir.value());
base::FilePath policy_files = user_data_dir.AppendASCII("policies");
success = success &&
base::PathService::Override(chrome::DIR_POLICY_FILES, policy_files);
#endif
return success;
}
}