blob: 25a4c33e77138550625225444c4499e1efe9e6e1 [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
10#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:0511#include "base/files/scoped_temp_dir.h"
avi6846aef2015-12-26 01:09:3812#include "base/macros.h"
Gabriel Charette078e3662017-08-28 22:59:0413#include "base/run_loop.h"
jam3f2d3932017-04-26 20:28:5114#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3815#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2016#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1417#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5618#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3819#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4220#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4421#include "chrome/browser/download/download_core_service.h"
22#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0523#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5624#include "chrome/browser/lifetime/application_lifetime.h"
Avi Drissmand30927342018-05-22 15:04:2725#include "chrome/browser/lifetime/browser_shutdown.h"
[email protected]edfca702013-08-16 08:58:1426#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5627#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1428#include "chrome/browser/profiles/profile.h"
29#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4030#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5631#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2032#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3733#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1434#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5635#include "chrome/browser/ui/tabs/tab_strip_model.h"
Scott Violet6200d332018-02-23 21:29:2336#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5637#include "chrome/common/chrome_switches.h"
38#include "chrome/common/url_constants.h"
39#include "chrome/test/base/in_process_browser_test.h"
40#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4241#include "components/app_modal/javascript_app_modal_dialog.h"
42#include "components/app_modal/native_app_modal_dialog.h"
Min Qina9f487872018-02-09 20:43:2343#include "components/download/public/common/download_item.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5944#include "components/keep_alive_registry/keep_alive_types.h"
45#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4046#include "components/sessions/core/tab_restore_service.h"
47#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1448#include "content/public/browser/browser_context.h"
Gabriel Charette790754c2018-03-16 21:32:5949#include "content/public/browser/browser_thread.h"
[email protected]edfca702013-08-16 08:58:1450#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5651#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3452#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4653#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5654#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4055#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1456#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0957#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5658#include "content/public/test/test_navigation_observer.h"
59#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5660
[email protected]f9357a442014-05-15 18:44:0761#if defined(OS_CHROMEOS)
62#include "chromeos/chromeos_switches.h"
63#endif
64
[email protected]2e9d79f2013-08-16 05:45:5665namespace {
66
oshima0929be2a2014-11-19 22:21:0367app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5268 app_modal::JavaScriptAppModalDialog* dialog =
69 ui_test_utils::WaitForAppModalDialog();
70 CHECK(dialog->native_dialog());
71 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3272}
[email protected]2e9d79f2013-08-16 05:45:5673
avi336125f72017-05-06 22:25:4074// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4675// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3276void AcceptClose() {
77 GetNextDialog()->AcceptAppModalDialog();
78}
[email protected]2e9d79f2013-08-16 05:45:5679
avi336125f72017-05-06 22:25:4080// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4681// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3282void CancelClose() {
83 GetNextDialog()->CancelAppModalDialog();
84}
[email protected]2e9d79f2013-08-16 05:45:5685
Scott Violet1f106b582017-07-12 15:49:5886class RepeatedNotificationObserver : public content::NotificationObserver {
87 public:
88 explicit RepeatedNotificationObserver(int type, int count)
89 : num_outstanding_(count), running_(false) {
90 registrar_.Add(this, type, content::NotificationService::AllSources());
91 }
92
93 void Observe(int type,
94 const content::NotificationSource& source,
95 const content::NotificationDetails& details) override {
96 ASSERT_GT(num_outstanding_, 0);
97 if (!--num_outstanding_ && running_) {
98 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
99 run_loop_.QuitClosure());
100 }
101 }
102
103 void Wait() {
104 if (num_outstanding_ <= 0)
105 return;
106
107 running_ = true;
108 run_loop_.Run();
109 running_ = false;
Weza78944d2018-05-17 16:56:29110
111 EXPECT_LE(num_outstanding_, 0);
Scott Violet1f106b582017-07-12 15:49:58112 }
113
114 private:
115 int num_outstanding_;
116 content::NotificationRegistrar registrar_;
117 bool running_;
118 base::RunLoop run_loop_;
119
120 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
121};
122
mkolom65a0b3c2017-03-02 06:11:40123class TabRestoreServiceChangesObserver
124 : public sessions::TabRestoreServiceObserver {
125 public:
126 explicit TabRestoreServiceChangesObserver(Profile* profile)
127 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
128 if (service_)
129 service_->AddObserver(this);
130 }
131
132 ~TabRestoreServiceChangesObserver() override {
133 if (service_)
134 service_->RemoveObserver(this);
135 }
136
137 size_t changes_count() const { return changes_count_; }
138
139 private:
140 // sessions::TabRestoreServiceObserver:
141 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
142 changes_count_++;
143 }
144
145 // sessions::TabRestoreServiceObserver:
146 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
147 service_ = nullptr;
148 }
149
150 sessions::TabRestoreService* service_ = nullptr;
151 size_t changes_count_ = 0;
152
153 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
154};
155
[email protected]edfca702013-08-16 08:58:14156class TestBrowserCloseManager : public BrowserCloseManager {
157 public:
158 enum UserChoice {
159 USER_CHOICE_USER_CANCELS_CLOSE,
160 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42161 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14162 };
163
164 static void AttemptClose(UserChoice user_choice) {
165 scoped_refptr<BrowserCloseManager> browser_close_manager =
166 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20167 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14168 browser_close_manager->StartClosingBrowsers();
169 }
170
171 protected:
Daniel Chenga542fca2014-10-21 09:51:29172 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14173
Daniel Chenga542fca2014-10-21 09:51:29174 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14175 int download_count,
mostynb2b52d1db2014-10-07 02:47:17176 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42177 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14178 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42179 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14180 case USER_CHOICE_USER_CANCELS_CLOSE: {
181 callback.Run(false);
182 break;
183 }
184 case USER_CHOICE_USER_ALLOWS_CLOSE: {
185 callback.Run(true);
186 break;
187 }
188 }
189 }
190
191 private:
192 explicit TestBrowserCloseManager(UserChoice user_choice)
193 : user_choice_(user_choice) {}
194
195 UserChoice user_choice_;
196
197 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
198};
199
[email protected]422a7d12013-10-21 12:10:42200class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
201 public:
202 explicit TestDownloadManagerDelegate(Profile* profile)
203 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23204 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42205 }
Daniel Chenga542fca2014-10-21 09:51:29206 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42207
Daniel Chenga542fca2014-10-21 09:51:29208 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23209 download::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17210 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42211 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41212 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42213 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
214 item, dangerous_callback);
215 }
216
Asanka Herath1ba0e9f2017-04-03 18:48:53217 static void SetDangerous(const content::DownloadTargetCallback& callback,
218 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23219 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35220 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53221 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16222 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35223 callback.Run(target_path, disp,
224 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53225 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42226 }
[email protected]422a7d12013-10-21 12:10:42227};
228
Denis Kuznetsov32099f32017-12-15 14:04:57229#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20230class FakeBackgroundModeManager : public BackgroundModeManager {
231 public:
232 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54233 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
234 &g_browser_process->profile_manager()
235 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20236 suspended_(false) {}
237
Daniel Chenga542fca2014-10-21 09:51:29238 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20239 BackgroundModeManager::SuspendBackgroundMode();
240 suspended_ = true;
241 }
242
Daniel Chenga542fca2014-10-21 09:51:29243 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20244 BackgroundModeManager::ResumeBackgroundMode();
245 suspended_ = false;
246 }
247
248 bool IsBackgroundModeSuspended() {
249 return suspended_;
250 }
251
252 private:
253 bool suspended_;
254
255 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
256};
Denis Kuznetsov32099f32017-12-15 14:04:57257#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20258
[email protected]2e9d79f2013-08-16 05:45:56259} // namespace
260
261class BrowserCloseManagerBrowserTest
262 : public InProcessBrowserTest,
263 public testing::WithParamInterface<bool> {
264 protected:
Daniel Chenga542fca2014-10-21 09:51:29265 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56266 SessionStartupPref::SetStartupPref(
267 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
268 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14269 content::BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33270 content::BrowserThread::IO, FROM_HERE,
271 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
Colin Blundellfb2cfb2b2018-01-18 18:50:09272 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
273 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
274 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56275 }
276
avi556c05022014-12-22 23:31:43277 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56278 if (GetParam())
279 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07280#if defined(OS_CHROMEOS)
281 command_line->AppendSwitch(
282 chromeos::switches::kIgnoreUserProfileMappingForTests);
283#endif
[email protected]2e9d79f2013-08-16 05:45:56284 }
285
[email protected]edfca702013-08-16 08:58:14286 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09287 ASSERT_TRUE(embedded_test_server()->Started());
288
289 GURL slow_download_url = embedded_test_server()->GetURL(
290 content::SlowDownloadHttpResponse::kKnownSizeUrl);
291
[email protected]edfca702013-08-16 08:58:14292 content::DownloadTestObserverInProgress observer(
293 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
294 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09295 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14296 ui_test_utils::BROWSER_TEST_NONE);
297 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23298 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
299 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14300 }
301
avi336125f72017-05-06 22:25:40302 void PrepareForDialog(content::WebContents* web_contents) {
303 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46304 }
305
avi336125f72017-05-06 22:25:40306 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46307 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40308 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46309 }
310
[email protected]2e9d79f2013-08-16 05:45:56311 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56312};
313
314IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56315 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
316 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40317 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46318
Scott Violet1f106b582017-07-12 15:49:58319 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56320 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20321 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32322 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56323 cancel_observer.Wait();
324 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
325 EXPECT_EQ(1, browser()->tab_strip_model()->count());
326
Scott Violet1f106b582017-07-12 15:49:58327 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56328 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20329 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32330 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56331 close_observer.Wait();
332 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37333 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56334}
335
336IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
337 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56338 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
339 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40340 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46341
Scott Violet1f106b582017-07-12 15:49:58342 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56343 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20344 chrome::CloseAllBrowsersAndQuit();
345 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32346 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56347 cancel_observer.Wait();
348 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
349 EXPECT_EQ(1, browser()->tab_strip_model()->count());
350
Scott Violet1f106b582017-07-12 15:49:58351 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56352 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20353 chrome::CloseAllBrowsersAndQuit();
354 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32355 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56356 close_observer.Wait();
357 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37358 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56359}
360
wafflesb63234052015-08-07 02:44:02361IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46362 PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56363 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
364 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
365 AddBlankTabAndShow(browser());
366 ASSERT_NO_FATAL_FAILURE(
367 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40368 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46369
Scott Violet1f106b582017-07-12 15:49:58370 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56371 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20372 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32373 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56374 cancel_observer.Wait();
375 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
376
377 browser()->tab_strip_model()
378 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
379 content::TestNavigationObserver navigation_observer(
380 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19381 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
382 browser(), GURL(chrome::kChromeUIVersionURL),
383 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32384 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56385 navigation_observer.Wait();
386
Scott Violet1f106b582017-07-12 15:49:58387 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56388 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20389 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56390 close_observer.Wait();
391 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37392 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56393}
394
395// Test that the tab closed after the aborted shutdown attempt is not re-opened
396// when restoring the session.
benwells70cc93222017-06-29 05:42:36397// Flaky on Windows trybots, see https://crbug.com/737860.
398#if defined(OS_WIN)
399#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
400#else
401#define MAYBE_TestSessionRestore TestSessionRestore
402#endif
wafflesb63234052015-08-07 02:44:02403IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36404 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56405 // The testing framework launches Chrome with about:blank as args.
406 EXPECT_EQ(2, browser()->tab_strip_model()->count());
407 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
408 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
409 EXPECT_EQ(GURL("about:blank"),
410 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
411}
412
413// Test that browser windows are only closed if all browsers are ready to close
414// and that all beforeunload dialogs are shown again after a cancel.
[email protected]40cd13a2013-10-24 13:42:01415IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56416 browsers_.push_back(CreateBrowser(browser()->profile()));
417 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
418 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
419 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
420 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40421 PrepareForDialog(browsers_[0]);
422 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56423
424 // Cancel shutdown on the first beforeunload event.
425 {
Scott Violet1f106b582017-07-12 15:49:58426 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56427 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20428 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32429 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56430 cancel_observer.Wait();
431 }
432 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
433 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
434 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
435
436 // Cancel shutdown on the second beforeunload event.
437 {
Scott Violet1f106b582017-07-12 15:49:58438 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56439 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20440 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32441 ASSERT_NO_FATAL_FAILURE(AcceptClose());
442 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56443 cancel_observer.Wait();
444 }
445 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
446 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
447 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
448
449 // Allow shutdown for both beforeunload events.
Scott Violet1f106b582017-07-12 15:49:58450 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56451 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20452 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32453 ASSERT_NO_FATAL_FAILURE(AcceptClose());
454 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56455 close_observer.Wait();
456 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37457 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56458}
459
460// Test that tabs in the same window with a beforeunload event that hangs are
461// treated the same as the user accepting the close, but do not close the tab
462// early.
463IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46464 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56465 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
466 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
467 AddBlankTabAndShow(browsers_[0]);
468 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
469 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
470 AddBlankTabAndShow(browsers_[0]);
471 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
472 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46473 // Disable the hang monitor in the tab that is not expected to hang, so that
474 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40475 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56476
Scott Violet1f106b582017-07-12 15:49:58477 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56478 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20479 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32480 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56481 cancel_observer.Wait();
482 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
483 // All tabs should still be open.
484 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
485
Scott Violet1f106b582017-07-12 15:49:58486 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56487 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20488 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32489 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56490 close_observer.Wait();
491 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37492 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56493}
494
495// Test that tabs in different windows with a beforeunload event that hangs are
496// treated the same as the user accepting the close, but do not close the tab
497// early.
498IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
499 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56500 browsers_.push_back(CreateBrowser(browser()->profile()));
501 browsers_.push_back(CreateBrowser(browser()->profile()));
502 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
503 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
504 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
505 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
506 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
507 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46508 // Disable the hang monitor in the tab that is not expected to hang, so that
509 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40510 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56511
Scott Violet1f106b582017-07-12 15:49:58512 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56513 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20514 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32515 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56516 cancel_observer.Wait();
517 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
518 // All windows should still be open.
519 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
520 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
521 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
522
Scott Violet1f106b582017-07-12 15:49:58523 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56524 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20525 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32526 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56527 close_observer.Wait();
528 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37529 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56530}
531
metaflow73fc9552016-05-23 18:26:48532// Test that tabs that are slow to respond are not closed prematurely.
533// Regression for crbug.com/365052 caused some of tabs to be closed even if
534// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36535// Flaky on ChromeOS ASan. https://crbug.com/805457
536#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
537#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
538#else
539#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
540#endif
metaflow73fc9552016-05-23 18:26:48541IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36542 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48543 const int kTabCount = 5;
544 const int kResposiveTabIndex = 2;
545 // Create tab strip with all tabs except one responding after
546 // RenderViewHostImpl::kUnloadTimeoutMS.
547 // Minimum configuration is two slow tabs and then responsive tab.
548 // But we also want to check how slow tabs behave in tail.
549 for (int i = 0; i < kTabCount; i++) {
550 if (i)
551 AddBlankTabAndShow(browsers_[0]);
552 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
553 browsers_[0],
554 embedded_test_server()->GetURL((i == kResposiveTabIndex)
555 ? "/beforeunload.html"
556 : "/beforeunload_slow.html")));
557 }
metaflow812bb8542016-05-24 21:10:46558 // Disable the hang monitor in the tab that is not expected to hang, so that
559 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40560 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46561 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
562
Scott Violet1f106b582017-07-12 15:49:58563 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48564 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
565 chrome::CloseAllBrowsersAndQuit();
566 ASSERT_NO_FATAL_FAILURE(CancelClose());
567 cancel_observer.Wait();
568 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
569
570 // All tabs should still be open.
571 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58572 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48573 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
574
575 // Quit, this time accepting close confirmation dialog.
576 chrome::CloseAllBrowsersAndQuit();
577 ASSERT_NO_FATAL_FAILURE(AcceptClose());
578 close_observer.Wait();
579 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
580 EXPECT_TRUE(BrowserList::GetInstance()->empty());
581}
582
583// Test that tabs in different windows with a slow beforeunload event response
584// are treated the same as the user accepting the close, but do not close the
585// tab early.
586// Regression for crbug.com/365052 caused CHECK in tabstrip.
jdoerrie6cc3bd82018-03-07 16:42:56587// Flaky on Mac and Linux: https://crbug.com/819541
588#if defined(OS_LINUX) || defined(OS_MACOSX)
589#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
590 DISABLED_TestBeforeUnloadMultipleSlowWindows
591#else
592#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
593 TestBeforeUnloadMultipleSlowWindows
594#endif
metaflow73fc9552016-05-23 18:26:48595IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
jdoerrie6cc3bd82018-03-07 16:42:56596 MAYBE_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48597 const int kBrowserCount = 5;
598 const int kResposiveBrowserIndex = 2;
599 // Create multiple browsers with all tabs except one responding after
600 // RenderViewHostImpl::kUnloadTimeoutMS .
601 // Minimum configuration is just one browser with slow tab and then
602 // browser with responsive tab.
603 // But we also want to check how slow tabs behave in tail and make test
604 // more robust.
605 for (int i = 0; i < kBrowserCount; i++) {
606 if (i)
607 browsers_.push_back(CreateBrowser(browser()->profile()));
608 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
609 browsers_[i],
610 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
611 ? "/beforeunload.html"
612 : "/beforeunload_slow.html")));
613 }
metaflow812bb8542016-05-24 21:10:46614 // Disable the hang monitor in the tab that is not expected to hang, so that
615 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40616 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48617
Scott Violet1f106b582017-07-12 15:49:58618 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48619 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
620 chrome::CloseAllBrowsersAndQuit();
621 ASSERT_NO_FATAL_FAILURE(CancelClose());
622 cancel_observer.Wait();
623 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
624
625 // All windows should still be open.
626 for (int i = 0; i < kBrowserCount; i++)
627 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
628
629 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58630 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48631 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
632 chrome::CloseAllBrowsersAndQuit();
633 ASSERT_NO_FATAL_FAILURE(AcceptClose());
634 close_observer.Wait();
635 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
636 EXPECT_TRUE(BrowserList::GetInstance()->empty());
637}
638
Jan Krcal9a23f222017-08-01 14:13:07639// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
640#if defined(OS_WIN) && !defined(NDEBUG)
641#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
642#else
643#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
644#endif
645
[email protected]2e9d79f2013-08-16 05:45:56646// Test that a window created during shutdown is closed.
647IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07648 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56649 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
650 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40651 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56652
Scott Violet1f106b582017-07-12 15:49:58653 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56654 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20655 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56656 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32657 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56658 close_observer.Wait();
659 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37660 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56661}
662
663// Test that a window created during shutdown with a beforeunload handler can
664// cancel the shutdown.
665IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01666 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56667 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
668 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40669 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56670
Scott Violet1f106b582017-07-12 15:49:58671 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56672 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20673 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56674 browsers_.push_back(CreateBrowser(browser()->profile()));
675 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
676 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40677 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32678 ASSERT_NO_FATAL_FAILURE(AcceptClose());
679 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56680 cancel_observer.Wait();
681 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
682 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
683 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
684
685 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58686 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56687 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20688 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32689 ASSERT_NO_FATAL_FAILURE(AcceptClose());
690 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56691 close_observer.Wait();
692 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37693 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56694}
695
696// Test that tabs added during shutdown are closed.
697IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46698 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56699 browsers_.push_back(CreateBrowser(browser()->profile()));
700 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
701 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
702 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
703 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40704 PrepareForDialog(browsers_[0]);
705 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56706
Scott Violet1f106b582017-07-12 15:49:58707 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56708 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20709 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32710 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56711 AddBlankTabAndShow(browsers_[0]);
712 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32713 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56714 close_observer.Wait();
715 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37716 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56717}
718
719// Test that tabs created during shutdown with beforeunload handlers can cancel
720// the shutdown.
metaflow812bb8542016-05-24 21:10:46721
[email protected]2e9d79f2013-08-16 05:45:56722IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46723 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56724 browsers_.push_back(CreateBrowser(browser()->profile()));
725 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
726 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
727 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
728 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40729 PrepareForDialog(browsers_[0]);
730 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46731
Scott Violet1f106b582017-07-12 15:49:58732 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56733 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20734 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32735 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56736 AddBlankTabAndShow(browsers_[0]);
737 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
738 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
739 AddBlankTabAndShow(browsers_[1]);
740 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
741 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40742 PrepareForDialog(browsers_[0]);
743 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32744 ASSERT_NO_FATAL_FAILURE(AcceptClose());
745 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56746 cancel_observer.Wait();
747 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
748 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
749 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
750
Scott Violet1f106b582017-07-12 15:49:58751 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56752 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20753 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32754 ASSERT_NO_FATAL_FAILURE(AcceptClose());
755 ASSERT_NO_FATAL_FAILURE(AcceptClose());
756 ASSERT_NO_FATAL_FAILURE(AcceptClose());
757 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56758
759 close_observer.Wait();
760 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37761 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56762}
763
rogermf18929c2017-04-19 19:59:33764// TODO(crbug/713201):
765// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
766#if defined(OS_MACOSX)
767#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
768#else
769#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
770#endif
771
[email protected]2e9d79f2013-08-16 05:45:56772IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33773 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40774 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
775 // And it is difficult to fix this issue without fixing that one.
776 if (GetParam())
777 return;
778
mkolom65a0b3c2017-03-02 06:11:40779 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
780 browser(), embedded_test_server()->GetURL("/title1.html")));
781
782 // Open second window.
783 ui_test_utils::NavigateToURLWithDisposition(
784 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
785 WindowOpenDisposition::NEW_WINDOW,
786 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
787 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
788 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
789 ? BrowserList::GetInstance()->get(0)
790 : BrowserList::GetInstance()->get(1);
791 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
792
793 // Let's work with second window only.
794 // This page has beforeunload handler already.
795 EXPECT_TRUE(browser2->tab_strip_model()
796 ->GetWebContentsAt(0)
797 ->NeedToFireBeforeUnload());
798 // This page doesn't have beforeunload handler. Yet.
799 ui_test_utils::NavigateToURLWithDisposition(
800 browser2, embedded_test_server()->GetURL("/title2.html"),
801 WindowOpenDisposition::NEW_FOREGROUND_TAB,
802 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
803 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
804 EXPECT_FALSE(browser2->tab_strip_model()
805 ->GetWebContentsAt(1)
806 ->NeedToFireBeforeUnload());
807 EXPECT_EQ(2, browser2->tab_strip_model()->count());
808
avi336125f72017-05-06 22:25:40809 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40810
811 // The test.
812
813 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
814 content::WindowedNotificationObserver observer(
815 chrome::NOTIFICATION_BROWSER_CLOSED,
816 content::NotificationService::AllSources());
817 chrome::CloseWindow(browser2);
818 // Just to be sure CloseWindow doesn't have asynchronous tasks
819 // that could have an impact.
820 content::RunAllPendingInMessageLoop();
821
822 // Closing browser shouldn't happen because of beforeunload handler.
823 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
824 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
825 // yet.
826 ASSERT_TRUE(content::ExecuteScript(
David Benjamin5a792652018-06-08 02:15:42827 browser2->tab_strip_model()->GetWebContentsAt(1),
mkolom65a0b3c2017-03-02 06:11:40828 "window.addEventListener('beforeunload', "
829 "function(event) { event.returnValue = 'Foo'; });"));
830 EXPECT_TRUE(browser2->tab_strip_model()
831 ->GetWebContentsAt(1)
832 ->NeedToFireBeforeUnload());
833 // Accept closing the first tab.
834 ASSERT_NO_FATAL_FAILURE(AcceptClose());
835 // Just to be sure accepting a dialog doesn't have asynchronous tasks
836 // that could have an impact.
837 content::RunAllPendingInMessageLoop();
838 // It shouldn't close the whole window/browser.
839 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
840 EXPECT_EQ(2, browser2->tab_strip_model()->count());
841 // Accept closing the second tab.
842 ASSERT_NO_FATAL_FAILURE(AcceptClose());
843 observer.Wait();
844 // Now the second window/browser should be closed.
845 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
846 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
847 EXPECT_EQ(1u, restore_observer.changes_count());
848
849 // Restore the closed browser.
850 content::WindowedNotificationObserver open_window_observer(
851 chrome::NOTIFICATION_BROWSER_OPENED,
852 content::NotificationService::AllSources());
853 chrome::OpenWindowWithRestoredTabs(browser()->profile());
854 open_window_observer.Wait();
855 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
856 browser2 = BrowserList::GetInstance()->get(0) != browser()
857 ? BrowserList::GetInstance()->get(0)
858 : BrowserList::GetInstance()->get(1);
859
860 // Check the restored browser contents.
861 EXPECT_EQ(2, browser2->tab_strip_model()->count());
862 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
863 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
864 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
865 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
866}
867
868IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42869 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56870 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
871 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40872 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46873
Scott Violet1f106b582017-07-12 15:49:58874 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56875 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20876 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56877
878 browsers_.push_back(CreateBrowser(browser()->profile()));
879 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
880 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40881 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56882 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32883 ASSERT_NO_FATAL_FAILURE(CancelClose());
884 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56885 cancel_observer.Wait();
886 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
887 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
888 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
889
Scott Violet1f106b582017-07-12 15:49:58890 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56891 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20892 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56893 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32894 ASSERT_NO_FATAL_FAILURE(AcceptClose());
895 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56896
897 close_observer.Wait();
898 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37899 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56900}
901
902IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46903 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56904 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
905 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40906 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46907
Scott Violet1f106b582017-07-12 15:49:58908 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56909 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20910 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56911
912 browsers_.push_back(CreateBrowser(browser()->profile()));
913 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
914 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40915 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56916 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32917 ASSERT_NO_FATAL_FAILURE(CancelClose());
918 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56919 cancel_observer.Wait();
920 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
921 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
922 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
923
Scott Violet1f106b582017-07-12 15:49:58924 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56925 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20926 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56927 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32928 ASSERT_NO_FATAL_FAILURE(AcceptClose());
929 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56930
931 close_observer.Wait();
932 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37933 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56934}
935
936IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
937 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56938 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
939 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
940 browsers_.push_back(CreateBrowser(browser()->profile()));
941 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
942 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40943 PrepareForDialog(browsers_[0]);
944 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46945
Scott Violet1f106b582017-07-12 15:49:58946 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56947 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20948 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56949
950 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32951 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56952 cancel_observer.Wait();
953 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
954 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
955 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
956
Scott Violet1f106b582017-07-12 15:49:58957 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56958 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20959 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56960 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32961 ASSERT_NO_FATAL_FAILURE(AcceptClose());
962 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56963
964 close_observer.Wait();
965 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37966 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56967}
968
jackhouae8e6e52015-05-29 06:36:46969// Mac has its own in-progress download prompt in app_controller_mac.mm, so
970// BrowserCloseManager should simply close all browsers. If there are no
971// browsers, it should not crash.
972#if defined(OS_MACOSX)
Mattias Nissler441cc652018-04-05 00:08:56973IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46974 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
975
Scott Violet1f106b582017-07-12 15:49:58976 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46977 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
978
979 TestBrowserCloseManager::AttemptClose(
980 TestBrowserCloseManager::NO_USER_CHOICE);
981 close_observer.Wait();
982 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37983 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44984 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46985
986 // Attempting to close again should not crash.
987 TestBrowserCloseManager::AttemptClose(
988 TestBrowserCloseManager::NO_USER_CHOICE);
989}
990#else // defined(OS_MACOSX)
991
[email protected]422a7d12013-10-21 12:10:42992// Test shutdown with a DANGEROUS_URL download undecided.
Mattias Nissler441cc652018-04-05 00:08:56993IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
994 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42995 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35996 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42997 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:44998 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:09999 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421000
1001 // Run a dangerous download, but the user doesn't make a decision.
1002 // This .swf normally would be categorized as DANGEROUS_FILE, but
1003 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:431004 GURL download_url(
1005 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421006 content::DownloadTestObserverInterrupted observer(
1007 content::BrowserContext::GetDownloadManager(browser()->profile()),
1008 1,
1009 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1010 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191011 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421012 ui_test_utils::BROWSER_TEST_NONE);
1013 observer.WaitForFinished();
1014
1015 // Check that the download manager has the expected state.
1016 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1017 browser()->profile())->InProgressCount());
1018 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1019 browser()->profile())->NonMaliciousInProgressCount());
1020
1021 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581022 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421023 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1024 TestBrowserCloseManager::AttemptClose(
1025 TestBrowserCloseManager::NO_USER_CHOICE);
1026 close_observer.Wait();
1027 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371028 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421029}
1030
[email protected]edfca702013-08-16 08:58:141031// Test shutdown with a download in progress.
Mattias Nissler441cc652018-04-05 00:08:561032IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:141033 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1034 content::TestNavigationObserver navigation_observer(
1035 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1036 TestBrowserCloseManager::AttemptClose(
1037 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1038 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1039 navigation_observer.Wait();
1040 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1041 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1042
Scott Violet1f106b582017-07-12 15:49:581043 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141044 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1045
1046 TestBrowserCloseManager::AttemptClose(
1047 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1048 close_observer.Wait();
1049 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371050 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381051 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441052 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381053 else
peterccb33e82017-05-02 19:00:441054 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141055}
1056
sammccd347712015-03-13 22:02:321057// Test shutdown with a download in progress in an off-the-record profile.
Mattias Nissler441cc652018-04-05 00:08:561058IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321059 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321060 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321061 Browser* otr_browser = CreateBrowser(otr_profile);
1062 {
Scott Violet1f106b582017-07-12 15:49:581063 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321064 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1065 browser()->window()->Close();
1066 close_observer.Wait();
1067 }
1068 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1069 content::TestNavigationObserver navigation_observer(
1070 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1071 TestBrowserCloseManager::AttemptClose(
1072 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1073 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1074 navigation_observer.Wait();
1075 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1076 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1077
Scott Violet1f106b582017-07-12 15:49:581078 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321079 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1080
1081 TestBrowserCloseManager::AttemptClose(
1082 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1083 close_observer.Wait();
1084 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371085 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441086 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321087}
1088
Asanka Herathd1341dc2017-04-19 18:28:391089// Test shutdown with a download in progress in a regular profile an inconito
1090// browser is opened and closed. While there are active downloads, closing the
1091// incognito window shouldn't block on the active downloads which belong to the
1092// parent profile.
Weza78944d2018-05-17 16:56:291093// TODO(https://crbug.com/844019): Fix the notification expectation around the
1094// call to AttemptClose.
Mattias Nissler441cc652018-04-05 00:08:561095IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291096 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391097 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391098 Browser* otr_browser = CreateBrowser(otr_profile);
1099 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1100
1101 content::TestNavigationObserver navigation_observer(
1102 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1103 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1104 navigation_observer.Wait();
1105
1106 int num_downloads_blocking = 0;
1107 ASSERT_EQ(
1108 Browser::DOWNLOAD_CLOSE_OK,
1109 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1110 ASSERT_EQ(0, num_downloads_blocking);
1111
1112 {
Scott Violet1f106b582017-07-12 15:49:581113 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391114 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1115 otr_browser->window()->Close();
1116 close_observer.Wait();
1117 }
1118
1119 ASSERT_EQ(
1120 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1121 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1122 ASSERT_EQ(1, num_downloads_blocking);
1123
1124 {
Scott Violet1f106b582017-07-12 15:49:581125 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391126 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1127 TestBrowserCloseManager::AttemptClose(
1128 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1129 close_observer.Wait();
1130 }
1131
1132 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1133 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1134 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441135 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391136 else
peterccb33e82017-05-02 19:00:441137 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391138}
1139
[email protected]edfca702013-08-16 08:58:141140// Test shutdown with a download in progress from one profile, where the only
1141// open windows are for another profile.
Mattias Nissler441cc652018-04-05 00:08:561142IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141143 TestWithDownloadsFromDifferentProfiles) {
1144 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511145 Profile* other_profile = nullptr;
1146 {
1147 base::FilePath path =
1148 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131149 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511150 if (!base::PathExists(path))
1151 ASSERT_TRUE(base::CreateDirectory(path));
1152 other_profile =
1153 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1154 }
[email protected]edfca702013-08-16 08:58:141155 profile_manager->RegisterTestingProfile(other_profile, true, false);
1156 Browser* other_profile_browser = CreateBrowser(other_profile);
1157
[email protected]edfca702013-08-16 08:58:141158 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1159 {
Scott Violet1f106b582017-07-12 15:49:581160 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141161 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1162 browser()->window()->Close();
1163 close_observer.Wait();
1164 }
1165
1166 // When the shutdown is cancelled, the downloads page should be opened in a
1167 // browser for that profile. Because there are no browsers for that profile, a
1168 // new browser should be opened.
1169 ui_test_utils::BrowserAddedObserver new_browser_observer;
1170 TestBrowserCloseManager::AttemptClose(
1171 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1172 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1173 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1174 EXPECT_EQ(
1175 GURL(chrome::kChromeUIDownloadsURL),
1176 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1177 EXPECT_EQ(GURL("about:blank"),
1178 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1179 ->GetURL());
1180
Scott Violet1f106b582017-07-12 15:49:581181 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141182 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1183 TestBrowserCloseManager::AttemptClose(
1184 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1185 close_observer.Wait();
1186 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371187 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381188 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441189 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381190 else
peterccb33e82017-05-02 19:00:441191 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141192}
1193
Vasilii Sukhanov4a65b862017-08-04 10:01:371194// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141195// Test shutdown with downloads in progress and beforeunload handlers.
Mattias Nissler441cc652018-04-05 00:08:561196IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371197 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141198 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1199 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1200 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401201 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141202
1203 content::WindowedNotificationObserver cancel_observer(
1204 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1205 content::NotificationService::AllSources());
1206 TestBrowserCloseManager::AttemptClose(
1207 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321208 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141209 cancel_observer.Wait();
1210 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1211
Scott Violet1f106b582017-07-12 15:49:581212 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141213 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1214 TestBrowserCloseManager::AttemptClose(
1215 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321216 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141217 close_observer.Wait();
1218 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371219 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141220}
1221
jackhouae8e6e52015-05-29 06:36:461222#endif // defined(OS_MACOSX)
1223
Mattias Nissler441cc652018-04-05 00:08:561224INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
1225 BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561226 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201227
Denis Kuznetsov32099f32017-12-15 14:04:571228#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1229
[email protected]0c95faf42013-10-28 06:27:201230class BrowserCloseManagerWithBackgroundModeBrowserTest
1231 : public BrowserCloseManagerBrowserTest {
1232 public:
1233 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1234
Daniel Chenga542fca2014-10-21 09:51:291235 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201236 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1237 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351238 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201239 }
1240
1241 bool IsBackgroundModeSuspended() {
1242 return static_cast<FakeBackgroundModeManager*>(
1243 g_browser_process->background_mode_manager())
1244 ->IsBackgroundModeSuspended();
1245 }
1246
1247 private:
1248 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1249};
1250
1251// Check that background mode is suspended when closing all browsers unless we
1252// are quitting and that background mode is resumed when a new browser window is
1253// opened.
1254IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1255 CloseAllBrowsersWithBackgroundMode) {
1256 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351257 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201258 Profile* profile = browser()->profile();
1259 {
Scott Violet1f106b582017-07-12 15:49:581260 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201261 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381262 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1263 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201264 chrome::CloseAllBrowsers();
1265 close_observer.Wait();
1266 }
1267 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371268 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201269 EXPECT_TRUE(IsBackgroundModeSuspended());
1270
1271 // Background mode should be resumed when a new browser window is opened.
1272 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441273 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201274 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381275 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201276 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581277 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201278 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1279
1280 // Background mode should not be suspended when quitting.
1281 chrome::CloseAllBrowsersAndQuit();
1282 close_observer.Wait();
1283 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371284 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201285 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201286}
1287
1288// Check that closing the last browser window individually does not affect
1289// background mode.
1290IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161291 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581292 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201293 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1294 EXPECT_FALSE(IsBackgroundModeSuspended());
1295 browser()->window()->Close();
1296 close_observer.Wait();
1297 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371298 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201299 EXPECT_FALSE(IsBackgroundModeSuspended());
1300}
1301
1302// Check that closing all browsers with no browser windows open suspends
1303// background mode but does not cause Chrome to quit.
1304IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031305 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581306 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201307 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1308 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381309 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1310 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201311 browser()->window()->Close();
1312 close_observer.Wait();
1313 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371314 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201315 EXPECT_FALSE(IsBackgroundModeSuspended());
1316
1317 chrome::CloseAllBrowsers();
1318 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371319 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201320 EXPECT_TRUE(IsBackgroundModeSuspended());
1321}
1322
1323INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1324 BrowserCloseManagerWithBackgroundModeBrowserTest,
1325 testing::Bool());
Denis Kuznetsov32099f32017-12-15 14:04:571326#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)