blob: 2e460126c784743c7ed0e9bbe3264be4a866affa [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/browser_shutdown.h"
19#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3820#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4221#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4422#include "chrome/browser/download/download_core_service.h"
23#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0524#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5625#include "chrome/browser/lifetime/application_lifetime.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"
36#include "chrome/common/chrome_switches.h"
Denis Kuznetsov32099f32017-12-15 14:04:5737#include "chrome/common/features.h"
[email protected]2e9d79f2013-08-16 05:45:5638#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"
Michael Giuffrida2dbce0d12017-09-02 03:30:5943#include "components/keep_alive_registry/keep_alive_types.h"
44#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4045#include "components/sessions/core/tab_restore_service.h"
46#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1447#include "content/public/browser/browser_context.h"
48#include "content/public/browser/download_item.h"
49#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5650#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3451#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4652#include "content/public/browser/render_view_host.h"
53#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;
110 }
111
112 private:
113 int num_outstanding_;
114 content::NotificationRegistrar registrar_;
115 bool running_;
116 base::RunLoop run_loop_;
117
118 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
119};
120
mkolom65a0b3c2017-03-02 06:11:40121class TabRestoreServiceChangesObserver
122 : public sessions::TabRestoreServiceObserver {
123 public:
124 explicit TabRestoreServiceChangesObserver(Profile* profile)
125 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
126 if (service_)
127 service_->AddObserver(this);
128 }
129
130 ~TabRestoreServiceChangesObserver() override {
131 if (service_)
132 service_->RemoveObserver(this);
133 }
134
135 size_t changes_count() const { return changes_count_; }
136
137 private:
138 // sessions::TabRestoreServiceObserver:
139 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
140 changes_count_++;
141 }
142
143 // sessions::TabRestoreServiceObserver:
144 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
145 service_ = nullptr;
146 }
147
148 sessions::TabRestoreService* service_ = nullptr;
149 size_t changes_count_ = 0;
150
151 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
152};
153
[email protected]edfca702013-08-16 08:58:14154class TestBrowserCloseManager : public BrowserCloseManager {
155 public:
156 enum UserChoice {
157 USER_CHOICE_USER_CANCELS_CLOSE,
158 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42159 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14160 };
161
162 static void AttemptClose(UserChoice user_choice) {
163 scoped_refptr<BrowserCloseManager> browser_close_manager =
164 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20165 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14166 browser_close_manager->StartClosingBrowsers();
167 }
168
169 protected:
Daniel Chenga542fca2014-10-21 09:51:29170 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14171
Daniel Chenga542fca2014-10-21 09:51:29172 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14173 int download_count,
mostynb2b52d1db2014-10-07 02:47:17174 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42175 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14176 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42177 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14178 case USER_CHOICE_USER_CANCELS_CLOSE: {
179 callback.Run(false);
180 break;
181 }
182 case USER_CHOICE_USER_ALLOWS_CLOSE: {
183 callback.Run(true);
184 break;
185 }
186 }
187 }
188
189 private:
190 explicit TestBrowserCloseManager(UserChoice user_choice)
191 : user_choice_(user_choice) {}
192
193 UserChoice user_choice_;
194
195 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
196};
197
[email protected]422a7d12013-10-21 12:10:42198class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
199 public:
200 explicit TestDownloadManagerDelegate(Profile* profile)
201 : ChromeDownloadManagerDelegate(profile) {
[email protected]861b4d0a2013-12-13 20:10:41202 GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42203 }
Daniel Chenga542fca2014-10-21 09:51:29204 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42205
Daniel Chenga542fca2014-10-21 09:51:29206 bool DetermineDownloadTarget(
[email protected]422a7d12013-10-21 12:10:42207 content::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17208 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42209 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41210 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42211 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
212 item, dangerous_callback);
213 }
214
Asanka Herath1ba0e9f2017-04-03 18:48:53215 static void SetDangerous(const content::DownloadTargetCallback& callback,
216 const base::FilePath& target_path,
217 content::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35218 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53219 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16220 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35221 callback.Run(target_path, disp,
222 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53223 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42224 }
[email protected]422a7d12013-10-21 12:10:42225};
226
Denis Kuznetsov32099f32017-12-15 14:04:57227#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20228class FakeBackgroundModeManager : public BackgroundModeManager {
229 public:
230 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54231 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
232 &g_browser_process->profile_manager()
233 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20234 suspended_(false) {}
235
Daniel Chenga542fca2014-10-21 09:51:29236 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20237 BackgroundModeManager::SuspendBackgroundMode();
238 suspended_ = true;
239 }
240
Daniel Chenga542fca2014-10-21 09:51:29241 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20242 BackgroundModeManager::ResumeBackgroundMode();
243 suspended_ = false;
244 }
245
246 bool IsBackgroundModeSuspended() {
247 return suspended_;
248 }
249
250 private:
251 bool suspended_;
252
253 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
254};
Denis Kuznetsov32099f32017-12-15 14:04:57255#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20256
[email protected]2e9d79f2013-08-16 05:45:56257} // namespace
258
259class BrowserCloseManagerBrowserTest
260 : public InProcessBrowserTest,
261 public testing::WithParamInterface<bool> {
262 protected:
Daniel Chenga542fca2014-10-21 09:51:29263 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56264 SessionStartupPref::SetStartupPref(
265 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
266 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14267 content::BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33268 content::BrowserThread::IO, FROM_HERE,
269 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
Colin Blundellfb2cfb2b2018-01-18 18:50:09270 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
271 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
272 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56273 }
274
avi556c05022014-12-22 23:31:43275 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56276 if (GetParam())
277 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07278#if defined(OS_CHROMEOS)
279 command_line->AppendSwitch(
280 chromeos::switches::kIgnoreUserProfileMappingForTests);
281#endif
[email protected]2e9d79f2013-08-16 05:45:56282 }
283
[email protected]edfca702013-08-16 08:58:14284 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09285 ASSERT_TRUE(embedded_test_server()->Started());
286
287 GURL slow_download_url = embedded_test_server()->GetURL(
288 content::SlowDownloadHttpResponse::kKnownSizeUrl);
289
[email protected]edfca702013-08-16 08:58:14290 content::DownloadTestObserverInProgress observer(
291 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
292 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09293 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14294 ui_test_utils::BROWSER_TEST_NONE);
295 observer.WaitForFinished();
296 EXPECT_EQ(
297 1UL,
298 observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS));
299 }
300
avi336125f72017-05-06 22:25:40301 void PrepareForDialog(content::WebContents* web_contents) {
302 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46303 }
304
avi336125f72017-05-06 22:25:40305 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46306 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40307 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46308 }
309
[email protected]2e9d79f2013-08-16 05:45:56310 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56311};
312
313IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56314 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
315 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40316 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46317
Scott Violet1f106b582017-07-12 15:49:58318 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56319 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20320 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32321 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56322 cancel_observer.Wait();
323 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
324 EXPECT_EQ(1, browser()->tab_strip_model()->count());
325
Scott Violet1f106b582017-07-12 15:49:58326 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56327 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20328 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32329 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56330 close_observer.Wait();
331 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37332 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56333}
334
335IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
336 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56337 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
338 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40339 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46340
Scott Violet1f106b582017-07-12 15:49:58341 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56342 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20343 chrome::CloseAllBrowsersAndQuit();
344 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32345 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56346 cancel_observer.Wait();
347 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
348 EXPECT_EQ(1, browser()->tab_strip_model()->count());
349
Scott Violet1f106b582017-07-12 15:49:58350 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56351 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20352 chrome::CloseAllBrowsersAndQuit();
353 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32354 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56355 close_observer.Wait();
356 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37357 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56358}
359
wafflesb63234052015-08-07 02:44:02360IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46361 PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56362 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
363 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
364 AddBlankTabAndShow(browser());
365 ASSERT_NO_FATAL_FAILURE(
366 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40367 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46368
Scott Violet1f106b582017-07-12 15:49:58369 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56370 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20371 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32372 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56373 cancel_observer.Wait();
374 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
375
376 browser()->tab_strip_model()
377 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
378 content::TestNavigationObserver navigation_observer(
379 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19380 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
381 browser(), GURL(chrome::kChromeUIVersionURL),
382 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32383 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56384 navigation_observer.Wait();
385
Scott Violet1f106b582017-07-12 15:49:58386 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56387 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20388 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56389 close_observer.Wait();
390 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
wafflesb63234052015-08-07 02:44:02402IN_PROC_BROWSER_TEST_P(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.
[email protected]40cd13a2013-10-24 13:42:01414IN_PROC_BROWSER_TEST_P(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 {
Scott Violet1f106b582017-07-12 15:49:58425 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56426 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());
[email protected]2e9d79f2013-08-16 05:45:56429 cancel_observer.Wait();
430 }
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 {
Scott Violet1f106b582017-07-12 15:49:58437 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56438 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());
[email protected]2e9d79f2013-08-16 05:45:56442 cancel_observer.Wait();
443 }
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.
Scott Violet1f106b582017-07-12 15:49:58449 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56450 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20451 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32452 ASSERT_NO_FATAL_FAILURE(AcceptClose());
453 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56454 close_observer.Wait();
455 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37456 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56457}
458
459// Test that tabs in the same window with a beforeunload event that hangs are
460// treated the same as the user accepting the close, but do not close the tab
461// early.
462IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46463 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56464 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
465 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
466 AddBlankTabAndShow(browsers_[0]);
467 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
468 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
469 AddBlankTabAndShow(browsers_[0]);
470 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
471 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46472 // Disable the hang monitor in the tab that is not expected to hang, so that
473 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40474 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56475
Scott Violet1f106b582017-07-12 15:49:58476 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56477 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20478 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32479 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56480 cancel_observer.Wait();
481 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
482 // All tabs should still be open.
483 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
484
Scott Violet1f106b582017-07-12 15:49:58485 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56486 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20487 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32488 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56489 close_observer.Wait();
490 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37491 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56492}
493
494// Test that tabs in different windows with a beforeunload event that hangs are
495// treated the same as the user accepting the close, but do not close the tab
496// early.
497IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
498 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56499 browsers_.push_back(CreateBrowser(browser()->profile()));
500 browsers_.push_back(CreateBrowser(browser()->profile()));
501 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
502 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
503 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
504 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
505 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
506 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46507 // Disable the hang monitor in the tab that is not expected to hang, so that
508 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40509 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56510
Scott Violet1f106b582017-07-12 15:49:58511 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56512 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20513 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32514 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56515 cancel_observer.Wait();
516 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
517 // All windows should still be open.
518 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
519 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
520 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
521
Scott Violet1f106b582017-07-12 15:49:58522 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56523 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20524 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32525 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56526 close_observer.Wait();
527 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37528 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56529}
530
metaflow73fc9552016-05-23 18:26:48531// Test that tabs that are slow to respond are not closed prematurely.
532// Regression for crbug.com/365052 caused some of tabs to be closed even if
533// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36534// Flaky on ChromeOS ASan. https://crbug.com/805457
535#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
536#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
537#else
538#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
539#endif
metaflow73fc9552016-05-23 18:26:48540IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36541 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48542 const int kTabCount = 5;
543 const int kResposiveTabIndex = 2;
544 // Create tab strip with all tabs except one responding after
545 // RenderViewHostImpl::kUnloadTimeoutMS.
546 // Minimum configuration is two slow tabs and then responsive tab.
547 // But we also want to check how slow tabs behave in tail.
548 for (int i = 0; i < kTabCount; i++) {
549 if (i)
550 AddBlankTabAndShow(browsers_[0]);
551 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
552 browsers_[0],
553 embedded_test_server()->GetURL((i == kResposiveTabIndex)
554 ? "/beforeunload.html"
555 : "/beforeunload_slow.html")));
556 }
metaflow812bb8542016-05-24 21:10:46557 // Disable the hang monitor in the tab that is not expected to hang, so that
558 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40559 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46560 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
561
Scott Violet1f106b582017-07-12 15:49:58562 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48563 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
564 chrome::CloseAllBrowsersAndQuit();
565 ASSERT_NO_FATAL_FAILURE(CancelClose());
566 cancel_observer.Wait();
567 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
568
569 // All tabs should still be open.
570 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58571 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48572 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
573
574 // Quit, this time accepting close confirmation dialog.
575 chrome::CloseAllBrowsersAndQuit();
576 ASSERT_NO_FATAL_FAILURE(AcceptClose());
577 close_observer.Wait();
578 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
579 EXPECT_TRUE(BrowserList::GetInstance()->empty());
580}
581
582// Test that tabs in different windows with a slow beforeunload event response
583// are treated the same as the user accepting the close, but do not close the
584// tab early.
585// Regression for crbug.com/365052 caused CHECK in tabstrip.
metaflow73fc9552016-05-23 18:26:48586IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46587 TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48588 const int kBrowserCount = 5;
589 const int kResposiveBrowserIndex = 2;
590 // Create multiple browsers with all tabs except one responding after
591 // RenderViewHostImpl::kUnloadTimeoutMS .
592 // Minimum configuration is just one browser with slow tab and then
593 // browser with responsive tab.
594 // But we also want to check how slow tabs behave in tail and make test
595 // more robust.
596 for (int i = 0; i < kBrowserCount; i++) {
597 if (i)
598 browsers_.push_back(CreateBrowser(browser()->profile()));
599 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
600 browsers_[i],
601 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
602 ? "/beforeunload.html"
603 : "/beforeunload_slow.html")));
604 }
metaflow812bb8542016-05-24 21:10:46605 // Disable the hang monitor in the tab that is not expected to hang, so that
606 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40607 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48608
Scott Violet1f106b582017-07-12 15:49:58609 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48610 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
611 chrome::CloseAllBrowsersAndQuit();
612 ASSERT_NO_FATAL_FAILURE(CancelClose());
613 cancel_observer.Wait();
614 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
615
616 // All windows should still be open.
617 for (int i = 0; i < kBrowserCount; i++)
618 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
619
620 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58621 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48622 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
623 chrome::CloseAllBrowsersAndQuit();
624 ASSERT_NO_FATAL_FAILURE(AcceptClose());
625 close_observer.Wait();
626 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
627 EXPECT_TRUE(BrowserList::GetInstance()->empty());
628}
629
Jan Krcal9a23f222017-08-01 14:13:07630// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
631#if defined(OS_WIN) && !defined(NDEBUG)
632#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
633#else
634#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
635#endif
636
[email protected]2e9d79f2013-08-16 05:45:56637// Test that a window created during shutdown is closed.
638IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07639 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56640 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
641 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40642 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56643
Scott Violet1f106b582017-07-12 15:49:58644 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56645 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20646 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56647 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32648 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56649 close_observer.Wait();
650 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37651 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56652}
653
654// Test that a window created during shutdown with a beforeunload handler can
655// cancel the shutdown.
656IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01657 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56658 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
659 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40660 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56661
Scott Violet1f106b582017-07-12 15:49:58662 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56663 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20664 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56665 browsers_.push_back(CreateBrowser(browser()->profile()));
666 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
667 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40668 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32669 ASSERT_NO_FATAL_FAILURE(AcceptClose());
670 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56671 cancel_observer.Wait();
672 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
673 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
674 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
675
676 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58677 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56678 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20679 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32680 ASSERT_NO_FATAL_FAILURE(AcceptClose());
681 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56682 close_observer.Wait();
683 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37684 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56685}
686
687// Test that tabs added during shutdown are closed.
688IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46689 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56690 browsers_.push_back(CreateBrowser(browser()->profile()));
691 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
692 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
693 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
694 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40695 PrepareForDialog(browsers_[0]);
696 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56697
Scott Violet1f106b582017-07-12 15:49:58698 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56699 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20700 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32701 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56702 AddBlankTabAndShow(browsers_[0]);
703 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32704 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56705 close_observer.Wait();
706 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37707 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56708}
709
710// Test that tabs created during shutdown with beforeunload handlers can cancel
711// the shutdown.
metaflow812bb8542016-05-24 21:10:46712
[email protected]2e9d79f2013-08-16 05:45:56713IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46714 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56715 browsers_.push_back(CreateBrowser(browser()->profile()));
716 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
717 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
718 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
719 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40720 PrepareForDialog(browsers_[0]);
721 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46722
Scott Violet1f106b582017-07-12 15:49:58723 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56724 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20725 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32726 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56727 AddBlankTabAndShow(browsers_[0]);
728 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
729 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
730 AddBlankTabAndShow(browsers_[1]);
731 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
732 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40733 PrepareForDialog(browsers_[0]);
734 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32735 ASSERT_NO_FATAL_FAILURE(AcceptClose());
736 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56737 cancel_observer.Wait();
738 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
739 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
740 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
741
Scott Violet1f106b582017-07-12 15:49:58742 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56743 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20744 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32745 ASSERT_NO_FATAL_FAILURE(AcceptClose());
746 ASSERT_NO_FATAL_FAILURE(AcceptClose());
747 ASSERT_NO_FATAL_FAILURE(AcceptClose());
748 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56749
750 close_observer.Wait();
751 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37752 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56753}
754
rogermf18929c2017-04-19 19:59:33755// TODO(crbug/713201):
756// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
757#if defined(OS_MACOSX)
758#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
759#else
760#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
761#endif
762
[email protected]2e9d79f2013-08-16 05:45:56763IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33764 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40765 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
766 // And it is difficult to fix this issue without fixing that one.
767 if (GetParam())
768 return;
769
mkolom65a0b3c2017-03-02 06:11:40770 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
771 browser(), embedded_test_server()->GetURL("/title1.html")));
772
773 // Open second window.
774 ui_test_utils::NavigateToURLWithDisposition(
775 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
776 WindowOpenDisposition::NEW_WINDOW,
777 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
778 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
779 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
780 ? BrowserList::GetInstance()->get(0)
781 : BrowserList::GetInstance()->get(1);
782 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
783
784 // Let's work with second window only.
785 // This page has beforeunload handler already.
786 EXPECT_TRUE(browser2->tab_strip_model()
787 ->GetWebContentsAt(0)
788 ->NeedToFireBeforeUnload());
789 // This page doesn't have beforeunload handler. Yet.
790 ui_test_utils::NavigateToURLWithDisposition(
791 browser2, embedded_test_server()->GetURL("/title2.html"),
792 WindowOpenDisposition::NEW_FOREGROUND_TAB,
793 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
794 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
795 EXPECT_FALSE(browser2->tab_strip_model()
796 ->GetWebContentsAt(1)
797 ->NeedToFireBeforeUnload());
798 EXPECT_EQ(2, browser2->tab_strip_model()->count());
799
avi336125f72017-05-06 22:25:40800 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40801
802 // The test.
803
804 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
805 content::WindowedNotificationObserver observer(
806 chrome::NOTIFICATION_BROWSER_CLOSED,
807 content::NotificationService::AllSources());
808 chrome::CloseWindow(browser2);
809 // Just to be sure CloseWindow doesn't have asynchronous tasks
810 // that could have an impact.
811 content::RunAllPendingInMessageLoop();
812
813 // Closing browser shouldn't happen because of beforeunload handler.
814 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
815 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
816 // yet.
817 ASSERT_TRUE(content::ExecuteScript(
818 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(),
819 "window.addEventListener('beforeunload', "
820 "function(event) { event.returnValue = 'Foo'; });"));
821 EXPECT_TRUE(browser2->tab_strip_model()
822 ->GetWebContentsAt(1)
823 ->NeedToFireBeforeUnload());
824 // Accept closing the first tab.
825 ASSERT_NO_FATAL_FAILURE(AcceptClose());
826 // Just to be sure accepting a dialog doesn't have asynchronous tasks
827 // that could have an impact.
828 content::RunAllPendingInMessageLoop();
829 // It shouldn't close the whole window/browser.
830 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
831 EXPECT_EQ(2, browser2->tab_strip_model()->count());
832 // Accept closing the second tab.
833 ASSERT_NO_FATAL_FAILURE(AcceptClose());
834 observer.Wait();
835 // Now the second window/browser should be closed.
836 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
837 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
838 EXPECT_EQ(1u, restore_observer.changes_count());
839
840 // Restore the closed browser.
841 content::WindowedNotificationObserver open_window_observer(
842 chrome::NOTIFICATION_BROWSER_OPENED,
843 content::NotificationService::AllSources());
844 chrome::OpenWindowWithRestoredTabs(browser()->profile());
845 open_window_observer.Wait();
846 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
847 browser2 = BrowserList::GetInstance()->get(0) != browser()
848 ? BrowserList::GetInstance()->get(0)
849 : BrowserList::GetInstance()->get(1);
850
851 // Check the restored browser contents.
852 EXPECT_EQ(2, browser2->tab_strip_model()->count());
853 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
854 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
855 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
856 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
857}
858
859IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42860 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56861 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
862 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40863 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46864
Scott Violet1f106b582017-07-12 15:49:58865 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56866 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20867 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56868
869 browsers_.push_back(CreateBrowser(browser()->profile()));
870 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
871 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40872 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56873 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32874 ASSERT_NO_FATAL_FAILURE(CancelClose());
875 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56876 cancel_observer.Wait();
877 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
878 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
879 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
880
Scott Violet1f106b582017-07-12 15:49:58881 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56882 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20883 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56884 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32885 ASSERT_NO_FATAL_FAILURE(AcceptClose());
886 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56887
888 close_observer.Wait();
889 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37890 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56891}
892
893IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46894 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56895 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
896 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40897 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46898
Scott Violet1f106b582017-07-12 15:49:58899 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56900 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20901 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56902
903 browsers_.push_back(CreateBrowser(browser()->profile()));
904 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
905 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40906 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56907 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32908 ASSERT_NO_FATAL_FAILURE(CancelClose());
909 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56910 cancel_observer.Wait();
911 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
Scott Violet1f106b582017-07-12 15:49:58915 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56916 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20917 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56918 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32919 ASSERT_NO_FATAL_FAILURE(AcceptClose());
920 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56921
922 close_observer.Wait();
923 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37924 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56925}
926
927IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
928 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56929 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
930 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
931 browsers_.push_back(CreateBrowser(browser()->profile()));
932 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
933 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40934 PrepareForDialog(browsers_[0]);
935 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46936
Scott Violet1f106b582017-07-12 15:49:58937 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56938 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20939 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56940
941 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32942 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56943 cancel_observer.Wait();
944 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
945 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
946 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
947
Scott Violet1f106b582017-07-12 15:49:58948 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56949 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20950 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56951 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32952 ASSERT_NO_FATAL_FAILURE(AcceptClose());
953 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56954
955 close_observer.Wait();
956 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37957 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56958}
959
[email protected]0ed53872014-05-10 05:51:05960INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
961 BrowserCloseManagerBrowserTest,
962 testing::Bool());
963
964class BrowserCloseManagerWithDownloadsBrowserTest :
965 public BrowserCloseManagerBrowserTest {
966 public:
967 BrowserCloseManagerWithDownloadsBrowserTest() {}
968 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
969
Daniel Chenga542fca2014-10-21 09:51:29970 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05971 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
972 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
973 }
974
975 void SetDownloadPathForProfile(Profile* profile) {
976 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
977 download_prefs->SetDownloadPath(download_path());
978 }
979
980 const base::FilePath& download_path() const {
vabr0c237ae2016-09-14 09:24:28981 return scoped_download_directory_.GetPath();
[email protected]0ed53872014-05-10 05:51:05982 }
983
984 private:
985 base::ScopedTempDir scoped_download_directory_;
986};
987
jackhouae8e6e52015-05-29 06:36:46988// Mac has its own in-progress download prompt in app_controller_mac.mm, so
989// BrowserCloseManager should simply close all browsers. If there are no
990// browsers, it should not crash.
991#if defined(OS_MACOSX)
992IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
993 TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46994 SetDownloadPathForProfile(browser()->profile());
995 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
996
Scott Violet1f106b582017-07-12 15:49:58997 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46998 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
999
1000 TestBrowserCloseManager::AttemptClose(
1001 TestBrowserCloseManager::NO_USER_CHOICE);
1002 close_observer.Wait();
1003 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371004 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441005 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:461006
1007 // Attempting to close again should not crash.
1008 TestBrowserCloseManager::AttemptClose(
1009 TestBrowserCloseManager::NO_USER_CHOICE);
1010}
1011#else // defined(OS_MACOSX)
1012
[email protected]422a7d12013-10-21 12:10:421013// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:051014IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:421015 TestWithDangerousUrlDownload) {
[email protected]0ed53872014-05-10 05:51:051016 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:421017
1018 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:351019 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:421020 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:441021 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091022 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421023
1024 // Run a dangerous download, but the user doesn't make a decision.
1025 // This .swf normally would be categorized as DANGEROUS_FILE, but
1026 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:431027 GURL download_url(
1028 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421029 content::DownloadTestObserverInterrupted observer(
1030 content::BrowserContext::GetDownloadManager(browser()->profile()),
1031 1,
1032 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1033 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191034 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421035 ui_test_utils::BROWSER_TEST_NONE);
1036 observer.WaitForFinished();
1037
1038 // Check that the download manager has the expected state.
1039 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1040 browser()->profile())->InProgressCount());
1041 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1042 browser()->profile())->NonMaliciousInProgressCount());
1043
1044 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581045 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421046 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1047 TestBrowserCloseManager::AttemptClose(
1048 TestBrowserCloseManager::NO_USER_CHOICE);
1049 close_observer.Wait();
1050 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371051 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421052}
1053
[email protected]edfca702013-08-16 08:58:141054// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:051055IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1056 TestWithDownloads) {
[email protected]0ed53872014-05-10 05:51:051057 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141058 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1059 content::TestNavigationObserver navigation_observer(
1060 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1061 TestBrowserCloseManager::AttemptClose(
1062 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1063 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1064 navigation_observer.Wait();
1065 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1066 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1067
Scott Violet1f106b582017-07-12 15:49:581068 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141069 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1070
1071 TestBrowserCloseManager::AttemptClose(
1072 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1073 close_observer.Wait();
1074 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371075 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381076 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441077 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381078 else
peterccb33e82017-05-02 19:00:441079 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141080}
1081
sammccd347712015-03-13 22:02:321082// Test shutdown with a download in progress in an off-the-record profile.
1083IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1084 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321085 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1086 SetDownloadPathForProfile(otr_profile);
1087 Browser* otr_browser = CreateBrowser(otr_profile);
1088 {
Scott Violet1f106b582017-07-12 15:49:581089 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321090 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1091 browser()->window()->Close();
1092 close_observer.Wait();
1093 }
1094 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1095 content::TestNavigationObserver navigation_observer(
1096 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1097 TestBrowserCloseManager::AttemptClose(
1098 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1099 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1100 navigation_observer.Wait();
1101 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1102 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1103
Scott Violet1f106b582017-07-12 15:49:581104 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321105 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1106
1107 TestBrowserCloseManager::AttemptClose(
1108 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1109 close_observer.Wait();
1110 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371111 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441112 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321113}
1114
Asanka Herathd1341dc2017-04-19 18:28:391115// Test shutdown with a download in progress in a regular profile an inconito
1116// browser is opened and closed. While there are active downloads, closing the
1117// incognito window shouldn't block on the active downloads which belong to the
1118// parent profile.
1119IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1120 TestWithOffTheRecordWindowAndRegularDownload) {
1121 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1122 SetDownloadPathForProfile(otr_profile);
1123 Browser* otr_browser = CreateBrowser(otr_profile);
1124 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1125
1126 content::TestNavigationObserver navigation_observer(
1127 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1128 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1129 navigation_observer.Wait();
1130
1131 int num_downloads_blocking = 0;
1132 ASSERT_EQ(
1133 Browser::DOWNLOAD_CLOSE_OK,
1134 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1135 ASSERT_EQ(0, num_downloads_blocking);
1136
1137 {
Scott Violet1f106b582017-07-12 15:49:581138 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391139 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1140 otr_browser->window()->Close();
1141 close_observer.Wait();
1142 }
1143
1144 ASSERT_EQ(
1145 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1146 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1147 ASSERT_EQ(1, num_downloads_blocking);
1148
1149 {
Scott Violet1f106b582017-07-12 15:49:581150 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391151 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1152 TestBrowserCloseManager::AttemptClose(
1153 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1154 close_observer.Wait();
1155 }
1156
1157 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1158 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1159 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441160 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391161 else
peterccb33e82017-05-02 19:00:441162 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391163}
1164
[email protected]edfca702013-08-16 08:58:141165// Test shutdown with a download in progress from one profile, where the only
1166// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:051167IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:141168 TestWithDownloadsFromDifferentProfiles) {
1169 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511170 Profile* other_profile = nullptr;
1171 {
1172 base::FilePath path =
1173 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131174 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511175 if (!base::PathExists(path))
1176 ASSERT_TRUE(base::CreateDirectory(path));
1177 other_profile =
1178 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1179 }
[email protected]edfca702013-08-16 08:58:141180 profile_manager->RegisterTestingProfile(other_profile, true, false);
1181 Browser* other_profile_browser = CreateBrowser(other_profile);
1182
[email protected]0ed53872014-05-10 05:51:051183 SetDownloadPathForProfile(browser()->profile());
1184 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:141185 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1186 {
Scott Violet1f106b582017-07-12 15:49:581187 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141188 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1189 browser()->window()->Close();
1190 close_observer.Wait();
1191 }
1192
1193 // When the shutdown is cancelled, the downloads page should be opened in a
1194 // browser for that profile. Because there are no browsers for that profile, a
1195 // new browser should be opened.
1196 ui_test_utils::BrowserAddedObserver new_browser_observer;
1197 TestBrowserCloseManager::AttemptClose(
1198 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1199 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1200 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1201 EXPECT_EQ(
1202 GURL(chrome::kChromeUIDownloadsURL),
1203 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1204 EXPECT_EQ(GURL("about:blank"),
1205 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1206 ->GetURL());
1207
Scott Violet1f106b582017-07-12 15:49:581208 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141209 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1210 TestBrowserCloseManager::AttemptClose(
1211 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1212 close_observer.Wait();
1213 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371214 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381215 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441216 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381217 else
peterccb33e82017-05-02 19:00:441218 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141219}
1220
Vasilii Sukhanov4a65b862017-08-04 10:01:371221// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141222// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]0ed53872014-05-10 05:51:051223IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371224 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]0ed53872014-05-10 05:51:051225 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141226 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1227 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1228 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401229 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141230
1231 content::WindowedNotificationObserver cancel_observer(
1232 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1233 content::NotificationService::AllSources());
1234 TestBrowserCloseManager::AttemptClose(
1235 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321236 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141237 cancel_observer.Wait();
1238 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1239
Scott Violet1f106b582017-07-12 15:49:581240 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141241 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1242 TestBrowserCloseManager::AttemptClose(
1243 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321244 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141245 close_observer.Wait();
1246 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371247 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141248}
1249
jackhouae8e6e52015-05-29 06:36:461250#endif // defined(OS_MACOSX)
1251
[email protected]0ed53872014-05-10 05:51:051252INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
1253 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561254 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201255
Denis Kuznetsov32099f32017-12-15 14:04:571256#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1257
[email protected]0c95faf42013-10-28 06:27:201258class BrowserCloseManagerWithBackgroundModeBrowserTest
1259 : public BrowserCloseManagerBrowserTest {
1260 public:
1261 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1262
Daniel Chenga542fca2014-10-21 09:51:291263 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201264 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1265 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351266 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201267 }
1268
1269 bool IsBackgroundModeSuspended() {
1270 return static_cast<FakeBackgroundModeManager*>(
1271 g_browser_process->background_mode_manager())
1272 ->IsBackgroundModeSuspended();
1273 }
1274
1275 private:
1276 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1277};
1278
1279// Check that background mode is suspended when closing all browsers unless we
1280// are quitting and that background mode is resumed when a new browser window is
1281// opened.
1282IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1283 CloseAllBrowsersWithBackgroundMode) {
1284 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351285 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201286 Profile* profile = browser()->profile();
1287 {
Scott Violet1f106b582017-07-12 15:49:581288 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201289 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381290 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1291 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201292 chrome::CloseAllBrowsers();
1293 close_observer.Wait();
1294 }
1295 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371296 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201297 EXPECT_TRUE(IsBackgroundModeSuspended());
1298
1299 // Background mode should be resumed when a new browser window is opened.
1300 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441301 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201302 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381303 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201304 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581305 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201306 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1307
1308 // Background mode should not be suspended when quitting.
1309 chrome::CloseAllBrowsersAndQuit();
1310 close_observer.Wait();
1311 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371312 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201313 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201314}
1315
1316// Check that closing the last browser window individually does not affect
1317// background mode.
1318IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161319 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581320 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201321 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1322 EXPECT_FALSE(IsBackgroundModeSuspended());
1323 browser()->window()->Close();
1324 close_observer.Wait();
1325 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371326 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201327 EXPECT_FALSE(IsBackgroundModeSuspended());
1328}
1329
1330// Check that closing all browsers with no browser windows open suspends
1331// background mode but does not cause Chrome to quit.
1332IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031333 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581334 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201335 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1336 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381337 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1338 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201339 browser()->window()->Close();
1340 close_observer.Wait();
1341 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371342 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201343 EXPECT_FALSE(IsBackgroundModeSuspended());
1344
1345 chrome::CloseAllBrowsers();
1346 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371347 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201348 EXPECT_TRUE(IsBackgroundModeSuspended());
1349}
1350
1351INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1352 BrowserCloseManagerWithBackgroundModeBrowserTest,
1353 testing::Bool());
Denis Kuznetsov32099f32017-12-15 14:04:571354#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)