#include "chrome/test/base/in_process_browser_test.h"
#include <map>
#include <string_view>
#include <utility>
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/sampling_heap_profiler/poisson_allocation_sampler.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_switches.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/after_startup_task_utils.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_browser_main.h"
#include "chrome/browser/chrome_browser_main_extra_parts.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/lifetime/termination_notification.h"
#include "chrome/browser/navigation_predictor/search_engine_preconnector.h"
#include "chrome/browser/net/chrome_network_delegate.h"
#include "chrome/browser/net/net_error_tab_helper.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/predictors/loading_predictor_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar_controller_util.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/logging_chrome.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/renderer/chrome_content_renderer_client.h"
#include "chrome/test/base/chrome_test_suite.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/test_launcher_utils.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/captive_portal/core/buildflags.h"
#include "components/feature_engagement/public/feature_list.h"
#include "components/google/core/common/google_util.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/os_crypt/async/browser/key_provider.h"
#include "components/os_crypt/sync/os_crypt_mocker.h"
#include "components/password_manager/core/browser/password_manager_switches.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_main_parts.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_launcher.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_features.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/device/public/cpp/device_features.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_features.h"
#if BUILDFLAG(IS_MAC)
#include "base/apple/scoped_nsautorelease_pool.h"
#include "chrome/test/base/scoped_bundle_swizzler_mac.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/test/test_file_util.h"
#include "base/win/scoped_com_initializer.h"
#include "base/win/windows_version.h"
#include "components/version_info/version_info.h"
#include "ui/base/win/atl_module.h"
#endif
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#include "services/device/public/cpp/test/fake_geolocation_system_permission_manager.h"
#endif
#if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION)
#include "components/captive_portal/content/captive_portal_service.h"
#endif
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service.h"
#include "chrome/browser/ui/webui/whats_new/whats_new_util.h"
#include "components/storage_monitor/test_storage_monitor.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/shell.h"
#include "base/system/sys_info.h"
#include "chrome/browser/ash/app_restore/full_restore_app_launch_handler.h"
#include "chrome/browser/ash/input_method/input_method_configuration.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/services/device_sync/device_sync_impl.h"
#include "chromeos/ash/services/device_sync/fake_device_sync.h"
#include "components/user_manager/user_names.h"
#include "ui/display/display_switches.h"
#include "ui/events/test/event_generator.h"
#endif
#if BUILDFLAG(IS_OZONE)
#include "ui/views/test/test_desktop_screen_ozone.h"
#endif
#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/test/views/accessibility_checker.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/widget.h"
#endif
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
#include "chrome/browser/ui/ui_features.h"
#endif
namespace {
#if BUILDFLAG(IS_CHROMEOS)
class FakeDeviceSyncImplFactory
: public ash::device_sync::DeviceSyncImpl::Factory {
public:
FakeDeviceSyncImplFactory() = default;
~FakeDeviceSyncImplFactory() override = default;
std::unique_ptr<ash::device_sync::DeviceSyncBase> CreateInstance(
signin::IdentityManager* identity_manager,
gcm::GCMDriver* gcm_driver,
instance_id::InstanceIDDriver* instance_id_driver,
PrefService* profile_prefs,
ash::device_sync::ClientAppMetadataProvider* client_app_metadata_provider,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
std::unique_ptr<base::OneShotTimer> timer,
ash::device_sync::AttestationCertificatesSyncer::
GetAttestationCertificatesFunction
get_attestation_certificates_function) override {
return std::make_unique<ash::device_sync::FakeDeviceSync>();
}
};
FakeDeviceSyncImplFactory* GetFakeDeviceSyncImplFactory() {
static base::NoDestructor<FakeDeviceSyncImplFactory> factory;
return factory.get();
}
#endif
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
class ChromeBrowserMainExtraPartsBrowserProcessInjection
: public ChromeBrowserMainExtraParts {
public:
ChromeBrowserMainExtraPartsBrowserProcessInjection() = default;
void PreCreateMainMessageLoop() override {
if (features::IsOsLevelGeolocationPermissionSupportEnabled()) {
auto fake_geolocation_system_permission_manager =
std::make_unique<device::FakeGeolocationSystemPermissionManager>();
fake_geolocation_system_permission_manager->SetSystemPermission(
device::LocationSystemPermissionStatus::kAllowed);
device::GeolocationSystemPermissionManager::SetInstance(
std::move(fake_geolocation_system_permission_manager));
}
}
ChromeBrowserMainExtraPartsBrowserProcessInjection(
const ChromeBrowserMainExtraPartsBrowserProcessInjection&) = delete;
ChromeBrowserMainExtraPartsBrowserProcessInjection& operator=(
const ChromeBrowserMainExtraPartsBrowserProcessInjection&) = delete;
};
#endif
class OSCryptAsyncExtraSetUp : public ChromeBrowserMainExtraParts {
public:
void PostEarlyInitialization() override {
g_browser_process->set_additional_os_crypt_async_provider_for_test(
1u,
std::make_unique<SlowTestKeyProvider>(base::Milliseconds(10)));
}
private:
class SlowTestKeyProvider : public os_crypt_async::KeyProvider {
public:
explicit SlowTestKeyProvider(base::TimeDelta sleep_time)
: sleep_time_(sleep_time) {}
private:
void GetKey(KeyCallback callback) override {
os_crypt_async::Encryptor::Key key(
std::vector<uint8_t>(
os_crypt_async::Encryptor::Key::kAES256GCMKeySize, 0xCE),
os_crypt_async::mojom::Algorithm::kAES256GCM);
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](KeyCallback callback, os_crypt_async::Encryptor::Key key) {
std::move(callback).Run("test_key_provider", std::move(key));
},
std::move(callback), std::move(key)),
sleep_time_);
}
bool UseForEncryption() override { return false; }
bool IsCompatibleWithOsCryptSync() override { return false; }
const base::TimeDelta sleep_time_;
};
};
void EnsureBrowserContextKeyedServiceFactoriesForTestingBuilt() {
NotificationDisplayServiceTester::EnsureFactoryBuilt();
}
InProcessBrowserTest* g_current_test;
}
InProcessBrowserTest::SetUpBrowserFunction*
InProcessBrowserTest::global_browser_set_up_function_ = nullptr;
InProcessBrowserTest::InProcessBrowserTest() {
Initialize();
#if defined(TOOLKIT_VIEWS)
views_delegate_ = std::make_unique<AccessibilityChecker>();
#endif
}
#if defined(TOOLKIT_VIEWS)
InProcessBrowserTest::InProcessBrowserTest(
std::unique_ptr<views::ViewsDelegate> views_delegate) {
Initialize();
views_delegate_ = std::move(views_delegate);
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
void InProcessBrowserTest::set_launch_browser_for_testing(
std::unique_ptr<ash::full_restore::ScopedLaunchBrowserForTesting>
launch_browser_for_testing) {
launch_browser_for_testing_ = std::move(launch_browser_for_testing);
}
#endif
void InProcessBrowserTest::RunScheduledLayouts() {
#if defined(TOOLKIT_VIEWS)
views::Widget::Widgets widgets_to_layout;
#if BUILDFLAG(IS_CHROMEOS)
for (aura::Window* root_window : ash::Shell::GetAllRootWindows()) {
widgets_to_layout.merge(views::Widget::GetAllChildWidgets(root_window));
}
#else
widgets_to_layout = views::test::WidgetTest::GetAllWidgets();
#endif
for (views::Widget* widget : widgets_to_layout) {
widget->LayoutRootViewIfNecessary();
}
#endif
}
void InProcessBrowserTest::Initialize() {
g_current_test = this;
chrome_test_utils::OverrideChromeTestDataDir();
#if BUILDFLAG(IS_MAC)
bundle_swizzler_ = std::make_unique<ScopedBundleSwizzlerMac>();
#endif
embedded_test_server()->AddDefaultHandlers(GetChromeTestDataDir());
InitializeHTTPSTestServer();
embedded_https_test_server().AddDefaultHandlers(GetChromeTestDataDir());
ToolbarControllerUtil::SetPreventOverflowForTesting(true);
std::vector<base::test::FeatureRef> disabled_features;
disabled_features.push_back(features::kPreconnectToSearch);
disabled_features.push_back(
features::kRestartNetworkServiceUnsandboxedForFailedLaunch);
disabled_features.push_back(
extensions_features::kExtensionDisableUnsupportedDeveloper);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
disabled_features.push_back(features::kSessionRestoreInfobar);
#endif
block_all_iph_feature_list_.InitWithNoFeaturesAllowed();
scoped_feature_list_.InitWithFeatures({}, disabled_features);
create_services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(base::BindRepeating(
&InProcessBrowserTest::OnWillCreateBrowserContextKeyedServices,
base::Unretained(this)));
#if BUILDFLAG(IS_CHROMEOS)
launch_browser_for_testing_ =
std::make_unique<ash::full_restore::ScopedLaunchBrowserForTesting>();
#endif
#if BUILDFLAG(IS_WIN)
base::GetPathsAllowedToLeak() = {L"\\Sync Data", L"\\Local Storage\\leveldb",
L"\\DataSharing", L"\\Collaboration"};
#endif
}
InProcessBrowserTest::~InProcessBrowserTest() {
g_current_test = nullptr;
}
InProcessBrowserTest* InProcessBrowserTest::GetCurrent() {
return g_current_test;
}
void InProcessBrowserTest::SetUp() {
DCHECK(!g_browser_process);
ui_controls::ResetUIControlsIfEnabled();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kDisableAutoReload);
SetUpCommandLine(command_line);
SetUpDefaultCommandLine(command_line);
base::PoissonAllocationSampler::Init();
ASSERT_TRUE(test_launcher_utils::CreateUserDataDir(&temp_user_data_dir_))
<< "Could not create user data directory.";
ASSERT_TRUE(SetUpUserDataDirectory())
<< "Could not set up user data directory.";
#if BUILDFLAG(IS_CHROMEOS)
command_line->AppendSwitch(switches::kDisableLoggingRedirect);
ash::input_method::DisableExtensionLoading();
if (!command_line->HasSwitch(switches::kHostWindowBounds) &&
!base::SysInfo::IsRunningOnChromeOS()) {
command_line->AppendSwitchASCII(switches::kHostWindowBounds,
"0+0-1280x800");
}
if (!command_line->HasSwitch(ash::switches::kLoginManager) &&
!command_line->HasSwitch(ash::switches::kLoginUser) &&
!command_line->HasSwitch(ash::switches::kGuestSession)) {
command_line->AppendSwitchASCII(
ash::switches::kLoginUser,
cryptohome::Identification(user_manager::StubAccountId()).id());
if (!command_line->HasSwitch(ash::switches::kLoginProfile)) {
command_line->AppendSwitchASCII(
ash::switches::kLoginProfile,
ash::BrowserContextHelper::kTestUserBrowserContextDirName);
}
}
#endif
SetScreenInstance();
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS)
OSCryptMocker::SetUp();
#elif BUILDFLAG(IS_LINUX)
CHECK(command_line->HasSwitch(password_manager::kPasswordStore));
#endif
#if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION)
captive_portal::CaptivePortalService::set_state_for_testing(
captive_portal::CaptivePortalService::DISABLED_FOR_TESTING);
#endif
chrome_browser_net::NetErrorTabHelper::set_state_for_testing(
chrome_browser_net::NetErrorTabHelper::TESTING_FORCE_DISABLED);
#if BUILDFLAG(IS_CHROMEOS)
ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(true);
ash::device_sync::DeviceSyncImpl::Factory::SetCustomFactory(
GetFakeDeviceSyncImplFactory());
ash::ShellTestApi::SetTabletControllerUseScreenshotForTest(false);
ash::ShellTestApi::SetUseLoginNotificationDelayForTest(false);
views::test::WidgetTest::SetRootWindowProvider(
base::BindRepeating(&ash::Shell::GetAllRootWindows));
#endif
ASSERT_TRUE(default_download_dir_.CreateUniqueTempDir());
CHECK(base::PathService::Override(chrome::DIR_DEFAULT_DOWNLOADS,
default_download_dir_.GetPath()));
#if defined(TOOLKIT_VIEWS)
Tab::SetShowHoverCardOnMouseHoverForTesting(false);
#endif
whats_new::DisableRemoteContentForTests();
#if !BUILDFLAG(IS_ANDROID)
SearchEngineChoiceDialogService::SetDialogDisabledForTests(
true);
#endif
EnsureBrowserContextKeyedServiceFactoriesForTestingBuilt();
BrowserTestBase::SetUp();
}
void InProcessBrowserTest::SetUpDefaultCommandLine(
base::CommandLine* command_line) {
test_launcher_utils::PrepareBrowserCommandLineForTests(command_line);
test_launcher_utils::PrepareBrowserCommandLineForBrowserTests(
command_line, open_about_blank_on_browser_launch_);
if (exit_when_last_browser_closes_)
command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests);
#if BUILDFLAG(IS_CHROMEOS)
command_line->AppendSwitch(switches::kDisableAutoMaximizeForTests);
#endif
}
void InProcessBrowserTest::TearDown() {
DCHECK(!g_browser_process);
#if BUILDFLAG(IS_WIN)
com_initializer_.reset();
#endif
BrowserTestBase::TearDown();
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
OSCryptMocker::TearDown();
#endif
if (embedded_https_test_server().Started()) {
ASSERT_TRUE(embedded_https_test_server().ShutdownAndWaitUntilComplete());
}
#if BUILDFLAG(IS_CHROMEOS)
ash::device_sync::DeviceSyncImpl::Factory::SetCustomFactory(nullptr);
launch_browser_for_testing_ = nullptr;
views::test::WidgetTest::SetRootWindowProvider(base::NullCallback());
#endif
}
size_t InProcessBrowserTest::GetTestPreCount() {
constexpr std::string_view kPreTestPrefix = "PRE_";
std::string_view test_name =
testing::UnitTest::GetInstance()->current_test_info()->name();
size_t count = 0;
while (base::StartsWith(test_name, kPreTestPrefix)) {
++count;
test_name = test_name.substr(kPreTestPrefix.size());
}
return count;
}
void InProcessBrowserTest::CreatedBrowserMainParts(
content::BrowserMainParts* parts) {
BrowserTestBase::CreatedBrowserMainParts(parts);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
static_cast<ChromeBrowserMainParts*>(parts)->AddParts(
std::make_unique<ChromeBrowserMainExtraPartsBrowserProcessInjection>());
#endif
static_cast<ChromeBrowserMainParts*>(parts)->AddParts(
std::make_unique<OSCryptAsyncExtraSetUp>());
}
void InProcessBrowserTest::SetBrowser(BrowserWindowInterface* browser) {
browser_ = browser ? browser->GetBrowserForMigrationOnly() : nullptr;
}
void InProcessBrowserTest::RecordPropertyFromMap(
const std::map<std::string, std::string>& tags) {
std::string result = "";
for (auto const& tag_pair : tags) {
DCHECK(tag_pair.first.find(";") == std::string::npos &&
tag_pair.first.find("=") == std::string::npos);
DCHECK(tag_pair.second.find(";") == std::string::npos &&
tag_pair.second.find("=") == std::string::npos);
if (!result.empty())
result = base::StrCat({result, ";"});
result = base::StrCat({result, tag_pair.first, "=", tag_pair.second});
}
if (!result.empty())
RecordProperty("gtest_tag", result);
}
void InProcessBrowserTest::SetUpLocalStatePrefService(
PrefService* local_state) {
#if BUILDFLAG(IS_WIN)
local_state->SetString(prefs::kShortcutMigrationVersion,
std::string(version_info::GetVersionNumber()));
#endif
}
Profile* InProcessBrowserTest::GetProfile() const {
return browser() ? browser()->profile() : nullptr;
}
void InProcessBrowserTest::CloseBrowserSynchronously(
BrowserWindowInterface* browser) {
CloseBrowserAsynchronously(browser);
ui_test_utils::WaitForBrowserToClose(browser);
}
void InProcessBrowserTest::CloseBrowserAsynchronously(
BrowserWindowInterface* browser) {
browser->GetWindow()->Close();
#if BUILDFLAG(IS_MAC)
AutoreleasePool()->Recycle();
#endif
}
void InProcessBrowserTest::CloseAllBrowsers() {
chrome::CloseAllBrowsers();
#if BUILDFLAG(IS_MAC)
AutoreleasePool()->Recycle();
#endif
}
void InProcessBrowserTest::RunUntilBrowserProcessQuits() {
std::exchange(run_loop_, nullptr)->Run();
}
bool InProcessBrowserTest::AddTabAtIndexToBrowser(
BrowserWindowInterface* browser,
int index,
const GURL& url,
ui::PageTransition transition,
bool check_navigation_success) {
return AddTabAtIndexToBrowser(browser, index, url, transition);
}
bool InProcessBrowserTest::AddTabAtIndexToBrowser(
BrowserWindowInterface* browser,
int index,
const GURL& url,
ui::PageTransition transition) {
NavigateParams params(browser, url, transition);
params.tabstrip_index = index;
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.pwa_navigation_capturing_force_off = true;
Navigate(¶ms);
RunScheduledLayouts();
return content::WaitForLoadStop(params.navigated_or_inserted_contents);
}
bool InProcessBrowserTest::AddTabAtIndex(int index,
const GURL& url,
ui::PageTransition transition) {
return AddTabAtIndexToBrowser(browser(), index, url, transition, true);
}
bool InProcessBrowserTest::SetUpUserDataDirectory() {
return true;
}
void InProcessBrowserTest::SetScreenInstance() {
#if BUILDFLAG(IS_LINUX)
if (!display::Screen::HasScreen() &&
views::test::TestDesktopScreenOzone::IsOzoneInitialized()) {
screen_ = views::test::TestDesktopScreenOzone::Create();
}
#endif
}
#if !BUILDFLAG(IS_MAC)
void InProcessBrowserTest::OpenDevToolsWindow(
content::WebContents* web_contents) {
ASSERT_FALSE(content::DevToolsAgentHost::HasFor(web_contents));
DevToolsWindow::OpenDevToolsWindow(web_contents,
DevToolsOpenedByAction::kUnknown);
ASSERT_TRUE(content::DevToolsAgentHost::HasFor(web_contents));
}
Browser* InProcessBrowserTest::OpenURLOffTheRecord(Profile* profile,
const GURL& url) {
chrome::OpenURLOffTheRecord(profile, url);
Browser* browser = chrome::FindTabbedBrowser(
profile->GetPrimaryOTRProfile(true), false);
content::TestNavigationObserver observer(
browser->tab_strip_model()->GetActiveWebContents());
observer.Wait();
return browser;
}
Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
Browser* browser = Browser::Create(Browser::CreateParams(profile, true));
AddBlankTabAndShow(browser);
return browser;
}
Browser* InProcessBrowserTest::CreateIncognitoBrowser(Profile* profile) {
if (!profile)
profile = browser()->profile();
Browser* incognito = Browser::Create(Browser::CreateParams(
profile->GetPrimaryOTRProfile(true), true));
AddBlankTabAndShow(incognito);
return incognito;
}
Browser* InProcessBrowserTest::CreateBrowserForPopup(Profile* profile) {
Browser* browser = Browser::Create(
Browser::CreateParams(Browser::TYPE_POPUP, profile, true));
AddBlankTabAndShow(browser);
return browser;
}
Browser* InProcessBrowserTest::CreateBrowserForApp(const std::string& app_name,
Profile* profile) {
Browser* browser = Browser::Create(Browser::CreateParams::CreateForApp(
app_name, false , gfx::Rect(), profile, true));
AddBlankTabAndShow(browser);
return browser;
}
#endif
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS)
Browser* InProcessBrowserTest::CreateGuestBrowser() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath guest_path = profile_manager->GetGuestProfilePath();
Profile& guest_profile =
profiles::testing::CreateProfileSync(profile_manager, guest_path);
Profile* guest_profile_otr =
guest_profile.GetPrimaryOTRProfile(true);
Browser* browser =
Browser::Create(Browser::CreateParams(guest_profile_otr, true));
AddBlankTabAndShow(browser);
return browser;
}
#endif
void InProcessBrowserTest::AddBlankTabAndShow(Browser* browser) {
content::WebContents* blank_tab = chrome::AddSelectedTabWithURL(
browser, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
content::TestNavigationObserver observer(blank_tab);
observer.Wait();
RunScheduledLayouts();
browser->window()->Show();
}
#if !BUILDFLAG(IS_MAC)
base::CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() {
base::CommandLine new_command_line(
base::CommandLine::ForCurrentProcess()->GetProgram());
base::CommandLine::SwitchMap switches =
base::CommandLine::ForCurrentProcess()->GetSwitches();
switches.erase(switches::kUserDataDir);
switches.erase(switches::kSingleProcessTests);
switches.erase(switches::kSingleProcess);
new_command_line.AppendSwitch(switches::kLaunchAsBrowser);
base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
new_command_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
for (base::CommandLine::SwitchMap::const_iterator iter = switches.begin();
iter != switches.end(); ++iter) {
new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
}
return new_command_line;
}
#endif
base::FilePath InProcessBrowserTest::GetChromeTestDataDir() const {
return chrome_test_utils::GetChromeTestDataDir();
}
void InProcessBrowserTest::PreRunTestOnMainThread() {
AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting();
run_loop_ = ChromeBrowserMainParts::TakeRunLoopForTest();
content::RunAllPendingInMessageLoop();
SetBrowser(GetLastActiveBrowserWindowInterfaceWithAnyProfile());
if (browser_ && !browser_->tab_strip_model()->empty()) {
base::WeakPtr<content::WebContents> tab =
browser_->tab_strip_model()->GetActiveWebContents()->GetWeakPtr();
content::WaitForLoadStop(tab.get());
if (tab) {
SetInitialWebContents(tab.get());
}
}
#if !BUILDFLAG(IS_ANDROID)
ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateForBrowserTests());
#endif
#if BUILDFLAG(IS_MAC)
autorelease_pool_.emplace();
#endif
content::RunAllPendingInMessageLoop();
if (browser_ && global_browser_set_up_function_) {
ASSERT_TRUE(global_browser_set_up_function_(browser_));
}
#if BUILDFLAG(IS_MAC)
autorelease_pool_->Recycle();
#endif
}
void InProcessBrowserTest::PostRunTestOnMainThread() {
#if BUILDFLAG(IS_MAC)
autorelease_pool_->Recycle();
#endif
QuitBrowsers();
CHECK(BrowserList::GetInstance()->empty());
}
void InProcessBrowserTest::QuitBrowsers() {
if (chrome::GetTotalBrowserCount() == 0) {
browser_shutdown::NotifyAppTerminating();
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&chrome::OnAppExiting));
content::RunAllPendingInMessageLoop();
#if BUILDFLAG(IS_MAC)
autorelease_pool_.reset();
#endif
return;
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&chrome::AttemptExit));
RunUntilBrowserProcessQuits();
#if BUILDFLAG(IS_MAC)
content::RunAllPendingInMessageLoop();
autorelease_pool_.reset();
#endif
}
void InProcessBrowserTest::OnWillCreateBrowserContextKeyedServices(
content::BrowserContext* context) {
SetUpBrowserContextKeyedServices(context);
}