blob: 227e91384a241685bdcc69442f352cf55f42056c [file] [log] [blame]
[email protected]2e9d79f2013-08-16 05:45:561// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dchenge73d8520c2015-12-27 01:19:095#include "chrome/browser/lifetime/browser_close_manager.h"
6
7#include <utility>
[email protected]2e9d79f2013-08-16 05:45:568#include <vector>
9
Sebastien Marchandf1349f52019-01-25 03:16:4110#include "base/bind.h"
[email protected]2e9d79f2013-08-16 05:45:5611#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:0512#include "base/files/scoped_temp_dir.h"
avi6846aef2015-12-26 01:09:3813#include "base/macros.h"
Gabriel Charette078e3662017-08-28 22:59:0414#include "base/run_loop.h"
Eric Seckler8652dcd52018-09-20 10:42:2815#include "base/task/post_task.h"
jam3f2d3932017-04-26 20:28:5116#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3817#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2018#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1419#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5620#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3821#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4222#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4423#include "chrome/browser/download/download_core_service.h"
24#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0525#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5626#include "chrome/browser/lifetime/application_lifetime.h"
Avi Drissmand30927342018-05-22 15:04:2727#include "chrome/browser/lifetime/browser_shutdown.h"
[email protected]2e9d79f2013-08-16 05:45:5628#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1429#include "chrome/browser/profiles/profile.h"
30#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4031#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5632#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2033#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3734#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1435#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5636#include "chrome/browser/ui/tabs/tab_strip_model.h"
Scott Violet6200d332018-02-23 21:29:2337#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5638#include "chrome/common/chrome_switches.h"
39#include "chrome/common/url_constants.h"
40#include "chrome/test/base/in_process_browser_test.h"
41#include "chrome/test/base/ui_test_utils.h"
Min Qina9f487872018-02-09 20:43:2342#include "components/download/public/common/download_item.h"
Evan Stade7220e472020-01-31 17:06:5743#include "components/javascript_dialogs/app_modal_dialog_controller.h"
44#include "components/javascript_dialogs/app_modal_dialog_view.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5945#include "components/keep_alive_registry/keep_alive_types.h"
46#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4047#include "components/sessions/core/tab_restore_service.h"
48#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1449#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2850#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:5951#include "content/public/browser/browser_thread.h"
[email protected]edfca702013-08-16 08:58:1452#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5653#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3454#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4655#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5656#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4057#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1458#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0959#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5660#include "content/public/test/test_navigation_observer.h"
61#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5662
[email protected]f9357a442014-05-15 18:44:0763#if defined(OS_CHROMEOS)
Steven Bennetts38e9bde22019-01-03 17:31:1064#include "chromeos/constants/chromeos_switches.h"
[email protected]f9357a442014-05-15 18:44:0765#endif
66
[email protected]2e9d79f2013-08-16 05:45:5667namespace {
68
Evan Stade7220e472020-01-31 17:06:5769javascript_dialogs::AppModalDialogView* GetNextDialog() {
70 javascript_dialogs::AppModalDialogController* dialog =
avi373e72a2017-05-26 20:33:5271 ui_test_utils::WaitForAppModalDialog();
Evan Stade7220e472020-01-31 17:06:5772 CHECK(dialog->view());
73 return dialog->view();
oshima82f72482014-10-24 14:14:3274}
[email protected]2e9d79f2013-08-16 05:45:5675
avi336125f72017-05-06 22:25:4076// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4677// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3278void AcceptClose() {
79 GetNextDialog()->AcceptAppModalDialog();
80}
[email protected]2e9d79f2013-08-16 05:45:5681
avi336125f72017-05-06 22:25:4082// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4683// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3284void CancelClose() {
85 GetNextDialog()->CancelAppModalDialog();
86}
[email protected]2e9d79f2013-08-16 05:45:5687
Scott Violet1f106b582017-07-12 15:49:5888class RepeatedNotificationObserver : public content::NotificationObserver {
89 public:
90 explicit RepeatedNotificationObserver(int type, int count)
91 : num_outstanding_(count), running_(false) {
92 registrar_.Add(this, type, content::NotificationService::AllSources());
93 }
94
95 void Observe(int type,
96 const content::NotificationSource& source,
97 const content::NotificationDetails& details) override {
98 ASSERT_GT(num_outstanding_, 0);
99 if (!--num_outstanding_ && running_) {
Sami Kyostila7d640eb2019-07-31 18:50:26100 base::PostTask(FROM_HERE, {content::BrowserThread::UI},
101 run_loop_.QuitClosure());
Scott Violet1f106b582017-07-12 15:49:58102 }
103 }
104
105 void Wait() {
106 if (num_outstanding_ <= 0)
107 return;
108
109 running_ = true;
110 run_loop_.Run();
111 running_ = false;
Weza78944d2018-05-17 16:56:29112
113 EXPECT_LE(num_outstanding_, 0);
Scott Violet1f106b582017-07-12 15:49:58114 }
115
116 private:
117 int num_outstanding_;
118 content::NotificationRegistrar registrar_;
119 bool running_;
120 base::RunLoop run_loop_;
121
122 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
123};
124
mkolom65a0b3c2017-03-02 06:11:40125class TabRestoreServiceChangesObserver
126 : public sessions::TabRestoreServiceObserver {
127 public:
128 explicit TabRestoreServiceChangesObserver(Profile* profile)
129 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
130 if (service_)
131 service_->AddObserver(this);
132 }
133
134 ~TabRestoreServiceChangesObserver() override {
135 if (service_)
136 service_->RemoveObserver(this);
137 }
138
139 size_t changes_count() const { return changes_count_; }
140
141 private:
142 // sessions::TabRestoreServiceObserver:
143 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
144 changes_count_++;
145 }
146
147 // sessions::TabRestoreServiceObserver:
148 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
149 service_ = nullptr;
150 }
151
152 sessions::TabRestoreService* service_ = nullptr;
153 size_t changes_count_ = 0;
154
155 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
156};
157
[email protected]edfca702013-08-16 08:58:14158class TestBrowserCloseManager : public BrowserCloseManager {
159 public:
160 enum UserChoice {
161 USER_CHOICE_USER_CANCELS_CLOSE,
162 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42163 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14164 };
165
166 static void AttemptClose(UserChoice user_choice) {
167 scoped_refptr<BrowserCloseManager> browser_close_manager =
168 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20169 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14170 browser_close_manager->StartClosingBrowsers();
171 }
172
173 protected:
Daniel Chenga542fca2014-10-21 09:51:29174 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14175
Daniel Chenga542fca2014-10-21 09:51:29176 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14177 int download_count,
mostynb2b52d1db2014-10-07 02:47:17178 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42179 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14180 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42181 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14182 case USER_CHOICE_USER_CANCELS_CLOSE: {
183 callback.Run(false);
184 break;
185 }
186 case USER_CHOICE_USER_ALLOWS_CLOSE: {
187 callback.Run(true);
188 break;
189 }
190 }
191 }
192
193 private:
194 explicit TestBrowserCloseManager(UserChoice user_choice)
195 : user_choice_(user_choice) {}
196
197 UserChoice user_choice_;
198
199 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
200};
201
[email protected]422a7d12013-10-21 12:10:42202class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
203 public:
204 explicit TestDownloadManagerDelegate(Profile* profile)
205 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23206 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42207 }
Daniel Chenga542fca2014-10-21 09:51:29208 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42209
Daniel Chenga542fca2014-10-21 09:51:29210 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23211 download::DownloadItem* item,
danakja3cfb8332019-12-10 21:13:33212 content::DownloadTargetCallback* callback) override {
213 content::DownloadTargetCallback dangerous_callback = base::BindOnce(
214 &TestDownloadManagerDelegate::SetDangerous, std::move(*callback));
215 bool run = ChromeDownloadManagerDelegate::DetermineDownloadTarget(
216 item, &dangerous_callback);
217 // ChromeDownloadManagerDelegate::DetermineDownloadTarget() needs to run the
218 // |callback|.
219 DCHECK(run);
220 DCHECK(!dangerous_callback);
221 return true;
[email protected]422a7d12013-10-21 12:10:42222 }
223
danakja3cfb8332019-12-10 21:13:33224 static void SetDangerous(content::DownloadTargetCallback callback,
Asanka Herath1ba0e9f2017-04-03 18:48:53225 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23226 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35227 download::DownloadDangerType danger_type,
Joe DeBlasioc427ca032020-01-17 19:15:43228 download::DownloadItem::MixedContentStatus mcs,
Asanka Herath1ba0e9f2017-04-03 18:48:53229 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16230 download::DownloadInterruptReason reason) {
danakja3cfb8332019-12-10 21:13:33231 std::move(callback).Run(target_path, disp,
Joe DeBlasioc427ca032020-01-17 19:15:43232 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, mcs,
danakja3cfb8332019-12-10 21:13:33233 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42234 }
[email protected]422a7d12013-10-21 12:10:42235};
236
Denis Kuznetsov32099f32017-12-15 14:04:57237#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20238class FakeBackgroundModeManager : public BackgroundModeManager {
239 public:
240 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54241 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
242 &g_browser_process->profile_manager()
243 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20244 suspended_(false) {}
245
Daniel Chenga542fca2014-10-21 09:51:29246 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20247 BackgroundModeManager::SuspendBackgroundMode();
248 suspended_ = true;
249 }
250
Daniel Chenga542fca2014-10-21 09:51:29251 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20252 BackgroundModeManager::ResumeBackgroundMode();
253 suspended_ = false;
254 }
255
256 bool IsBackgroundModeSuspended() {
257 return suspended_;
258 }
259
260 private:
261 bool suspended_;
262
263 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
264};
Denis Kuznetsov32099f32017-12-15 14:04:57265#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20266
[email protected]2e9d79f2013-08-16 05:45:56267} // namespace
268
Avi Drissman78267f52019-01-16 21:21:42269class BrowserCloseManagerBrowserTest : public InProcessBrowserTest {
[email protected]2e9d79f2013-08-16 05:45:56270 protected:
Daniel Chenga542fca2014-10-21 09:51:29271 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56272 SessionStartupPref::SetStartupPref(
273 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
274 browsers_.push_back(browser());
Colin Blundellfb2cfb2b2018-01-18 18:50:09275 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
276 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
277 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56278 }
279
avi556c05022014-12-22 23:31:43280 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]f9357a442014-05-15 18:44:07281#if defined(OS_CHROMEOS)
282 command_line->AppendSwitch(
283 chromeos::switches::kIgnoreUserProfileMappingForTests);
284#endif
[email protected]2e9d79f2013-08-16 05:45:56285 }
286
[email protected]edfca702013-08-16 08:58:14287 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09288 ASSERT_TRUE(embedded_test_server()->Started());
289
290 GURL slow_download_url = embedded_test_server()->GetURL(
291 content::SlowDownloadHttpResponse::kKnownSizeUrl);
292
[email protected]edfca702013-08-16 08:58:14293 content::DownloadTestObserverInProgress observer(
294 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
295 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09296 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14297 ui_test_utils::BROWSER_TEST_NONE);
298 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23299 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
300 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14301 }
302
avi336125f72017-05-06 22:25:40303 void PrepareForDialog(content::WebContents* web_contents) {
304 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46305 }
306
avi336125f72017-05-06 22:25:40307 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46308 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40309 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46310 }
311
Evan Stade4487c452019-07-11 00:58:06312 void WaitForAllBrowsersToClose() {
Evan Stade67ddae22019-08-30 15:33:30313 for (size_t i = 0U; i < browsers_.size(); ++i)
Evan Stade4487c452019-07-11 00:58:06314 ui_test_utils::WaitForBrowserToClose();
315 }
316
[email protected]2e9d79f2013-08-16 05:45:56317 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56318};
319
Avi Drissman78267f52019-01-16 21:21:42320IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56321 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
322 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40323 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46324
Evan Stade67ddae22019-08-30 15:33:30325 RepeatedNotificationObserver cancel_observer(
326 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20327 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32328 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30329 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56330 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
331 EXPECT_EQ(1, browser()->tab_strip_model()->count());
332
[email protected]0c95faf42013-10-28 06:27:20333 chrome::CloseAllBrowsersAndQuit();
Evan Stade67ddae22019-08-30 15:33:30334 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06335 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56336 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37337 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56338}
339
Avi Drissman78267f52019-01-16 21:21:42340IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56341 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56342 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
343 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40344 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46345
Evan Stade67ddae22019-08-30 15:33:30346 RepeatedNotificationObserver cancel_observer(
347 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20348 chrome::CloseAllBrowsersAndQuit();
349 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32350 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30351 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56352 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
353 EXPECT_EQ(1, browser()->tab_strip_model()->count());
354
[email protected]0c95faf42013-10-28 06:27:20355 chrome::CloseAllBrowsersAndQuit();
356 chrome::CloseAllBrowsersAndQuit();
Evan Stade67ddae22019-08-30 15:33:30357 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06358 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56359 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37360 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56361}
362
Avi Drissman78267f52019-01-16 21:21:42363IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56364 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
365 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
366 AddBlankTabAndShow(browser());
367 ASSERT_NO_FATAL_FAILURE(
368 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40369 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46370
Evan Stade67ddae22019-08-30 15:33:30371 RepeatedNotificationObserver cancel_observer(
372 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20373 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32374 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30375 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56376 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
377
378 browser()->tab_strip_model()
379 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
380 content::TestNavigationObserver navigation_observer(
381 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19382 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
383 browser(), GURL(chrome::kChromeUIVersionURL),
384 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32385 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56386 navigation_observer.Wait();
387
[email protected]0c95faf42013-10-28 06:27:20388 chrome::CloseAllBrowsersAndQuit();
Evan Stade4487c452019-07-11 00:58:06389 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56390 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37391 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56392}
393
394// Test that the tab closed after the aborted shutdown attempt is not re-opened
395// when restoring the session.
benwells70cc93222017-06-29 05:42:36396// Flaky on Windows trybots, see https://crbug.com/737860.
397#if defined(OS_WIN)
398#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
399#else
400#define MAYBE_TestSessionRestore TestSessionRestore
401#endif
Avi Drissman78267f52019-01-16 21:21:42402IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36403 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56404 // The testing framework launches Chrome with about:blank as args.
405 EXPECT_EQ(2, browser()->tab_strip_model()->count());
406 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
407 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
408 EXPECT_EQ(GURL("about:blank"),
409 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
410}
411
412// Test that browser windows are only closed if all browsers are ready to close
413// and that all beforeunload dialogs are shown again after a cancel.
Evan Stade67ddae22019-08-30 15:33:30414IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56415 browsers_.push_back(CreateBrowser(browser()->profile()));
416 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
417 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
418 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
419 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40420 PrepareForDialog(browsers_[0]);
421 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56422
423 // Cancel shutdown on the first beforeunload event.
424 {
Evan Stade67ddae22019-08-30 15:33:30425 RepeatedNotificationObserver cancel_observer(
426 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20427 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32428 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30429 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56430 }
431 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
432 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
433 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
434
435 // Cancel shutdown on the second beforeunload event.
436 {
Evan Stade67ddae22019-08-30 15:33:30437 RepeatedNotificationObserver cancel_observer(
438 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20439 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32440 ASSERT_NO_FATAL_FAILURE(AcceptClose());
441 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30442 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56443 }
444 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
445 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
446 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
447
448 // Allow shutdown for both beforeunload events.
[email protected]0c95faf42013-10-28 06:27:20449 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32450 ASSERT_NO_FATAL_FAILURE(AcceptClose());
451 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06452 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56453 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37454 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56455}
456
457// Test that tabs in the same window with a beforeunload event that hangs are
458// treated the same as the user accepting the close, but do not close the tab
459// early.
Avi Drissman78267f52019-01-16 21:21:42460IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Evan Stade67ddae22019-08-30 15:33:30461 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56462 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
463 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
464 AddBlankTabAndShow(browsers_[0]);
465 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
466 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
467 AddBlankTabAndShow(browsers_[0]);
468 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
469 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46470 // Disable the hang monitor in the tab that is not expected to hang, so that
471 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40472 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56473
Evan Stade67ddae22019-08-30 15:33:30474 RepeatedNotificationObserver cancel_observer(
475 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20476 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32477 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30478 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56479 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
480 // All tabs should still be open.
481 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
482
[email protected]0c95faf42013-10-28 06:27:20483 chrome::CloseAllBrowsersAndQuit();
Evan Stade67ddae22019-08-30 15:33:30484
485 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06486 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56487 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37488 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56489}
490
491// Test that tabs in different windows with a beforeunload event that hangs are
492// treated the same as the user accepting the close, but do not close the tab
493// early.
Avi Drissman78267f52019-01-16 21:21:42494IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56495 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56496 browsers_.push_back(CreateBrowser(browser()->profile()));
497 browsers_.push_back(CreateBrowser(browser()->profile()));
498 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
499 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
500 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
501 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
502 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
503 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46504 // Disable the hang monitor in the tab that is not expected to hang, so that
505 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40506 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56507
Evan Stade67ddae22019-08-30 15:33:30508 RepeatedNotificationObserver cancel_observer(
509 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20510 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32511 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30512 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56513 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
514 // All windows should still be open.
515 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
516 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
517 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
518
[email protected]0c95faf42013-10-28 06:27:20519 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32520 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06521
522 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56523}
524
metaflow73fc9552016-05-23 18:26:48525// Test that tabs that are slow to respond are not closed prematurely.
526// Regression for crbug.com/365052 caused some of tabs to be closed even if
527// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36528// Flaky on ChromeOS ASan. https://crbug.com/805457
529#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
530#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
531#else
532#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
533#endif
Avi Drissman78267f52019-01-16 21:21:42534IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Evan Stade67ddae22019-08-30 15:33:30535 TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48536 const int kTabCount = 5;
537 const int kResposiveTabIndex = 2;
538 // Create tab strip with all tabs except one responding after
539 // RenderViewHostImpl::kUnloadTimeoutMS.
540 // Minimum configuration is two slow tabs and then responsive tab.
541 // But we also want to check how slow tabs behave in tail.
542 for (int i = 0; i < kTabCount; i++) {
543 if (i)
544 AddBlankTabAndShow(browsers_[0]);
545 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
546 browsers_[0],
547 embedded_test_server()->GetURL((i == kResposiveTabIndex)
548 ? "/beforeunload.html"
549 : "/beforeunload_slow.html")));
550 }
metaflow812bb8542016-05-24 21:10:46551 // Disable the hang monitor in the tab that is not expected to hang, so that
552 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40553 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46554 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
555
Evan Stade67ddae22019-08-30 15:33:30556 RepeatedNotificationObserver cancel_observer(
557 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
metaflow73fc9552016-05-23 18:26:48558 chrome::CloseAllBrowsersAndQuit();
559 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30560 cancel_observer.Wait();
metaflow73fc9552016-05-23 18:26:48561 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
562
563 // All tabs should still be open.
564 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
metaflow73fc9552016-05-23 18:26:48565
566 // Quit, this time accepting close confirmation dialog.
567 chrome::CloseAllBrowsersAndQuit();
Evan Stade67ddae22019-08-30 15:33:30568 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06569 ui_test_utils::WaitForBrowserToClose();
metaflow73fc9552016-05-23 18:26:48570 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
571 EXPECT_TRUE(BrowserList::GetInstance()->empty());
572}
573
574// Test that tabs in different windows with a slow beforeunload event response
575// are treated the same as the user accepting the close, but do not close the
576// tab early.
577// Regression for crbug.com/365052 caused CHECK in tabstrip.
Evan Stade67ddae22019-08-30 15:33:30578// Flaky: https://crbug.com/819541
Avi Drissman78267f52019-01-16 21:21:42579IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Evan Stade67ddae22019-08-30 15:33:30580 DISABLED_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48581 const int kBrowserCount = 5;
Evan Stade67ddae22019-08-30 15:33:30582 const int kResposiveBrowserIndex = 2;
metaflow73fc9552016-05-23 18:26:48583 // Create multiple browsers with all tabs except one responding after
584 // RenderViewHostImpl::kUnloadTimeoutMS .
585 // Minimum configuration is just one browser with slow tab and then
586 // browser with responsive tab.
587 // But we also want to check how slow tabs behave in tail and make test
588 // more robust.
589 for (int i = 0; i < kBrowserCount; i++) {
590 if (i)
591 browsers_.push_back(CreateBrowser(browser()->profile()));
592 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
593 browsers_[i],
Evan Stade67ddae22019-08-30 15:33:30594 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
metaflow73fc9552016-05-23 18:26:48595 ? "/beforeunload.html"
596 : "/beforeunload_slow.html")));
597 }
metaflow812bb8542016-05-24 21:10:46598 // Disable the hang monitor in the tab that is not expected to hang, so that
599 // the dialog is guaranteed to show.
Evan Stade67ddae22019-08-30 15:33:30600 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48601
Evan Stade67ddae22019-08-30 15:33:30602 RepeatedNotificationObserver cancel_observer(
603 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
metaflow73fc9552016-05-23 18:26:48604 chrome::CloseAllBrowsersAndQuit();
605 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30606 cancel_observer.Wait();
metaflow73fc9552016-05-23 18:26:48607 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
608
609 // All windows should still be open.
610 for (int i = 0; i < kBrowserCount; i++)
611 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
612
613 // Quit, this time accepting close confirmation dialog.
metaflow73fc9552016-05-23 18:26:48614 chrome::CloseAllBrowsersAndQuit();
615 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06616 WaitForAllBrowsersToClose();
metaflow73fc9552016-05-23 18:26:48617 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
618 EXPECT_TRUE(BrowserList::GetInstance()->empty());
619}
620
Jan Krcal9a23f222017-08-01 14:13:07621// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
622#if defined(OS_WIN) && !defined(NDEBUG)
623#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
624#else
625#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
626#endif
627
[email protected]2e9d79f2013-08-16 05:45:56628// Test that a window created during shutdown is closed.
Avi Drissman78267f52019-01-16 21:21:42629IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07630 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56631 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
632 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40633 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56634
[email protected]0c95faf42013-10-28 06:27:20635 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56636 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32637 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06638 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56639 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37640 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56641}
642
643// Test that a window created during shutdown with a beforeunload handler can
644// cancel the shutdown.
Avi Drissman78267f52019-01-16 21:21:42645IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01646 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56647 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
648 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40649 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56650
Evan Stade67ddae22019-08-30 15:33:30651 RepeatedNotificationObserver cancel_observer(
652 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20653 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56654 browsers_.push_back(CreateBrowser(browser()->profile()));
655 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
656 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40657 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32658 ASSERT_NO_FATAL_FAILURE(AcceptClose());
659 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30660 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56661 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
662 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
663 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
664
665 // Allow shutdown for both beforeunload dialogs.
[email protected]0c95faf42013-10-28 06:27:20666 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32667 ASSERT_NO_FATAL_FAILURE(AcceptClose());
668 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06669 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56670 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37671 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56672}
673
674// Test that tabs added during shutdown are closed.
Avi Drissman78267f52019-01-16 21:21:42675IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46676 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56677 browsers_.push_back(CreateBrowser(browser()->profile()));
678 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
679 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
680 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
681 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40682 PrepareForDialog(browsers_[0]);
683 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56684
[email protected]0c95faf42013-10-28 06:27:20685 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32686 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56687 AddBlankTabAndShow(browsers_[0]);
688 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32689 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06690 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56691 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37692 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56693}
694
695// Test that tabs created during shutdown with beforeunload handlers can cancel
696// the shutdown.
metaflow812bb8542016-05-24 21:10:46697
Avi Drissman78267f52019-01-16 21:21:42698IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46699 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56700 browsers_.push_back(CreateBrowser(browser()->profile()));
701 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
702 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
703 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
704 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40705 PrepareForDialog(browsers_[0]);
706 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46707
Evan Stade67ddae22019-08-30 15:33:30708 RepeatedNotificationObserver cancel_observer(
709 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20710 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32711 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56712 AddBlankTabAndShow(browsers_[0]);
713 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
714 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
715 AddBlankTabAndShow(browsers_[1]);
716 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
717 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40718 PrepareForDialog(browsers_[0]);
719 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32720 ASSERT_NO_FATAL_FAILURE(AcceptClose());
721 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30722 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56723 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
724 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
725 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
726
[email protected]0c95faf42013-10-28 06:27:20727 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32728 ASSERT_NO_FATAL_FAILURE(AcceptClose());
729 ASSERT_NO_FATAL_FAILURE(AcceptClose());
730 ASSERT_NO_FATAL_FAILURE(AcceptClose());
731 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56732
Evan Stade4487c452019-07-11 00:58:06733 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56734 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37735 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56736}
737
rogermf18929c2017-04-19 19:59:33738// TODO(crbug/713201):
739// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
740#if defined(OS_MACOSX)
741#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
742#else
743#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
744#endif
745
Avi Drissman78267f52019-01-16 21:21:42746IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33747 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40748 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
749 browser(), embedded_test_server()->GetURL("/title1.html")));
750
751 // Open second window.
752 ui_test_utils::NavigateToURLWithDisposition(
753 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
754 WindowOpenDisposition::NEW_WINDOW,
755 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
756 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
757 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
758 ? BrowserList::GetInstance()->get(0)
759 : BrowserList::GetInstance()->get(1);
760 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
761
762 // Let's work with second window only.
763 // This page has beforeunload handler already.
764 EXPECT_TRUE(browser2->tab_strip_model()
765 ->GetWebContentsAt(0)
Alex Moshchuk5bcd7c12019-11-12 22:55:31766 ->NeedToFireBeforeUnloadOrUnload());
mkolom65a0b3c2017-03-02 06:11:40767 // This page doesn't have beforeunload handler. Yet.
768 ui_test_utils::NavigateToURLWithDisposition(
769 browser2, embedded_test_server()->GetURL("/title2.html"),
770 WindowOpenDisposition::NEW_FOREGROUND_TAB,
Fergal Dalyffa9bba2020-01-27 23:45:02771 ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
mkolom65a0b3c2017-03-02 06:11:40772 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
773 EXPECT_FALSE(browser2->tab_strip_model()
774 ->GetWebContentsAt(1)
Alex Moshchuk5bcd7c12019-11-12 22:55:31775 ->NeedToFireBeforeUnloadOrUnload());
mkolom65a0b3c2017-03-02 06:11:40776 EXPECT_EQ(2, browser2->tab_strip_model()->count());
777
avi336125f72017-05-06 22:25:40778 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40779
780 // The test.
781
782 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
mkolom65a0b3c2017-03-02 06:11:40783 chrome::CloseWindow(browser2);
784 // Just to be sure CloseWindow doesn't have asynchronous tasks
785 // that could have an impact.
Evan Stade67ddae22019-08-30 15:33:30786 content::RunAllPendingInMessageLoop();
mkolom65a0b3c2017-03-02 06:11:40787
788 // Closing browser shouldn't happen because of beforeunload handler.
789 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
790 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
791 // yet.
792 ASSERT_TRUE(content::ExecuteScript(
David Benjamin5a792652018-06-08 02:15:42793 browser2->tab_strip_model()->GetWebContentsAt(1),
mkolom65a0b3c2017-03-02 06:11:40794 "window.addEventListener('beforeunload', "
795 "function(event) { event.returnValue = 'Foo'; });"));
796 EXPECT_TRUE(browser2->tab_strip_model()
797 ->GetWebContentsAt(1)
Alex Moshchuk5bcd7c12019-11-12 22:55:31798 ->NeedToFireBeforeUnloadOrUnload());
mkolom65a0b3c2017-03-02 06:11:40799 // Accept closing the first tab.
800 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade67ddae22019-08-30 15:33:30801 // Just to be sure accepting a dialog doesn't have asynchronous tasks
802 // that could have an impact.
803 content::RunAllPendingInMessageLoop();
mkolom65a0b3c2017-03-02 06:11:40804 // It shouldn't close the whole window/browser.
805 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
806 EXPECT_EQ(2, browser2->tab_strip_model()->count());
807 // Accept closing the second tab.
Evan Stade67ddae22019-08-30 15:33:30808 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06809 ui_test_utils::WaitForBrowserToClose();
mkolom65a0b3c2017-03-02 06:11:40810 // Now the second window/browser should be closed.
811 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
812 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
813 EXPECT_EQ(1u, restore_observer.changes_count());
814
815 // Restore the closed browser.
mkolom65a0b3c2017-03-02 06:11:40816 chrome::OpenWindowWithRestoredTabs(browser()->profile());
mkolom65a0b3c2017-03-02 06:11:40817 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
818 browser2 = BrowserList::GetInstance()->get(0) != browser()
819 ? BrowserList::GetInstance()->get(0)
820 : BrowserList::GetInstance()->get(1);
821
822 // Check the restored browser contents.
823 EXPECT_EQ(2, browser2->tab_strip_model()->count());
824 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
825 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
826 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
827 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
828}
829
Avi Drissman78267f52019-01-16 21:21:42830IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42831 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56832 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
833 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40834 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46835
Evan Stade67ddae22019-08-30 15:33:30836 RepeatedNotificationObserver cancel_observer(
837 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20838 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56839
840 browsers_.push_back(CreateBrowser(browser()->profile()));
841 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
842 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40843 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56844 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32845 ASSERT_NO_FATAL_FAILURE(CancelClose());
846 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30847 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56848 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
849 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
850 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
851
[email protected]0c95faf42013-10-28 06:27:20852 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56853 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32854 ASSERT_NO_FATAL_FAILURE(AcceptClose());
855 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56856
Evan Stade4487c452019-07-11 00:58:06857 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56858 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37859 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56860}
861
Avi Drissman78267f52019-01-16 21:21:42862IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46863 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56864 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
865 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40866 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46867
Evan Stade67ddae22019-08-30 15:33:30868 RepeatedNotificationObserver cancel_observer(
869 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20870 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56871
872 browsers_.push_back(CreateBrowser(browser()->profile()));
873 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
874 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40875 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56876 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32877 ASSERT_NO_FATAL_FAILURE(CancelClose());
878 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30879 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56880 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
881 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
882 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
883
[email protected]0c95faf42013-10-28 06:27:20884 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56885 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32886 ASSERT_NO_FATAL_FAILURE(AcceptClose());
887 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56888
Evan Stade4487c452019-07-11 00:58:06889 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56890 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37891 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56892}
893
Avi Drissman78267f52019-01-16 21:21:42894IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56895 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56896 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
897 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
898 browsers_.push_back(CreateBrowser(browser()->profile()));
899 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
900 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40901 PrepareForDialog(browsers_[0]);
902 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46903
Evan Stade67ddae22019-08-30 15:33:30904 RepeatedNotificationObserver cancel_observer(
905 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20906 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56907
908 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32909 ASSERT_NO_FATAL_FAILURE(CancelClose());
Evan Stade67ddae22019-08-30 15:33:30910 cancel_observer.Wait();
[email protected]2e9d79f2013-08-16 05:45:56911 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
912 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
913 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
914
[email protected]0c95faf42013-10-28 06:27:20915 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56916 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32917 ASSERT_NO_FATAL_FAILURE(AcceptClose());
918 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56919
Evan Stade4487c452019-07-11 00:58:06920 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56921 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37922 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56923}
924
jackhouae8e6e52015-05-29 06:36:46925// Mac has its own in-progress download prompt in app_controller_mac.mm, so
926// BrowserCloseManager should simply close all browsers. If there are no
927// browsers, it should not crash.
928#if defined(OS_MACOSX)
Avi Drissman78267f52019-01-16 21:21:42929IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46930 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
931
jackhouae8e6e52015-05-29 06:36:46932 TestBrowserCloseManager::AttemptClose(
933 TestBrowserCloseManager::NO_USER_CHOICE);
Evan Stade4487c452019-07-11 00:58:06934 WaitForAllBrowsersToClose();
jackhouae8e6e52015-05-29 06:36:46935 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37936 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44937 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46938
939 // Attempting to close again should not crash.
940 TestBrowserCloseManager::AttemptClose(
941 TestBrowserCloseManager::NO_USER_CHOICE);
942}
943#else // defined(OS_MACOSX)
944
[email protected]422a7d12013-10-21 12:10:42945// Test shutdown with a DANGEROUS_URL download undecided.
Avi Drissman78267f52019-01-16 21:21:42946IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Mattias Nissler441cc652018-04-05 00:08:56947 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42948 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35949 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42950 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:44951 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:09952 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:42953
954 // Run a dangerous download, but the user doesn't make a decision.
955 // This .swf normally would be categorized as DANGEROUS_FILE, but
956 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:43957 GURL download_url(
958 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:42959 content::DownloadTestObserverInterrupted observer(
960 content::BrowserContext::GetDownloadManager(browser()->profile()),
961 1,
962 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
963 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19964 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:42965 ui_test_utils::BROWSER_TEST_NONE);
966 observer.WaitForFinished();
967
968 // Check that the download manager has the expected state.
969 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
970 browser()->profile())->InProgressCount());
971 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
972 browser()->profile())->NonMaliciousInProgressCount());
973
974 // Close the browser with no user action.
[email protected]422a7d12013-10-21 12:10:42975 TestBrowserCloseManager::AttemptClose(
976 TestBrowserCloseManager::NO_USER_CHOICE);
Evan Stade4487c452019-07-11 00:58:06977 WaitForAllBrowsersToClose();
[email protected]422a7d12013-10-21 12:10:42978 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37979 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:42980}
981
[email protected]edfca702013-08-16 08:58:14982// Test shutdown with a download in progress.
Avi Drissman78267f52019-01-16 21:21:42983IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:14984 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
985 content::TestNavigationObserver navigation_observer(
986 browser()->tab_strip_model()->GetActiveWebContents(), 1);
987 TestBrowserCloseManager::AttemptClose(
988 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
989 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
990 navigation_observer.Wait();
991 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
992 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
993
[email protected]edfca702013-08-16 08:58:14994 TestBrowserCloseManager::AttemptClose(
995 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:06996 WaitForAllBrowsersToClose();
[email protected]edfca702013-08-16 08:58:14997 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37998 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:38999 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441000 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381001 else
peterccb33e82017-05-02 19:00:441002 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141003}
1004
sammccd347712015-03-13 22:02:321005// Test shutdown with a download in progress in an off-the-record profile.
Avi Drissman78267f52019-01-16 21:21:421006IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321007 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321008 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321009 Browser* otr_browser = CreateBrowser(otr_profile);
1010 {
sammccd347712015-03-13 22:02:321011 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061012 ui_test_utils::WaitForBrowserToClose();
sammccd347712015-03-13 22:02:321013 }
1014 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1015 content::TestNavigationObserver navigation_observer(
1016 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1017 TestBrowserCloseManager::AttemptClose(
1018 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1019 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1020 navigation_observer.Wait();
1021 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1022 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1023
sammccd347712015-03-13 22:02:321024 TestBrowserCloseManager::AttemptClose(
1025 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:061026 ui_test_utils::WaitForBrowserToClose();
sammccd347712015-03-13 22:02:321027 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371028 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441029 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321030}
1031
Asanka Herathd1341dc2017-04-19 18:28:391032// Test shutdown with a download in progress in a regular profile an inconito
1033// browser is opened and closed. While there are active downloads, closing the
1034// incognito window shouldn't block on the active downloads which belong to the
1035// parent profile.
Weza78944d2018-05-17 16:56:291036// TODO(https://crbug.com/844019): Fix the notification expectation around the
1037// call to AttemptClose.
Avi Drissman78267f52019-01-16 21:21:421038IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291039 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391040 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391041 Browser* otr_browser = CreateBrowser(otr_profile);
1042 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1043
1044 content::TestNavigationObserver navigation_observer(
1045 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1046 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1047 navigation_observer.Wait();
1048
1049 int num_downloads_blocking = 0;
1050 ASSERT_EQ(
Min Qin276df8c2019-08-30 21:05:281051 Browser::DownloadCloseType::kOk,
Asanka Herathd1341dc2017-04-19 18:28:391052 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1053 ASSERT_EQ(0, num_downloads_blocking);
1054
1055 {
Asanka Herathd1341dc2017-04-19 18:28:391056 otr_browser->window()->Close();
Evan Stade4487c452019-07-11 00:58:061057 ui_test_utils::WaitForBrowserToClose();
Asanka Herathd1341dc2017-04-19 18:28:391058 }
1059
1060 ASSERT_EQ(
Min Qin276df8c2019-08-30 21:05:281061 Browser::DownloadCloseType::kBrowserShutdown,
Asanka Herathd1341dc2017-04-19 18:28:391062 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1063 ASSERT_EQ(1, num_downloads_blocking);
1064
1065 {
Asanka Herathd1341dc2017-04-19 18:28:391066 TestBrowserCloseManager::AttemptClose(
1067 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:061068 ui_test_utils::WaitForBrowserToClose();
Asanka Herathd1341dc2017-04-19 18:28:391069 }
1070
1071 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1072 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1073 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441074 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391075 else
peterccb33e82017-05-02 19:00:441076 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391077}
1078
[email protected]edfca702013-08-16 08:58:141079// Test shutdown with a download in progress from one profile, where the only
1080// open windows are for another profile.
Avi Drissman78267f52019-01-16 21:21:421081IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141082 TestWithDownloadsFromDifferentProfiles) {
1083 ProfileManager* profile_manager = g_browser_process->profile_manager();
Hui Yingstbfedafc2019-05-14 00:56:071084 std::unique_ptr<Profile> other_profile;
jam3f2d3932017-04-26 20:28:511085 {
1086 base::FilePath path =
1087 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131088 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511089 if (!base::PathExists(path))
1090 ASSERT_TRUE(base::CreateDirectory(path));
1091 other_profile =
1092 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1093 }
Hui Yingstbfedafc2019-05-14 00:56:071094 Profile* other_profile_ptr = other_profile.get();
Daniel Hosseinian8f0b7482019-06-07 18:21:041095 profile_manager->RegisterTestingProfile(std::move(other_profile), true,
1096 false);
Hui Yingstbfedafc2019-05-14 00:56:071097 Browser* other_profile_browser = CreateBrowser(other_profile_ptr);
[email protected]edfca702013-08-16 08:58:141098
[email protected]edfca702013-08-16 08:58:141099 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1100 {
[email protected]edfca702013-08-16 08:58:141101 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061102 ui_test_utils::WaitForBrowserToClose();
[email protected]edfca702013-08-16 08:58:141103 }
1104
1105 // When the shutdown is cancelled, the downloads page should be opened in a
1106 // browser for that profile. Because there are no browsers for that profile, a
1107 // new browser should be opened.
[email protected]edfca702013-08-16 08:58:141108 TestBrowserCloseManager::AttemptClose(
1109 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1110 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
Evan Stade9ce42602019-07-29 23:56:141111 Browser* opened_browser = BrowserList::GetInstance()->GetLastActive();
1112 ASSERT_TRUE(opened_browser);
1113 EXPECT_NE(other_profile_ptr, opened_browser->profile());
[email protected]edfca702013-08-16 08:58:141114 EXPECT_EQ(
1115 GURL(chrome::kChromeUIDownloadsURL),
1116 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1117 EXPECT_EQ(GURL("about:blank"),
1118 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1119 ->GetURL());
1120
[email protected]edfca702013-08-16 08:58:141121 TestBrowserCloseManager::AttemptClose(
1122 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:061123 ui_test_utils::WaitForBrowserToClose();
1124 ui_test_utils::WaitForBrowserToClose();
[email protected]edfca702013-08-16 08:58:141125 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371126 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381127 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441128 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381129 else
peterccb33e82017-05-02 19:00:441130 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141131}
1132
Vasilii Sukhanov4a65b862017-08-04 10:01:371133// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141134// Test shutdown with downloads in progress and beforeunload handlers.
Avi Drissman78267f52019-01-16 21:21:421135IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371136 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141137 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1138 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1139 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401140 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141141
Evan Stade67ddae22019-08-30 15:33:301142 content::WindowedNotificationObserver cancel_observer(
1143 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1144 content::NotificationService::AllSources());
[email protected]edfca702013-08-16 08:58:141145 TestBrowserCloseManager::AttemptClose(
1146 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321147 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade67ddae22019-08-30 15:33:301148 cancel_observer.Wait();
[email protected]edfca702013-08-16 08:58:141149 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1150
[email protected]edfca702013-08-16 08:58:141151 TestBrowserCloseManager::AttemptClose(
1152 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321153 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:061154 ui_test_utils::WaitForBrowserToClose();
[email protected]edfca702013-08-16 08:58:141155 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371156 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141157}
1158
jackhouae8e6e52015-05-29 06:36:461159#endif // defined(OS_MACOSX)
1160
Denis Kuznetsov32099f32017-12-15 14:04:571161#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1162
[email protected]0c95faf42013-10-28 06:27:201163class BrowserCloseManagerWithBackgroundModeBrowserTest
1164 : public BrowserCloseManagerBrowserTest {
1165 public:
1166 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1167
Daniel Chenga542fca2014-10-21 09:51:291168 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201169 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1170 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351171 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201172 }
1173
1174 bool IsBackgroundModeSuspended() {
1175 return static_cast<FakeBackgroundModeManager*>(
1176 g_browser_process->background_mode_manager())
1177 ->IsBackgroundModeSuspended();
1178 }
1179
1180 private:
1181 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1182};
1183
1184// Check that background mode is suspended when closing all browsers unless we
1185// are quitting and that background mode is resumed when a new browser window is
1186// opened.
Avi Drissman78267f52019-01-16 21:21:421187IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
[email protected]0c95faf42013-10-28 06:27:201188 CloseAllBrowsersWithBackgroundMode) {
1189 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351190 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201191 Profile* profile = browser()->profile();
1192 {
dgn02377782016-03-12 00:58:381193 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1194 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201195 chrome::CloseAllBrowsers();
Evan Stade4487c452019-07-11 00:58:061196 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201197 }
1198 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371199 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201200 EXPECT_TRUE(IsBackgroundModeSuspended());
1201
1202 // Background mode should be resumed when a new browser window is opened.
scottmg851949002016-02-09 20:09:441203 chrome::NewEmptyWindow(profile);
Evan Stade9ce42602019-07-29 23:56:141204 ui_test_utils::WaitForBrowserToOpen();
dgn02377782016-03-12 00:58:381205 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201206 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201207
1208 // Background mode should not be suspended when quitting.
1209 chrome::CloseAllBrowsersAndQuit();
Evan Stade4487c452019-07-11 00:58:061210 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201211 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371212 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201213 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201214}
1215
1216// Check that closing the last browser window individually does not affect
1217// background mode.
Avi Drissman78267f52019-01-16 21:21:421218IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161219 DISABLED_CloseSingleBrowserWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201220 EXPECT_FALSE(IsBackgroundModeSuspended());
1221 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061222 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201223 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371224 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201225 EXPECT_FALSE(IsBackgroundModeSuspended());
1226}
1227
1228// Check that closing all browsers with no browser windows open suspends
1229// background mode but does not cause Chrome to quit.
Avi Drissman78267f52019-01-16 21:21:421230IN_PROC_BROWSER_TEST_F(
1231 BrowserCloseManagerWithBackgroundModeBrowserTest,
1232 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201233 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381234 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1235 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201236 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061237 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201238 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371239 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201240 EXPECT_FALSE(IsBackgroundModeSuspended());
1241
1242 chrome::CloseAllBrowsers();
1243 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371244 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201245 EXPECT_TRUE(IsBackgroundModeSuspended());
1246}
1247
Denis Kuznetsov32099f32017-12-15 14:04:571248#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)