blob: f1f0bd0fdd0d0c32166d397635075f7e87896918 [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"
Eric Seckler8652dcd52018-09-20 10:42:2814#include "base/task/post_task.h"
jam3f2d3932017-04-26 20:28:5115#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3816#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2017#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1418#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5619#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"
Avi Drissmand30927342018-05-22 15:04:2726#include "chrome/browser/lifetime/browser_shutdown.h"
[email protected]edfca702013-08-16 08:58:1427#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5628#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1429#include "chrome/browser/profiles/profile.h"
30#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4031#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5632#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2033#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3734#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1435#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5636#include "chrome/browser/ui/tabs/tab_strip_model.h"
Scott Violet6200d332018-02-23 21:29:2337#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5638#include "chrome/common/chrome_switches.h"
39#include "chrome/common/url_constants.h"
40#include "chrome/test/base/in_process_browser_test.h"
41#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4242#include "components/app_modal/javascript_app_modal_dialog.h"
43#include "components/app_modal/native_app_modal_dialog.h"
Min Qina9f487872018-02-09 20:43:2344#include "components/download/public/common/download_item.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5945#include "components/keep_alive_registry/keep_alive_types.h"
46#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4047#include "components/sessions/core/tab_restore_service.h"
48#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1449#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2850#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:5951#include "content/public/browser/browser_thread.h"
[email protected]edfca702013-08-16 08:58:1452#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5653#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3454#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4655#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5656#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4057#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1458#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0959#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5660#include "content/public/test/test_navigation_observer.h"
61#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5662
[email protected]f9357a442014-05-15 18:44:0763#if defined(OS_CHROMEOS)
64#include "chromeos/chromeos_switches.h"
65#endif
66
[email protected]2e9d79f2013-08-16 05:45:5667namespace {
68
oshima0929be2a2014-11-19 22:21:0369app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5270 app_modal::JavaScriptAppModalDialog* dialog =
71 ui_test_utils::WaitForAppModalDialog();
72 CHECK(dialog->native_dialog());
73 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3274}
[email protected]2e9d79f2013-08-16 05:45:5675
avi336125f72017-05-06 22:25:4076// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4677// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3278void AcceptClose() {
79 GetNextDialog()->AcceptAppModalDialog();
80}
[email protected]2e9d79f2013-08-16 05:45:5681
avi336125f72017-05-06 22:25:4082// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4683// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3284void CancelClose() {
85 GetNextDialog()->CancelAppModalDialog();
86}
[email protected]2e9d79f2013-08-16 05:45:5687
Scott Violet1f106b582017-07-12 15:49:5888class RepeatedNotificationObserver : public content::NotificationObserver {
89 public:
90 explicit RepeatedNotificationObserver(int type, int count)
91 : num_outstanding_(count), running_(false) {
92 registrar_.Add(this, type, content::NotificationService::AllSources());
93 }
94
95 void Observe(int type,
96 const content::NotificationSource& source,
97 const content::NotificationDetails& details) override {
98 ASSERT_GT(num_outstanding_, 0);
99 if (!--num_outstanding_ && running_) {
Eric Seckler8652dcd52018-09-20 10:42:28100 base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
101 run_loop_.QuitClosure());
Scott Violet1f106b582017-07-12 15:49:58102 }
103 }
104
105 void Wait() {
106 if (num_outstanding_ <= 0)
107 return;
108
109 running_ = true;
110 run_loop_.Run();
111 running_ = false;
Weza78944d2018-05-17 16:56:29112
113 EXPECT_LE(num_outstanding_, 0);
Scott Violet1f106b582017-07-12 15:49:58114 }
115
116 private:
117 int num_outstanding_;
118 content::NotificationRegistrar registrar_;
119 bool running_;
120 base::RunLoop run_loop_;
121
122 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
123};
124
mkolom65a0b3c2017-03-02 06:11:40125class TabRestoreServiceChangesObserver
126 : public sessions::TabRestoreServiceObserver {
127 public:
128 explicit TabRestoreServiceChangesObserver(Profile* profile)
129 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
130 if (service_)
131 service_->AddObserver(this);
132 }
133
134 ~TabRestoreServiceChangesObserver() override {
135 if (service_)
136 service_->RemoveObserver(this);
137 }
138
139 size_t changes_count() const { return changes_count_; }
140
141 private:
142 // sessions::TabRestoreServiceObserver:
143 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
144 changes_count_++;
145 }
146
147 // sessions::TabRestoreServiceObserver:
148 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
149 service_ = nullptr;
150 }
151
152 sessions::TabRestoreService* service_ = nullptr;
153 size_t changes_count_ = 0;
154
155 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
156};
157
[email protected]edfca702013-08-16 08:58:14158class TestBrowserCloseManager : public BrowserCloseManager {
159 public:
160 enum UserChoice {
161 USER_CHOICE_USER_CANCELS_CLOSE,
162 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42163 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14164 };
165
166 static void AttemptClose(UserChoice user_choice) {
167 scoped_refptr<BrowserCloseManager> browser_close_manager =
168 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20169 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14170 browser_close_manager->StartClosingBrowsers();
171 }
172
173 protected:
Daniel Chenga542fca2014-10-21 09:51:29174 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14175
Daniel Chenga542fca2014-10-21 09:51:29176 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14177 int download_count,
mostynb2b52d1db2014-10-07 02:47:17178 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42179 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14180 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42181 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14182 case USER_CHOICE_USER_CANCELS_CLOSE: {
183 callback.Run(false);
184 break;
185 }
186 case USER_CHOICE_USER_ALLOWS_CLOSE: {
187 callback.Run(true);
188 break;
189 }
190 }
191 }
192
193 private:
194 explicit TestBrowserCloseManager(UserChoice user_choice)
195 : user_choice_(user_choice) {}
196
197 UserChoice user_choice_;
198
199 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
200};
201
[email protected]422a7d12013-10-21 12:10:42202class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
203 public:
204 explicit TestDownloadManagerDelegate(Profile* profile)
205 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23206 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42207 }
Daniel Chenga542fca2014-10-21 09:51:29208 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42209
Daniel Chenga542fca2014-10-21 09:51:29210 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23211 download::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17212 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42213 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41214 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42215 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
216 item, dangerous_callback);
217 }
218
Asanka Herath1ba0e9f2017-04-03 18:48:53219 static void SetDangerous(const content::DownloadTargetCallback& callback,
220 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23221 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35222 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53223 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16224 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35225 callback.Run(target_path, disp,
226 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53227 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42228 }
[email protected]422a7d12013-10-21 12:10:42229};
230
Denis Kuznetsov32099f32017-12-15 14:04:57231#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20232class FakeBackgroundModeManager : public BackgroundModeManager {
233 public:
234 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54235 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
236 &g_browser_process->profile_manager()
237 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20238 suspended_(false) {}
239
Daniel Chenga542fca2014-10-21 09:51:29240 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20241 BackgroundModeManager::SuspendBackgroundMode();
242 suspended_ = true;
243 }
244
Daniel Chenga542fca2014-10-21 09:51:29245 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20246 BackgroundModeManager::ResumeBackgroundMode();
247 suspended_ = false;
248 }
249
250 bool IsBackgroundModeSuspended() {
251 return suspended_;
252 }
253
254 private:
255 bool suspended_;
256
257 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
258};
Denis Kuznetsov32099f32017-12-15 14:04:57259#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20260
[email protected]2e9d79f2013-08-16 05:45:56261} // namespace
262
263class BrowserCloseManagerBrowserTest
264 : public InProcessBrowserTest,
265 public testing::WithParamInterface<bool> {
266 protected:
Daniel Chenga542fca2014-10-21 09:51:29267 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56268 SessionStartupPref::SetStartupPref(
269 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
270 browsers_.push_back(browser());
Eric Seckler8652dcd52018-09-20 10:42:28271 base::PostTaskWithTraits(
272 FROM_HERE, {content::BrowserThread::IO},
tzik3f7781d2017-04-20 17:09:33273 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
Colin Blundellfb2cfb2b2018-01-18 18:50:09274 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
275 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
276 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56277 }
278
avi556c05022014-12-22 23:31:43279 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56280 if (GetParam())
281 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07282#if defined(OS_CHROMEOS)
283 command_line->AppendSwitch(
284 chromeos::switches::kIgnoreUserProfileMappingForTests);
285#endif
[email protected]2e9d79f2013-08-16 05:45:56286 }
287
[email protected]edfca702013-08-16 08:58:14288 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09289 ASSERT_TRUE(embedded_test_server()->Started());
290
291 GURL slow_download_url = embedded_test_server()->GetURL(
292 content::SlowDownloadHttpResponse::kKnownSizeUrl);
293
[email protected]edfca702013-08-16 08:58:14294 content::DownloadTestObserverInProgress observer(
295 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
296 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09297 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14298 ui_test_utils::BROWSER_TEST_NONE);
299 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23300 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
301 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14302 }
303
avi336125f72017-05-06 22:25:40304 void PrepareForDialog(content::WebContents* web_contents) {
305 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46306 }
307
avi336125f72017-05-06 22:25:40308 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46309 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40310 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46311 }
312
[email protected]2e9d79f2013-08-16 05:45:56313 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56314};
315
316IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56317 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
318 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40319 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46320
Scott Violet1f106b582017-07-12 15:49:58321 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56322 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20323 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32324 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56325 cancel_observer.Wait();
326 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
327 EXPECT_EQ(1, browser()->tab_strip_model()->count());
328
Scott Violet1f106b582017-07-12 15:49:58329 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56330 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20331 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32332 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56333 close_observer.Wait();
334 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37335 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56336}
337
338IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
339 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56340 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
341 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40342 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46343
Scott Violet1f106b582017-07-12 15:49:58344 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56345 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20346 chrome::CloseAllBrowsersAndQuit();
347 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32348 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56349 cancel_observer.Wait();
350 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
351 EXPECT_EQ(1, browser()->tab_strip_model()->count());
352
Scott Violet1f106b582017-07-12 15:49:58353 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56354 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20355 chrome::CloseAllBrowsersAndQuit();
356 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32357 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56358 close_observer.Wait();
359 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37360 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56361}
362
wafflesb63234052015-08-07 02:44:02363IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46364 PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56365 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
366 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
367 AddBlankTabAndShow(browser());
368 ASSERT_NO_FATAL_FAILURE(
369 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40370 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46371
Scott Violet1f106b582017-07-12 15:49:58372 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56373 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20374 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32375 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56376 cancel_observer.Wait();
377 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
378
379 browser()->tab_strip_model()
380 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
381 content::TestNavigationObserver navigation_observer(
382 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19383 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
384 browser(), GURL(chrome::kChromeUIVersionURL),
385 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32386 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56387 navigation_observer.Wait();
388
Scott Violet1f106b582017-07-12 15:49:58389 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56390 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20391 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56392 close_observer.Wait();
393 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37394 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56395}
396
397// Test that the tab closed after the aborted shutdown attempt is not re-opened
398// when restoring the session.
benwells70cc93222017-06-29 05:42:36399// Flaky on Windows trybots, see https://crbug.com/737860.
400#if defined(OS_WIN)
401#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
402#else
403#define MAYBE_TestSessionRestore TestSessionRestore
404#endif
wafflesb63234052015-08-07 02:44:02405IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36406 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56407 // The testing framework launches Chrome with about:blank as args.
408 EXPECT_EQ(2, browser()->tab_strip_model()->count());
409 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
410 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
411 EXPECT_EQ(GURL("about:blank"),
412 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
413}
414
415// Test that browser windows are only closed if all browsers are ready to close
416// and that all beforeunload dialogs are shown again after a cancel.
[email protected]40cd13a2013-10-24 13:42:01417IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56418 browsers_.push_back(CreateBrowser(browser()->profile()));
419 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
420 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
421 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
422 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40423 PrepareForDialog(browsers_[0]);
424 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56425
426 // Cancel shutdown on the first beforeunload event.
427 {
Scott Violet1f106b582017-07-12 15:49:58428 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56429 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20430 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32431 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56432 cancel_observer.Wait();
433 }
434 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
435 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
436 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
437
438 // Cancel shutdown on the second beforeunload event.
439 {
Scott Violet1f106b582017-07-12 15:49:58440 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56441 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20442 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32443 ASSERT_NO_FATAL_FAILURE(AcceptClose());
444 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56445 cancel_observer.Wait();
446 }
447 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
448 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
449 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
450
451 // Allow shutdown for both beforeunload events.
Scott Violet1f106b582017-07-12 15:49:58452 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56453 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20454 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32455 ASSERT_NO_FATAL_FAILURE(AcceptClose());
456 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56457 close_observer.Wait();
458 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37459 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56460}
461
462// Test that tabs in the same window with a beforeunload event that hangs are
463// treated the same as the user accepting the close, but do not close the tab
464// early.
465IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46466 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56467 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
468 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
469 AddBlankTabAndShow(browsers_[0]);
470 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
471 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
472 AddBlankTabAndShow(browsers_[0]);
473 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
474 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46475 // Disable the hang monitor in the tab that is not expected to hang, so that
476 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40477 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56478
Scott Violet1f106b582017-07-12 15:49:58479 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56480 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20481 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32482 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56483 cancel_observer.Wait();
484 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
485 // All tabs should still be open.
486 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
487
Scott Violet1f106b582017-07-12 15:49:58488 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56489 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20490 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32491 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56492 close_observer.Wait();
493 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37494 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56495}
496
497// Test that tabs in different windows with a beforeunload event that hangs are
498// treated the same as the user accepting the close, but do not close the tab
499// early.
500IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
501 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56502 browsers_.push_back(CreateBrowser(browser()->profile()));
503 browsers_.push_back(CreateBrowser(browser()->profile()));
504 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
505 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
506 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
507 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
508 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
509 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46510 // Disable the hang monitor in the tab that is not expected to hang, so that
511 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40512 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56513
Scott Violet1f106b582017-07-12 15:49:58514 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56515 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20516 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32517 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56518 cancel_observer.Wait();
519 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
520 // All windows should still be open.
521 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
522 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
523 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
524
Scott Violet1f106b582017-07-12 15:49:58525 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56526 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20527 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32528 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56529 close_observer.Wait();
530 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37531 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56532}
533
metaflow73fc9552016-05-23 18:26:48534// Test that tabs that are slow to respond are not closed prematurely.
535// Regression for crbug.com/365052 caused some of tabs to be closed even if
536// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36537// Flaky on ChromeOS ASan. https://crbug.com/805457
538#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
539#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
540#else
541#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
542#endif
metaflow73fc9552016-05-23 18:26:48543IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36544 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48545 const int kTabCount = 5;
546 const int kResposiveTabIndex = 2;
547 // Create tab strip with all tabs except one responding after
548 // RenderViewHostImpl::kUnloadTimeoutMS.
549 // Minimum configuration is two slow tabs and then responsive tab.
550 // But we also want to check how slow tabs behave in tail.
551 for (int i = 0; i < kTabCount; i++) {
552 if (i)
553 AddBlankTabAndShow(browsers_[0]);
554 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
555 browsers_[0],
556 embedded_test_server()->GetURL((i == kResposiveTabIndex)
557 ? "/beforeunload.html"
558 : "/beforeunload_slow.html")));
559 }
metaflow812bb8542016-05-24 21:10:46560 // Disable the hang monitor in the tab that is not expected to hang, so that
561 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40562 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46563 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
564
Scott Violet1f106b582017-07-12 15:49:58565 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48566 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
567 chrome::CloseAllBrowsersAndQuit();
568 ASSERT_NO_FATAL_FAILURE(CancelClose());
569 cancel_observer.Wait();
570 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
571
572 // All tabs should still be open.
573 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58574 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48575 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
576
577 // Quit, this time accepting close confirmation dialog.
578 chrome::CloseAllBrowsersAndQuit();
579 ASSERT_NO_FATAL_FAILURE(AcceptClose());
580 close_observer.Wait();
581 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
582 EXPECT_TRUE(BrowserList::GetInstance()->empty());
583}
584
585// Test that tabs in different windows with a slow beforeunload event response
586// are treated the same as the user accepting the close, but do not close the
587// tab early.
588// Regression for crbug.com/365052 caused CHECK in tabstrip.
jdoerrie6cc3bd82018-03-07 16:42:56589// Flaky on Mac and Linux: https://crbug.com/819541
590#if defined(OS_LINUX) || defined(OS_MACOSX)
591#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
592 DISABLED_TestBeforeUnloadMultipleSlowWindows
593#else
594#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
595 TestBeforeUnloadMultipleSlowWindows
596#endif
metaflow73fc9552016-05-23 18:26:48597IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
jdoerrie6cc3bd82018-03-07 16:42:56598 MAYBE_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48599 const int kBrowserCount = 5;
600 const int kResposiveBrowserIndex = 2;
601 // Create multiple browsers with all tabs except one responding after
602 // RenderViewHostImpl::kUnloadTimeoutMS .
603 // Minimum configuration is just one browser with slow tab and then
604 // browser with responsive tab.
605 // But we also want to check how slow tabs behave in tail and make test
606 // more robust.
607 for (int i = 0; i < kBrowserCount; i++) {
608 if (i)
609 browsers_.push_back(CreateBrowser(browser()->profile()));
610 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
611 browsers_[i],
612 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
613 ? "/beforeunload.html"
614 : "/beforeunload_slow.html")));
615 }
metaflow812bb8542016-05-24 21:10:46616 // Disable the hang monitor in the tab that is not expected to hang, so that
617 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40618 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48619
Scott Violet1f106b582017-07-12 15:49:58620 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48621 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
622 chrome::CloseAllBrowsersAndQuit();
623 ASSERT_NO_FATAL_FAILURE(CancelClose());
624 cancel_observer.Wait();
625 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
626
627 // All windows should still be open.
628 for (int i = 0; i < kBrowserCount; i++)
629 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
630
631 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58632 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48633 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
634 chrome::CloseAllBrowsersAndQuit();
635 ASSERT_NO_FATAL_FAILURE(AcceptClose());
636 close_observer.Wait();
637 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
638 EXPECT_TRUE(BrowserList::GetInstance()->empty());
639}
640
Jan Krcal9a23f222017-08-01 14:13:07641// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
642#if defined(OS_WIN) && !defined(NDEBUG)
643#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
644#else
645#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
646#endif
647
[email protected]2e9d79f2013-08-16 05:45:56648// Test that a window created during shutdown is closed.
649IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07650 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56651 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
652 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40653 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56654
Scott Violet1f106b582017-07-12 15:49:58655 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56656 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20657 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56658 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32659 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56660 close_observer.Wait();
661 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37662 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56663}
664
665// Test that a window created during shutdown with a beforeunload handler can
666// cancel the shutdown.
667IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01668 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56669 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
670 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40671 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56672
Scott Violet1f106b582017-07-12 15:49:58673 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56674 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20675 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56676 browsers_.push_back(CreateBrowser(browser()->profile()));
677 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
678 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40679 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32680 ASSERT_NO_FATAL_FAILURE(AcceptClose());
681 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56682 cancel_observer.Wait();
683 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
684 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
685 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
686
687 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58688 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56689 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20690 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32691 ASSERT_NO_FATAL_FAILURE(AcceptClose());
692 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56693 close_observer.Wait();
694 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37695 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56696}
697
698// Test that tabs added during shutdown are closed.
699IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46700 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56701 browsers_.push_back(CreateBrowser(browser()->profile()));
702 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
703 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
704 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
705 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40706 PrepareForDialog(browsers_[0]);
707 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56708
Scott Violet1f106b582017-07-12 15:49:58709 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56710 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20711 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32712 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56713 AddBlankTabAndShow(browsers_[0]);
714 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32715 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56716 close_observer.Wait();
717 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37718 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56719}
720
721// Test that tabs created during shutdown with beforeunload handlers can cancel
722// the shutdown.
metaflow812bb8542016-05-24 21:10:46723
[email protected]2e9d79f2013-08-16 05:45:56724IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46725 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56726 browsers_.push_back(CreateBrowser(browser()->profile()));
727 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
728 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
729 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
730 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40731 PrepareForDialog(browsers_[0]);
732 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46733
Scott Violet1f106b582017-07-12 15:49:58734 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56735 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20736 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32737 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56738 AddBlankTabAndShow(browsers_[0]);
739 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
740 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
741 AddBlankTabAndShow(browsers_[1]);
742 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
743 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40744 PrepareForDialog(browsers_[0]);
745 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32746 ASSERT_NO_FATAL_FAILURE(AcceptClose());
747 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56748 cancel_observer.Wait();
749 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
750 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
751 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
752
Scott Violet1f106b582017-07-12 15:49:58753 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56754 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20755 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32756 ASSERT_NO_FATAL_FAILURE(AcceptClose());
757 ASSERT_NO_FATAL_FAILURE(AcceptClose());
758 ASSERT_NO_FATAL_FAILURE(AcceptClose());
759 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56760
761 close_observer.Wait();
762 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37763 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56764}
765
rogermf18929c2017-04-19 19:59:33766// TODO(crbug/713201):
767// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
768#if defined(OS_MACOSX)
769#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
770#else
771#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
772#endif
773
[email protected]2e9d79f2013-08-16 05:45:56774IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33775 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40776 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
777 // And it is difficult to fix this issue without fixing that one.
778 if (GetParam())
779 return;
780
mkolom65a0b3c2017-03-02 06:11:40781 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
782 browser(), embedded_test_server()->GetURL("/title1.html")));
783
784 // Open second window.
785 ui_test_utils::NavigateToURLWithDisposition(
786 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
787 WindowOpenDisposition::NEW_WINDOW,
788 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
789 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
790 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
791 ? BrowserList::GetInstance()->get(0)
792 : BrowserList::GetInstance()->get(1);
793 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
794
795 // Let's work with second window only.
796 // This page has beforeunload handler already.
797 EXPECT_TRUE(browser2->tab_strip_model()
798 ->GetWebContentsAt(0)
799 ->NeedToFireBeforeUnload());
800 // This page doesn't have beforeunload handler. Yet.
801 ui_test_utils::NavigateToURLWithDisposition(
802 browser2, embedded_test_server()->GetURL("/title2.html"),
803 WindowOpenDisposition::NEW_FOREGROUND_TAB,
804 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
805 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
806 EXPECT_FALSE(browser2->tab_strip_model()
807 ->GetWebContentsAt(1)
808 ->NeedToFireBeforeUnload());
809 EXPECT_EQ(2, browser2->tab_strip_model()->count());
810
avi336125f72017-05-06 22:25:40811 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40812
813 // The test.
814
815 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
816 content::WindowedNotificationObserver observer(
817 chrome::NOTIFICATION_BROWSER_CLOSED,
818 content::NotificationService::AllSources());
819 chrome::CloseWindow(browser2);
820 // Just to be sure CloseWindow doesn't have asynchronous tasks
821 // that could have an impact.
822 content::RunAllPendingInMessageLoop();
823
824 // Closing browser shouldn't happen because of beforeunload handler.
825 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
826 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
827 // yet.
828 ASSERT_TRUE(content::ExecuteScript(
David Benjamin5a792652018-06-08 02:15:42829 browser2->tab_strip_model()->GetWebContentsAt(1),
mkolom65a0b3c2017-03-02 06:11:40830 "window.addEventListener('beforeunload', "
831 "function(event) { event.returnValue = 'Foo'; });"));
832 EXPECT_TRUE(browser2->tab_strip_model()
833 ->GetWebContentsAt(1)
834 ->NeedToFireBeforeUnload());
835 // Accept closing the first tab.
836 ASSERT_NO_FATAL_FAILURE(AcceptClose());
837 // Just to be sure accepting a dialog doesn't have asynchronous tasks
838 // that could have an impact.
839 content::RunAllPendingInMessageLoop();
840 // It shouldn't close the whole window/browser.
841 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
842 EXPECT_EQ(2, browser2->tab_strip_model()->count());
843 // Accept closing the second tab.
844 ASSERT_NO_FATAL_FAILURE(AcceptClose());
845 observer.Wait();
846 // Now the second window/browser should be closed.
847 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
848 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
849 EXPECT_EQ(1u, restore_observer.changes_count());
850
851 // Restore the closed browser.
852 content::WindowedNotificationObserver open_window_observer(
853 chrome::NOTIFICATION_BROWSER_OPENED,
854 content::NotificationService::AllSources());
855 chrome::OpenWindowWithRestoredTabs(browser()->profile());
856 open_window_observer.Wait();
857 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
858 browser2 = BrowserList::GetInstance()->get(0) != browser()
859 ? BrowserList::GetInstance()->get(0)
860 : BrowserList::GetInstance()->get(1);
861
862 // Check the restored browser contents.
863 EXPECT_EQ(2, browser2->tab_strip_model()->count());
864 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
865 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
866 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
867 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
868}
869
870IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42871 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56872 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
873 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40874 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46875
Scott Violet1f106b582017-07-12 15:49:58876 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56877 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20878 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56879
880 browsers_.push_back(CreateBrowser(browser()->profile()));
881 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
882 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40883 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56884 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32885 ASSERT_NO_FATAL_FAILURE(CancelClose());
886 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56887 cancel_observer.Wait();
888 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
889 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
890 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
891
Scott Violet1f106b582017-07-12 15:49:58892 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56893 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20894 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56895 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32896 ASSERT_NO_FATAL_FAILURE(AcceptClose());
897 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56898
899 close_observer.Wait();
900 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37901 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56902}
903
904IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46905 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56906 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
907 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40908 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46909
Scott Violet1f106b582017-07-12 15:49:58910 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56911 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20912 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56913
914 browsers_.push_back(CreateBrowser(browser()->profile()));
915 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
916 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40917 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56918 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32919 ASSERT_NO_FATAL_FAILURE(CancelClose());
920 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56921 cancel_observer.Wait();
922 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
923 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
924 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
925
Scott Violet1f106b582017-07-12 15:49:58926 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56927 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20928 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56929 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32930 ASSERT_NO_FATAL_FAILURE(AcceptClose());
931 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56932
933 close_observer.Wait();
934 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37935 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56936}
937
938IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
939 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56940 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
941 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
942 browsers_.push_back(CreateBrowser(browser()->profile()));
943 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
944 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40945 PrepareForDialog(browsers_[0]);
946 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46947
Scott Violet1f106b582017-07-12 15:49:58948 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56949 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20950 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56951
952 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32953 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56954 cancel_observer.Wait();
955 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
956 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
957 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
958
Scott Violet1f106b582017-07-12 15:49:58959 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56960 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20961 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56962 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32963 ASSERT_NO_FATAL_FAILURE(AcceptClose());
964 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56965
966 close_observer.Wait();
967 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37968 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56969}
970
jackhouae8e6e52015-05-29 06:36:46971// Mac has its own in-progress download prompt in app_controller_mac.mm, so
972// BrowserCloseManager should simply close all browsers. If there are no
973// browsers, it should not crash.
974#if defined(OS_MACOSX)
Mattias Nissler441cc652018-04-05 00:08:56975IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46976 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
977
Scott Violet1f106b582017-07-12 15:49:58978 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46979 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
980
981 TestBrowserCloseManager::AttemptClose(
982 TestBrowserCloseManager::NO_USER_CHOICE);
983 close_observer.Wait();
984 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37985 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44986 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46987
988 // Attempting to close again should not crash.
989 TestBrowserCloseManager::AttemptClose(
990 TestBrowserCloseManager::NO_USER_CHOICE);
991}
992#else // defined(OS_MACOSX)
993
[email protected]422a7d12013-10-21 12:10:42994// Test shutdown with a DANGEROUS_URL download undecided.
Mattias Nissler441cc652018-04-05 00:08:56995IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
996 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42997 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35998 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42999 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:441000 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091001 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421002
1003 // Run a dangerous download, but the user doesn't make a decision.
1004 // This .swf normally would be categorized as DANGEROUS_FILE, but
1005 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:431006 GURL download_url(
1007 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421008 content::DownloadTestObserverInterrupted observer(
1009 content::BrowserContext::GetDownloadManager(browser()->profile()),
1010 1,
1011 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1012 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191013 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421014 ui_test_utils::BROWSER_TEST_NONE);
1015 observer.WaitForFinished();
1016
1017 // Check that the download manager has the expected state.
1018 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1019 browser()->profile())->InProgressCount());
1020 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1021 browser()->profile())->NonMaliciousInProgressCount());
1022
1023 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581024 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421025 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1026 TestBrowserCloseManager::AttemptClose(
1027 TestBrowserCloseManager::NO_USER_CHOICE);
1028 close_observer.Wait();
1029 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371030 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421031}
1032
[email protected]edfca702013-08-16 08:58:141033// Test shutdown with a download in progress.
Mattias Nissler441cc652018-04-05 00:08:561034IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:141035 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1036 content::TestNavigationObserver navigation_observer(
1037 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1038 TestBrowserCloseManager::AttemptClose(
1039 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1040 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1041 navigation_observer.Wait();
1042 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1043 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1044
Scott Violet1f106b582017-07-12 15:49:581045 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141046 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1047
1048 TestBrowserCloseManager::AttemptClose(
1049 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1050 close_observer.Wait();
1051 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371052 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381053 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441054 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381055 else
peterccb33e82017-05-02 19:00:441056 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141057}
1058
sammccd347712015-03-13 22:02:321059// Test shutdown with a download in progress in an off-the-record profile.
Mattias Nissler441cc652018-04-05 00:08:561060IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321061 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321062 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321063 Browser* otr_browser = CreateBrowser(otr_profile);
1064 {
Scott Violet1f106b582017-07-12 15:49:581065 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321066 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1067 browser()->window()->Close();
1068 close_observer.Wait();
1069 }
1070 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1071 content::TestNavigationObserver navigation_observer(
1072 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1073 TestBrowserCloseManager::AttemptClose(
1074 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1075 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1076 navigation_observer.Wait();
1077 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1078 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1079
Scott Violet1f106b582017-07-12 15:49:581080 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321081 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1082
1083 TestBrowserCloseManager::AttemptClose(
1084 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1085 close_observer.Wait();
1086 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371087 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441088 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321089}
1090
Asanka Herathd1341dc2017-04-19 18:28:391091// Test shutdown with a download in progress in a regular profile an inconito
1092// browser is opened and closed. While there are active downloads, closing the
1093// incognito window shouldn't block on the active downloads which belong to the
1094// parent profile.
Weza78944d2018-05-17 16:56:291095// TODO(https://crbug.com/844019): Fix the notification expectation around the
1096// call to AttemptClose.
Mattias Nissler441cc652018-04-05 00:08:561097IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291098 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391099 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391100 Browser* otr_browser = CreateBrowser(otr_profile);
1101 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1102
1103 content::TestNavigationObserver navigation_observer(
1104 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1105 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1106 navigation_observer.Wait();
1107
1108 int num_downloads_blocking = 0;
1109 ASSERT_EQ(
1110 Browser::DOWNLOAD_CLOSE_OK,
1111 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1112 ASSERT_EQ(0, num_downloads_blocking);
1113
1114 {
Scott Violet1f106b582017-07-12 15:49:581115 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391116 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1117 otr_browser->window()->Close();
1118 close_observer.Wait();
1119 }
1120
1121 ASSERT_EQ(
1122 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1123 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1124 ASSERT_EQ(1, num_downloads_blocking);
1125
1126 {
Scott Violet1f106b582017-07-12 15:49:581127 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391128 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1129 TestBrowserCloseManager::AttemptClose(
1130 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1131 close_observer.Wait();
1132 }
1133
1134 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1135 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1136 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441137 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391138 else
peterccb33e82017-05-02 19:00:441139 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391140}
1141
[email protected]edfca702013-08-16 08:58:141142// Test shutdown with a download in progress from one profile, where the only
1143// open windows are for another profile.
Mattias Nissler441cc652018-04-05 00:08:561144IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141145 TestWithDownloadsFromDifferentProfiles) {
1146 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511147 Profile* other_profile = nullptr;
1148 {
1149 base::FilePath path =
1150 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131151 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511152 if (!base::PathExists(path))
1153 ASSERT_TRUE(base::CreateDirectory(path));
1154 other_profile =
1155 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1156 }
[email protected]edfca702013-08-16 08:58:141157 profile_manager->RegisterTestingProfile(other_profile, true, false);
1158 Browser* other_profile_browser = CreateBrowser(other_profile);
1159
[email protected]edfca702013-08-16 08:58:141160 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1161 {
Scott Violet1f106b582017-07-12 15:49:581162 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141163 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1164 browser()->window()->Close();
1165 close_observer.Wait();
1166 }
1167
1168 // When the shutdown is cancelled, the downloads page should be opened in a
1169 // browser for that profile. Because there are no browsers for that profile, a
1170 // new browser should be opened.
1171 ui_test_utils::BrowserAddedObserver new_browser_observer;
1172 TestBrowserCloseManager::AttemptClose(
1173 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1174 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1175 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1176 EXPECT_EQ(
1177 GURL(chrome::kChromeUIDownloadsURL),
1178 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1179 EXPECT_EQ(GURL("about:blank"),
1180 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1181 ->GetURL());
1182
Scott Violet1f106b582017-07-12 15:49:581183 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141184 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1185 TestBrowserCloseManager::AttemptClose(
1186 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1187 close_observer.Wait();
1188 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371189 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381190 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441191 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381192 else
peterccb33e82017-05-02 19:00:441193 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141194}
1195
Vasilii Sukhanov4a65b862017-08-04 10:01:371196// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141197// Test shutdown with downloads in progress and beforeunload handlers.
Mattias Nissler441cc652018-04-05 00:08:561198IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371199 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141200 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1201 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1202 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401203 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141204
1205 content::WindowedNotificationObserver cancel_observer(
1206 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1207 content::NotificationService::AllSources());
1208 TestBrowserCloseManager::AttemptClose(
1209 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321210 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141211 cancel_observer.Wait();
1212 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1213
Scott Violet1f106b582017-07-12 15:49:581214 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141215 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1216 TestBrowserCloseManager::AttemptClose(
1217 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321218 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141219 close_observer.Wait();
1220 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371221 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141222}
1223
jackhouae8e6e52015-05-29 06:36:461224#endif // defined(OS_MACOSX)
1225
Mattias Nissler441cc652018-04-05 00:08:561226INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
1227 BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561228 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201229
Denis Kuznetsov32099f32017-12-15 14:04:571230#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1231
[email protected]0c95faf42013-10-28 06:27:201232class BrowserCloseManagerWithBackgroundModeBrowserTest
1233 : public BrowserCloseManagerBrowserTest {
1234 public:
1235 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1236
Daniel Chenga542fca2014-10-21 09:51:291237 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201238 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1239 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351240 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201241 }
1242
1243 bool IsBackgroundModeSuspended() {
1244 return static_cast<FakeBackgroundModeManager*>(
1245 g_browser_process->background_mode_manager())
1246 ->IsBackgroundModeSuspended();
1247 }
1248
1249 private:
1250 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1251};
1252
1253// Check that background mode is suspended when closing all browsers unless we
1254// are quitting and that background mode is resumed when a new browser window is
1255// opened.
1256IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1257 CloseAllBrowsersWithBackgroundMode) {
1258 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351259 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201260 Profile* profile = browser()->profile();
1261 {
Scott Violet1f106b582017-07-12 15:49:581262 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201263 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381264 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1265 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201266 chrome::CloseAllBrowsers();
1267 close_observer.Wait();
1268 }
1269 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371270 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201271 EXPECT_TRUE(IsBackgroundModeSuspended());
1272
1273 // Background mode should be resumed when a new browser window is opened.
1274 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441275 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201276 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381277 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201278 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581279 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201280 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1281
1282 // Background mode should not be suspended when quitting.
1283 chrome::CloseAllBrowsersAndQuit();
1284 close_observer.Wait();
1285 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371286 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201287 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201288}
1289
1290// Check that closing the last browser window individually does not affect
1291// background mode.
1292IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161293 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581294 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201295 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1296 EXPECT_FALSE(IsBackgroundModeSuspended());
1297 browser()->window()->Close();
1298 close_observer.Wait();
1299 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371300 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201301 EXPECT_FALSE(IsBackgroundModeSuspended());
1302}
1303
1304// Check that closing all browsers with no browser windows open suspends
1305// background mode but does not cause Chrome to quit.
1306IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031307 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581308 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201309 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1310 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381311 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1312 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201313 browser()->window()->Close();
1314 close_observer.Wait();
1315 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371316 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201317 EXPECT_FALSE(IsBackgroundModeSuspended());
1318
1319 chrome::CloseAllBrowsers();
1320 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371321 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201322 EXPECT_TRUE(IsBackgroundModeSuspended());
1323}
1324
1325INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1326 BrowserCloseManagerWithBackgroundModeBrowserTest,
1327 testing::Bool());
Denis Kuznetsov32099f32017-12-15 14:04:571328#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)