#include "chrome/browser/lifetime/browser_close_manager.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/background/background_mode_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_browsertest_utils.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_target_info.h"
#include "components/javascript_dialogs/app_modal_dialog_controller.h"
#include "components/javascript_dialogs/app_modal_dialog_view.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/sessions/core/tab_restore_service.h"
#include "components/sessions/core/tab_restore_service_observer.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/download_test_observer.h"
#include "content/public/test/slow_download_http_response.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_switches.h"
#endif
namespace {
javascript_dialogs::AppModalDialogView* GetNextDialog() { … }
void AcceptClose() { … }
void CancelClose() { … }
class AllBrowsersClosingCancelledObserver { … };
class TabRestoreServiceChangesObserver
: public sessions::TabRestoreServiceObserver { … };
class TestBrowserCloseManager : public BrowserCloseManager { … };
class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate { … };
#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
class FakeBackgroundModeManager : public BackgroundModeManager { … };
#endif
}
class BrowserCloseManagerBrowserTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestShutdownMoreThanOnce) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, PRE_TestSessionRestore) { … }
#if BUILDFLAG(IS_WIN)
#define MAYBE_TestSessionRestore …
#else
#define MAYBE_TestSessionRestore …
#endif
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
MAYBE_TestSessionRestore) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestMultipleWindows) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestHangInBeforeUnloadMultipleTabs) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestHangInBeforeUnloadMultipleWindows) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestUnloadMultipleSlowTabs) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
DISABLED_TestBeforeUnloadMultipleSlowWindows) { … }
#if BUILDFLAG(IS_WIN) && !defined(NDEBUG)
#define MAYBE_TestAddWindowDuringShutdown …
#else
#define MAYBE_TestAddWindowDuringShutdown …
#endif
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
MAYBE_TestAddWindowDuringShutdown) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestAddWindowWithBeforeUnloadDuringShutdown) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestAddTabDuringShutdown) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestAddTabWithBeforeUnloadDuringShutdown) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_AddBeforeUnloadDuringClosing …
#else
#define MAYBE_AddBeforeUnloadDuringClosing …
#endif
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
MAYBE_AddBeforeUnloadDuringClosing) { … }
#if BUILDFLAG(IS_LINUX)
#define MAYBE_TestCloseTabDuringShutdown …
#else
#define MAYBE_TestCloseTabDuringShutdown …
#endif
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
MAYBE_TestCloseTabDuringShutdown) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestOpenAndCloseWindowDuringShutdown) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestCloseWindowDuringShutdown) { … }
#if BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
TestBrowserCloseManager::AttemptClose(
TestBrowserCloseManager::NO_USER_CHOICE);
WaitForAllBrowsersToClose();
EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
EXPECT_TRUE(BrowserList::GetInstance()->empty());
EXPECT_EQ(1, DownloadCoreService::BlockingShutdownCountAllProfiles());
TestBrowserCloseManager::AttemptClose(
TestBrowserCloseManager::NO_USER_CHOICE);
}
#else
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestWithDangerousUrlDownload) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestWithOffTheRecordDownloads) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
DISABLED_TestWithOffTheRecordWindowAndRegularDownload) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
TestWithDownloadsFromDifferentProfiles) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
DISABLED_TestBeforeUnloadAndDownloads) { … }
#endif
#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
class BrowserCloseManagerWithBackgroundModeBrowserTest
: public BrowserCloseManagerBrowserTest { … };
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
CloseAllBrowsersWithBackgroundMode) { … }
IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
DISABLED_CloseSingleBrowserWithBackgroundMode) { … }
IN_PROC_BROWSER_TEST_F(
BrowserCloseManagerWithBackgroundModeBrowserTest,
DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) { … }
#endif