blob: 8ef59bba355460ea546902b8c073218671fd7398 [file] [log] [blame]
[email protected]2e9d79f2013-08-16 05:45:561// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dchenge73d8520c2015-12-27 01:19:095#include "chrome/browser/lifetime/browser_close_manager.h"
6
7#include <utility>
[email protected]2e9d79f2013-08-16 05:45:568#include <vector>
9
Sebastien Marchandf1349f52019-01-25 03:16:4110#include "base/bind.h"
[email protected]2e9d79f2013-08-16 05:45:5611#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:0512#include "base/files/scoped_temp_dir.h"
avi6846aef2015-12-26 01:09:3813#include "base/macros.h"
Gabriel Charette078e3662017-08-28 22:59:0414#include "base/run_loop.h"
Eric Seckler8652dcd52018-09-20 10:42:2815#include "base/task/post_task.h"
jam3f2d3932017-04-26 20:28:5116#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3817#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2018#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1419#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5620#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3821#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4222#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4423#include "chrome/browser/download/download_core_service.h"
24#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0525#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5626#include "chrome/browser/lifetime/application_lifetime.h"
Avi Drissmand30927342018-05-22 15:04:2727#include "chrome/browser/lifetime/browser_shutdown.h"
[email protected]2e9d79f2013-08-16 05:45:5628#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1429#include "chrome/browser/profiles/profile.h"
30#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4031#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5632#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2033#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3734#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1435#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5636#include "chrome/browser/ui/tabs/tab_strip_model.h"
Scott Violet6200d332018-02-23 21:29:2337#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5638#include "chrome/common/chrome_switches.h"
39#include "chrome/common/url_constants.h"
40#include "chrome/test/base/in_process_browser_test.h"
41#include "chrome/test/base/ui_test_utils.h"
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)
Steven Bennetts38e9bde22019-01-03 17:31:1064#include "chromeos/constants/chromeos_switches.h"
[email protected]f9357a442014-05-15 18:44:0765#endif
66
[email protected]2e9d79f2013-08-16 05:45:5667namespace {
68
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
Avi Drissman78267f52019-01-16 21:21:42263class BrowserCloseManagerBrowserTest : public InProcessBrowserTest {
[email protected]2e9d79f2013-08-16 05:45:56264 protected:
Daniel Chenga542fca2014-10-21 09:51:29265 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56266 SessionStartupPref::SetStartupPref(
267 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
268 browsers_.push_back(browser());
Colin Blundellfb2cfb2b2018-01-18 18:50:09269 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
270 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
271 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56272 }
273
avi556c05022014-12-22 23:31:43274 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]f9357a442014-05-15 18:44:07275#if defined(OS_CHROMEOS)
276 command_line->AppendSwitch(
277 chromeos::switches::kIgnoreUserProfileMappingForTests);
278#endif
[email protected]2e9d79f2013-08-16 05:45:56279 }
280
[email protected]edfca702013-08-16 08:58:14281 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09282 ASSERT_TRUE(embedded_test_server()->Started());
283
284 GURL slow_download_url = embedded_test_server()->GetURL(
285 content::SlowDownloadHttpResponse::kKnownSizeUrl);
286
[email protected]edfca702013-08-16 08:58:14287 content::DownloadTestObserverInProgress observer(
288 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
289 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09290 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14291 ui_test_utils::BROWSER_TEST_NONE);
292 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23293 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
294 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14295 }
296
avi336125f72017-05-06 22:25:40297 void PrepareForDialog(content::WebContents* web_contents) {
298 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46299 }
300
avi336125f72017-05-06 22:25:40301 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46302 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40303 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46304 }
305
Evan Stade4487c452019-07-11 00:58:06306 void WaitForAllBrowsersToClose() {
307 for (size_t i = 0U; i < browsers_.size(); ++i)
308 ui_test_utils::WaitForBrowserToClose();
309 }
310
[email protected]2e9d79f2013-08-16 05:45:56311 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56312};
313
Avi Drissman78267f52019-01-16 21:21:42314IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56315 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
316 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40317 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46318
Scott Violet1f106b582017-07-12 15:49:58319 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56320 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20321 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32322 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56323 cancel_observer.Wait();
324 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
325 EXPECT_EQ(1, browser()->tab_strip_model()->count());
326
[email protected]0c95faf42013-10-28 06:27:20327 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32328 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06329 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56330 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37331 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56332}
333
Avi Drissman78267f52019-01-16 21:21:42334IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56335 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56336 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
337 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40338 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46339
Scott Violet1f106b582017-07-12 15:49:58340 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56341 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20342 chrome::CloseAllBrowsersAndQuit();
343 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32344 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56345 cancel_observer.Wait();
346 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
347 EXPECT_EQ(1, browser()->tab_strip_model()->count());
348
[email protected]0c95faf42013-10-28 06:27:20349 chrome::CloseAllBrowsersAndQuit();
350 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32351 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06352 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56353 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37354 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56355}
356
Avi Drissman78267f52019-01-16 21:21:42357IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56358 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
359 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
360 AddBlankTabAndShow(browser());
361 ASSERT_NO_FATAL_FAILURE(
362 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40363 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46364
Scott Violet1f106b582017-07-12 15:49:58365 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56366 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20367 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32368 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56369 cancel_observer.Wait();
370 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
371
372 browser()->tab_strip_model()
373 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
374 content::TestNavigationObserver navigation_observer(
375 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19376 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
377 browser(), GURL(chrome::kChromeUIVersionURL),
378 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32379 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56380 navigation_observer.Wait();
381
[email protected]0c95faf42013-10-28 06:27:20382 chrome::CloseAllBrowsersAndQuit();
Evan Stade4487c452019-07-11 00:58:06383 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56384 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37385 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56386}
387
388// Test that the tab closed after the aborted shutdown attempt is not re-opened
389// when restoring the session.
benwells70cc93222017-06-29 05:42:36390// Flaky on Windows trybots, see https://crbug.com/737860.
391#if defined(OS_WIN)
392#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
393#else
394#define MAYBE_TestSessionRestore TestSessionRestore
395#endif
Avi Drissman78267f52019-01-16 21:21:42396IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36397 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56398 // The testing framework launches Chrome with about:blank as args.
399 EXPECT_EQ(2, browser()->tab_strip_model()->count());
400 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
401 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
402 EXPECT_EQ(GURL("about:blank"),
403 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
404}
405
406// Test that browser windows are only closed if all browsers are ready to close
407// and that all beforeunload dialogs are shown again after a cancel.
Avi Drissman78267f52019-01-16 21:21:42408IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56409 browsers_.push_back(CreateBrowser(browser()->profile()));
410 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
411 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
412 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
413 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40414 PrepareForDialog(browsers_[0]);
415 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56416
417 // Cancel shutdown on the first beforeunload event.
418 {
Scott Violet1f106b582017-07-12 15:49:58419 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56420 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20421 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32422 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56423 cancel_observer.Wait();
424 }
425 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
426 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
427 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
428
429 // Cancel shutdown on the second beforeunload event.
430 {
Scott Violet1f106b582017-07-12 15:49:58431 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56432 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20433 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32434 ASSERT_NO_FATAL_FAILURE(AcceptClose());
435 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56436 cancel_observer.Wait();
437 }
438 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
439 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
440 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
441
442 // Allow shutdown for both beforeunload events.
[email protected]0c95faf42013-10-28 06:27:20443 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32444 ASSERT_NO_FATAL_FAILURE(AcceptClose());
445 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06446 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56447 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37448 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56449}
450
451// Test that tabs in the same window with a beforeunload event that hangs are
452// treated the same as the user accepting the close, but do not close the tab
453// early.
Avi Drissman78267f52019-01-16 21:21:42454IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46455 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56456 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
457 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
458 AddBlankTabAndShow(browsers_[0]);
459 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
460 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
461 AddBlankTabAndShow(browsers_[0]);
462 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
463 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46464 // Disable the hang monitor in the tab that is not expected to hang, so that
465 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40466 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56467
Scott Violet1f106b582017-07-12 15:49:58468 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56469 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20470 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32471 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56472 cancel_observer.Wait();
473 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
474 // All tabs should still be open.
475 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
476
[email protected]0c95faf42013-10-28 06:27:20477 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32478 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06479 ui_test_utils::WaitForBrowserToClose();
[email protected]2e9d79f2013-08-16 05:45:56480 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37481 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56482}
483
484// Test that tabs in different windows with a beforeunload event that hangs are
485// treated the same as the user accepting the close, but do not close the tab
486// early.
Avi Drissman78267f52019-01-16 21:21:42487IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56488 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56489 browsers_.push_back(CreateBrowser(browser()->profile()));
490 browsers_.push_back(CreateBrowser(browser()->profile()));
491 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
492 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
493 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
494 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
495 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
496 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46497 // Disable the hang monitor in the tab that is not expected to hang, so that
498 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40499 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56500
Scott Violet1f106b582017-07-12 15:49:58501 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56502 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20503 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32504 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56505 cancel_observer.Wait();
506 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
507 // All windows should still be open.
508 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
509 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
510 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
511
[email protected]0c95faf42013-10-28 06:27:20512 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32513 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06514
515 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56516}
517
metaflow73fc9552016-05-23 18:26:48518// Test that tabs that are slow to respond are not closed prematurely.
519// Regression for crbug.com/365052 caused some of tabs to be closed even if
520// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36521// Flaky on ChromeOS ASan. https://crbug.com/805457
522#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
523#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
524#else
525#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
526#endif
Avi Drissman78267f52019-01-16 21:21:42527IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36528 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48529 const int kTabCount = 5;
530 const int kResposiveTabIndex = 2;
531 // Create tab strip with all tabs except one responding after
532 // RenderViewHostImpl::kUnloadTimeoutMS.
533 // Minimum configuration is two slow tabs and then responsive tab.
534 // But we also want to check how slow tabs behave in tail.
535 for (int i = 0; i < kTabCount; i++) {
536 if (i)
537 AddBlankTabAndShow(browsers_[0]);
538 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
539 browsers_[0],
540 embedded_test_server()->GetURL((i == kResposiveTabIndex)
541 ? "/beforeunload.html"
542 : "/beforeunload_slow.html")));
543 }
metaflow812bb8542016-05-24 21:10:46544 // Disable the hang monitor in the tab that is not expected to hang, so that
545 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40546 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46547 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
548
Scott Violet1f106b582017-07-12 15:49:58549 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48550 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
551 chrome::CloseAllBrowsersAndQuit();
552 ASSERT_NO_FATAL_FAILURE(CancelClose());
553 cancel_observer.Wait();
554 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
555
556 // All tabs should still be open.
557 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
metaflow73fc9552016-05-23 18:26:48558
559 // Quit, this time accepting close confirmation dialog.
560 chrome::CloseAllBrowsersAndQuit();
561 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06562 ui_test_utils::WaitForBrowserToClose();
metaflow73fc9552016-05-23 18:26:48563 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
564 EXPECT_TRUE(BrowserList::GetInstance()->empty());
565}
566
567// Test that tabs in different windows with a slow beforeunload event response
568// are treated the same as the user accepting the close, but do not close the
569// tab early.
570// Regression for crbug.com/365052 caused CHECK in tabstrip.
jdoerrie6cc3bd82018-03-07 16:42:56571// Flaky on Mac and Linux: https://crbug.com/819541
572#if defined(OS_LINUX) || defined(OS_MACOSX)
573#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
574 DISABLED_TestBeforeUnloadMultipleSlowWindows
575#else
576#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
577 TestBeforeUnloadMultipleSlowWindows
578#endif
Avi Drissman78267f52019-01-16 21:21:42579IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
jdoerrie6cc3bd82018-03-07 16:42:56580 MAYBE_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48581 const int kBrowserCount = 5;
582 const int kResposiveBrowserIndex = 2;
583 // Create multiple browsers with all tabs except one responding after
584 // RenderViewHostImpl::kUnloadTimeoutMS .
585 // Minimum configuration is just one browser with slow tab and then
586 // browser with responsive tab.
587 // But we also want to check how slow tabs behave in tail and make test
588 // more robust.
589 for (int i = 0; i < kBrowserCount; i++) {
590 if (i)
591 browsers_.push_back(CreateBrowser(browser()->profile()));
592 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
593 browsers_[i],
594 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
595 ? "/beforeunload.html"
596 : "/beforeunload_slow.html")));
597 }
metaflow812bb8542016-05-24 21:10:46598 // Disable the hang monitor in the tab that is not expected to hang, so that
599 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40600 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48601
Scott Violet1f106b582017-07-12 15:49:58602 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48603 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
604 chrome::CloseAllBrowsersAndQuit();
605 ASSERT_NO_FATAL_FAILURE(CancelClose());
606 cancel_observer.Wait();
607 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
608
609 // All windows should still be open.
610 for (int i = 0; i < kBrowserCount; i++)
611 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
612
613 // Quit, this time accepting close confirmation dialog.
metaflow73fc9552016-05-23 18:26:48614 chrome::CloseAllBrowsersAndQuit();
615 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06616 WaitForAllBrowsersToClose();
metaflow73fc9552016-05-23 18:26:48617 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
618 EXPECT_TRUE(BrowserList::GetInstance()->empty());
619}
620
Jan Krcal9a23f222017-08-01 14:13:07621// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
622#if defined(OS_WIN) && !defined(NDEBUG)
623#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
624#else
625#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
626#endif
627
[email protected]2e9d79f2013-08-16 05:45:56628// Test that a window created during shutdown is closed.
Avi Drissman78267f52019-01-16 21:21:42629IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07630 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56631 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
632 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40633 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56634
[email protected]0c95faf42013-10-28 06:27:20635 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56636 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32637 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06638 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56639 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37640 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56641}
642
643// Test that a window created during shutdown with a beforeunload handler can
644// cancel the shutdown.
Avi Drissman78267f52019-01-16 21:21:42645IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01646 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56647 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
648 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40649 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56650
Scott Violet1f106b582017-07-12 15:49:58651 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56652 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20653 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56654 browsers_.push_back(CreateBrowser(browser()->profile()));
655 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
656 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40657 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32658 ASSERT_NO_FATAL_FAILURE(AcceptClose());
659 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56660 cancel_observer.Wait();
661 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
662 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
663 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
664
665 // Allow shutdown for both beforeunload dialogs.
[email protected]0c95faf42013-10-28 06:27:20666 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32667 ASSERT_NO_FATAL_FAILURE(AcceptClose());
668 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06669 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56670 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37671 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56672}
673
674// Test that tabs added during shutdown are closed.
Avi Drissman78267f52019-01-16 21:21:42675IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46676 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56677 browsers_.push_back(CreateBrowser(browser()->profile()));
678 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
679 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
680 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
681 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40682 PrepareForDialog(browsers_[0]);
683 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56684
[email protected]0c95faf42013-10-28 06:27:20685 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32686 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56687 AddBlankTabAndShow(browsers_[0]);
688 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32689 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06690 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56691 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37692 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56693}
694
695// Test that tabs created during shutdown with beforeunload handlers can cancel
696// the shutdown.
metaflow812bb8542016-05-24 21:10:46697
Avi Drissman78267f52019-01-16 21:21:42698IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46699 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56700 browsers_.push_back(CreateBrowser(browser()->profile()));
701 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
702 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
703 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
704 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40705 PrepareForDialog(browsers_[0]);
706 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46707
Scott Violet1f106b582017-07-12 15:49:58708 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56709 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20710 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32711 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56712 AddBlankTabAndShow(browsers_[0]);
713 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
714 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
715 AddBlankTabAndShow(browsers_[1]);
716 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
717 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40718 PrepareForDialog(browsers_[0]);
719 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32720 ASSERT_NO_FATAL_FAILURE(AcceptClose());
721 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56722 cancel_observer.Wait();
723 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
724 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
725 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
726
[email protected]0c95faf42013-10-28 06:27:20727 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32728 ASSERT_NO_FATAL_FAILURE(AcceptClose());
729 ASSERT_NO_FATAL_FAILURE(AcceptClose());
730 ASSERT_NO_FATAL_FAILURE(AcceptClose());
731 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56732
Evan Stade4487c452019-07-11 00:58:06733 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56734 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37735 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56736}
737
rogermf18929c2017-04-19 19:59:33738// TODO(crbug/713201):
739// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
740#if defined(OS_MACOSX)
741#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
742#else
743#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
744#endif
745
Avi Drissman78267f52019-01-16 21:21:42746IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33747 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40748 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
749 browser(), embedded_test_server()->GetURL("/title1.html")));
750
751 // Open second window.
752 ui_test_utils::NavigateToURLWithDisposition(
753 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
754 WindowOpenDisposition::NEW_WINDOW,
755 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
756 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
757 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
758 ? BrowserList::GetInstance()->get(0)
759 : BrowserList::GetInstance()->get(1);
760 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
761
762 // Let's work with second window only.
763 // This page has beforeunload handler already.
764 EXPECT_TRUE(browser2->tab_strip_model()
765 ->GetWebContentsAt(0)
766 ->NeedToFireBeforeUnload());
767 // This page doesn't have beforeunload handler. Yet.
768 ui_test_utils::NavigateToURLWithDisposition(
769 browser2, embedded_test_server()->GetURL("/title2.html"),
770 WindowOpenDisposition::NEW_FOREGROUND_TAB,
771 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
772 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
773 EXPECT_FALSE(browser2->tab_strip_model()
774 ->GetWebContentsAt(1)
775 ->NeedToFireBeforeUnload());
776 EXPECT_EQ(2, browser2->tab_strip_model()->count());
777
avi336125f72017-05-06 22:25:40778 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40779
780 // The test.
781
782 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
mkolom65a0b3c2017-03-02 06:11:40783 chrome::CloseWindow(browser2);
784 // Just to be sure CloseWindow doesn't have asynchronous tasks
785 // that could have an impact.
786 content::RunAllPendingInMessageLoop();
787
788 // Closing browser shouldn't happen because of beforeunload handler.
789 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
790 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
791 // yet.
792 ASSERT_TRUE(content::ExecuteScript(
David Benjamin5a792652018-06-08 02:15:42793 browser2->tab_strip_model()->GetWebContentsAt(1),
mkolom65a0b3c2017-03-02 06:11:40794 "window.addEventListener('beforeunload', "
795 "function(event) { event.returnValue = 'Foo'; });"));
796 EXPECT_TRUE(browser2->tab_strip_model()
797 ->GetWebContentsAt(1)
798 ->NeedToFireBeforeUnload());
799 // Accept closing the first tab.
800 ASSERT_NO_FATAL_FAILURE(AcceptClose());
801 // Just to be sure accepting a dialog doesn't have asynchronous tasks
802 // that could have an impact.
803 content::RunAllPendingInMessageLoop();
804 // It shouldn't close the whole window/browser.
805 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
806 EXPECT_EQ(2, browser2->tab_strip_model()->count());
807 // Accept closing the second tab.
808 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:06809 ui_test_utils::WaitForBrowserToClose();
mkolom65a0b3c2017-03-02 06:11:40810 // Now the second window/browser should be closed.
811 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
812 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
813 EXPECT_EQ(1u, restore_observer.changes_count());
814
815 // Restore the closed browser.
mkolom65a0b3c2017-03-02 06:11:40816 chrome::OpenWindowWithRestoredTabs(browser()->profile());
mkolom65a0b3c2017-03-02 06:11:40817 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
818 browser2 = BrowserList::GetInstance()->get(0) != browser()
819 ? BrowserList::GetInstance()->get(0)
820 : BrowserList::GetInstance()->get(1);
821
822 // Check the restored browser contents.
823 EXPECT_EQ(2, browser2->tab_strip_model()->count());
824 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
825 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
826 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
827 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
828}
829
Avi Drissman78267f52019-01-16 21:21:42830IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42831 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56832 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
833 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40834 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46835
Scott Violet1f106b582017-07-12 15:49:58836 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56837 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20838 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56839
840 browsers_.push_back(CreateBrowser(browser()->profile()));
841 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
842 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40843 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56844 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32845 ASSERT_NO_FATAL_FAILURE(CancelClose());
846 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56847 cancel_observer.Wait();
848 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
849 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
850 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
851
[email protected]0c95faf42013-10-28 06:27:20852 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56853 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32854 ASSERT_NO_FATAL_FAILURE(AcceptClose());
855 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56856
Evan Stade4487c452019-07-11 00:58:06857 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56858 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37859 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56860}
861
Avi Drissman78267f52019-01-16 21:21:42862IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46863 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56864 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
865 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40866 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46867
Scott Violet1f106b582017-07-12 15:49:58868 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56869 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20870 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56871
872 browsers_.push_back(CreateBrowser(browser()->profile()));
873 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
874 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40875 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56876 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32877 ASSERT_NO_FATAL_FAILURE(CancelClose());
878 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56879 cancel_observer.Wait();
880 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
881 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
882 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
883
[email protected]0c95faf42013-10-28 06:27:20884 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56885 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32886 ASSERT_NO_FATAL_FAILURE(AcceptClose());
887 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56888
Evan Stade4487c452019-07-11 00:58:06889 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56890 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37891 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56892}
893
Avi Drissman78267f52019-01-16 21:21:42894IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56895 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56896 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
897 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
898 browsers_.push_back(CreateBrowser(browser()->profile()));
899 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
900 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40901 PrepareForDialog(browsers_[0]);
902 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46903
Scott Violet1f106b582017-07-12 15:49:58904 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56905 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20906 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56907
908 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32909 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56910 cancel_observer.Wait();
911 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
912 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
913 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
914
[email protected]0c95faf42013-10-28 06:27:20915 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56916 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32917 ASSERT_NO_FATAL_FAILURE(AcceptClose());
918 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56919
Evan Stade4487c452019-07-11 00:58:06920 WaitForAllBrowsersToClose();
[email protected]2e9d79f2013-08-16 05:45:56921 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37922 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56923}
924
jackhouae8e6e52015-05-29 06:36:46925// Mac has its own in-progress download prompt in app_controller_mac.mm, so
926// BrowserCloseManager should simply close all browsers. If there are no
927// browsers, it should not crash.
928#if defined(OS_MACOSX)
Avi Drissman78267f52019-01-16 21:21:42929IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46930 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
931
jackhouae8e6e52015-05-29 06:36:46932 TestBrowserCloseManager::AttemptClose(
933 TestBrowserCloseManager::NO_USER_CHOICE);
Evan Stade4487c452019-07-11 00:58:06934 WaitForAllBrowsersToClose();
jackhouae8e6e52015-05-29 06:36:46935 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37936 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44937 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46938
939 // Attempting to close again should not crash.
940 TestBrowserCloseManager::AttemptClose(
941 TestBrowserCloseManager::NO_USER_CHOICE);
942}
943#else // defined(OS_MACOSX)
944
[email protected]422a7d12013-10-21 12:10:42945// Test shutdown with a DANGEROUS_URL download undecided.
Avi Drissman78267f52019-01-16 21:21:42946IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Mattias Nissler441cc652018-04-05 00:08:56947 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42948 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35949 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42950 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:44951 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:09952 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:42953
954 // Run a dangerous download, but the user doesn't make a decision.
955 // This .swf normally would be categorized as DANGEROUS_FILE, but
956 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:43957 GURL download_url(
958 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:42959 content::DownloadTestObserverInterrupted observer(
960 content::BrowserContext::GetDownloadManager(browser()->profile()),
961 1,
962 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
963 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19964 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:42965 ui_test_utils::BROWSER_TEST_NONE);
966 observer.WaitForFinished();
967
968 // Check that the download manager has the expected state.
969 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
970 browser()->profile())->InProgressCount());
971 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
972 browser()->profile())->NonMaliciousInProgressCount());
973
974 // Close the browser with no user action.
[email protected]422a7d12013-10-21 12:10:42975 TestBrowserCloseManager::AttemptClose(
976 TestBrowserCloseManager::NO_USER_CHOICE);
Evan Stade4487c452019-07-11 00:58:06977 WaitForAllBrowsersToClose();
[email protected]422a7d12013-10-21 12:10:42978 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37979 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:42980}
981
[email protected]edfca702013-08-16 08:58:14982// Test shutdown with a download in progress.
Avi Drissman78267f52019-01-16 21:21:42983IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:14984 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
985 content::TestNavigationObserver navigation_observer(
986 browser()->tab_strip_model()->GetActiveWebContents(), 1);
987 TestBrowserCloseManager::AttemptClose(
988 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
989 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
990 navigation_observer.Wait();
991 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
992 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
993
[email protected]edfca702013-08-16 08:58:14994 TestBrowserCloseManager::AttemptClose(
995 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:06996 WaitForAllBrowsersToClose();
[email protected]edfca702013-08-16 08:58:14997 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37998 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:38999 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441000 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381001 else
peterccb33e82017-05-02 19:00:441002 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141003}
1004
sammccd347712015-03-13 22:02:321005// Test shutdown with a download in progress in an off-the-record profile.
Avi Drissman78267f52019-01-16 21:21:421006IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321007 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321008 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321009 Browser* otr_browser = CreateBrowser(otr_profile);
1010 {
sammccd347712015-03-13 22:02:321011 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061012 ui_test_utils::WaitForBrowserToClose();
sammccd347712015-03-13 22:02:321013 }
1014 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1015 content::TestNavigationObserver navigation_observer(
1016 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1017 TestBrowserCloseManager::AttemptClose(
1018 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1019 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1020 navigation_observer.Wait();
1021 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1022 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1023
sammccd347712015-03-13 22:02:321024 TestBrowserCloseManager::AttemptClose(
1025 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:061026 ui_test_utils::WaitForBrowserToClose();
sammccd347712015-03-13 22:02:321027 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371028 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441029 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321030}
1031
Asanka Herathd1341dc2017-04-19 18:28:391032// Test shutdown with a download in progress in a regular profile an inconito
1033// browser is opened and closed. While there are active downloads, closing the
1034// incognito window shouldn't block on the active downloads which belong to the
1035// parent profile.
Weza78944d2018-05-17 16:56:291036// TODO(https://crbug.com/844019): Fix the notification expectation around the
1037// call to AttemptClose.
Avi Drissman78267f52019-01-16 21:21:421038IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291039 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391040 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391041 Browser* otr_browser = CreateBrowser(otr_profile);
1042 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1043
1044 content::TestNavigationObserver navigation_observer(
1045 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1046 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1047 navigation_observer.Wait();
1048
1049 int num_downloads_blocking = 0;
1050 ASSERT_EQ(
1051 Browser::DOWNLOAD_CLOSE_OK,
1052 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1053 ASSERT_EQ(0, num_downloads_blocking);
1054
1055 {
Asanka Herathd1341dc2017-04-19 18:28:391056 otr_browser->window()->Close();
Evan Stade4487c452019-07-11 00:58:061057 ui_test_utils::WaitForBrowserToClose();
Asanka Herathd1341dc2017-04-19 18:28:391058 }
1059
1060 ASSERT_EQ(
1061 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1062 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1063 ASSERT_EQ(1, num_downloads_blocking);
1064
1065 {
Asanka Herathd1341dc2017-04-19 18:28:391066 TestBrowserCloseManager::AttemptClose(
1067 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:061068 ui_test_utils::WaitForBrowserToClose();
Asanka Herathd1341dc2017-04-19 18:28:391069 }
1070
1071 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1072 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1073 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441074 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391075 else
peterccb33e82017-05-02 19:00:441076 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391077}
1078
[email protected]edfca702013-08-16 08:58:141079// Test shutdown with a download in progress from one profile, where the only
1080// open windows are for another profile.
Avi Drissman78267f52019-01-16 21:21:421081IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141082 TestWithDownloadsFromDifferentProfiles) {
1083 ProfileManager* profile_manager = g_browser_process->profile_manager();
Hui Yingstbfedafc2019-05-14 00:56:071084 std::unique_ptr<Profile> other_profile;
jam3f2d3932017-04-26 20:28:511085 {
1086 base::FilePath path =
1087 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131088 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511089 if (!base::PathExists(path))
1090 ASSERT_TRUE(base::CreateDirectory(path));
1091 other_profile =
1092 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1093 }
Hui Yingstbfedafc2019-05-14 00:56:071094 Profile* other_profile_ptr = other_profile.get();
Daniel Hosseinian8f0b7482019-06-07 18:21:041095 profile_manager->RegisterTestingProfile(std::move(other_profile), true,
1096 false);
Hui Yingstbfedafc2019-05-14 00:56:071097 Browser* other_profile_browser = CreateBrowser(other_profile_ptr);
[email protected]edfca702013-08-16 08:58:141098
[email protected]edfca702013-08-16 08:58:141099 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1100 {
[email protected]edfca702013-08-16 08:58:141101 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061102 ui_test_utils::WaitForBrowserToClose();
[email protected]edfca702013-08-16 08:58:141103 }
1104
1105 // When the shutdown is cancelled, the downloads page should be opened in a
1106 // browser for that profile. Because there are no browsers for that profile, a
1107 // new browser should be opened.
1108 ui_test_utils::BrowserAddedObserver new_browser_observer;
1109 TestBrowserCloseManager::AttemptClose(
1110 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1111 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1112 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1113 EXPECT_EQ(
1114 GURL(chrome::kChromeUIDownloadsURL),
1115 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1116 EXPECT_EQ(GURL("about:blank"),
1117 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1118 ->GetURL());
1119
[email protected]edfca702013-08-16 08:58:141120 TestBrowserCloseManager::AttemptClose(
1121 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
Evan Stade4487c452019-07-11 00:58:061122 ui_test_utils::WaitForBrowserToClose();
1123 ui_test_utils::WaitForBrowserToClose();
[email protected]edfca702013-08-16 08:58:141124 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371125 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381126 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441127 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381128 else
peterccb33e82017-05-02 19:00:441129 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141130}
1131
Vasilii Sukhanov4a65b862017-08-04 10:01:371132// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141133// Test shutdown with downloads in progress and beforeunload handlers.
Avi Drissman78267f52019-01-16 21:21:421134IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371135 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141136 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1137 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1138 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401139 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141140
1141 content::WindowedNotificationObserver cancel_observer(
1142 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1143 content::NotificationService::AllSources());
1144 TestBrowserCloseManager::AttemptClose(
1145 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321146 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141147 cancel_observer.Wait();
1148 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1149
[email protected]edfca702013-08-16 08:58:141150 TestBrowserCloseManager::AttemptClose(
1151 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321152 ASSERT_NO_FATAL_FAILURE(AcceptClose());
Evan Stade4487c452019-07-11 00:58:061153 ui_test_utils::WaitForBrowserToClose();
[email protected]edfca702013-08-16 08:58:141154 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371155 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141156}
1157
jackhouae8e6e52015-05-29 06:36:461158#endif // defined(OS_MACOSX)
1159
Denis Kuznetsov32099f32017-12-15 14:04:571160#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1161
[email protected]0c95faf42013-10-28 06:27:201162class BrowserCloseManagerWithBackgroundModeBrowserTest
1163 : public BrowserCloseManagerBrowserTest {
1164 public:
1165 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1166
Daniel Chenga542fca2014-10-21 09:51:291167 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201168 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1169 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351170 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201171 }
1172
1173 bool IsBackgroundModeSuspended() {
1174 return static_cast<FakeBackgroundModeManager*>(
1175 g_browser_process->background_mode_manager())
1176 ->IsBackgroundModeSuspended();
1177 }
1178
1179 private:
1180 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1181};
1182
1183// Check that background mode is suspended when closing all browsers unless we
1184// are quitting and that background mode is resumed when a new browser window is
1185// opened.
Avi Drissman78267f52019-01-16 21:21:421186IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
[email protected]0c95faf42013-10-28 06:27:201187 CloseAllBrowsersWithBackgroundMode) {
1188 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351189 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201190 Profile* profile = browser()->profile();
1191 {
dgn02377782016-03-12 00:58:381192 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1193 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201194 chrome::CloseAllBrowsers();
Evan Stade4487c452019-07-11 00:58:061195 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201196 }
1197 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371198 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201199 EXPECT_TRUE(IsBackgroundModeSuspended());
1200
1201 // Background mode should be resumed when a new browser window is opened.
1202 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441203 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201204 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381205 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201206 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201207
1208 // Background mode should not be suspended when quitting.
1209 chrome::CloseAllBrowsersAndQuit();
Evan Stade4487c452019-07-11 00:58:061210 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201211 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371212 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201213 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201214}
1215
1216// Check that closing the last browser window individually does not affect
1217// background mode.
Avi Drissman78267f52019-01-16 21:21:421218IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161219 DISABLED_CloseSingleBrowserWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201220 EXPECT_FALSE(IsBackgroundModeSuspended());
1221 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061222 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201223 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371224 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201225 EXPECT_FALSE(IsBackgroundModeSuspended());
1226}
1227
1228// Check that closing all browsers with no browser windows open suspends
1229// background mode but does not cause Chrome to quit.
Avi Drissman78267f52019-01-16 21:21:421230IN_PROC_BROWSER_TEST_F(
1231 BrowserCloseManagerWithBackgroundModeBrowserTest,
1232 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201233 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381234 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1235 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201236 browser()->window()->Close();
Evan Stade4487c452019-07-11 00:58:061237 ui_test_utils::WaitForBrowserToClose();
[email protected]0c95faf42013-10-28 06:27:201238 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371239 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201240 EXPECT_FALSE(IsBackgroundModeSuspended());
1241
1242 chrome::CloseAllBrowsers();
1243 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371244 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201245 EXPECT_TRUE(IsBackgroundModeSuspended());
1246}
1247
Denis Kuznetsov32099f32017-12-15 14:04:571248#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)