blob: 16b1785927a623f4a8fd2362c462e874013053c7 [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"
Scott Violet6200d332018-02-23 21:29:2336#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5637#include "chrome/common/chrome_switches.h"
[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"
Min Qina9f487872018-02-09 20:43:2343#include "components/download/public/common/download_item.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5944#include "components/keep_alive_registry/keep_alive_types.h"
45#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4046#include "components/sessions/core/tab_restore_service.h"
47#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1448#include "content/public/browser/browser_context.h"
Gabriel Charette790754c2018-03-16 21:32:5949#include "content/public/browser/browser_thread.h"
[email protected]edfca702013-08-16 08:58:1450#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5651#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3452#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4653#include "content/public/browser/render_view_host.h"
54#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5655#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4056#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1457#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0958#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5659#include "content/public/test/test_navigation_observer.h"
60#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5661
[email protected]f9357a442014-05-15 18:44:0762#if defined(OS_CHROMEOS)
63#include "chromeos/chromeos_switches.h"
64#endif
65
[email protected]2e9d79f2013-08-16 05:45:5666namespace {
67
oshima0929be2a2014-11-19 22:21:0368app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5269 app_modal::JavaScriptAppModalDialog* dialog =
70 ui_test_utils::WaitForAppModalDialog();
71 CHECK(dialog->native_dialog());
72 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3273}
[email protected]2e9d79f2013-08-16 05:45:5674
avi336125f72017-05-06 22:25:4075// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4676// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3277void AcceptClose() {
78 GetNextDialog()->AcceptAppModalDialog();
79}
[email protected]2e9d79f2013-08-16 05:45:5680
avi336125f72017-05-06 22:25:4081// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4682// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3283void CancelClose() {
84 GetNextDialog()->CancelAppModalDialog();
85}
[email protected]2e9d79f2013-08-16 05:45:5686
Scott Violet1f106b582017-07-12 15:49:5887class RepeatedNotificationObserver : public content::NotificationObserver {
88 public:
89 explicit RepeatedNotificationObserver(int type, int count)
90 : num_outstanding_(count), running_(false) {
91 registrar_.Add(this, type, content::NotificationService::AllSources());
92 }
93
94 void Observe(int type,
95 const content::NotificationSource& source,
96 const content::NotificationDetails& details) override {
97 ASSERT_GT(num_outstanding_, 0);
98 if (!--num_outstanding_ && running_) {
99 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
100 run_loop_.QuitClosure());
101 }
102 }
103
104 void Wait() {
105 if (num_outstanding_ <= 0)
106 return;
107
108 running_ = true;
109 run_loop_.Run();
110 running_ = false;
Weza78944d2018-05-17 16:56:29111
112 EXPECT_LE(num_outstanding_, 0);
Scott Violet1f106b582017-07-12 15:49:58113 }
114
115 private:
116 int num_outstanding_;
117 content::NotificationRegistrar registrar_;
118 bool running_;
119 base::RunLoop run_loop_;
120
121 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
122};
123
mkolom65a0b3c2017-03-02 06:11:40124class TabRestoreServiceChangesObserver
125 : public sessions::TabRestoreServiceObserver {
126 public:
127 explicit TabRestoreServiceChangesObserver(Profile* profile)
128 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
129 if (service_)
130 service_->AddObserver(this);
131 }
132
133 ~TabRestoreServiceChangesObserver() override {
134 if (service_)
135 service_->RemoveObserver(this);
136 }
137
138 size_t changes_count() const { return changes_count_; }
139
140 private:
141 // sessions::TabRestoreServiceObserver:
142 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
143 changes_count_++;
144 }
145
146 // sessions::TabRestoreServiceObserver:
147 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
148 service_ = nullptr;
149 }
150
151 sessions::TabRestoreService* service_ = nullptr;
152 size_t changes_count_ = 0;
153
154 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
155};
156
[email protected]edfca702013-08-16 08:58:14157class TestBrowserCloseManager : public BrowserCloseManager {
158 public:
159 enum UserChoice {
160 USER_CHOICE_USER_CANCELS_CLOSE,
161 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42162 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14163 };
164
165 static void AttemptClose(UserChoice user_choice) {
166 scoped_refptr<BrowserCloseManager> browser_close_manager =
167 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20168 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14169 browser_close_manager->StartClosingBrowsers();
170 }
171
172 protected:
Daniel Chenga542fca2014-10-21 09:51:29173 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14174
Daniel Chenga542fca2014-10-21 09:51:29175 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14176 int download_count,
mostynb2b52d1db2014-10-07 02:47:17177 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42178 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14179 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42180 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14181 case USER_CHOICE_USER_CANCELS_CLOSE: {
182 callback.Run(false);
183 break;
184 }
185 case USER_CHOICE_USER_ALLOWS_CLOSE: {
186 callback.Run(true);
187 break;
188 }
189 }
190 }
191
192 private:
193 explicit TestBrowserCloseManager(UserChoice user_choice)
194 : user_choice_(user_choice) {}
195
196 UserChoice user_choice_;
197
198 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
199};
200
[email protected]422a7d12013-10-21 12:10:42201class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
202 public:
203 explicit TestDownloadManagerDelegate(Profile* profile)
204 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23205 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42206 }
Daniel Chenga542fca2014-10-21 09:51:29207 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42208
Daniel Chenga542fca2014-10-21 09:51:29209 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23210 download::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17211 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42212 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41213 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42214 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
215 item, dangerous_callback);
216 }
217
Asanka Herath1ba0e9f2017-04-03 18:48:53218 static void SetDangerous(const content::DownloadTargetCallback& callback,
219 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23220 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35221 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53222 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16223 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35224 callback.Run(target_path, disp,
225 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53226 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42227 }
[email protected]422a7d12013-10-21 12:10:42228};
229
Denis Kuznetsov32099f32017-12-15 14:04:57230#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20231class FakeBackgroundModeManager : public BackgroundModeManager {
232 public:
233 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54234 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
235 &g_browser_process->profile_manager()
236 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20237 suspended_(false) {}
238
Daniel Chenga542fca2014-10-21 09:51:29239 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20240 BackgroundModeManager::SuspendBackgroundMode();
241 suspended_ = true;
242 }
243
Daniel Chenga542fca2014-10-21 09:51:29244 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20245 BackgroundModeManager::ResumeBackgroundMode();
246 suspended_ = false;
247 }
248
249 bool IsBackgroundModeSuspended() {
250 return suspended_;
251 }
252
253 private:
254 bool suspended_;
255
256 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
257};
Denis Kuznetsov32099f32017-12-15 14:04:57258#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20259
[email protected]2e9d79f2013-08-16 05:45:56260} // namespace
261
262class BrowserCloseManagerBrowserTest
263 : public InProcessBrowserTest,
264 public testing::WithParamInterface<bool> {
265 protected:
Daniel Chenga542fca2014-10-21 09:51:29266 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56267 SessionStartupPref::SetStartupPref(
268 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
269 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14270 content::BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33271 content::BrowserThread::IO, FROM_HERE,
272 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
Colin Blundellfb2cfb2b2018-01-18 18:50:09273 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
274 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
275 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56276 }
277
avi556c05022014-12-22 23:31:43278 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56279 if (GetParam())
280 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07281#if defined(OS_CHROMEOS)
282 command_line->AppendSwitch(
283 chromeos::switches::kIgnoreUserProfileMappingForTests);
284#endif
[email protected]2e9d79f2013-08-16 05:45:56285 }
286
[email protected]edfca702013-08-16 08:58:14287 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09288 ASSERT_TRUE(embedded_test_server()->Started());
289
290 GURL slow_download_url = embedded_test_server()->GetURL(
291 content::SlowDownloadHttpResponse::kKnownSizeUrl);
292
[email protected]edfca702013-08-16 08:58:14293 content::DownloadTestObserverInProgress observer(
294 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
295 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09296 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14297 ui_test_utils::BROWSER_TEST_NONE);
298 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23299 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
300 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14301 }
302
avi336125f72017-05-06 22:25:40303 void PrepareForDialog(content::WebContents* web_contents) {
304 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46305 }
306
avi336125f72017-05-06 22:25:40307 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46308 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40309 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46310 }
311
[email protected]2e9d79f2013-08-16 05:45:56312 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56313};
314
315IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56316 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
317 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40318 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46319
Scott Violet1f106b582017-07-12 15:49:58320 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56321 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20322 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32323 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56324 cancel_observer.Wait();
325 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
326 EXPECT_EQ(1, browser()->tab_strip_model()->count());
327
Scott Violet1f106b582017-07-12 15:49:58328 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56329 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20330 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32331 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56332 close_observer.Wait();
333 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37334 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56335}
336
337IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
338 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56339 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
340 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40341 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46342
Scott Violet1f106b582017-07-12 15:49:58343 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56344 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20345 chrome::CloseAllBrowsersAndQuit();
346 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32347 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56348 cancel_observer.Wait();
349 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
350 EXPECT_EQ(1, browser()->tab_strip_model()->count());
351
Scott Violet1f106b582017-07-12 15:49:58352 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56353 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20354 chrome::CloseAllBrowsersAndQuit();
355 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32356 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56357 close_observer.Wait();
358 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37359 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56360}
361
wafflesb63234052015-08-07 02:44:02362IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46363 PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56364 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
365 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
366 AddBlankTabAndShow(browser());
367 ASSERT_NO_FATAL_FAILURE(
368 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40369 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46370
Scott Violet1f106b582017-07-12 15:49:58371 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56372 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20373 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32374 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56375 cancel_observer.Wait();
376 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
377
378 browser()->tab_strip_model()
379 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
380 content::TestNavigationObserver navigation_observer(
381 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19382 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
383 browser(), GURL(chrome::kChromeUIVersionURL),
384 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32385 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56386 navigation_observer.Wait();
387
Scott Violet1f106b582017-07-12 15:49:58388 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56389 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20390 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56391 close_observer.Wait();
392 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37393 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56394}
395
396// Test that the tab closed after the aborted shutdown attempt is not re-opened
397// when restoring the session.
benwells70cc93222017-06-29 05:42:36398// Flaky on Windows trybots, see https://crbug.com/737860.
399#if defined(OS_WIN)
400#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
401#else
402#define MAYBE_TestSessionRestore TestSessionRestore
403#endif
wafflesb63234052015-08-07 02:44:02404IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36405 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56406 // The testing framework launches Chrome with about:blank as args.
407 EXPECT_EQ(2, browser()->tab_strip_model()->count());
408 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
409 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
410 EXPECT_EQ(GURL("about:blank"),
411 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
412}
413
414// Test that browser windows are only closed if all browsers are ready to close
415// and that all beforeunload dialogs are shown again after a cancel.
[email protected]40cd13a2013-10-24 13:42:01416IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56417 browsers_.push_back(CreateBrowser(browser()->profile()));
418 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
419 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
420 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
421 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40422 PrepareForDialog(browsers_[0]);
423 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56424
425 // Cancel shutdown on the first beforeunload event.
426 {
Scott Violet1f106b582017-07-12 15:49:58427 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56428 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20429 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32430 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56431 cancel_observer.Wait();
432 }
433 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
434 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
435 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
436
437 // Cancel shutdown on the second beforeunload event.
438 {
Scott Violet1f106b582017-07-12 15:49:58439 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56440 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20441 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32442 ASSERT_NO_FATAL_FAILURE(AcceptClose());
443 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56444 cancel_observer.Wait();
445 }
446 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
447 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
448 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
449
450 // Allow shutdown for both beforeunload events.
Scott Violet1f106b582017-07-12 15:49:58451 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56452 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20453 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32454 ASSERT_NO_FATAL_FAILURE(AcceptClose());
455 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56456 close_observer.Wait();
457 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37458 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56459}
460
461// Test that tabs in the same window with a beforeunload event that hangs are
462// treated the same as the user accepting the close, but do not close the tab
463// early.
464IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46465 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56466 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
467 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
468 AddBlankTabAndShow(browsers_[0]);
469 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
470 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
471 AddBlankTabAndShow(browsers_[0]);
472 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
473 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46474 // Disable the hang monitor in the tab that is not expected to hang, so that
475 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40476 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56477
Scott Violet1f106b582017-07-12 15:49:58478 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56479 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20480 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32481 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56482 cancel_observer.Wait();
483 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
484 // All tabs should still be open.
485 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
486
Scott Violet1f106b582017-07-12 15:49:58487 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56488 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20489 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32490 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56491 close_observer.Wait();
492 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37493 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56494}
495
496// Test that tabs in different windows with a beforeunload event that hangs are
497// treated the same as the user accepting the close, but do not close the tab
498// early.
499IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
500 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56501 browsers_.push_back(CreateBrowser(browser()->profile()));
502 browsers_.push_back(CreateBrowser(browser()->profile()));
503 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
504 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
505 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
506 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
507 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
508 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46509 // Disable the hang monitor in the tab that is not expected to hang, so that
510 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40511 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56512
Scott Violet1f106b582017-07-12 15:49:58513 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56514 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20515 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32516 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56517 cancel_observer.Wait();
518 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
519 // All windows should still be open.
520 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
521 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
522 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
523
Scott Violet1f106b582017-07-12 15:49:58524 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56525 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20526 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32527 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56528 close_observer.Wait();
529 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37530 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56531}
532
metaflow73fc9552016-05-23 18:26:48533// Test that tabs that are slow to respond are not closed prematurely.
534// Regression for crbug.com/365052 caused some of tabs to be closed even if
535// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36536// Flaky on ChromeOS ASan. https://crbug.com/805457
537#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
538#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
539#else
540#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
541#endif
metaflow73fc9552016-05-23 18:26:48542IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36543 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48544 const int kTabCount = 5;
545 const int kResposiveTabIndex = 2;
546 // Create tab strip with all tabs except one responding after
547 // RenderViewHostImpl::kUnloadTimeoutMS.
548 // Minimum configuration is two slow tabs and then responsive tab.
549 // But we also want to check how slow tabs behave in tail.
550 for (int i = 0; i < kTabCount; i++) {
551 if (i)
552 AddBlankTabAndShow(browsers_[0]);
553 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
554 browsers_[0],
555 embedded_test_server()->GetURL((i == kResposiveTabIndex)
556 ? "/beforeunload.html"
557 : "/beforeunload_slow.html")));
558 }
metaflow812bb8542016-05-24 21:10:46559 // Disable the hang monitor in the tab that is not expected to hang, so that
560 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40561 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46562 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
563
Scott Violet1f106b582017-07-12 15:49:58564 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48565 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
566 chrome::CloseAllBrowsersAndQuit();
567 ASSERT_NO_FATAL_FAILURE(CancelClose());
568 cancel_observer.Wait();
569 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
570
571 // All tabs should still be open.
572 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58573 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48574 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
575
576 // Quit, this time accepting close confirmation dialog.
577 chrome::CloseAllBrowsersAndQuit();
578 ASSERT_NO_FATAL_FAILURE(AcceptClose());
579 close_observer.Wait();
580 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
581 EXPECT_TRUE(BrowserList::GetInstance()->empty());
582}
583
584// Test that tabs in different windows with a slow beforeunload event response
585// are treated the same as the user accepting the close, but do not close the
586// tab early.
587// Regression for crbug.com/365052 caused CHECK in tabstrip.
jdoerrie6cc3bd82018-03-07 16:42:56588// Flaky on Mac and Linux: https://crbug.com/819541
589#if defined(OS_LINUX) || defined(OS_MACOSX)
590#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
591 DISABLED_TestBeforeUnloadMultipleSlowWindows
592#else
593#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
594 TestBeforeUnloadMultipleSlowWindows
595#endif
metaflow73fc9552016-05-23 18:26:48596IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
jdoerrie6cc3bd82018-03-07 16:42:56597 MAYBE_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48598 const int kBrowserCount = 5;
599 const int kResposiveBrowserIndex = 2;
600 // Create multiple browsers with all tabs except one responding after
601 // RenderViewHostImpl::kUnloadTimeoutMS .
602 // Minimum configuration is just one browser with slow tab and then
603 // browser with responsive tab.
604 // But we also want to check how slow tabs behave in tail and make test
605 // more robust.
606 for (int i = 0; i < kBrowserCount; i++) {
607 if (i)
608 browsers_.push_back(CreateBrowser(browser()->profile()));
609 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
610 browsers_[i],
611 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
612 ? "/beforeunload.html"
613 : "/beforeunload_slow.html")));
614 }
metaflow812bb8542016-05-24 21:10:46615 // Disable the hang monitor in the tab that is not expected to hang, so that
616 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40617 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48618
Scott Violet1f106b582017-07-12 15:49:58619 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48620 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
621 chrome::CloseAllBrowsersAndQuit();
622 ASSERT_NO_FATAL_FAILURE(CancelClose());
623 cancel_observer.Wait();
624 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
625
626 // All windows should still be open.
627 for (int i = 0; i < kBrowserCount; i++)
628 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
629
630 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58631 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48632 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
633 chrome::CloseAllBrowsersAndQuit();
634 ASSERT_NO_FATAL_FAILURE(AcceptClose());
635 close_observer.Wait();
636 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
637 EXPECT_TRUE(BrowserList::GetInstance()->empty());
638}
639
Jan Krcal9a23f222017-08-01 14:13:07640// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
641#if defined(OS_WIN) && !defined(NDEBUG)
642#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
643#else
644#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
645#endif
646
[email protected]2e9d79f2013-08-16 05:45:56647// Test that a window created during shutdown is closed.
648IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07649 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56650 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
651 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40652 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56653
Scott Violet1f106b582017-07-12 15:49:58654 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56655 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20656 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56657 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32658 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56659 close_observer.Wait();
660 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37661 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56662}
663
664// Test that a window created during shutdown with a beforeunload handler can
665// cancel the shutdown.
666IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01667 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56668 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
669 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40670 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56671
Scott Violet1f106b582017-07-12 15:49:58672 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56673 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20674 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56675 browsers_.push_back(CreateBrowser(browser()->profile()));
676 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
677 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40678 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32679 ASSERT_NO_FATAL_FAILURE(AcceptClose());
680 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56681 cancel_observer.Wait();
682 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
683 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
684 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
685
686 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58687 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56688 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20689 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32690 ASSERT_NO_FATAL_FAILURE(AcceptClose());
691 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56692 close_observer.Wait();
693 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37694 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56695}
696
697// Test that tabs added during shutdown are closed.
698IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46699 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56700 browsers_.push_back(CreateBrowser(browser()->profile()));
701 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
702 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
703 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
704 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40705 PrepareForDialog(browsers_[0]);
706 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56707
Scott Violet1f106b582017-07-12 15:49:58708 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56709 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20710 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32711 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56712 AddBlankTabAndShow(browsers_[0]);
713 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32714 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56715 close_observer.Wait();
716 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37717 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56718}
719
720// Test that tabs created during shutdown with beforeunload handlers can cancel
721// the shutdown.
metaflow812bb8542016-05-24 21:10:46722
[email protected]2e9d79f2013-08-16 05:45:56723IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46724 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56725 browsers_.push_back(CreateBrowser(browser()->profile()));
726 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
727 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
728 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
729 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40730 PrepareForDialog(browsers_[0]);
731 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46732
Scott Violet1f106b582017-07-12 15:49:58733 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56734 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20735 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32736 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56737 AddBlankTabAndShow(browsers_[0]);
738 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
739 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
740 AddBlankTabAndShow(browsers_[1]);
741 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
742 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40743 PrepareForDialog(browsers_[0]);
744 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32745 ASSERT_NO_FATAL_FAILURE(AcceptClose());
746 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56747 cancel_observer.Wait();
748 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
749 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
750 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
751
Scott Violet1f106b582017-07-12 15:49:58752 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56753 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20754 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32755 ASSERT_NO_FATAL_FAILURE(AcceptClose());
756 ASSERT_NO_FATAL_FAILURE(AcceptClose());
757 ASSERT_NO_FATAL_FAILURE(AcceptClose());
758 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56759
760 close_observer.Wait();
761 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37762 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56763}
764
rogermf18929c2017-04-19 19:59:33765// TODO(crbug/713201):
766// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
767#if defined(OS_MACOSX)
768#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
769#else
770#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
771#endif
772
[email protected]2e9d79f2013-08-16 05:45:56773IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33774 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40775 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
776 // And it is difficult to fix this issue without fixing that one.
777 if (GetParam())
778 return;
779
mkolom65a0b3c2017-03-02 06:11:40780 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
781 browser(), embedded_test_server()->GetURL("/title1.html")));
782
783 // Open second window.
784 ui_test_utils::NavigateToURLWithDisposition(
785 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
786 WindowOpenDisposition::NEW_WINDOW,
787 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
788 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
789 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
790 ? BrowserList::GetInstance()->get(0)
791 : BrowserList::GetInstance()->get(1);
792 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
793
794 // Let's work with second window only.
795 // This page has beforeunload handler already.
796 EXPECT_TRUE(browser2->tab_strip_model()
797 ->GetWebContentsAt(0)
798 ->NeedToFireBeforeUnload());
799 // This page doesn't have beforeunload handler. Yet.
800 ui_test_utils::NavigateToURLWithDisposition(
801 browser2, embedded_test_server()->GetURL("/title2.html"),
802 WindowOpenDisposition::NEW_FOREGROUND_TAB,
803 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
804 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
805 EXPECT_FALSE(browser2->tab_strip_model()
806 ->GetWebContentsAt(1)
807 ->NeedToFireBeforeUnload());
808 EXPECT_EQ(2, browser2->tab_strip_model()->count());
809
avi336125f72017-05-06 22:25:40810 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40811
812 // The test.
813
814 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
815 content::WindowedNotificationObserver observer(
816 chrome::NOTIFICATION_BROWSER_CLOSED,
817 content::NotificationService::AllSources());
818 chrome::CloseWindow(browser2);
819 // Just to be sure CloseWindow doesn't have asynchronous tasks
820 // that could have an impact.
821 content::RunAllPendingInMessageLoop();
822
823 // Closing browser shouldn't happen because of beforeunload handler.
824 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
825 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
826 // yet.
827 ASSERT_TRUE(content::ExecuteScript(
828 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(),
829 "window.addEventListener('beforeunload', "
830 "function(event) { event.returnValue = 'Foo'; });"));
831 EXPECT_TRUE(browser2->tab_strip_model()
832 ->GetWebContentsAt(1)
833 ->NeedToFireBeforeUnload());
834 // Accept closing the first tab.
835 ASSERT_NO_FATAL_FAILURE(AcceptClose());
836 // Just to be sure accepting a dialog doesn't have asynchronous tasks
837 // that could have an impact.
838 content::RunAllPendingInMessageLoop();
839 // It shouldn't close the whole window/browser.
840 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
841 EXPECT_EQ(2, browser2->tab_strip_model()->count());
842 // Accept closing the second tab.
843 ASSERT_NO_FATAL_FAILURE(AcceptClose());
844 observer.Wait();
845 // Now the second window/browser should be closed.
846 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
847 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
848 EXPECT_EQ(1u, restore_observer.changes_count());
849
850 // Restore the closed browser.
851 content::WindowedNotificationObserver open_window_observer(
852 chrome::NOTIFICATION_BROWSER_OPENED,
853 content::NotificationService::AllSources());
854 chrome::OpenWindowWithRestoredTabs(browser()->profile());
855 open_window_observer.Wait();
856 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
857 browser2 = BrowserList::GetInstance()->get(0) != browser()
858 ? BrowserList::GetInstance()->get(0)
859 : BrowserList::GetInstance()->get(1);
860
861 // Check the restored browser contents.
862 EXPECT_EQ(2, browser2->tab_strip_model()->count());
863 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
864 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
865 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
866 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
867}
868
869IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42870 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56871 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
872 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40873 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46874
Scott Violet1f106b582017-07-12 15:49:58875 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56876 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20877 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56878
879 browsers_.push_back(CreateBrowser(browser()->profile()));
880 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
881 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40882 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56883 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32884 ASSERT_NO_FATAL_FAILURE(CancelClose());
885 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56886 cancel_observer.Wait();
887 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
888 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
889 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
890
Scott Violet1f106b582017-07-12 15:49:58891 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56892 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20893 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56894 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32895 ASSERT_NO_FATAL_FAILURE(AcceptClose());
896 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56897
898 close_observer.Wait();
899 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37900 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56901}
902
903IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46904 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56905 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
906 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40907 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46908
Scott Violet1f106b582017-07-12 15:49:58909 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56910 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20911 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56912
913 browsers_.push_back(CreateBrowser(browser()->profile()));
914 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
915 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40916 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56917 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32918 ASSERT_NO_FATAL_FAILURE(CancelClose());
919 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56920 cancel_observer.Wait();
921 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
922 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
923 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
924
Scott Violet1f106b582017-07-12 15:49:58925 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56926 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20927 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56928 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32929 ASSERT_NO_FATAL_FAILURE(AcceptClose());
930 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56931
932 close_observer.Wait();
933 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37934 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56935}
936
937IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
938 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56939 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
940 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
941 browsers_.push_back(CreateBrowser(browser()->profile()));
942 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
943 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40944 PrepareForDialog(browsers_[0]);
945 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46946
Scott Violet1f106b582017-07-12 15:49:58947 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56948 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20949 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56950
951 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32952 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56953 cancel_observer.Wait();
954 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
955 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
956 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
957
Scott Violet1f106b582017-07-12 15:49:58958 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56959 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20960 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56961 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32962 ASSERT_NO_FATAL_FAILURE(AcceptClose());
963 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56964
965 close_observer.Wait();
966 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37967 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56968}
969
jackhouae8e6e52015-05-29 06:36:46970// Mac has its own in-progress download prompt in app_controller_mac.mm, so
971// BrowserCloseManager should simply close all browsers. If there are no
972// browsers, it should not crash.
973#if defined(OS_MACOSX)
Mattias Nissler441cc652018-04-05 00:08:56974IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46975 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
976
Scott Violet1f106b582017-07-12 15:49:58977 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46978 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
979
980 TestBrowserCloseManager::AttemptClose(
981 TestBrowserCloseManager::NO_USER_CHOICE);
982 close_observer.Wait();
983 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37984 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44985 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46986
987 // Attempting to close again should not crash.
988 TestBrowserCloseManager::AttemptClose(
989 TestBrowserCloseManager::NO_USER_CHOICE);
990}
991#else // defined(OS_MACOSX)
992
[email protected]422a7d12013-10-21 12:10:42993// Test shutdown with a DANGEROUS_URL download undecided.
Mattias Nissler441cc652018-04-05 00:08:56994IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
995 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42996 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35997 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42998 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:44999 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091000 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421001
1002 // Run a dangerous download, but the user doesn't make a decision.
1003 // This .swf normally would be categorized as DANGEROUS_FILE, but
1004 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:431005 GURL download_url(
1006 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421007 content::DownloadTestObserverInterrupted observer(
1008 content::BrowserContext::GetDownloadManager(browser()->profile()),
1009 1,
1010 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1011 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191012 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421013 ui_test_utils::BROWSER_TEST_NONE);
1014 observer.WaitForFinished();
1015
1016 // Check that the download manager has the expected state.
1017 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1018 browser()->profile())->InProgressCount());
1019 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1020 browser()->profile())->NonMaliciousInProgressCount());
1021
1022 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581023 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421024 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1025 TestBrowserCloseManager::AttemptClose(
1026 TestBrowserCloseManager::NO_USER_CHOICE);
1027 close_observer.Wait();
1028 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371029 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421030}
1031
[email protected]edfca702013-08-16 08:58:141032// Test shutdown with a download in progress.
Mattias Nissler441cc652018-04-05 00:08:561033IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:141034 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1035 content::TestNavigationObserver navigation_observer(
1036 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1037 TestBrowserCloseManager::AttemptClose(
1038 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1039 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1040 navigation_observer.Wait();
1041 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1042 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1043
Scott Violet1f106b582017-07-12 15:49:581044 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141045 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1046
1047 TestBrowserCloseManager::AttemptClose(
1048 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1049 close_observer.Wait();
1050 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371051 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381052 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441053 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381054 else
peterccb33e82017-05-02 19:00:441055 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141056}
1057
sammccd347712015-03-13 22:02:321058// Test shutdown with a download in progress in an off-the-record profile.
Mattias Nissler441cc652018-04-05 00:08:561059IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321060 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321061 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321062 Browser* otr_browser = CreateBrowser(otr_profile);
1063 {
Scott Violet1f106b582017-07-12 15:49:581064 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321065 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1066 browser()->window()->Close();
1067 close_observer.Wait();
1068 }
1069 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1070 content::TestNavigationObserver navigation_observer(
1071 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1072 TestBrowserCloseManager::AttemptClose(
1073 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1074 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1075 navigation_observer.Wait();
1076 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1077 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1078
Scott Violet1f106b582017-07-12 15:49:581079 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321080 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1081
1082 TestBrowserCloseManager::AttemptClose(
1083 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1084 close_observer.Wait();
1085 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371086 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441087 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321088}
1089
Asanka Herathd1341dc2017-04-19 18:28:391090// Test shutdown with a download in progress in a regular profile an inconito
1091// browser is opened and closed. While there are active downloads, closing the
1092// incognito window shouldn't block on the active downloads which belong to the
1093// parent profile.
Weza78944d2018-05-17 16:56:291094// TODO(https://crbug.com/844019): Fix the notification expectation around the
1095// call to AttemptClose.
Mattias Nissler441cc652018-04-05 00:08:561096IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291097 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391098 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391099 Browser* otr_browser = CreateBrowser(otr_profile);
1100 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1101
1102 content::TestNavigationObserver navigation_observer(
1103 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1104 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1105 navigation_observer.Wait();
1106
1107 int num_downloads_blocking = 0;
1108 ASSERT_EQ(
1109 Browser::DOWNLOAD_CLOSE_OK,
1110 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1111 ASSERT_EQ(0, num_downloads_blocking);
1112
1113 {
Scott Violet1f106b582017-07-12 15:49:581114 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391115 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1116 otr_browser->window()->Close();
1117 close_observer.Wait();
1118 }
1119
1120 ASSERT_EQ(
1121 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1122 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1123 ASSERT_EQ(1, num_downloads_blocking);
1124
1125 {
Scott Violet1f106b582017-07-12 15:49:581126 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391127 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1128 TestBrowserCloseManager::AttemptClose(
1129 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1130 close_observer.Wait();
1131 }
1132
1133 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1134 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1135 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441136 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391137 else
peterccb33e82017-05-02 19:00:441138 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391139}
1140
[email protected]edfca702013-08-16 08:58:141141// Test shutdown with a download in progress from one profile, where the only
1142// open windows are for another profile.
Mattias Nissler441cc652018-04-05 00:08:561143IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141144 TestWithDownloadsFromDifferentProfiles) {
1145 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511146 Profile* other_profile = nullptr;
1147 {
1148 base::FilePath path =
1149 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131150 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511151 if (!base::PathExists(path))
1152 ASSERT_TRUE(base::CreateDirectory(path));
1153 other_profile =
1154 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1155 }
[email protected]edfca702013-08-16 08:58:141156 profile_manager->RegisterTestingProfile(other_profile, true, false);
1157 Browser* other_profile_browser = CreateBrowser(other_profile);
1158
[email protected]edfca702013-08-16 08:58:141159 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1160 {
Scott Violet1f106b582017-07-12 15:49:581161 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141162 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1163 browser()->window()->Close();
1164 close_observer.Wait();
1165 }
1166
1167 // When the shutdown is cancelled, the downloads page should be opened in a
1168 // browser for that profile. Because there are no browsers for that profile, a
1169 // new browser should be opened.
1170 ui_test_utils::BrowserAddedObserver new_browser_observer;
1171 TestBrowserCloseManager::AttemptClose(
1172 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1173 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1174 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1175 EXPECT_EQ(
1176 GURL(chrome::kChromeUIDownloadsURL),
1177 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1178 EXPECT_EQ(GURL("about:blank"),
1179 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1180 ->GetURL());
1181
Scott Violet1f106b582017-07-12 15:49:581182 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141183 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1184 TestBrowserCloseManager::AttemptClose(
1185 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1186 close_observer.Wait();
1187 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371188 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381189 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441190 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381191 else
peterccb33e82017-05-02 19:00:441192 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141193}
1194
Vasilii Sukhanov4a65b862017-08-04 10:01:371195// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141196// Test shutdown with downloads in progress and beforeunload handlers.
Mattias Nissler441cc652018-04-05 00:08:561197IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371198 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141199 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1200 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1201 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401202 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141203
1204 content::WindowedNotificationObserver cancel_observer(
1205 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1206 content::NotificationService::AllSources());
1207 TestBrowserCloseManager::AttemptClose(
1208 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321209 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141210 cancel_observer.Wait();
1211 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1212
Scott Violet1f106b582017-07-12 15:49:581213 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141214 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1215 TestBrowserCloseManager::AttemptClose(
1216 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321217 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141218 close_observer.Wait();
1219 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371220 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141221}
1222
jackhouae8e6e52015-05-29 06:36:461223#endif // defined(OS_MACOSX)
1224
Mattias Nissler441cc652018-04-05 00:08:561225INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
1226 BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561227 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201228
Denis Kuznetsov32099f32017-12-15 14:04:571229#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1230
[email protected]0c95faf42013-10-28 06:27:201231class BrowserCloseManagerWithBackgroundModeBrowserTest
1232 : public BrowserCloseManagerBrowserTest {
1233 public:
1234 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1235
Daniel Chenga542fca2014-10-21 09:51:291236 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201237 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1238 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351239 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201240 }
1241
1242 bool IsBackgroundModeSuspended() {
1243 return static_cast<FakeBackgroundModeManager*>(
1244 g_browser_process->background_mode_manager())
1245 ->IsBackgroundModeSuspended();
1246 }
1247
1248 private:
1249 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1250};
1251
1252// Check that background mode is suspended when closing all browsers unless we
1253// are quitting and that background mode is resumed when a new browser window is
1254// opened.
1255IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1256 CloseAllBrowsersWithBackgroundMode) {
1257 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351258 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201259 Profile* profile = browser()->profile();
1260 {
Scott Violet1f106b582017-07-12 15:49:581261 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201262 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381263 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1264 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201265 chrome::CloseAllBrowsers();
1266 close_observer.Wait();
1267 }
1268 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371269 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201270 EXPECT_TRUE(IsBackgroundModeSuspended());
1271
1272 // Background mode should be resumed when a new browser window is opened.
1273 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441274 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201275 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381276 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201277 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581278 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201279 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1280
1281 // Background mode should not be suspended when quitting.
1282 chrome::CloseAllBrowsersAndQuit();
1283 close_observer.Wait();
1284 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371285 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201286 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201287}
1288
1289// Check that closing the last browser window individually does not affect
1290// background mode.
1291IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161292 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581293 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201294 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1295 EXPECT_FALSE(IsBackgroundModeSuspended());
1296 browser()->window()->Close();
1297 close_observer.Wait();
1298 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371299 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201300 EXPECT_FALSE(IsBackgroundModeSuspended());
1301}
1302
1303// Check that closing all browsers with no browser windows open suspends
1304// background mode but does not cause Chrome to quit.
1305IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031306 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581307 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201308 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1309 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381310 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1311 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201312 browser()->window()->Close();
1313 close_observer.Wait();
1314 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371315 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201316 EXPECT_FALSE(IsBackgroundModeSuspended());
1317
1318 chrome::CloseAllBrowsers();
1319 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371320 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201321 EXPECT_TRUE(IsBackgroundModeSuspended());
1322}
1323
1324INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1325 BrowserCloseManagerWithBackgroundModeBrowserTest,
1326 testing::Bool());
Denis Kuznetsov32099f32017-12-15 14:04:571327#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)