blob: 8fd2a4c667997ab5694c38aac7b53376a045f349 [file] [log] [blame]
[email protected]2e9d79f2013-08-16 05:45:561// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dchenge73d8520c2015-12-27 01:19:095#include "chrome/browser/lifetime/browser_close_manager.h"
6
7#include <utility>
[email protected]2e9d79f2013-08-16 05:45:568#include <vector>
9
10#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:0511#include "base/files/scoped_temp_dir.h"
avi6846aef2015-12-26 01:09:3812#include "base/macros.h"
Gabriel Charette078e3662017-08-28 22:59:0413#include "base/run_loop.h"
Eric Seckler8652dcd52018-09-20 10:42:2814#include "base/task/post_task.h"
jam3f2d3932017-04-26 20:28:5115#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3816#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2017#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1418#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5619#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3820#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4221#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4422#include "chrome/browser/download/download_core_service.h"
23#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0524#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5625#include "chrome/browser/lifetime/application_lifetime.h"
Avi Drissmand30927342018-05-22 15:04:2726#include "chrome/browser/lifetime/browser_shutdown.h"
[email protected]edfca702013-08-16 08:58:1427#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5628#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1429#include "chrome/browser/profiles/profile.h"
30#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4031#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5632#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2033#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3734#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1435#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5636#include "chrome/browser/ui/tabs/tab_strip_model.h"
Scott Violet6200d332018-02-23 21:29:2337#include "chrome/common/buildflags.h"
[email protected]2e9d79f2013-08-16 05:45:5638#include "chrome/common/chrome_switches.h"
39#include "chrome/common/url_constants.h"
40#include "chrome/test/base/in_process_browser_test.h"
41#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4242#include "components/app_modal/javascript_app_modal_dialog.h"
43#include "components/app_modal/native_app_modal_dialog.h"
Min Qina9f487872018-02-09 20:43:2344#include "components/download/public/common/download_item.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5945#include "components/keep_alive_registry/keep_alive_types.h"
46#include "components/keep_alive_registry/scoped_keep_alive.h"
mkolom65a0b3c2017-03-02 06:11:4047#include "components/sessions/core/tab_restore_service.h"
48#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1449#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2850#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:5951#include "content/public/browser/browser_thread.h"
[email protected]edfca702013-08-16 08:58:1452#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5653#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3454#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4655#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5656#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4057#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1458#include "content/public/test/download_test_observer.h"
Colin Blundellfb2cfb2b2018-01-18 18:50:0959#include "content/public/test/slow_download_http_response.h"
[email protected]2e9d79f2013-08-16 05:45:5660#include "content/public/test/test_navigation_observer.h"
61#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]2e9d79f2013-08-16 05:45:5662
[email protected]f9357a442014-05-15 18:44:0763#if defined(OS_CHROMEOS)
Steven Bennetts38e9bde22019-01-03 17:31:1064#include "chromeos/constants/chromeos_switches.h"
[email protected]f9357a442014-05-15 18:44:0765#endif
66
[email protected]2e9d79f2013-08-16 05:45:5667namespace {
68
oshima0929be2a2014-11-19 22:21:0369app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5270 app_modal::JavaScriptAppModalDialog* dialog =
71 ui_test_utils::WaitForAppModalDialog();
72 CHECK(dialog->native_dialog());
73 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3274}
[email protected]2e9d79f2013-08-16 05:45:5675
avi336125f72017-05-06 22:25:4076// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4677// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3278void AcceptClose() {
79 GetNextDialog()->AcceptAppModalDialog();
80}
[email protected]2e9d79f2013-08-16 05:45:5681
avi336125f72017-05-06 22:25:4082// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4683// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3284void CancelClose() {
85 GetNextDialog()->CancelAppModalDialog();
86}
[email protected]2e9d79f2013-08-16 05:45:5687
Scott Violet1f106b582017-07-12 15:49:5888class RepeatedNotificationObserver : public content::NotificationObserver {
89 public:
90 explicit RepeatedNotificationObserver(int type, int count)
91 : num_outstanding_(count), running_(false) {
92 registrar_.Add(this, type, content::NotificationService::AllSources());
93 }
94
95 void Observe(int type,
96 const content::NotificationSource& source,
97 const content::NotificationDetails& details) override {
98 ASSERT_GT(num_outstanding_, 0);
99 if (!--num_outstanding_ && running_) {
Eric Seckler8652dcd52018-09-20 10:42:28100 base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
101 run_loop_.QuitClosure());
Scott Violet1f106b582017-07-12 15:49:58102 }
103 }
104
105 void Wait() {
106 if (num_outstanding_ <= 0)
107 return;
108
109 running_ = true;
110 run_loop_.Run();
111 running_ = false;
Weza78944d2018-05-17 16:56:29112
113 EXPECT_LE(num_outstanding_, 0);
Scott Violet1f106b582017-07-12 15:49:58114 }
115
116 private:
117 int num_outstanding_;
118 content::NotificationRegistrar registrar_;
119 bool running_;
120 base::RunLoop run_loop_;
121
122 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
123};
124
mkolom65a0b3c2017-03-02 06:11:40125class TabRestoreServiceChangesObserver
126 : public sessions::TabRestoreServiceObserver {
127 public:
128 explicit TabRestoreServiceChangesObserver(Profile* profile)
129 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
130 if (service_)
131 service_->AddObserver(this);
132 }
133
134 ~TabRestoreServiceChangesObserver() override {
135 if (service_)
136 service_->RemoveObserver(this);
137 }
138
139 size_t changes_count() const { return changes_count_; }
140
141 private:
142 // sessions::TabRestoreServiceObserver:
143 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
144 changes_count_++;
145 }
146
147 // sessions::TabRestoreServiceObserver:
148 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
149 service_ = nullptr;
150 }
151
152 sessions::TabRestoreService* service_ = nullptr;
153 size_t changes_count_ = 0;
154
155 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
156};
157
[email protected]edfca702013-08-16 08:58:14158class TestBrowserCloseManager : public BrowserCloseManager {
159 public:
160 enum UserChoice {
161 USER_CHOICE_USER_CANCELS_CLOSE,
162 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42163 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14164 };
165
166 static void AttemptClose(UserChoice user_choice) {
167 scoped_refptr<BrowserCloseManager> browser_close_manager =
168 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20169 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14170 browser_close_manager->StartClosingBrowsers();
171 }
172
173 protected:
Daniel Chenga542fca2014-10-21 09:51:29174 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14175
Daniel Chenga542fca2014-10-21 09:51:29176 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14177 int download_count,
mostynb2b52d1db2014-10-07 02:47:17178 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42179 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14180 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42181 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14182 case USER_CHOICE_USER_CANCELS_CLOSE: {
183 callback.Run(false);
184 break;
185 }
186 case USER_CHOICE_USER_ALLOWS_CLOSE: {
187 callback.Run(true);
188 break;
189 }
190 }
191 }
192
193 private:
194 explicit TestBrowserCloseManager(UserChoice user_choice)
195 : user_choice_(user_choice) {}
196
197 UserChoice user_choice_;
198
199 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
200};
201
[email protected]422a7d12013-10-21 12:10:42202class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
203 public:
204 explicit TestDownloadManagerDelegate(Profile* profile)
205 : ChromeDownloadManagerDelegate(profile) {
Min Qina9f487872018-02-09 20:43:23206 GetDownloadIdReceiverCallback().Run(download::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42207 }
Daniel Chenga542fca2014-10-21 09:51:29208 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42209
Daniel Chenga542fca2014-10-21 09:51:29210 bool DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:23211 download::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17212 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42213 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41214 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42215 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
216 item, dangerous_callback);
217 }
218
Asanka Herath1ba0e9f2017-04-03 18:48:53219 static void SetDangerous(const content::DownloadTargetCallback& callback,
220 const base::FilePath& target_path,
Min Qina9f487872018-02-09 20:43:23221 download::DownloadItem::TargetDisposition disp,
Min Qin0ca8e1ee2018-01-31 00:49:35222 download::DownloadDangerType danger_type,
Asanka Herath1ba0e9f2017-04-03 18:48:53223 const base::FilePath& intermediate_path,
Min Qineb78b7a2018-02-03 00:43:16224 download::DownloadInterruptReason reason) {
Min Qin0ca8e1ee2018-01-31 00:49:35225 callback.Run(target_path, disp,
226 download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
Asanka Herath1ba0e9f2017-04-03 18:48:53227 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42228 }
[email protected]422a7d12013-10-21 12:10:42229};
230
Denis Kuznetsov32099f32017-12-15 14:04:57231#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20232class FakeBackgroundModeManager : public BackgroundModeManager {
233 public:
234 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54235 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
236 &g_browser_process->profile_manager()
237 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20238 suspended_(false) {}
239
Daniel Chenga542fca2014-10-21 09:51:29240 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20241 BackgroundModeManager::SuspendBackgroundMode();
242 suspended_ = true;
243 }
244
Daniel Chenga542fca2014-10-21 09:51:29245 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20246 BackgroundModeManager::ResumeBackgroundMode();
247 suspended_ = false;
248 }
249
250 bool IsBackgroundModeSuspended() {
251 return suspended_;
252 }
253
254 private:
255 bool suspended_;
256
257 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
258};
Denis Kuznetsov32099f32017-12-15 14:04:57259#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
[email protected]0c95faf42013-10-28 06:27:20260
[email protected]2e9d79f2013-08-16 05:45:56261} // namespace
262
Avi Drissman78267f52019-01-16 21:21:42263class BrowserCloseManagerBrowserTest : public InProcessBrowserTest {
[email protected]2e9d79f2013-08-16 05:45:56264 protected:
Daniel Chenga542fca2014-10-21 09:51:29265 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56266 SessionStartupPref::SetStartupPref(
267 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
268 browsers_.push_back(browser());
Eric Seckler8652dcd52018-09-20 10:42:28269 base::PostTaskWithTraits(
270 FROM_HERE, {content::BrowserThread::IO},
tzik3f7781d2017-04-20 17:09:33271 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
Colin Blundellfb2cfb2b2018-01-18 18:50:09272 embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
273 &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
274 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56275 }
276
avi556c05022014-12-22 23:31:43277 void SetUpCommandLine(base::CommandLine* command_line) override {
[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
Avi Drissman78267f52019-01-16 21:21:42312IN_PROC_BROWSER_TEST_F(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
Avi Drissman78267f52019-01-16 21:21:42334IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56335 TestShutdownMoreThanOnce) {
[email protected]2e9d79f2013-08-16 05:45:56336 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
337 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40338 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46339
Scott Violet1f106b582017-07-12 15:49:58340 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56341 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20342 chrome::CloseAllBrowsersAndQuit();
343 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32344 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56345 cancel_observer.Wait();
346 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
347 EXPECT_EQ(1, browser()->tab_strip_model()->count());
348
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
Avi Drissman78267f52019-01-16 21:21:42359IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56360 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
361 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
362 AddBlankTabAndShow(browser());
363 ASSERT_NO_FATAL_FAILURE(
364 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40365 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46366
Scott Violet1f106b582017-07-12 15:49:58367 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56368 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20369 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32370 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56371 cancel_observer.Wait();
372 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
373
374 browser()->tab_strip_model()
375 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
376 content::TestNavigationObserver navigation_observer(
377 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19378 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
379 browser(), GURL(chrome::kChromeUIVersionURL),
380 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32381 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56382 navigation_observer.Wait();
383
Scott Violet1f106b582017-07-12 15:49:58384 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56385 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20386 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56387 close_observer.Wait();
388 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37389 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56390}
391
392// Test that the tab closed after the aborted shutdown attempt is not re-opened
393// when restoring the session.
benwells70cc93222017-06-29 05:42:36394// Flaky on Windows trybots, see https://crbug.com/737860.
395#if defined(OS_WIN)
396#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
397#else
398#define MAYBE_TestSessionRestore TestSessionRestore
399#endif
Avi Drissman78267f52019-01-16 21:21:42400IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36401 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56402 // The testing framework launches Chrome with about:blank as args.
403 EXPECT_EQ(2, browser()->tab_strip_model()->count());
404 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
405 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
406 EXPECT_EQ(GURL("about:blank"),
407 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
408}
409
410// Test that browser windows are only closed if all browsers are ready to close
411// and that all beforeunload dialogs are shown again after a cancel.
Avi Drissman78267f52019-01-16 21:21:42412IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56413 browsers_.push_back(CreateBrowser(browser()->profile()));
414 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
415 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
416 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
417 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40418 PrepareForDialog(browsers_[0]);
419 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56420
421 // Cancel shutdown on the first beforeunload event.
422 {
Scott Violet1f106b582017-07-12 15:49:58423 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56424 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20425 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32426 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56427 cancel_observer.Wait();
428 }
429 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
430 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
431 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
432
433 // Cancel shutdown on the second beforeunload event.
434 {
Scott Violet1f106b582017-07-12 15:49:58435 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56436 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20437 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32438 ASSERT_NO_FATAL_FAILURE(AcceptClose());
439 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56440 cancel_observer.Wait();
441 }
442 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
443 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
444 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
445
446 // Allow shutdown for both beforeunload events.
Scott Violet1f106b582017-07-12 15:49:58447 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56448 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20449 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32450 ASSERT_NO_FATAL_FAILURE(AcceptClose());
451 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56452 close_observer.Wait();
453 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37454 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56455}
456
457// Test that tabs in the same window with a beforeunload event that hangs are
458// treated the same as the user accepting the close, but do not close the tab
459// early.
Avi Drissman78267f52019-01-16 21:21:42460IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46461 TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56462 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
463 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
464 AddBlankTabAndShow(browsers_[0]);
465 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
466 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
467 AddBlankTabAndShow(browsers_[0]);
468 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
469 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46470 // Disable the hang monitor in the tab that is not expected to hang, so that
471 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40472 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56473
Scott Violet1f106b582017-07-12 15:49:58474 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56475 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20476 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32477 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56478 cancel_observer.Wait();
479 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
480 // All tabs should still be open.
481 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
482
Scott Violet1f106b582017-07-12 15:49:58483 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56484 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20485 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32486 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56487 close_observer.Wait();
488 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37489 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56490}
491
492// Test that tabs in different windows with a beforeunload event that hangs are
493// treated the same as the user accepting the close, but do not close the tab
494// early.
Avi Drissman78267f52019-01-16 21:21:42495IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56496 TestHangInBeforeUnloadMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56497 browsers_.push_back(CreateBrowser(browser()->profile()));
498 browsers_.push_back(CreateBrowser(browser()->profile()));
499 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
500 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
501 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
502 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
503 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
504 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46505 // Disable the hang monitor in the tab that is not expected to hang, so that
506 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40507 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56508
Scott Violet1f106b582017-07-12 15:49:58509 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56510 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20511 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32512 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56513 cancel_observer.Wait();
514 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
515 // All windows should still be open.
516 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
517 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
518 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
519
Scott Violet1f106b582017-07-12 15:49:58520 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56521 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20522 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32523 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56524 close_observer.Wait();
525 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37526 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56527}
528
metaflow73fc9552016-05-23 18:26:48529// Test that tabs that are slow to respond are not closed prematurely.
530// Regression for crbug.com/365052 caused some of tabs to be closed even if
531// user chose to cancel browser close.
Kevin McNee98ba00f2018-02-09 19:19:36532// Flaky on ChromeOS ASan. https://crbug.com/805457
533#if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)
534#define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs
535#else
536#define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs
537#endif
Avi Drissman78267f52019-01-16 21:21:42538IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Kevin McNee98ba00f2018-02-09 19:19:36539 MAYBE_TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48540 const int kTabCount = 5;
541 const int kResposiveTabIndex = 2;
542 // Create tab strip with all tabs except one responding after
543 // RenderViewHostImpl::kUnloadTimeoutMS.
544 // Minimum configuration is two slow tabs and then responsive tab.
545 // But we also want to check how slow tabs behave in tail.
546 for (int i = 0; i < kTabCount; i++) {
547 if (i)
548 AddBlankTabAndShow(browsers_[0]);
549 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
550 browsers_[0],
551 embedded_test_server()->GetURL((i == kResposiveTabIndex)
552 ? "/beforeunload.html"
553 : "/beforeunload_slow.html")));
554 }
metaflow812bb8542016-05-24 21:10:46555 // Disable the hang monitor in the tab that is not expected to hang, so that
556 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40557 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46558 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
559
Scott Violet1f106b582017-07-12 15:49:58560 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48561 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
562 chrome::CloseAllBrowsersAndQuit();
563 ASSERT_NO_FATAL_FAILURE(CancelClose());
564 cancel_observer.Wait();
565 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
566
567 // All tabs should still be open.
568 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58569 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48570 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
571
572 // Quit, this time accepting close confirmation dialog.
573 chrome::CloseAllBrowsersAndQuit();
574 ASSERT_NO_FATAL_FAILURE(AcceptClose());
575 close_observer.Wait();
576 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
577 EXPECT_TRUE(BrowserList::GetInstance()->empty());
578}
579
580// Test that tabs in different windows with a slow beforeunload event response
581// are treated the same as the user accepting the close, but do not close the
582// tab early.
583// Regression for crbug.com/365052 caused CHECK in tabstrip.
jdoerrie6cc3bd82018-03-07 16:42:56584// Flaky on Mac and Linux: https://crbug.com/819541
585#if defined(OS_LINUX) || defined(OS_MACOSX)
586#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
587 DISABLED_TestBeforeUnloadMultipleSlowWindows
588#else
589#define MAYBE_TestBeforeUnloadMultipleSlowWindows \
590 TestBeforeUnloadMultipleSlowWindows
591#endif
Avi Drissman78267f52019-01-16 21:21:42592IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
jdoerrie6cc3bd82018-03-07 16:42:56593 MAYBE_TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48594 const int kBrowserCount = 5;
595 const int kResposiveBrowserIndex = 2;
596 // Create multiple browsers with all tabs except one responding after
597 // RenderViewHostImpl::kUnloadTimeoutMS .
598 // Minimum configuration is just one browser with slow tab and then
599 // browser with responsive tab.
600 // But we also want to check how slow tabs behave in tail and make test
601 // more robust.
602 for (int i = 0; i < kBrowserCount; i++) {
603 if (i)
604 browsers_.push_back(CreateBrowser(browser()->profile()));
605 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
606 browsers_[i],
607 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
608 ? "/beforeunload.html"
609 : "/beforeunload_slow.html")));
610 }
metaflow812bb8542016-05-24 21:10:46611 // Disable the hang monitor in the tab that is not expected to hang, so that
612 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40613 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48614
Scott Violet1f106b582017-07-12 15:49:58615 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48616 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
617 chrome::CloseAllBrowsersAndQuit();
618 ASSERT_NO_FATAL_FAILURE(CancelClose());
619 cancel_observer.Wait();
620 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
621
622 // All windows should still be open.
623 for (int i = 0; i < kBrowserCount; i++)
624 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
625
626 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58627 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48628 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
629 chrome::CloseAllBrowsersAndQuit();
630 ASSERT_NO_FATAL_FAILURE(AcceptClose());
631 close_observer.Wait();
632 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
633 EXPECT_TRUE(BrowserList::GetInstance()->empty());
634}
635
Jan Krcal9a23f222017-08-01 14:13:07636// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
637#if defined(OS_WIN) && !defined(NDEBUG)
638#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
639#else
640#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
641#endif
642
[email protected]2e9d79f2013-08-16 05:45:56643// Test that a window created during shutdown is closed.
Avi Drissman78267f52019-01-16 21:21:42644IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07645 MAYBE_TestAddWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56646 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
647 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40648 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56649
Scott Violet1f106b582017-07-12 15:49:58650 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56651 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20652 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56653 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32654 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56655 close_observer.Wait();
656 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37657 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56658}
659
660// Test that a window created during shutdown with a beforeunload handler can
661// cancel the shutdown.
Avi Drissman78267f52019-01-16 21:21:42662IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01663 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56664 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
665 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40666 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56667
Scott Violet1f106b582017-07-12 15:49:58668 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56669 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20670 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56671 browsers_.push_back(CreateBrowser(browser()->profile()));
672 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
673 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40674 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32675 ASSERT_NO_FATAL_FAILURE(AcceptClose());
676 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56677 cancel_observer.Wait();
678 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
679 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
680 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
681
682 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58683 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56684 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20685 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32686 ASSERT_NO_FATAL_FAILURE(AcceptClose());
687 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56688 close_observer.Wait();
689 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37690 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56691}
692
693// Test that tabs added during shutdown are closed.
Avi Drissman78267f52019-01-16 21:21:42694IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46695 TestAddTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56696 browsers_.push_back(CreateBrowser(browser()->profile()));
697 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
698 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
699 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
700 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40701 PrepareForDialog(browsers_[0]);
702 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56703
Scott Violet1f106b582017-07-12 15:49:58704 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56705 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20706 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32707 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56708 AddBlankTabAndShow(browsers_[0]);
709 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32710 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56711 close_observer.Wait();
712 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37713 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56714}
715
716// Test that tabs created during shutdown with beforeunload handlers can cancel
717// the shutdown.
metaflow812bb8542016-05-24 21:10:46718
Avi Drissman78267f52019-01-16 21:21:42719IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46720 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56721 browsers_.push_back(CreateBrowser(browser()->profile()));
722 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
723 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
724 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
725 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40726 PrepareForDialog(browsers_[0]);
727 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46728
Scott Violet1f106b582017-07-12 15:49:58729 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56730 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20731 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32732 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56733 AddBlankTabAndShow(browsers_[0]);
734 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
735 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
736 AddBlankTabAndShow(browsers_[1]);
737 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
738 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40739 PrepareForDialog(browsers_[0]);
740 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32741 ASSERT_NO_FATAL_FAILURE(AcceptClose());
742 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56743 cancel_observer.Wait();
744 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
745 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
746 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
747
Scott Violet1f106b582017-07-12 15:49:58748 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56749 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20750 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32751 ASSERT_NO_FATAL_FAILURE(AcceptClose());
752 ASSERT_NO_FATAL_FAILURE(AcceptClose());
753 ASSERT_NO_FATAL_FAILURE(AcceptClose());
754 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56755
756 close_observer.Wait();
757 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37758 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56759}
760
rogermf18929c2017-04-19 19:59:33761// TODO(crbug/713201):
762// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
763#if defined(OS_MACOSX)
764#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
765#else
766#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
767#endif
768
Avi Drissman78267f52019-01-16 21:21:42769IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33770 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40771 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
772 browser(), embedded_test_server()->GetURL("/title1.html")));
773
774 // Open second window.
775 ui_test_utils::NavigateToURLWithDisposition(
776 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
777 WindowOpenDisposition::NEW_WINDOW,
778 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
779 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
780 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
781 ? BrowserList::GetInstance()->get(0)
782 : BrowserList::GetInstance()->get(1);
783 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
784
785 // Let's work with second window only.
786 // This page has beforeunload handler already.
787 EXPECT_TRUE(browser2->tab_strip_model()
788 ->GetWebContentsAt(0)
789 ->NeedToFireBeforeUnload());
790 // This page doesn't have beforeunload handler. Yet.
791 ui_test_utils::NavigateToURLWithDisposition(
792 browser2, embedded_test_server()->GetURL("/title2.html"),
793 WindowOpenDisposition::NEW_FOREGROUND_TAB,
794 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
795 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
796 EXPECT_FALSE(browser2->tab_strip_model()
797 ->GetWebContentsAt(1)
798 ->NeedToFireBeforeUnload());
799 EXPECT_EQ(2, browser2->tab_strip_model()->count());
800
avi336125f72017-05-06 22:25:40801 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40802
803 // The test.
804
805 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
806 content::WindowedNotificationObserver observer(
807 chrome::NOTIFICATION_BROWSER_CLOSED,
808 content::NotificationService::AllSources());
809 chrome::CloseWindow(browser2);
810 // Just to be sure CloseWindow doesn't have asynchronous tasks
811 // that could have an impact.
812 content::RunAllPendingInMessageLoop();
813
814 // Closing browser shouldn't happen because of beforeunload handler.
815 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
816 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
817 // yet.
818 ASSERT_TRUE(content::ExecuteScript(
David Benjamin5a792652018-06-08 02:15:42819 browser2->tab_strip_model()->GetWebContentsAt(1),
mkolom65a0b3c2017-03-02 06:11:40820 "window.addEventListener('beforeunload', "
821 "function(event) { event.returnValue = 'Foo'; });"));
822 EXPECT_TRUE(browser2->tab_strip_model()
823 ->GetWebContentsAt(1)
824 ->NeedToFireBeforeUnload());
825 // Accept closing the first tab.
826 ASSERT_NO_FATAL_FAILURE(AcceptClose());
827 // Just to be sure accepting a dialog doesn't have asynchronous tasks
828 // that could have an impact.
829 content::RunAllPendingInMessageLoop();
830 // It shouldn't close the whole window/browser.
831 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
832 EXPECT_EQ(2, browser2->tab_strip_model()->count());
833 // Accept closing the second tab.
834 ASSERT_NO_FATAL_FAILURE(AcceptClose());
835 observer.Wait();
836 // Now the second window/browser should be closed.
837 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
838 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
839 EXPECT_EQ(1u, restore_observer.changes_count());
840
841 // Restore the closed browser.
842 content::WindowedNotificationObserver open_window_observer(
843 chrome::NOTIFICATION_BROWSER_OPENED,
844 content::NotificationService::AllSources());
845 chrome::OpenWindowWithRestoredTabs(browser()->profile());
846 open_window_observer.Wait();
847 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
848 browser2 = BrowserList::GetInstance()->get(0) != browser()
849 ? BrowserList::GetInstance()->get(0)
850 : BrowserList::GetInstance()->get(1);
851
852 // Check the restored browser contents.
853 EXPECT_EQ(2, browser2->tab_strip_model()->count());
854 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
855 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
856 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
857 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
858}
859
Avi Drissman78267f52019-01-16 21:21:42860IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42861 TestCloseTabDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56862 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
863 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40864 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46865
Scott Violet1f106b582017-07-12 15:49:58866 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56867 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20868 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56869
870 browsers_.push_back(CreateBrowser(browser()->profile()));
871 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
872 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40873 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56874 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32875 ASSERT_NO_FATAL_FAILURE(CancelClose());
876 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56877 cancel_observer.Wait();
878 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
879 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
880 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
881
Scott Violet1f106b582017-07-12 15:49:58882 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56883 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20884 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56885 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32886 ASSERT_NO_FATAL_FAILURE(AcceptClose());
887 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56888
889 close_observer.Wait();
890 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37891 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56892}
893
Avi Drissman78267f52019-01-16 21:21:42894IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46895 TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56896 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
897 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40898 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46899
Scott Violet1f106b582017-07-12 15:49:58900 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56901 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20902 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56903
904 browsers_.push_back(CreateBrowser(browser()->profile()));
905 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
906 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40907 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56908 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32909 ASSERT_NO_FATAL_FAILURE(CancelClose());
910 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56911 cancel_observer.Wait();
912 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
913 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
914 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
915
Scott Violet1f106b582017-07-12 15:49:58916 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56917 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20918 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56919 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32920 ASSERT_NO_FATAL_FAILURE(AcceptClose());
921 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56922
923 close_observer.Wait();
924 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37925 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56926}
927
Avi Drissman78267f52019-01-16 21:21:42928IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56929 TestCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56930 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
931 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
932 browsers_.push_back(CreateBrowser(browser()->profile()));
933 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
934 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40935 PrepareForDialog(browsers_[0]);
936 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46937
Scott Violet1f106b582017-07-12 15:49:58938 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56939 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20940 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56941
942 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32943 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56944 cancel_observer.Wait();
945 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
946 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
947 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
948
Scott Violet1f106b582017-07-12 15:49:58949 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56950 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20951 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56952 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32953 ASSERT_NO_FATAL_FAILURE(AcceptClose());
954 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56955
956 close_observer.Wait();
957 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37958 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56959}
960
jackhouae8e6e52015-05-29 06:36:46961// Mac has its own in-progress download prompt in app_controller_mac.mm, so
962// BrowserCloseManager should simply close all browsers. If there are no
963// browsers, it should not crash.
964#if defined(OS_MACOSX)
Avi Drissman78267f52019-01-16 21:21:42965IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
jackhouae8e6e52015-05-29 06:36:46966 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
967
Scott Violet1f106b582017-07-12 15:49:58968 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46969 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
970
971 TestBrowserCloseManager::AttemptClose(
972 TestBrowserCloseManager::NO_USER_CHOICE);
973 close_observer.Wait();
974 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37975 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:44976 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:46977
978 // Attempting to close again should not crash.
979 TestBrowserCloseManager::AttemptClose(
980 TestBrowserCloseManager::NO_USER_CHOICE);
981}
982#else // defined(OS_MACOSX)
983
[email protected]422a7d12013-10-21 12:10:42984// Test shutdown with a DANGEROUS_URL download undecided.
Avi Drissman78267f52019-01-16 21:21:42985IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Mattias Nissler441cc652018-04-05 00:08:56986 TestWithDangerousUrlDownload) {
[email protected]422a7d12013-10-21 12:10:42987 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:35988 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42989 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:44990 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:09991 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:42992
993 // Run a dangerous download, but the user doesn't make a decision.
994 // This .swf normally would be categorized as DANGEROUS_FILE, but
995 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
Jun Cai3ec06a122017-11-07 21:01:43996 GURL download_url(
997 embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:42998 content::DownloadTestObserverInterrupted observer(
999 content::BrowserContext::GetDownloadManager(browser()->profile()),
1000 1,
1001 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1002 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191003 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421004 ui_test_utils::BROWSER_TEST_NONE);
1005 observer.WaitForFinished();
1006
1007 // Check that the download manager has the expected state.
1008 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1009 browser()->profile())->InProgressCount());
1010 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1011 browser()->profile())->NonMaliciousInProgressCount());
1012
1013 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581014 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421015 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1016 TestBrowserCloseManager::AttemptClose(
1017 TestBrowserCloseManager::NO_USER_CHOICE);
1018 close_observer.Wait();
1019 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371020 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421021}
1022
[email protected]edfca702013-08-16 08:58:141023// Test shutdown with a download in progress.
Avi Drissman78267f52019-01-16 21:21:421024IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest, TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:141025 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1026 content::TestNavigationObserver navigation_observer(
1027 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1028 TestBrowserCloseManager::AttemptClose(
1029 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1030 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1031 navigation_observer.Wait();
1032 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1033 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1034
Scott Violet1f106b582017-07-12 15:49:581035 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141036 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1037
1038 TestBrowserCloseManager::AttemptClose(
1039 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1040 close_observer.Wait();
1041 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371042 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381043 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441044 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381045 else
peterccb33e82017-05-02 19:00:441046 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141047}
1048
sammccd347712015-03-13 22:02:321049// Test shutdown with a download in progress in an off-the-record profile.
Avi Drissman78267f52019-01-16 21:21:421050IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
sammccd347712015-03-13 22:02:321051 TestWithOffTheRecordDownloads) {
sammccd347712015-03-13 22:02:321052 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
sammccd347712015-03-13 22:02:321053 Browser* otr_browser = CreateBrowser(otr_profile);
1054 {
Scott Violet1f106b582017-07-12 15:49:581055 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321056 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1057 browser()->window()->Close();
1058 close_observer.Wait();
1059 }
1060 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1061 content::TestNavigationObserver navigation_observer(
1062 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1063 TestBrowserCloseManager::AttemptClose(
1064 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1065 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1066 navigation_observer.Wait();
1067 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1068 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1069
Scott Violet1f106b582017-07-12 15:49:581070 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321071 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1072
1073 TestBrowserCloseManager::AttemptClose(
1074 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1075 close_observer.Wait();
1076 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371077 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441078 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321079}
1080
Asanka Herathd1341dc2017-04-19 18:28:391081// Test shutdown with a download in progress in a regular profile an inconito
1082// browser is opened and closed. While there are active downloads, closing the
1083// incognito window shouldn't block on the active downloads which belong to the
1084// parent profile.
Weza78944d2018-05-17 16:56:291085// TODO(https://crbug.com/844019): Fix the notification expectation around the
1086// call to AttemptClose.
Avi Drissman78267f52019-01-16 21:21:421087IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Weza78944d2018-05-17 16:56:291088 DISABLED_TestWithOffTheRecordWindowAndRegularDownload) {
Asanka Herathd1341dc2017-04-19 18:28:391089 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
Asanka Herathd1341dc2017-04-19 18:28:391090 Browser* otr_browser = CreateBrowser(otr_profile);
1091 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1092
1093 content::TestNavigationObserver navigation_observer(
1094 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1095 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1096 navigation_observer.Wait();
1097
1098 int num_downloads_blocking = 0;
1099 ASSERT_EQ(
1100 Browser::DOWNLOAD_CLOSE_OK,
1101 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1102 ASSERT_EQ(0, num_downloads_blocking);
1103
1104 {
Scott Violet1f106b582017-07-12 15:49:581105 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391106 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1107 otr_browser->window()->Close();
1108 close_observer.Wait();
1109 }
1110
1111 ASSERT_EQ(
1112 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1113 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1114 ASSERT_EQ(1, num_downloads_blocking);
1115
1116 {
Scott Violet1f106b582017-07-12 15:49:581117 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391118 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1119 TestBrowserCloseManager::AttemptClose(
1120 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1121 close_observer.Wait();
1122 }
1123
1124 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1125 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1126 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441127 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391128 else
peterccb33e82017-05-02 19:00:441129 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391130}
1131
[email protected]edfca702013-08-16 08:58:141132// Test shutdown with a download in progress from one profile, where the only
1133// open windows are for another profile.
Avi Drissman78267f52019-01-16 21:21:421134IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
[email protected]edfca702013-08-16 08:58:141135 TestWithDownloadsFromDifferentProfiles) {
1136 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511137 Profile* other_profile = nullptr;
1138 {
1139 base::FilePath path =
1140 profile_manager->user_data_dir().AppendASCII("test_profile");
Francois Doraye6fb2d02017-10-18 21:29:131141 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:511142 if (!base::PathExists(path))
1143 ASSERT_TRUE(base::CreateDirectory(path));
1144 other_profile =
1145 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1146 }
[email protected]edfca702013-08-16 08:58:141147 profile_manager->RegisterTestingProfile(other_profile, true, false);
1148 Browser* other_profile_browser = CreateBrowser(other_profile);
1149
[email protected]edfca702013-08-16 08:58:141150 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1151 {
Scott Violet1f106b582017-07-12 15:49:581152 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141153 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1154 browser()->window()->Close();
1155 close_observer.Wait();
1156 }
1157
1158 // When the shutdown is cancelled, the downloads page should be opened in a
1159 // browser for that profile. Because there are no browsers for that profile, a
1160 // new browser should be opened.
1161 ui_test_utils::BrowserAddedObserver new_browser_observer;
1162 TestBrowserCloseManager::AttemptClose(
1163 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1164 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1165 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1166 EXPECT_EQ(
1167 GURL(chrome::kChromeUIDownloadsURL),
1168 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1169 EXPECT_EQ(GURL("about:blank"),
1170 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1171 ->GetURL());
1172
Scott Violet1f106b582017-07-12 15:49:581173 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141174 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1175 TestBrowserCloseManager::AttemptClose(
1176 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1177 close_observer.Wait();
1178 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371179 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381180 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441181 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381182 else
peterccb33e82017-05-02 19:00:441183 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141184}
1185
Vasilii Sukhanov4a65b862017-08-04 10:01:371186// Fails on ChromeOS and Linux, times out on Win. crbug.com/749098
[email protected]edfca702013-08-16 08:58:141187// Test shutdown with downloads in progress and beforeunload handlers.
Avi Drissman78267f52019-01-16 21:21:421188IN_PROC_BROWSER_TEST_F(BrowserCloseManagerBrowserTest,
Vasilii Sukhanov4a65b862017-08-04 10:01:371189 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:141190 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1191 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1192 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401193 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141194
1195 content::WindowedNotificationObserver cancel_observer(
1196 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1197 content::NotificationService::AllSources());
1198 TestBrowserCloseManager::AttemptClose(
1199 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321200 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141201 cancel_observer.Wait();
1202 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1203
Scott Violet1f106b582017-07-12 15:49:581204 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141205 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1206 TestBrowserCloseManager::AttemptClose(
1207 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321208 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141209 close_observer.Wait();
1210 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371211 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141212}
1213
jackhouae8e6e52015-05-29 06:36:461214#endif // defined(OS_MACOSX)
1215
Denis Kuznetsov32099f32017-12-15 14:04:571216#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
1217
[email protected]0c95faf42013-10-28 06:27:201218class BrowserCloseManagerWithBackgroundModeBrowserTest
1219 : public BrowserCloseManagerBrowserTest {
1220 public:
1221 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1222
Daniel Chenga542fca2014-10-21 09:51:291223 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201224 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1225 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351226 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201227 }
1228
1229 bool IsBackgroundModeSuspended() {
1230 return static_cast<FakeBackgroundModeManager*>(
1231 g_browser_process->background_mode_manager())
1232 ->IsBackgroundModeSuspended();
1233 }
1234
1235 private:
1236 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1237};
1238
1239// Check that background mode is suspended when closing all browsers unless we
1240// are quitting and that background mode is resumed when a new browser window is
1241// opened.
Avi Drissman78267f52019-01-16 21:21:421242IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
[email protected]0c95faf42013-10-28 06:27:201243 CloseAllBrowsersWithBackgroundMode) {
1244 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351245 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201246 Profile* profile = browser()->profile();
1247 {
Scott Violet1f106b582017-07-12 15:49:581248 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201249 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381250 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1251 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201252 chrome::CloseAllBrowsers();
1253 close_observer.Wait();
1254 }
1255 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371256 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201257 EXPECT_TRUE(IsBackgroundModeSuspended());
1258
1259 // Background mode should be resumed when a new browser window is opened.
1260 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441261 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201262 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381263 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201264 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581265 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201266 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1267
1268 // Background mode should not be suspended when quitting.
1269 chrome::CloseAllBrowsersAndQuit();
1270 close_observer.Wait();
1271 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371272 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201273 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201274}
1275
1276// Check that closing the last browser window individually does not affect
1277// background mode.
Avi Drissman78267f52019-01-16 21:21:421278IN_PROC_BROWSER_TEST_F(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161279 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581280 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201281 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1282 EXPECT_FALSE(IsBackgroundModeSuspended());
1283 browser()->window()->Close();
1284 close_observer.Wait();
1285 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371286 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201287 EXPECT_FALSE(IsBackgroundModeSuspended());
1288}
1289
1290// Check that closing all browsers with no browser windows open suspends
1291// background mode but does not cause Chrome to quit.
Avi Drissman78267f52019-01-16 21:21:421292IN_PROC_BROWSER_TEST_F(
1293 BrowserCloseManagerWithBackgroundModeBrowserTest,
1294 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581295 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201296 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1297 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381298 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1299 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201300 browser()->window()->Close();
1301 close_observer.Wait();
1302 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371303 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201304 EXPECT_FALSE(IsBackgroundModeSuspended());
1305
1306 chrome::CloseAllBrowsers();
1307 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371308 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201309 EXPECT_TRUE(IsBackgroundModeSuspended());
1310}
1311
Denis Kuznetsov32099f32017-12-15 14:04:571312#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)