blob: e3ee338ace631896c554c9a95b99d2ce01de49f1 [file] [log] [blame]
[email protected]2e9d79f2013-08-16 05:45:561// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dchenge73d8520c2015-12-27 01:19:095#include "chrome/browser/lifetime/browser_close_manager.h"
6
7#include <utility>
[email protected]2e9d79f2013-08-16 05:45:568#include <vector>
9
10#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:0511#include "base/files/scoped_temp_dir.h"
avi6846aef2015-12-26 01:09:3812#include "base/macros.h"
Gabriel Charette078e3662017-08-28 22:59:0413#include "base/run_loop.h"
jam3f2d3932017-04-26 20:28:5114#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3815#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2016#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1417#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5618#include "chrome/browser/browser_shutdown.h"
19#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3820#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4221#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4422#include "chrome/browser/download/download_core_service.h"
23#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0524#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5625#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]edfca702013-08-16 08:58:1426#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5627#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1428#include "chrome/browser/profiles/profile.h"
29#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4030#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5631#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2032#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3733#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1434#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5635#include "chrome/browser/ui/tabs/tab_strip_model.h"
36#include "chrome/common/chrome_switches.h"
Denis Kuznetsov32099f32017-12-15 14:04:5737#include "chrome/common/features.h"
[email protected]2e9d79f2013-08-16 05:45:5638#include "chrome/common/url_constants.h"
39#include "chrome/test/base/in_process_browser_test.h"
40#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4241#include "components/app_modal/javascript_app_modal_dialog.h"
42#include "components/app_modal/native_app_modal_dialog.h"
Min Qina9f487872018-02-09 20:43:2343#include "components/download/public/common/download_item.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5944#include "components/keep_alive_registry/keep_alive_types.h"
45#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4046#include "components/sessions/core/tab_restore_service.h"
47#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1448#include "content/public/browser/browser_context.h"
[email protected]edfca702013-08-16 08:58:1449#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5650#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3451#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4652#include "content/public/browser/render_view_host.h"
53#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5654#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4055#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1456#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0957#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5658#include "content/public/test/test_navigation_observer.h"
59#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5660
[email protected]f9357a442014-05-15 18:44:0761#if defined(OS_CHROMEOS)
62#include "chromeos/chromeos_switches.h"
63#endif
64
[email protected]2e9d79f2013-08-16 05:45:5665namespace {
66
oshima0929be2a2014-11-19 22:21:0367app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5268 app_modal::JavaScriptAppModalDialog* dialog =
69 ui_test_utils::WaitForAppModalDialog();
70 CHECK(dialog->native_dialog());
71 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3272}
[email protected]2e9d79f2013-08-16 05:45:5673
avi336125f72017-05-06 22:25:4074// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4675// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3276void AcceptClose() {
77 GetNextDialog()->AcceptAppModalDialog();
78}
[email protected]2e9d79f2013-08-16 05:45:5679
avi336125f72017-05-06 22:25:4080// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4681// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3282void CancelClose() {
83 GetNextDialog()->CancelAppModalDialog();
84}
[email protected]2e9d79f2013-08-16 05:45:5685
Scott Violet1f106b582017-07-12 15:49:5886class RepeatedNotificationObserver : public content::NotificationObserver {
87 public:
88 explicit RepeatedNotificationObserver(int type, int count)
89 : num_outstanding_(count), running_(false) {
90 registrar_.Add(this, type, content::NotificationService::AllSources());
91 }
92
93 void Observe(int type,
94 const content::NotificationSource& source,
95 const content::NotificationDetails& details) override {
96 ASSERT_GT(num_outstanding_, 0);
97 if (!--num_outstanding_ && running_) {
98 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
99 run_loop_.QuitClosure());
100 }
101 }
102
103 void Wait() {
104 if (num_outstanding_ <= 0)
105 return;
106
107 running_ = true;
108 run_loop_.Run();
109 running_ = false;
110 }
111
112 private:
113 int num_outstanding_;
114 content::NotificationRegistrar registrar_;
115 bool running_;
116 base::RunLoop run_loop_;
117
118 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
119};
120
mkolom65a0b3c2017-03-02 06:11:40121class TabRestoreServiceChangesObserver
122 : public sessions::TabRestoreServiceObserver {
123 public:
124 explicit TabRestoreServiceChangesObserver(Profile* profile)
125 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
126 if (service_)
127 service_->AddObserver(this);
128 }
129
130 ~TabRestoreServiceChangesObserver() override {
131 if (service_)
132 service_->RemoveObserver(this);
133 }
134
135 size_t changes_count() const { return changes_count_; }
136
137 private:
138 // sessions::TabRestoreServiceObserver:
139 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
140 changes_count_++;
141 }
142
143 // sessions::TabRestoreServiceObserver:
144 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
145 service_ = nullptr;
146 }
147
148 sessions::TabRestoreService* service_ = nullptr;
149 size_t changes_count_ = 0;
150
151 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
152};
153
[email protected]edfca702013-08-16 08:58:14154class TestBrowserCloseManager : public BrowserCloseManager {
155 public:
156 enum UserChoice {
157 USER_CHOICE_USER_CANCELS_CLOSE,
158 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42159 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14160 };
161
162 static void AttemptClose(UserChoice user_choice) {
163 scoped_refptr<BrowserCloseManager> browser_close_manager =
164 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20165 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14166 browser_close_manager->StartClosingBrowsers();
167 }
168
169 protected:
Daniel Chenga542fca2014-10-21 09:51:29170 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14171
Daniel Chenga542fca2014-10-21 09:51:29172 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14173 int download_count,
mostynb2b52d1db2014-10-07 02:47:17174 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42175 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14176 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42177 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14178 case USER_CHOICE_USER_CANCELS_CLOSE: {
179 callback.Run(false);
180 break;
181 }
182 case USER_CHOICE_USER_ALLOWS_CLOSE: {
183 callback.Run(true);
184 break;
185 }
186 }
187 }
188
189 private:
190 explicit TestBrowserCloseManager(UserChoice user_choice)
191 : user_choice_(user_choice) {}
192
193 UserChoice user_choice_;
194
195 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
196};
197
[email protected]422a7d12013-10-21 12:10:42198class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
199 public:
200 explicit TestDownloadManagerDelegate(Profile* profile)
201 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23202 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42203 }
Daniel Chenga542fca2014-10-21 09:51:29204 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42205
Daniel Chenga542fca2014-10-21 09:51:29206 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23207 download::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17208 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42209 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41210 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42211 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
212 item, dangerous_callback);
213 }
214
Asanka Herath1ba0e9f2017-04-03 18:48:53215 static void SetDangerous(const content::DownloadTargetCallback& callback,
216 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23217 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35218 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53219 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16220 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35221 callback.Run(target_path, disp,
222 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53223 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42224 }
[email protected]422a7d12013-10-21 12:10:42225};
226
Denis Kuznetsov32099f32017-12-15 14:04:57227#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20228class FakeBackgroundModeManager : public BackgroundModeManager {
229 public:
230 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54231 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
232 &g_browser_process->profile_manager()
233 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20234 suspended_(false) {}
235
Daniel Chenga542fca2014-10-21 09:51:29236 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20237 BackgroundModeManager::SuspendBackgroundMode();
238 suspended_ = true;
239 }
240
Daniel Chenga542fca2014-10-21 09:51:29241 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20242 BackgroundModeManager::ResumeBackgroundMode();
243 suspended_ = false;
244 }
245
246 bool IsBackgroundModeSuspended() {
247 return suspended_;
248 }
249
250 private:
251 bool suspended_;
252
253 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
254};
Denis Kuznetsov32099f32017-12-15 14:04:57255#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20256
[email protected]2e9d79f2013-08-16 05:45:56257} // namespace
258
259class BrowserCloseManagerBrowserTest
260 : public InProcessBrowserTest,
261 public testing::WithParamInterface<bool> {
262 protected:
Daniel Chenga542fca2014-10-21 09:51:29263 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56264 SessionStartupPref::SetStartupPref(
265 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
266 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14267 content::BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33268 content::BrowserThread::IO, FROM_HERE,
269 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
Colin Blundellfb2cfb2b2018-01-18 18:50:09270 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
271 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
272 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56273 }
274
avi556c05022014-12-22 23:31:43275 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56276 if (GetParam())
277 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07278#if defined(OS_CHROMEOS)
279 command_line->AppendSwitch(
280 chromeos::switches::kIgnoreUserProfileMappingForTests);
281#endif
[email protected]2e9d79f2013-08-16 05:45:56282 }
283
[email protected]edfca702013-08-16 08:58:14284 void CreateStalledDownload(Browser* browser) {
Colin Blundellfb2cfb2b2018-01-18 18:50:09285 ASSERT_TRUE(embedded_test_server()->Started());
286
287 GURL slow_download_url = embedded_test_server()->GetURL(
288 content::SlowDownloadHttpResponse::kKnownSizeUrl);
289
[email protected]edfca702013-08-16 08:58:14290 content::DownloadTestObserverInProgress observer(
291 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
292 ui_test_utils::NavigateToURLWithDisposition(
Colin Blundellfb2cfb2b2018-01-18 18:50:09293 browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14294 ui_test_utils::BROWSER_TEST_NONE);
295 observer.WaitForFinished();
Min Qina9f487872018-02-09 20:43:23296 EXPECT_EQ(1UL, observer.NumDownloadsSeenInState(
297 download::DownloadItem::IN_PROGRESS));
[email protected]edfca702013-08-16 08:58:14298 }
299
avi336125f72017-05-06 22:25:40300 void PrepareForDialog(content::WebContents* web_contents) {
301 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46302 }
303
avi336125f72017-05-06 22:25:40304 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46305 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40306 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46307 }
308
[email protected]2e9d79f2013-08-16 05:45:56309 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56310};
311
312IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56313 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
314 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40315 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46316
Scott Violet1f106b582017-07-12 15:49:58317 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56318 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20319 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32320 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56321 cancel_observer.Wait();
322 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
323 EXPECT_EQ(1, browser()->tab_strip_model()->count());
324
Scott Violet1f106b582017-07-12 15:49:58325 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56326 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20327 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32328 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56329 close_observer.Wait();
330 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
334IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
335 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
Scott Violet1f106b582017-07-12 15:49:58349 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56350 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20351 chrome::CloseAllBrowsersAndQuit();
352 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32353 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56354 close_observer.Wait();
355 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37356 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56357}
358
wafflesb63234052015-08-07 02:44:02359IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46360 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
wafflesb63234052015-08-07 02:44:02401IN_PROC_BROWSER_TEST_P(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.
[email protected]40cd13a2013-10-24 13:42:01413IN_PROC_BROWSER_TEST_P(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.
461IN_PROC_BROWSER_TEST_P(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.
496IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
497 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
metaflow73fc9552016-05-23 18:26:48539IN_PROC_BROWSER_TEST_P(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.
metaflow73fc9552016-05-23 18:26:48585IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46586 TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48587 const int kBrowserCount = 5;
588 const int kResposiveBrowserIndex = 2;
589 // Create multiple browsers with all tabs except one responding after
590 // RenderViewHostImpl::kUnloadTimeoutMS .
591 // Minimum configuration is just one browser with slow tab and then
592 // browser with responsive tab.
593 // But we also want to check how slow tabs behave in tail and make test
594 // more robust.
595 for (int i = 0; i < kBrowserCount; i++) {
596 if (i)
597 browsers_.push_back(CreateBrowser(browser()->profile()));
598 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
599 browsers_[i],
600 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
601 ? "/beforeunload.html"
602 : "/beforeunload_slow.html")));
603 }
metaflow812bb8542016-05-24 21:10:46604 // Disable the hang monitor in the tab that is not expected to hang, so that
605 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40606 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48607
Scott Violet1f106b582017-07-12 15:49:58608 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48609 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
610 chrome::CloseAllBrowsersAndQuit();
611 ASSERT_NO_FATAL_FAILURE(CancelClose());
612 cancel_observer.Wait();
613 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
614
615 // All windows should still be open.
616 for (int i = 0; i < kBrowserCount; i++)
617 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
618
619 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58620 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48621 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
622 chrome::CloseAllBrowsersAndQuit();
623 ASSERT_NO_FATAL_FAILURE(AcceptClose());
624 close_observer.Wait();
625 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
626 EXPECT_TRUE(BrowserList::GetInstance()->empty());
627}
628
Jan Krcal9a23f222017-08-01 14:13:07629// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
630#if defined(OS_WIN) && !defined(NDEBUG)
631#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
632#else
633#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
634#endif
635
[email protected]2e9d79f2013-08-16 05:45:56636// Test that a window created during shutdown is closed.
637IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07638 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56639 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
640 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40641 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56642
Scott Violet1f106b582017-07-12 15:49:58643 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56644 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20645 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56646 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32647 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56648 close_observer.Wait();
649 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37650 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56651}
652
653// Test that a window created during shutdown with a beforeunload handler can
654// cancel the shutdown.
655IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01656 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56657 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
658 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40659 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56660
Scott Violet1f106b582017-07-12 15:49:58661 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56662 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20663 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56664 browsers_.push_back(CreateBrowser(browser()->profile()));
665 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
666 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40667 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32668 ASSERT_NO_FATAL_FAILURE(AcceptClose());
669 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56670 cancel_observer.Wait();
671 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
672 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
673 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
674
675 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58676 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56677 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20678 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32679 ASSERT_NO_FATAL_FAILURE(AcceptClose());
680 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56681 close_observer.Wait();
682 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37683 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56684}
685
686// Test that tabs added during shutdown are closed.
687IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46688 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56689 browsers_.push_back(CreateBrowser(browser()->profile()));
690 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
691 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
692 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
693 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40694 PrepareForDialog(browsers_[0]);
695 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56696
Scott Violet1f106b582017-07-12 15:49:58697 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56698 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20699 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32700 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56701 AddBlankTabAndShow(browsers_[0]);
702 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32703 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56704 close_observer.Wait();
705 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37706 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56707}
708
709// Test that tabs created during shutdown with beforeunload handlers can cancel
710// the shutdown.
metaflow812bb8542016-05-24 21:10:46711
[email protected]2e9d79f2013-08-16 05:45:56712IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46713 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56714 browsers_.push_back(CreateBrowser(browser()->profile()));
715 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
716 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
717 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
718 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40719 PrepareForDialog(browsers_[0]);
720 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46721
Scott Violet1f106b582017-07-12 15:49:58722 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56723 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20724 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32725 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56726 AddBlankTabAndShow(browsers_[0]);
727 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
728 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
729 AddBlankTabAndShow(browsers_[1]);
730 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
731 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40732 PrepareForDialog(browsers_[0]);
733 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32734 ASSERT_NO_FATAL_FAILURE(AcceptClose());
735 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56736 cancel_observer.Wait();
737 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
738 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
739 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
740
Scott Violet1f106b582017-07-12 15:49:58741 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56742 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20743 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32744 ASSERT_NO_FATAL_FAILURE(AcceptClose());
745 ASSERT_NO_FATAL_FAILURE(AcceptClose());
746 ASSERT_NO_FATAL_FAILURE(AcceptClose());
747 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56748
749 close_observer.Wait();
750 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37751 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56752}
753
rogermf18929c2017-04-19 19:59:33754// TODO(crbug/713201):
755// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
756#if defined(OS_MACOSX)
757#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
758#else
759#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
760#endif
761
[email protected]2e9d79f2013-08-16 05:45:56762IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33763 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40764 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
765 // And it is difficult to fix this issue without fixing that one.
766 if (GetParam())
767 return;
768
mkolom65a0b3c2017-03-02 06:11:40769 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
770 browser(), embedded_test_server()->GetURL("/title1.html")));
771
772 // Open second window.
773 ui_test_utils::NavigateToURLWithDisposition(
774 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
775 WindowOpenDisposition::NEW_WINDOW,
776 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
777 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
778 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
779 ? BrowserList::GetInstance()->get(0)
780 : BrowserList::GetInstance()->get(1);
781 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
782
783 // Let's work with second window only.
784 // This page has beforeunload handler already.
785 EXPECT_TRUE(browser2->tab_strip_model()
786 ->GetWebContentsAt(0)
787 ->NeedToFireBeforeUnload());
788 // This page doesn't have beforeunload handler. Yet.
789 ui_test_utils::NavigateToURLWithDisposition(
790 browser2, embedded_test_server()->GetURL("/title2.html"),
791 WindowOpenDisposition::NEW_FOREGROUND_TAB,
792 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
793 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
794 EXPECT_FALSE(browser2->tab_strip_model()
795 ->GetWebContentsAt(1)
796 ->NeedToFireBeforeUnload());
797 EXPECT_EQ(2, browser2->tab_strip_model()->count());
798
avi336125f72017-05-06 22:25:40799 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40800
801 // The test.
802
803 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
804 content::WindowedNotificationObserver observer(
805 chrome::NOTIFICATION_BROWSER_CLOSED,
806 content::NotificationService::AllSources());
807 chrome::CloseWindow(browser2);
808 // Just to be sure CloseWindow doesn't have asynchronous tasks
809 // that could have an impact.
810 content::RunAllPendingInMessageLoop();
811
812 // Closing browser shouldn't happen because of beforeunload handler.
813 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
814 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
815 // yet.
816 ASSERT_TRUE(content::ExecuteScript(
817 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(),
818 "window.addEventListener('beforeunload', "
819 "function(event) { event.returnValue = 'Foo'; });"));
820 EXPECT_TRUE(browser2->tab_strip_model()
821 ->GetWebContentsAt(1)
822 ->NeedToFireBeforeUnload());
823 // Accept closing the first tab.
824 ASSERT_NO_FATAL_FAILURE(AcceptClose());
825 // Just to be sure accepting a dialog doesn't have asynchronous tasks
826 // that could have an impact.
827 content::RunAllPendingInMessageLoop();
828 // It shouldn't close the whole window/browser.
829 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
830 EXPECT_EQ(2, browser2->tab_strip_model()->count());
831 // Accept closing the second tab.
832 ASSERT_NO_FATAL_FAILURE(AcceptClose());
833 observer.Wait();
834 // Now the second window/browser should be closed.
835 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
836 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
837 EXPECT_EQ(1u, restore_observer.changes_count());
838
839 // Restore the closed browser.
840 content::WindowedNotificationObserver open_window_observer(
841 chrome::NOTIFICATION_BROWSER_OPENED,
842 content::NotificationService::AllSources());
843 chrome::OpenWindowWithRestoredTabs(browser()->profile());
844 open_window_observer.Wait();
845 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
846 browser2 = BrowserList::GetInstance()->get(0) != browser()
847 ? BrowserList::GetInstance()->get(0)
848 : BrowserList::GetInstance()->get(1);
849
850 // Check the restored browser contents.
851 EXPECT_EQ(2, browser2->tab_strip_model()->count());
852 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
853 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
854 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
855 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
856}
857
858IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42859 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56860 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
861 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40862 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46863
Scott Violet1f106b582017-07-12 15:49:58864 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56865 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20866 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56867
868 browsers_.push_back(CreateBrowser(browser()->profile()));
869 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
870 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40871 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56872 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32873 ASSERT_NO_FATAL_FAILURE(CancelClose());
874 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56875 cancel_observer.Wait();
876 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
877 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
878 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
879
Scott Violet1f106b582017-07-12 15:49:58880 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56881 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20882 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56883 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32884 ASSERT_NO_FATAL_FAILURE(AcceptClose());
885 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56886
887 close_observer.Wait();
888 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37889 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56890}
891
892IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46893 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56894 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
895 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40896 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46897
Scott Violet1f106b582017-07-12 15:49:58898 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56899 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20900 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56901
902 browsers_.push_back(CreateBrowser(browser()->profile()));
903 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
904 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40905 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56906 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32907 ASSERT_NO_FATAL_FAILURE(CancelClose());
908 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56909 cancel_observer.Wait();
910 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
911 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
912 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
913
Scott Violet1f106b582017-07-12 15:49:58914 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56915 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20916 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56917 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32918 ASSERT_NO_FATAL_FAILURE(AcceptClose());
919 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56920
921 close_observer.Wait();
922 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37923 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56924}
925
926IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
927 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56928 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
929 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
930 browsers_.push_back(CreateBrowser(browser()->profile()));
931 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
932 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40933 PrepareForDialog(browsers_[0]);
934 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46935
Scott Violet1f106b582017-07-12 15:49:58936 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56937 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20938 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56939
940 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32941 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56942 cancel_observer.Wait();
943 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
944 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
945 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
946
Scott Violet1f106b582017-07-12 15:49:58947 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56948 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20949 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56950 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32951 ASSERT_NO_FATAL_FAILURE(AcceptClose());
952 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56953
954 close_observer.Wait();
955 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37956 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56957}
958
[email protected]0ed53872014-05-10 05:51:05959INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
960 BrowserCloseManagerBrowserTest,
961 testing::Bool());
962
963class BrowserCloseManagerWithDownloadsBrowserTest :
964 public BrowserCloseManagerBrowserTest {
965 public:
966 BrowserCloseManagerWithDownloadsBrowserTest() {}
967 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
968
Daniel Chenga542fca2014-10-21 09:51:29969 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05970 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
971 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
972 }
973
974 void SetDownloadPathForProfile(Profile* profile) {
975 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
976 download_prefs->SetDownloadPath(download_path());
977 }
978
979 const base::FilePath& download_path() const {
vabr0c237ae2016-09-14 09:24:28980 return scoped_download_directory_.GetPath();
[email protected]0ed53872014-05-10 05:51:05981 }
982
983 private:
984 base::ScopedTempDir scoped_download_directory_;
985};
986
jackhouae8e6e52015-05-29 06:36:46987// Mac has its own in-progress download prompt in app_controller_mac.mm, so
988// BrowserCloseManager should simply close all browsers. If there are no
989// browsers, it should not crash.
990#if defined(OS_MACOSX)
991IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
992 TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46993 SetDownloadPathForProfile(browser()->profile());
994 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
995
Scott Violet1f106b582017-07-12 15:49:58996 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46997 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
998
999 TestBrowserCloseManager::AttemptClose(
1000 TestBrowserCloseManager::NO_USER_CHOICE);
1001 close_observer.Wait();
1002 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371003 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441004 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:461005
1006 // Attempting to close again should not crash.
1007 TestBrowserCloseManager::AttemptClose(
1008 TestBrowserCloseManager::NO_USER_CHOICE);
1009}
1010#else // defined(OS_MACOSX)
1011
[email protected]422a7d12013-10-21 12:10:421012// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:051013IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:421014 TestWithDangerousUrlDownload) {
[email protected]0ed53872014-05-10 05:51:051015 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:421016
1017 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:351018 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:421019 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:441020 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091021 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421022
1023 // Run a dangerous download, but the user doesn't make a decision.
1024 // This .swf normally would be categorized as DANGEROUS_FILE, but
1025 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:431026 GURL download_url(
1027 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421028 content::DownloadTestObserverInterrupted observer(
1029 content::BrowserContext::GetDownloadManager(browser()->profile()),
1030 1,
1031 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1032 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191033 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421034 ui_test_utils::BROWSER_TEST_NONE);
1035 observer.WaitForFinished();
1036
1037 // Check that the download manager has the expected state.
1038 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1039 browser()->profile())->InProgressCount());
1040 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1041 browser()->profile())->NonMaliciousInProgressCount());
1042
1043 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581044 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421045 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1046 TestBrowserCloseManager::AttemptClose(
1047 TestBrowserCloseManager::NO_USER_CHOICE);
1048 close_observer.Wait();
1049 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371050 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421051}
1052
[email protected]edfca702013-08-16 08:58:141053// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:051054IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1055 TestWithDownloads) {
[email protected]0ed53872014-05-10 05:51:051056 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141057 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1058 content::TestNavigationObserver navigation_observer(
1059 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1060 TestBrowserCloseManager::AttemptClose(
1061 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1062 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1063 navigation_observer.Wait();
1064 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1065 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1066
Scott Violet1f106b582017-07-12 15:49:581067 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141068 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1069
1070 TestBrowserCloseManager::AttemptClose(
1071 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1072 close_observer.Wait();
1073 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371074 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381075 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441076 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381077 else
peterccb33e82017-05-02 19:00:441078 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141079}
1080
sammccd347712015-03-13 22:02:321081// Test shutdown with a download in progress in an off-the-record profile.
1082IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1083 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321084 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1085 SetDownloadPathForProfile(otr_profile);
1086 Browser* otr_browser = CreateBrowser(otr_profile);
1087 {
Scott Violet1f106b582017-07-12 15:49:581088 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321089 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1090 browser()->window()->Close();
1091 close_observer.Wait();
1092 }
1093 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1094 content::TestNavigationObserver navigation_observer(
1095 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1096 TestBrowserCloseManager::AttemptClose(
1097 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1098 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1099 navigation_observer.Wait();
1100 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1101 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1102
Scott Violet1f106b582017-07-12 15:49:581103 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321104 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1105
1106 TestBrowserCloseManager::AttemptClose(
1107 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1108 close_observer.Wait();
1109 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371110 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441111 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321112}
1113
Asanka Herathd1341dc2017-04-19 18:28:391114// Test shutdown with a download in progress in a regular profile an inconito
1115// browser is opened and closed. While there are active downloads, closing the
1116// incognito window shouldn't block on the active downloads which belong to the
1117// parent profile.
1118IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1119 TestWithOffTheRecordWindowAndRegularDownload) {
1120 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1121 SetDownloadPathForProfile(otr_profile);
1122 Browser* otr_browser = CreateBrowser(otr_profile);
1123 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1124
1125 content::TestNavigationObserver navigation_observer(
1126 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1127 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1128 navigation_observer.Wait();
1129
1130 int num_downloads_blocking = 0;
1131 ASSERT_EQ(
1132 Browser::DOWNLOAD_CLOSE_OK,
1133 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1134 ASSERT_EQ(0, num_downloads_blocking);
1135
1136 {
Scott Violet1f106b582017-07-12 15:49:581137 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391138 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1139 otr_browser->window()->Close();
1140 close_observer.Wait();
1141 }
1142
1143 ASSERT_EQ(
1144 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1145 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1146 ASSERT_EQ(1, num_downloads_blocking);
1147
1148 {
Scott Violet1f106b582017-07-12 15:49:581149 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391150 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1151 TestBrowserCloseManager::AttemptClose(
1152 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1153 close_observer.Wait();
1154 }
1155
1156 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1157 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1158 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441159 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391160 else
peterccb33e82017-05-02 19:00:441161 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391162}
1163
[email protected]edfca702013-08-16 08:58:141164// Test shutdown with a download in progress from one profile, where the only
1165// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:051166IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:141167 TestWithDownloadsFromDifferentProfiles) {
1168 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511169 Profile* other_profile = nullptr;
1170 {
1171 base::FilePath path =
1172 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131173 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511174 if (!base::PathExists(path))
1175 ASSERT_TRUE(base::CreateDirectory(path));
1176 other_profile =
1177 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1178 }
[email protected]edfca702013-08-16 08:58:141179 profile_manager->RegisterTestingProfile(other_profile, true, false);
1180 Browser* other_profile_browser = CreateBrowser(other_profile);
1181
[email protected]0ed53872014-05-10 05:51:051182 SetDownloadPathForProfile(browser()->profile());
1183 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:141184 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1185 {
Scott Violet1f106b582017-07-12 15:49:581186 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141187 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1188 browser()->window()->Close();
1189 close_observer.Wait();
1190 }
1191
1192 // When the shutdown is cancelled, the downloads page should be opened in a
1193 // browser for that profile. Because there are no browsers for that profile, a
1194 // new browser should be opened.
1195 ui_test_utils::BrowserAddedObserver new_browser_observer;
1196 TestBrowserCloseManager::AttemptClose(
1197 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1198 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1199 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1200 EXPECT_EQ(
1201 GURL(chrome::kChromeUIDownloadsURL),
1202 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1203 EXPECT_EQ(GURL("about:blank"),
1204 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1205 ->GetURL());
1206
Scott Violet1f106b582017-07-12 15:49:581207 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141208 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1209 TestBrowserCloseManager::AttemptClose(
1210 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1211 close_observer.Wait();
1212 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371213 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381214 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441215 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381216 else
peterccb33e82017-05-02 19:00:441217 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141218}
1219
Vasilii Sukhanov4a65b862017-08-04 10:01:371220// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141221// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]0ed53872014-05-10 05:51:051222IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371223 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]0ed53872014-05-10 05:51:051224 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141225 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1226 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1227 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401228 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141229
1230 content::WindowedNotificationObserver cancel_observer(
1231 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1232 content::NotificationService::AllSources());
1233 TestBrowserCloseManager::AttemptClose(
1234 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321235 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141236 cancel_observer.Wait();
1237 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1238
Scott Violet1f106b582017-07-12 15:49:581239 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141240 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1241 TestBrowserCloseManager::AttemptClose(
1242 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321243 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141244 close_observer.Wait();
1245 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371246 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141247}
1248
jackhouae8e6e52015-05-29 06:36:461249#endif // defined(OS_MACOSX)
1250
[email protected]0ed53872014-05-10 05:51:051251INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
1252 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561253 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201254
Denis Kuznetsov32099f32017-12-15 14:04:571255#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1256
[email protected]0c95faf42013-10-28 06:27:201257class BrowserCloseManagerWithBackgroundModeBrowserTest
1258 : public BrowserCloseManagerBrowserTest {
1259 public:
1260 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1261
Daniel Chenga542fca2014-10-21 09:51:291262 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201263 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1264 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351265 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201266 }
1267
1268 bool IsBackgroundModeSuspended() {
1269 return static_cast<FakeBackgroundModeManager*>(
1270 g_browser_process->background_mode_manager())
1271 ->IsBackgroundModeSuspended();
1272 }
1273
1274 private:
1275 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1276};
1277
1278// Check that background mode is suspended when closing all browsers unless we
1279// are quitting and that background mode is resumed when a new browser window is
1280// opened.
1281IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1282 CloseAllBrowsersWithBackgroundMode) {
1283 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351284 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201285 Profile* profile = browser()->profile();
1286 {
Scott Violet1f106b582017-07-12 15:49:581287 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201288 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381289 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1290 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201291 chrome::CloseAllBrowsers();
1292 close_observer.Wait();
1293 }
1294 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371295 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201296 EXPECT_TRUE(IsBackgroundModeSuspended());
1297
1298 // Background mode should be resumed when a new browser window is opened.
1299 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441300 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201301 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381302 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201303 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581304 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201305 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1306
1307 // Background mode should not be suspended when quitting.
1308 chrome::CloseAllBrowsersAndQuit();
1309 close_observer.Wait();
1310 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371311 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201312 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201313}
1314
1315// Check that closing the last browser window individually does not affect
1316// background mode.
1317IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161318 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581319 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201320 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1321 EXPECT_FALSE(IsBackgroundModeSuspended());
1322 browser()->window()->Close();
1323 close_observer.Wait();
1324 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371325 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201326 EXPECT_FALSE(IsBackgroundModeSuspended());
1327}
1328
1329// Check that closing all browsers with no browser windows open suspends
1330// background mode but does not cause Chrome to quit.
1331IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031332 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581333 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201334 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1335 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381336 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1337 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201338 browser()->window()->Close();
1339 close_observer.Wait();
1340 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371341 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201342 EXPECT_FALSE(IsBackgroundModeSuspended());
1343
1344 chrome::CloseAllBrowsers();
1345 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371346 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201347 EXPECT_TRUE(IsBackgroundModeSuspended());
1348}
1349
1350INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1351 BrowserCloseManagerWithBackgroundModeBrowserTest,
1352 testing::Bool());
Denis Kuznetsov32099f32017-12-15 14:04:571353#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)