blob: 5532e35fb04e022af1ca7368b73caafe26b032a9 [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]edfca702013-08-16 08:58:1428#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5629#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1430#include "chrome/browser/profiles/profile.h"
31#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4032#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5633#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2034#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3735#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1436#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5637#include "chrome/browser/ui/tabs/tab_strip_model.h"
Scott Violet6200d332018-02-23 21:29:2338#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5639#include "chrome/common/chrome_switches.h"
40#include "chrome/common/url_constants.h"
41#include "chrome/test/base/in_process_browser_test.h"
42#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4243#include "components/app_modal/javascript_app_modal_dialog.h"
44#include "components/app_modal/native_app_modal_dialog.h"
Min Qina9f487872018-02-09 20:43:2345#include "components/download/public/common/download_item.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5946#include "components/keep_alive_registry/keep_alive_types.h"
47#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4048#include "components/sessions/core/tab_restore_service.h"
49#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1450#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2851#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:5952#include "content/public/browser/browser_thread.h"
[email protected]edfca702013-08-16 08:58:1453#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5654#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3455#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4656#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5657#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4058#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1459#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0960#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5661#include "content/public/test/test_navigation_observer.h"
62#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5663
[email protected]f9357a442014-05-15 18:44:0764#if defined(OS_CHROMEOS)
Steven Bennetts38e9bde22019-01-03 17:31:1065#include "chromeos/constants/chromeos_switches.h"
[email protected]f9357a442014-05-15 18:44:0766#endif
67
[email protected]2e9d79f2013-08-16 05:45:5668namespace {
69
oshima0929be2a2014-11-19 22:21:0370app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5271 app_modal::JavaScriptAppModalDialog* dialog =
72 ui_test_utils::WaitForAppModalDialog();
73 CHECK(dialog->native_dialog());
74 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3275}
[email protected]2e9d79f2013-08-16 05:45:5676
avi336125f72017-05-06 22:25:4077// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4678// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3279void AcceptClose() {
80 GetNextDialog()->AcceptAppModalDialog();
81}
[email protected]2e9d79f2013-08-16 05:45:5682
avi336125f72017-05-06 22:25:4083// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4684// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3285void CancelClose() {
86 GetNextDialog()->CancelAppModalDialog();
87}
[email protected]2e9d79f2013-08-16 05:45:5688
Scott Violet1f106b582017-07-12 15:49:5889class RepeatedNotificationObserver : public content::NotificationObserver {
90 public:
91 explicit RepeatedNotificationObserver(int type, int count)
92 : num_outstanding_(count), running_(false) {
93 registrar_.Add(this, type, content::NotificationService::AllSources());
94 }
95
96 void Observe(int type,
97 const content::NotificationSource& source,
98 const content::NotificationDetails& details) override {
99 ASSERT_GT(num_outstanding_, 0);
100 if (!--num_outstanding_ && running_) {
Eric Seckler8652dcd52018-09-20 10:42:28101 base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
102 run_loop_.QuitClosure());
Scott Violet1f106b582017-07-12 15:49:58103 }
104 }
105
106 void Wait() {
107 if (num_outstanding_ <= 0)
108 return;
109
110 running_ = true;
111 run_loop_.Run();
112 running_ = false;
Weza78944d2018-05-17 16:56:29113
114 EXPECT_LE(num_outstanding_, 0);
Scott Violet1f106b582017-07-12 15:49:58115 }
116
117 private:
118 int num_outstanding_;
119 content::NotificationRegistrar registrar_;
120 bool running_;
121 base::RunLoop run_loop_;
122
123 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
124};
125
mkolom65a0b3c2017-03-02 06:11:40126class TabRestoreServiceChangesObserver
127 : public sessions::TabRestoreServiceObserver {
128 public:
129 explicit TabRestoreServiceChangesObserver(Profile* profile)
130 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
131 if (service_)
132 service_->AddObserver(this);
133 }
134
135 ~TabRestoreServiceChangesObserver() override {
136 if (service_)
137 service_->RemoveObserver(this);
138 }
139
140 size_t changes_count() const { return changes_count_; }
141
142 private:
143 // sessions::TabRestoreServiceObserver:
144 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
145 changes_count_++;
146 }
147
148 // sessions::TabRestoreServiceObserver:
149 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
150 service_ = nullptr;
151 }
152
153 sessions::TabRestoreService* service_ = nullptr;
154 size_t changes_count_ = 0;
155
156 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
157};
158
[email protected]edfca702013-08-16 08:58:14159class TestBrowserCloseManager : public BrowserCloseManager {
160 public:
161 enum UserChoice {
162 USER_CHOICE_USER_CANCELS_CLOSE,
163 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42164 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14165 };
166
167 static void AttemptClose(UserChoice user_choice) {
168 scoped_refptr<BrowserCloseManager> browser_close_manager =
169 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20170 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14171 browser_close_manager->StartClosingBrowsers();
172 }
173
174 protected:
Daniel Chenga542fca2014-10-21 09:51:29175 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14176
Daniel Chenga542fca2014-10-21 09:51:29177 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14178 int download_count,
mostynb2b52d1db2014-10-07 02:47:17179 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42180 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14181 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42182 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14183 case USER_CHOICE_USER_CANCELS_CLOSE: {
184 callback.Run(false);
185 break;
186 }
187 case USER_CHOICE_USER_ALLOWS_CLOSE: {
188 callback.Run(true);
189 break;
190 }
191 }
192 }
193
194 private:
195 explicit TestBrowserCloseManager(UserChoice user_choice)
196 : user_choice_(user_choice) {}
197
198 UserChoice user_choice_;
199
200 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
201};
202
[email protected]422a7d12013-10-21 12:10:42203class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
204 public:
205 explicit TestDownloadManagerDelegate(Profile* profile)
206 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23207 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42208 }
Daniel Chenga542fca2014-10-21 09:51:29209 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42210
Daniel Chenga542fca2014-10-21 09:51:29211 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23212 download::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17213 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42214 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41215 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42216 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
217 item, dangerous_callback);
218 }
219
Asanka Herath1ba0e9f2017-04-03 18:48:53220 static void SetDangerous(const content::DownloadTargetCallback& callback,
221 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23222 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35223 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53224 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16225 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35226 callback.Run(target_path, disp,
227 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53228 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42229 }
[email protected]422a7d12013-10-21 12:10:42230};
231
Denis Kuznetsov32099f32017-12-15 14:04:57232#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20233class FakeBackgroundModeManager : public BackgroundModeManager {
234 public:
235 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54236 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
237 &g_browser_process->profile_manager()
238 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20239 suspended_(false) {}
240
Daniel Chenga542fca2014-10-21 09:51:29241 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20242 BackgroundModeManager::SuspendBackgroundMode();
243 suspended_ = true;
244 }
245
Daniel Chenga542fca2014-10-21 09:51:29246 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20247 BackgroundModeManager::ResumeBackgroundMode();
248 suspended_ = false;
249 }
250
251 bool IsBackgroundModeSuspended() {
252 return suspended_;
253 }
254
255 private:
256 bool suspended_;
257
258 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
259};
Denis Kuznetsov32099f32017-12-15 14:04:57260#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20261
[email protected]2e9d79f2013-08-16 05:45:56262} // namespace
263
Avi Drissman78267f52019-01-16 21:21:42264class BrowserCloseManagerBrowserTest : public InProcessBrowserTest {
[email protected]2e9d79f2013-08-16 05:45:56265 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());
Eric Seckler8652dcd52018-09-20 10:42:28270 base::PostTaskWithTraits(
271 FROM_HERE, {content::BrowserThread::IO},
tzik3f7781d2017-04-20 17:09:33272 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]f9357a442014-05-15 18:44:07279#if defined(OS_CHROMEOS)
280 command_line->AppendSwitch(
281 chromeos::switches::kIgnoreUserProfileMappingForTests);
282#endif
[email protected]2e9d79f2013-08-16 05:45:56283 }
284
[email protected]edfca702013-08-16 08:58:14285 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09286 ASSERT_TRUE(embedded_test_server()->Started());
287
288 GURL slow_download_url = embedded_test_server()->GetURL(
289 content::SlowDownloadHttpResponse::kKnownSizeUrl);
290
[email protected]edfca702013-08-16 08:58:14291 content::DownloadTestObserverInProgress observer(
292 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
293 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09294 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14295 ui_test_utils::BROWSER_TEST_NONE);
296 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23297 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
298 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14299 }
300
avi336125f72017-05-06 22:25:40301 void PrepareForDialog(content::WebContents* web_contents) {
302 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46303 }
304
avi336125f72017-05-06 22:25:40305 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46306 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40307 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46308 }
309
[email protected]2e9d79f2013-08-16 05:45:56310 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56311};
312
Avi Drissman78267f52019-01-16 21:21:42313IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56314 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
315 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40316 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46317
Scott Violet1f106b582017-07-12 15:49:58318 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56319 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20320 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32321 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56322 cancel_observer.Wait();
323 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
324 EXPECT_EQ(1, browser()->tab_strip_model()->count());
325
Scott Violet1f106b582017-07-12 15:49:58326 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56327 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20328 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32329 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56330 close_observer.Wait();
331 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37332 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56333}
334
Avi Drissman78267f52019-01-16 21:21:42335IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56336 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56337 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
338 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40339 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46340
Scott Violet1f106b582017-07-12 15:49:58341 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56342 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20343 chrome::CloseAllBrowsersAndQuit();
344 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32345 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56346 cancel_observer.Wait();
347 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
348 EXPECT_EQ(1, browser()->tab_strip_model()->count());
349
Scott Violet1f106b582017-07-12 15:49:58350 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56351 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20352 chrome::CloseAllBrowsersAndQuit();
353 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32354 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56355 close_observer.Wait();
356 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37357 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56358}
359
Avi Drissman78267f52019-01-16 21:21:42360IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56361 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
362 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
363 AddBlankTabAndShow(browser());
364 ASSERT_NO_FATAL_FAILURE(
365 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40366 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46367
Scott Violet1f106b582017-07-12 15:49:58368 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56369 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20370 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32371 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56372 cancel_observer.Wait();
373 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
374
375 browser()->tab_strip_model()
376 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
377 content::TestNavigationObserver navigation_observer(
378 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19379 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
380 browser(), GURL(chrome::kChromeUIVersionURL),
381 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32382 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56383 navigation_observer.Wait();
384
Scott Violet1f106b582017-07-12 15:49:58385 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56386 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20387 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56388 close_observer.Wait();
389 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37390 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56391}
392
393// Test that the tab closed after the aborted shutdown attempt is not re-opened
394// when restoring the session.
benwells70cc93222017-06-29 05:42:36395// Flaky on Windows trybots, see https://crbug.com/737860.
396#if defined(OS_WIN)
397#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
398#else
399#define MAYBE_TestSessionRestore TestSessionRestore
400#endif
Avi Drissman78267f52019-01-16 21:21:42401IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36402 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56403 // The testing framework launches Chrome with about:blank as args.
404 EXPECT_EQ(2, browser()->tab_strip_model()->count());
405 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
406 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
407 EXPECT_EQ(GURL("about:blank"),
408 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
409}
410
411// Test that browser windows are only closed if all browsers are ready to close
412// and that all beforeunload dialogs are shown again after a cancel.
Avi Drissman78267f52019-01-16 21:21:42413IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56414 browsers_.push_back(CreateBrowser(browser()->profile()));
415 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
416 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
417 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
418 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40419 PrepareForDialog(browsers_[0]);
420 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56421
422 // Cancel shutdown on the first beforeunload event.
423 {
Scott Violet1f106b582017-07-12 15:49:58424 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56425 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20426 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32427 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56428 cancel_observer.Wait();
429 }
430 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
431 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
432 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
433
434 // Cancel shutdown on the second beforeunload event.
435 {
Scott Violet1f106b582017-07-12 15:49:58436 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56437 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20438 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32439 ASSERT_NO_FATAL_FAILURE(AcceptClose());
440 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56441 cancel_observer.Wait();
442 }
443 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
444 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
445 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
446
447 // Allow shutdown for both beforeunload events.
Scott Violet1f106b582017-07-12 15:49:58448 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56449 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20450 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32451 ASSERT_NO_FATAL_FAILURE(AcceptClose());
452 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56453 close_observer.Wait();
454 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37455 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56456}
457
458// Test that tabs in the same window with a beforeunload event that hangs are
459// treated the same as the user accepting the close, but do not close the tab
460// early.
Avi Drissman78267f52019-01-16 21:21:42461IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46462 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56463 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
464 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
465 AddBlankTabAndShow(browsers_[0]);
466 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
467 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
468 AddBlankTabAndShow(browsers_[0]);
469 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
470 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46471 // Disable the hang monitor in the tab that is not expected to hang, so that
472 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40473 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56474
Scott Violet1f106b582017-07-12 15:49:58475 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56476 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20477 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32478 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56479 cancel_observer.Wait();
480 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
481 // All tabs should still be open.
482 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
483
Scott Violet1f106b582017-07-12 15:49:58484 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56485 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20486 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32487 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56488 close_observer.Wait();
489 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37490 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56491}
492
493// Test that tabs in different windows with a beforeunload event that hangs are
494// treated the same as the user accepting the close, but do not close the tab
495// early.
Avi Drissman78267f52019-01-16 21:21:42496IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56497 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56498 browsers_.push_back(CreateBrowser(browser()->profile()));
499 browsers_.push_back(CreateBrowser(browser()->profile()));
500 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
501 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
502 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
503 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
504 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
505 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46506 // Disable the hang monitor in the tab that is not expected to hang, so that
507 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40508 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56509
Scott Violet1f106b582017-07-12 15:49:58510 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56511 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20512 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32513 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56514 cancel_observer.Wait();
515 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
516 // All windows should still be open.
517 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
518 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
519 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
520
Scott Violet1f106b582017-07-12 15:49:58521 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56522 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20523 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32524 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56525 close_observer.Wait();
526 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37527 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56528}
529
metaflow73fc9552016-05-23 18:26:48530// Test that tabs that are slow to respond are not closed prematurely.
531// Regression for crbug.com/365052 caused some of tabs to be closed even if
532// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36533// Flaky on ChromeOS ASan. https://crbug.com/805457
534#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
535#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
536#else
537#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
538#endif
Avi Drissman78267f52019-01-16 21:21:42539IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36540 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48541 const int kTabCount = 5;
542 const int kResposiveTabIndex = 2;
543 // Create tab strip with all tabs except one responding after
544 // RenderViewHostImpl::kUnloadTimeoutMS.
545 // Minimum configuration is two slow tabs and then responsive tab.
546 // But we also want to check how slow tabs behave in tail.
547 for (int i = 0; i < kTabCount; i++) {
548 if (i)
549 AddBlankTabAndShow(browsers_[0]);
550 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
551 browsers_[0],
552 embedded_test_server()->GetURL((i == kResposiveTabIndex)
553 ? "/beforeunload.html"
554 : "/beforeunload_slow.html")));
555 }
metaflow812bb8542016-05-24 21:10:46556 // Disable the hang monitor in the tab that is not expected to hang, so that
557 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40558 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46559 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
560
Scott Violet1f106b582017-07-12 15:49:58561 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48562 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
563 chrome::CloseAllBrowsersAndQuit();
564 ASSERT_NO_FATAL_FAILURE(CancelClose());
565 cancel_observer.Wait();
566 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
567
568 // All tabs should still be open.
569 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58570 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48571 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
572
573 // Quit, this time accepting close confirmation dialog.
574 chrome::CloseAllBrowsersAndQuit();
575 ASSERT_NO_FATAL_FAILURE(AcceptClose());
576 close_observer.Wait();
577 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
578 EXPECT_TRUE(BrowserList::GetInstance()->empty());
579}
580
581// Test that tabs in different windows with a slow beforeunload event response
582// are treated the same as the user accepting the close, but do not close the
583// tab early.
584// Regression for crbug.com/365052 caused CHECK in tabstrip.
jdoerrie6cc3bd82018-03-07 16:42:56585// Flaky on Mac and Linux: https://crbug.com/819541
586#if defined(OS_LINUX) || defined(OS_MACOSX)
587#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
588 DISABLED_TestBeforeUnloadMultipleSlowWindows
589#else
590#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
591 TestBeforeUnloadMultipleSlowWindows
592#endif
Avi Drissman78267f52019-01-16 21:21:42593IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
jdoerrie6cc3bd82018-03-07 16:42:56594 MAYBE_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48595 const int kBrowserCount = 5;
596 const int kResposiveBrowserIndex = 2;
597 // Create multiple browsers with all tabs except one responding after
598 // RenderViewHostImpl::kUnloadTimeoutMS .
599 // Minimum configuration is just one browser with slow tab and then
600 // browser with responsive tab.
601 // But we also want to check how slow tabs behave in tail and make test
602 // more robust.
603 for (int i = 0; i < kBrowserCount; i++) {
604 if (i)
605 browsers_.push_back(CreateBrowser(browser()->profile()));
606 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
607 browsers_[i],
608 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
609 ? "/beforeunload.html"
610 : "/beforeunload_slow.html")));
611 }
metaflow812bb8542016-05-24 21:10:46612 // Disable the hang monitor in the tab that is not expected to hang, so that
613 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40614 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48615
Scott Violet1f106b582017-07-12 15:49:58616 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48617 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
618 chrome::CloseAllBrowsersAndQuit();
619 ASSERT_NO_FATAL_FAILURE(CancelClose());
620 cancel_observer.Wait();
621 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
622
623 // All windows should still be open.
624 for (int i = 0; i < kBrowserCount; i++)
625 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
626
627 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58628 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48629 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
630 chrome::CloseAllBrowsersAndQuit();
631 ASSERT_NO_FATAL_FAILURE(AcceptClose());
632 close_observer.Wait();
633 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
634 EXPECT_TRUE(BrowserList::GetInstance()->empty());
635}
636
Jan Krcal9a23f222017-08-01 14:13:07637// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
638#if defined(OS_WIN) && !defined(NDEBUG)
639#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
640#else
641#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
642#endif
643
[email protected]2e9d79f2013-08-16 05:45:56644// Test that a window created during shutdown is closed.
Avi Drissman78267f52019-01-16 21:21:42645IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07646 MAYBE_TestAddWindowDuringShutdown) {
[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 close_observer(
[email protected]2e9d79f2013-08-16 05:45:56652 chrome::NOTIFICATION_BROWSER_CLOSED, 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()));
oshima82f72482014-10-24 14:14:32655 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56656 close_observer.Wait();
657 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37658 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56659}
660
661// Test that a window created during shutdown with a beforeunload handler can
662// cancel the shutdown.
Avi Drissman78267f52019-01-16 21:21:42663IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01664 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56665 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
666 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40667 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56668
Scott Violet1f106b582017-07-12 15:49:58669 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56670 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20671 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56672 browsers_.push_back(CreateBrowser(browser()->profile()));
673 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
674 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40675 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32676 ASSERT_NO_FATAL_FAILURE(AcceptClose());
677 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56678 cancel_observer.Wait();
679 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
680 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
681 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
682
683 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58684 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56685 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20686 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32687 ASSERT_NO_FATAL_FAILURE(AcceptClose());
688 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56689 close_observer.Wait();
690 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37691 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56692}
693
694// Test that tabs added during shutdown are closed.
Avi Drissman78267f52019-01-16 21:21:42695IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46696 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56697 browsers_.push_back(CreateBrowser(browser()->profile()));
698 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
699 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
700 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
701 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40702 PrepareForDialog(browsers_[0]);
703 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56704
Scott Violet1f106b582017-07-12 15:49:58705 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56706 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20707 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32708 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56709 AddBlankTabAndShow(browsers_[0]);
710 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32711 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56712 close_observer.Wait();
713 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37714 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56715}
716
717// Test that tabs created during shutdown with beforeunload handlers can cancel
718// the shutdown.
metaflow812bb8542016-05-24 21:10:46719
Avi Drissman78267f52019-01-16 21:21:42720IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46721 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56722 browsers_.push_back(CreateBrowser(browser()->profile()));
723 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
724 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
725 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
726 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40727 PrepareForDialog(browsers_[0]);
728 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46729
Scott Violet1f106b582017-07-12 15:49:58730 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56731 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20732 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32733 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56734 AddBlankTabAndShow(browsers_[0]);
735 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
736 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
737 AddBlankTabAndShow(browsers_[1]);
738 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
739 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40740 PrepareForDialog(browsers_[0]);
741 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32742 ASSERT_NO_FATAL_FAILURE(AcceptClose());
743 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56744 cancel_observer.Wait();
745 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
746 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
747 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
748
Scott Violet1f106b582017-07-12 15:49:58749 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56750 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20751 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32752 ASSERT_NO_FATAL_FAILURE(AcceptClose());
753 ASSERT_NO_FATAL_FAILURE(AcceptClose());
754 ASSERT_NO_FATAL_FAILURE(AcceptClose());
755 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56756
757 close_observer.Wait();
758 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37759 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56760}
761
rogermf18929c2017-04-19 19:59:33762// TODO(crbug/713201):
763// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
764#if defined(OS_MACOSX)
765#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
766#else
767#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
768#endif
769
Avi Drissman78267f52019-01-16 21:21:42770IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33771 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40772 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
773 browser(), embedded_test_server()->GetURL("/title1.html")));
774
775 // Open second window.
776 ui_test_utils::NavigateToURLWithDisposition(
777 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
778 WindowOpenDisposition::NEW_WINDOW,
779 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
780 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
781 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
782 ? BrowserList::GetInstance()->get(0)
783 : BrowserList::GetInstance()->get(1);
784 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
785
786 // Let's work with second window only.
787 // This page has beforeunload handler already.
788 EXPECT_TRUE(browser2->tab_strip_model()
789 ->GetWebContentsAt(0)
790 ->NeedToFireBeforeUnload());
791 // This page doesn't have beforeunload handler. Yet.
792 ui_test_utils::NavigateToURLWithDisposition(
793 browser2, embedded_test_server()->GetURL("/title2.html"),
794 WindowOpenDisposition::NEW_FOREGROUND_TAB,
795 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
796 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
797 EXPECT_FALSE(browser2->tab_strip_model()
798 ->GetWebContentsAt(1)
799 ->NeedToFireBeforeUnload());
800 EXPECT_EQ(2, browser2->tab_strip_model()->count());
801
avi336125f72017-05-06 22:25:40802 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40803
804 // The test.
805
806 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
807 content::WindowedNotificationObserver observer(
808 chrome::NOTIFICATION_BROWSER_CLOSED,
809 content::NotificationService::AllSources());
810 chrome::CloseWindow(browser2);
811 // Just to be sure CloseWindow doesn't have asynchronous tasks
812 // that could have an impact.
813 content::RunAllPendingInMessageLoop();
814
815 // Closing browser shouldn't happen because of beforeunload handler.
816 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
817 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
818 // yet.
819 ASSERT_TRUE(content::ExecuteScript(
David Benjamin5a792652018-06-08 02:15:42820 browser2->tab_strip_model()->GetWebContentsAt(1),
mkolom65a0b3c2017-03-02 06:11:40821 "window.addEventListener('beforeunload', "
822 "function(event) { event.returnValue = 'Foo'; });"));
823 EXPECT_TRUE(browser2->tab_strip_model()
824 ->GetWebContentsAt(1)
825 ->NeedToFireBeforeUnload());
826 // Accept closing the first tab.
827 ASSERT_NO_FATAL_FAILURE(AcceptClose());
828 // Just to be sure accepting a dialog doesn't have asynchronous tasks
829 // that could have an impact.
830 content::RunAllPendingInMessageLoop();
831 // It shouldn't close the whole window/browser.
832 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
833 EXPECT_EQ(2, browser2->tab_strip_model()->count());
834 // Accept closing the second tab.
835 ASSERT_NO_FATAL_FAILURE(AcceptClose());
836 observer.Wait();
837 // Now the second window/browser should be closed.
838 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
839 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
840 EXPECT_EQ(1u, restore_observer.changes_count());
841
842 // Restore the closed browser.
843 content::WindowedNotificationObserver open_window_observer(
844 chrome::NOTIFICATION_BROWSER_OPENED,
845 content::NotificationService::AllSources());
846 chrome::OpenWindowWithRestoredTabs(browser()->profile());
847 open_window_observer.Wait();
848 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
849 browser2 = BrowserList::GetInstance()->get(0) != browser()
850 ? BrowserList::GetInstance()->get(0)
851 : BrowserList::GetInstance()->get(1);
852
853 // Check the restored browser contents.
854 EXPECT_EQ(2, browser2->tab_strip_model()->count());
855 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
856 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
857 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
858 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
859}
860
Avi Drissman78267f52019-01-16 21:21:42861IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42862 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56863 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
864 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40865 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46866
Scott Violet1f106b582017-07-12 15:49:58867 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56868 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20869 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56870
871 browsers_.push_back(CreateBrowser(browser()->profile()));
872 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
873 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40874 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56875 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32876 ASSERT_NO_FATAL_FAILURE(CancelClose());
877 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56878 cancel_observer.Wait();
879 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
880 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
881 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
882
Scott Violet1f106b582017-07-12 15:49:58883 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56884 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20885 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56886 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32887 ASSERT_NO_FATAL_FAILURE(AcceptClose());
888 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56889
890 close_observer.Wait();
891 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37892 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56893}
894
Avi Drissman78267f52019-01-16 21:21:42895IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46896 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56897 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
898 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40899 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46900
Scott Violet1f106b582017-07-12 15:49:58901 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56902 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20903 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56904
905 browsers_.push_back(CreateBrowser(browser()->profile()));
906 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
907 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40908 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56909 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32910 ASSERT_NO_FATAL_FAILURE(CancelClose());
911 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56912 cancel_observer.Wait();
913 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
914 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
915 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
916
Scott Violet1f106b582017-07-12 15:49:58917 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56918 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20919 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56920 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32921 ASSERT_NO_FATAL_FAILURE(AcceptClose());
922 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56923
924 close_observer.Wait();
925 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37926 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56927}
928
Avi Drissman78267f52019-01-16 21:21:42929IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56930 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56931 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
932 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
933 browsers_.push_back(CreateBrowser(browser()->profile()));
934 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
935 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40936 PrepareForDialog(browsers_[0]);
937 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46938
Scott Violet1f106b582017-07-12 15:49:58939 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56940 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20941 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56942
943 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32944 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56945 cancel_observer.Wait();
946 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
947 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
948 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
949
Scott Violet1f106b582017-07-12 15:49:58950 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56951 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20952 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56953 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32954 ASSERT_NO_FATAL_FAILURE(AcceptClose());
955 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56956
957 close_observer.Wait();
958 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37959 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56960}
961
jackhouae8e6e52015-05-29 06:36:46962// Mac has its own in-progress download prompt in app_controller_mac.mm, so
963// BrowserCloseManager should simply close all browsers. If there are no
964// browsers, it should not crash.
965#if defined(OS_MACOSX)
Avi Drissman78267f52019-01-16 21:21:42966IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46967 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
968
Scott Violet1f106b582017-07-12 15:49:58969 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46970 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
971
972 TestBrowserCloseManager::AttemptClose(
973 TestBrowserCloseManager::NO_USER_CHOICE);
974 close_observer.Wait();
975 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37976 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44977 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46978
979 // Attempting to close again should not crash.
980 TestBrowserCloseManager::AttemptClose(
981 TestBrowserCloseManager::NO_USER_CHOICE);
982}
983#else // defined(OS_MACOSX)
984
[email protected]422a7d12013-10-21 12:10:42985// Test shutdown with a DANGEROUS_URL download undecided.
Avi Drissman78267f52019-01-16 21:21:42986IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Mattias Nissler441cc652018-04-05 00:08:56987 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42988 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35989 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42990 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:44991 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:09992 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:42993
994 // Run a dangerous download, but the user doesn't make a decision.
995 // This .swf normally would be categorized as DANGEROUS_FILE, but
996 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:43997 GURL download_url(
998 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:42999 content::DownloadTestObserverInterrupted observer(
1000 content::BrowserContext::GetDownloadManager(browser()->profile()),
1001 1,
1002 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1003 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191004 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421005 ui_test_utils::BROWSER_TEST_NONE);
1006 observer.WaitForFinished();
1007
1008 // Check that the download manager has the expected state.
1009 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1010 browser()->profile())->InProgressCount());
1011 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1012 browser()->profile())->NonMaliciousInProgressCount());
1013
1014 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581015 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421016 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1017 TestBrowserCloseManager::AttemptClose(
1018 TestBrowserCloseManager::NO_USER_CHOICE);
1019 close_observer.Wait();
1020 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371021 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421022}
1023
[email protected]edfca702013-08-16 08:58:141024// Test shutdown with a download in progress.
Avi Drissman78267f52019-01-16 21:21:421025IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:141026 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1027 content::TestNavigationObserver navigation_observer(
1028 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1029 TestBrowserCloseManager::AttemptClose(
1030 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1031 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1032 navigation_observer.Wait();
1033 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1034 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1035
Scott Violet1f106b582017-07-12 15:49:581036 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141037 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1038
1039 TestBrowserCloseManager::AttemptClose(
1040 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1041 close_observer.Wait();
1042 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371043 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381044 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441045 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381046 else
peterccb33e82017-05-02 19:00:441047 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141048}
1049
sammccd347712015-03-13 22:02:321050// Test shutdown with a download in progress in an off-the-record profile.
Avi Drissman78267f52019-01-16 21:21:421051IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321052 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321053 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321054 Browser* otr_browser = CreateBrowser(otr_profile);
1055 {
Scott Violet1f106b582017-07-12 15:49:581056 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321057 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1058 browser()->window()->Close();
1059 close_observer.Wait();
1060 }
1061 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1062 content::TestNavigationObserver navigation_observer(
1063 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1064 TestBrowserCloseManager::AttemptClose(
1065 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1066 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1067 navigation_observer.Wait();
1068 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1069 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1070
Scott Violet1f106b582017-07-12 15:49:581071 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321072 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1073
1074 TestBrowserCloseManager::AttemptClose(
1075 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1076 close_observer.Wait();
1077 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371078 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441079 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321080}
1081
Asanka Herathd1341dc2017-04-19 18:28:391082// Test shutdown with a download in progress in a regular profile an inconito
1083// browser is opened and closed. While there are active downloads, closing the
1084// incognito window shouldn't block on the active downloads which belong to the
1085// parent profile.
Weza78944d2018-05-17 16:56:291086// TODO(https://crbug.com/844019): Fix the notification expectation around the
1087// call to AttemptClose.
Avi Drissman78267f52019-01-16 21:21:421088IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291089 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391090 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391091 Browser* otr_browser = CreateBrowser(otr_profile);
1092 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1093
1094 content::TestNavigationObserver navigation_observer(
1095 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1096 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1097 navigation_observer.Wait();
1098
1099 int num_downloads_blocking = 0;
1100 ASSERT_EQ(
1101 Browser::DOWNLOAD_CLOSE_OK,
1102 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1103 ASSERT_EQ(0, num_downloads_blocking);
1104
1105 {
Scott Violet1f106b582017-07-12 15:49:581106 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391107 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1108 otr_browser->window()->Close();
1109 close_observer.Wait();
1110 }
1111
1112 ASSERT_EQ(
1113 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1114 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1115 ASSERT_EQ(1, num_downloads_blocking);
1116
1117 {
Scott Violet1f106b582017-07-12 15:49:581118 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391119 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1120 TestBrowserCloseManager::AttemptClose(
1121 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1122 close_observer.Wait();
1123 }
1124
1125 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1126 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1127 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441128 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391129 else
peterccb33e82017-05-02 19:00:441130 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391131}
1132
[email protected]edfca702013-08-16 08:58:141133// Test shutdown with a download in progress from one profile, where the only
1134// open windows are for another profile.
Avi Drissman78267f52019-01-16 21:21:421135IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141136 TestWithDownloadsFromDifferentProfiles) {
1137 ProfileManager* profile_manager = g_browser_process->profile_manager();
Hui Yingstbfedafc2019-05-14 00:56:071138 std::unique_ptr<Profile> other_profile;
jam3f2d3932017-04-26 20:28:511139 {
1140 base::FilePath path =
1141 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131142 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511143 if (!base::PathExists(path))
1144 ASSERT_TRUE(base::CreateDirectory(path));
1145 other_profile =
1146 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1147 }
Hui Yingstbfedafc2019-05-14 00:56:071148 Profile* other_profile_ptr = other_profile.get();
1149 profile_manager->RegisterTestingProfile(other_profile.release(), true, false);
1150 Browser* other_profile_browser = CreateBrowser(other_profile_ptr);
[email protected]edfca702013-08-16 08:58:141151
[email protected]edfca702013-08-16 08:58:141152 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1153 {
Scott Violet1f106b582017-07-12 15:49:581154 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141155 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1156 browser()->window()->Close();
1157 close_observer.Wait();
1158 }
1159
1160 // When the shutdown is cancelled, the downloads page should be opened in a
1161 // browser for that profile. Because there are no browsers for that profile, a
1162 // new browser should be opened.
1163 ui_test_utils::BrowserAddedObserver new_browser_observer;
1164 TestBrowserCloseManager::AttemptClose(
1165 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1166 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1167 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1168 EXPECT_EQ(
1169 GURL(chrome::kChromeUIDownloadsURL),
1170 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1171 EXPECT_EQ(GURL("about:blank"),
1172 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1173 ->GetURL());
1174
Scott Violet1f106b582017-07-12 15:49:581175 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141176 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1177 TestBrowserCloseManager::AttemptClose(
1178 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1179 close_observer.Wait();
1180 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371181 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381182 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441183 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381184 else
peterccb33e82017-05-02 19:00:441185 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141186}
1187
Vasilii Sukhanov4a65b862017-08-04 10:01:371188// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141189// Test shutdown with downloads in progress and beforeunload handlers.
Avi Drissman78267f52019-01-16 21:21:421190IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371191 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141192 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1193 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1194 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401195 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141196
1197 content::WindowedNotificationObserver cancel_observer(
1198 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1199 content::NotificationService::AllSources());
1200 TestBrowserCloseManager::AttemptClose(
1201 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321202 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141203 cancel_observer.Wait();
1204 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1205
Scott Violet1f106b582017-07-12 15:49:581206 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141207 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1208 TestBrowserCloseManager::AttemptClose(
1209 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321210 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141211 close_observer.Wait();
1212 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371213 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141214}
1215
jackhouae8e6e52015-05-29 06:36:461216#endif // defined(OS_MACOSX)
1217
Denis Kuznetsov32099f32017-12-15 14:04:571218#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1219
[email protected]0c95faf42013-10-28 06:27:201220class BrowserCloseManagerWithBackgroundModeBrowserTest
1221 : public BrowserCloseManagerBrowserTest {
1222 public:
1223 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1224
Daniel Chenga542fca2014-10-21 09:51:291225 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201226 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1227 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351228 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201229 }
1230
1231 bool IsBackgroundModeSuspended() {
1232 return static_cast<FakeBackgroundModeManager*>(
1233 g_browser_process->background_mode_manager())
1234 ->IsBackgroundModeSuspended();
1235 }
1236
1237 private:
1238 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1239};
1240
1241// Check that background mode is suspended when closing all browsers unless we
1242// are quitting and that background mode is resumed when a new browser window is
1243// opened.
Avi Drissman78267f52019-01-16 21:21:421244IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
[email protected]0c95faf42013-10-28 06:27:201245 CloseAllBrowsersWithBackgroundMode) {
1246 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351247 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201248 Profile* profile = browser()->profile();
1249 {
Scott Violet1f106b582017-07-12 15:49:581250 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201251 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381252 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1253 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201254 chrome::CloseAllBrowsers();
1255 close_observer.Wait();
1256 }
1257 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371258 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201259 EXPECT_TRUE(IsBackgroundModeSuspended());
1260
1261 // Background mode should be resumed when a new browser window is opened.
1262 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441263 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201264 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381265 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201266 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581267 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201268 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1269
1270 // Background mode should not be suspended when quitting.
1271 chrome::CloseAllBrowsersAndQuit();
1272 close_observer.Wait();
1273 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371274 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201275 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201276}
1277
1278// Check that closing the last browser window individually does not affect
1279// background mode.
Avi Drissman78267f52019-01-16 21:21:421280IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161281 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581282 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201283 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1284 EXPECT_FALSE(IsBackgroundModeSuspended());
1285 browser()->window()->Close();
1286 close_observer.Wait();
1287 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371288 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201289 EXPECT_FALSE(IsBackgroundModeSuspended());
1290}
1291
1292// Check that closing all browsers with no browser windows open suspends
1293// background mode but does not cause Chrome to quit.
Avi Drissman78267f52019-01-16 21:21:421294IN_PROC_BROWSER_TEST_F(
1295 BrowserCloseManagerWithBackgroundModeBrowserTest,
1296 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581297 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201298 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1299 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381300 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1301 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201302 browser()->window()->Close();
1303 close_observer.Wait();
1304 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371305 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201306 EXPECT_FALSE(IsBackgroundModeSuspended());
1307
1308 chrome::CloseAllBrowsers();
1309 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371310 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201311 EXPECT_TRUE(IsBackgroundModeSuspended());
1312}
1313
Denis Kuznetsov32099f32017-12-15 14:04:571314#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)