blob: f711acc05d5c688f1600af093148435bf5d60b80 [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"
13#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2014#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1415#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5616#include "chrome/browser/browser_shutdown.h"
17#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3818#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4219#include "chrome/browser/download/chrome_download_manager_delegate.h"
[email protected]0ed53872014-05-10 05:51:0520#include "chrome/browser/download/download_prefs.h"
[email protected]422a7d12013-10-21 12:10:4221#include "chrome/browser/download/download_service.h"
22#include "chrome/browser/download/download_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5623#include "chrome/browser/lifetime/application_lifetime.h"
dgn02377782016-03-12 00:58:3824#include "chrome/browser/lifetime/keep_alive_types.h"
25#include "chrome/browser/lifetime/scoped_keep_alive.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"
37#include "chrome/common/url_constants.h"
38#include "chrome/test/base/in_process_browser_test.h"
39#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4240#include "components/app_modal/javascript_app_modal_dialog.h"
41#include "components/app_modal/native_app_modal_dialog.h"
mkolom65a0b3c2017-03-02 06:11:4042#include "components/sessions/core/tab_restore_service.h"
43#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1444#include "content/public/browser/browser_context.h"
45#include "content/public/browser/download_item.h"
46#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5647#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3448#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4649#include "content/public/browser/render_view_host.h"
50#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5651#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4052#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1453#include "content/public/test/download_test_observer.h"
[email protected]2e9d79f2013-08-16 05:45:5654#include "content/public/test/test_navigation_observer.h"
55#include "net/test/embedded_test_server/embedded_test_server.h"
xunjieli0332c192014-09-10 23:23:3156#include "net/test/url_request/url_request_mock_http_job.h"
jam8e45cd72015-01-20 16:33:4457#include "net/test/url_request/url_request_slow_download_job.h"
[email protected]2e9d79f2013-08-16 05:45:5658
[email protected]f9357a442014-05-15 18:44:0759#if defined(OS_CHROMEOS)
60#include "chromeos/chromeos_switches.h"
61#endif
62
[email protected]2e9d79f2013-08-16 05:45:5663namespace {
64
oshima0929be2a2014-11-19 22:21:0365app_modal::NativeAppModalDialog* GetNextDialog() {
66 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
oshima82f72482014-10-24 14:14:3267 EXPECT_TRUE(dialog->IsJavaScriptModalDialog());
oshima0929be2a2014-11-19 22:21:0368 app_modal::JavaScriptAppModalDialog* js_dialog =
69 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
oshima82f72482014-10-24 14:14:3270 CHECK(js_dialog->native_dialog());
71 return js_dialog->native_dialog();
72}
[email protected]2e9d79f2013-08-16 05:45:5673
metaflow812bb8542016-05-24 21:10:4674// Note: call |DisableHangMonitor| on the relevant WebContents or Browser before
75// 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
metaflow812bb8542016-05-24 21:10:4680// Note: call |DisableHangMonitor| on the relevant WebContents or Browser before
81// 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
86class 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
Daniel Chenga542fca2014-10-21 09:51:2993 void Observe(int type,
94 const content::NotificationSource& source,
95 const content::NotificationDetails& details) override {
[email protected]2e9d79f2013-08-16 05:45:5696 ASSERT_GT(num_outstanding_, 0);
97 if (!--num_outstanding_ && running_) {
98 content::BrowserThread::PostTask(
99 content::BrowserThread::UI, FROM_HERE, 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) {
[email protected]861b4d0a2013-12-13 20:10:41202 GetDownloadIdReceiverCallback().Run(content::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(
[email protected]422a7d12013-10-21 12:10:42207 content::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,
217 content::DownloadItem::TargetDisposition disp,
218 content::DownloadDangerType danger_type,
219 const base::FilePath& intermediate_path,
220 content::DownloadInterruptReason reason) {
221 callback.Run(target_path, disp, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
222 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42223 }
[email protected]422a7d12013-10-21 12:10:42224};
225
[email protected]0c95faf42013-10-28 06:27:20226class FakeBackgroundModeManager : public BackgroundModeManager {
227 public:
228 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54229 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
230 &g_browser_process->profile_manager()
231 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20232 suspended_(false) {}
233
Daniel Chenga542fca2014-10-21 09:51:29234 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20235 BackgroundModeManager::SuspendBackgroundMode();
236 suspended_ = true;
237 }
238
Daniel Chenga542fca2014-10-21 09:51:29239 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20240 BackgroundModeManager::ResumeBackgroundMode();
241 suspended_ = false;
242 }
243
244 bool IsBackgroundModeSuspended() {
245 return suspended_;
246 }
247
248 private:
249 bool suspended_;
250
251 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
252};
253
[email protected]2e9d79f2013-08-16 05:45:56254} // namespace
255
256class BrowserCloseManagerBrowserTest
257 : public InProcessBrowserTest,
258 public testing::WithParamInterface<bool> {
259 protected:
Daniel Chenga542fca2014-10-21 09:51:29260 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56261 InProcessBrowserTest::SetUpOnMainThread();
262 SessionStartupPref::SetStartupPref(
263 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
264 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14265 content::BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33266 content::BrowserThread::IO, FROM_HERE,
267 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
[email protected]2e9d79f2013-08-16 05:45:56268 }
269
avi556c05022014-12-22 23:31:43270 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56271 if (GetParam())
272 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07273#if defined(OS_CHROMEOS)
274 command_line->AppendSwitch(
275 chromeos::switches::kIgnoreUserProfileMappingForTests);
276#endif
[email protected]2e9d79f2013-08-16 05:45:56277 }
278
[email protected]edfca702013-08-16 08:58:14279 void CreateStalledDownload(Browser* browser) {
280 content::DownloadTestObserverInProgress observer(
281 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
282 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19283 browser, GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl),
284 WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14285 ui_test_utils::BROWSER_TEST_NONE);
286 observer.WaitForFinished();
287 EXPECT_EQ(
288 1UL,
289 observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS));
290 }
291
avi64b8b65b2017-03-29 18:50:34292 // Makes sure that the beforeunload hang monitor will not trigger. That must
293 // be called before close action when using |AcceptClose| or |CancelClose|, to
294 // ensure the timeout does not prevent the dialog from appearing.
295 // https://crbug.com/519646
metaflow812bb8542016-05-24 21:10:46296 void DisableHangMonitor(content::WebContents* web_contents) {
avi64b8b65b2017-03-29 18:50:34297 web_contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
metaflow812bb8542016-05-24 21:10:46298 }
299
300 void DisableHangMonitor(Browser* browser) {
301 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
302 DisableHangMonitor(browser->tab_strip_model()->GetWebContentsAt(i));
303 }
304
[email protected]2e9d79f2013-08-16 05:45:56305 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56306};
307
308IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
svaldeza01f7d92015-11-18 17:47:56309 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56310 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
311 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46312 DisableHangMonitor(browser());
313
[email protected]2e9d79f2013-08-16 05:45:56314 RepeatedNotificationObserver cancel_observer(
315 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20316 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32317 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56318 cancel_observer.Wait();
319 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
320 EXPECT_EQ(1, browser()->tab_strip_model()->count());
321
322 RepeatedNotificationObserver close_observer(
323 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20324 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32325 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56326 close_observer.Wait();
327 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37328 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56329}
330
331IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
332 TestShutdownMoreThanOnce) {
svaldeza01f7d92015-11-18 17:47:56333 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56334 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
335 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46336 DisableHangMonitor(browser());
337
[email protected]2e9d79f2013-08-16 05:45:56338 RepeatedNotificationObserver cancel_observer(
339 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20340 chrome::CloseAllBrowsersAndQuit();
341 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32342 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56343 cancel_observer.Wait();
344 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
345 EXPECT_EQ(1, browser()->tab_strip_model()->count());
346
347 RepeatedNotificationObserver close_observer(
348 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20349 chrome::CloseAllBrowsersAndQuit();
350 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32351 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56352 close_observer.Wait();
353 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37354 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56355}
356
wafflesb63234052015-08-07 02:44:02357IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46358 PRE_TestSessionRestore) {
svaldeza01f7d92015-11-18 17:47:56359 ASSERT_TRUE(embedded_test_server()->Start());
[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)));
metaflow812bb8542016-05-24 21:10:46365 DisableHangMonitor(browser());
366
[email protected]2e9d79f2013-08-16 05:45:56367 RepeatedNotificationObserver cancel_observer(
368 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
384 RepeatedNotificationObserver close_observer(
385 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.
wafflesb63234052015-08-07 02:44:02394IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46395 TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56396 // The testing framework launches Chrome with about:blank as args.
397 EXPECT_EQ(2, browser()->tab_strip_model()->count());
398 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
399 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
400 EXPECT_EQ(GURL("about:blank"),
401 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
402}
403
404// Test that browser windows are only closed if all browsers are ready to close
405// and that all beforeunload dialogs are shown again after a cancel.
[email protected]40cd13a2013-10-24 13:42:01406IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
svaldeza01f7d92015-11-18 17:47:56407 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56408 browsers_.push_back(CreateBrowser(browser()->profile()));
409 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
410 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
411 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
412 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46413 DisableHangMonitor(browsers_[0]);
414 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56415
416 // Cancel shutdown on the first beforeunload event.
417 {
418 RepeatedNotificationObserver cancel_observer(
419 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20420 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32421 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56422 cancel_observer.Wait();
423 }
424 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
425 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
426 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
427
428 // Cancel shutdown on the second beforeunload event.
429 {
430 RepeatedNotificationObserver cancel_observer(
431 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20432 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32433 ASSERT_NO_FATAL_FAILURE(AcceptClose());
434 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56435 cancel_observer.Wait();
436 }
437 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
438 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
439 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
440
441 // Allow shutdown for both beforeunload events.
442 RepeatedNotificationObserver close_observer(
443 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20444 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32445 ASSERT_NO_FATAL_FAILURE(AcceptClose());
446 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56447 close_observer.Wait();
448 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37449 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56450}
451
452// Test that tabs in the same window with a beforeunload event that hangs are
453// treated the same as the user accepting the close, but do not close the tab
454// early.
[email protected]2e9d79f2013-08-16 05:45:56455IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46456 TestHangInBeforeUnloadMultipleTabs) {
svaldeza01f7d92015-11-18 17:47:56457 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56458 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
459 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
460 AddBlankTabAndShow(browsers_[0]);
461 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
462 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
463 AddBlankTabAndShow(browsers_[0]);
464 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
465 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46466 // Disable the hang monitor in the tab that is not expected to hang, so that
467 // the dialog is guaranteed to show.
468 DisableHangMonitor(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56469
470 RepeatedNotificationObserver cancel_observer(
471 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20472 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32473 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56474 cancel_observer.Wait();
475 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
476 // All tabs should still be open.
477 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
478
479 RepeatedNotificationObserver close_observer(
480 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20481 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32482 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56483 close_observer.Wait();
484 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37485 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56486}
487
488// Test that tabs in different windows with a beforeunload event that hangs are
489// treated the same as the user accepting the close, but do not close the tab
490// early.
491IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
492 TestHangInBeforeUnloadMultipleWindows) {
svaldeza01f7d92015-11-18 17:47:56493 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56494 browsers_.push_back(CreateBrowser(browser()->profile()));
495 browsers_.push_back(CreateBrowser(browser()->profile()));
496 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
497 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
498 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
499 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
500 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
501 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46502 // Disable the hang monitor in the tab that is not expected to hang, so that
503 // the dialog is guaranteed to show.
504 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56505
506 RepeatedNotificationObserver cancel_observer(
507 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20508 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32509 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56510 cancel_observer.Wait();
511 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
512 // All windows should still be open.
513 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
514 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
515 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
516
517 RepeatedNotificationObserver close_observer(
518 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20519 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32520 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56521 close_observer.Wait();
522 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37523 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56524}
525
metaflow73fc9552016-05-23 18:26:48526// Test that tabs that are slow to respond are not closed prematurely.
527// Regression for crbug.com/365052 caused some of tabs to be closed even if
528// user chose to cancel browser close.
metaflow73fc9552016-05-23 18:26:48529IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46530 TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48531 ASSERT_TRUE(embedded_test_server()->Start());
532 const int kTabCount = 5;
533 const int kResposiveTabIndex = 2;
534 // Create tab strip with all tabs except one responding after
535 // RenderViewHostImpl::kUnloadTimeoutMS.
536 // Minimum configuration is two slow tabs and then responsive tab.
537 // But we also want to check how slow tabs behave in tail.
538 for (int i = 0; i < kTabCount; i++) {
539 if (i)
540 AddBlankTabAndShow(browsers_[0]);
541 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
542 browsers_[0],
543 embedded_test_server()->GetURL((i == kResposiveTabIndex)
544 ? "/beforeunload.html"
545 : "/beforeunload_slow.html")));
546 }
metaflow812bb8542016-05-24 21:10:46547 // Disable the hang monitor in the tab that is not expected to hang, so that
548 // the dialog is guaranteed to show.
549 DisableHangMonitor(
550 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
551
metaflow73fc9552016-05-23 18:26:48552 RepeatedNotificationObserver cancel_observer(
553 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
554 chrome::CloseAllBrowsersAndQuit();
555 ASSERT_NO_FATAL_FAILURE(CancelClose());
556 cancel_observer.Wait();
557 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
558
559 // All tabs should still be open.
560 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
561 RepeatedNotificationObserver close_observer(
562 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
563
564 // Quit, this time accepting close confirmation dialog.
565 chrome::CloseAllBrowsersAndQuit();
566 ASSERT_NO_FATAL_FAILURE(AcceptClose());
567 close_observer.Wait();
568 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
569 EXPECT_TRUE(BrowserList::GetInstance()->empty());
570}
571
572// Test that tabs in different windows with a slow beforeunload event response
573// are treated the same as the user accepting the close, but do not close the
574// tab early.
575// Regression for crbug.com/365052 caused CHECK in tabstrip.
metaflow73fc9552016-05-23 18:26:48576IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46577 TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48578 ASSERT_TRUE(embedded_test_server()->Start());
579 const int kBrowserCount = 5;
580 const int kResposiveBrowserIndex = 2;
581 // Create multiple browsers with all tabs except one responding after
582 // RenderViewHostImpl::kUnloadTimeoutMS .
583 // Minimum configuration is just one browser with slow tab and then
584 // browser with responsive tab.
585 // But we also want to check how slow tabs behave in tail and make test
586 // more robust.
587 for (int i = 0; i < kBrowserCount; i++) {
588 if (i)
589 browsers_.push_back(CreateBrowser(browser()->profile()));
590 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
591 browsers_[i],
592 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
593 ? "/beforeunload.html"
594 : "/beforeunload_slow.html")));
595 }
metaflow812bb8542016-05-24 21:10:46596 // Disable the hang monitor in the tab that is not expected to hang, so that
597 // the dialog is guaranteed to show.
598 DisableHangMonitor(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48599
600 RepeatedNotificationObserver cancel_observer(
601 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
602 chrome::CloseAllBrowsersAndQuit();
603 ASSERT_NO_FATAL_FAILURE(CancelClose());
604 cancel_observer.Wait();
605 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
606
607 // All windows should still be open.
608 for (int i = 0; i < kBrowserCount; i++)
609 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
610
611 // Quit, this time accepting close confirmation dialog.
612 RepeatedNotificationObserver close_observer(
613 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
614 chrome::CloseAllBrowsersAndQuit();
615 ASSERT_NO_FATAL_FAILURE(AcceptClose());
616 close_observer.Wait();
617 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
618 EXPECT_TRUE(BrowserList::GetInstance()->empty());
619}
620
[email protected]2e9d79f2013-08-16 05:45:56621// Test that a window created during shutdown is closed.
622IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
623 TestAddWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56624 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56625 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
626 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46627 DisableHangMonitor(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56628
629 RepeatedNotificationObserver close_observer(
630 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20631 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56632 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32633 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56634 close_observer.Wait();
635 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37636 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56637}
638
639// Test that a window created during shutdown with a beforeunload handler can
640// cancel the shutdown.
641IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01642 TestAddWindowWithBeforeUnloadDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56643 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56644 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
645 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46646 DisableHangMonitor(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56647
648 RepeatedNotificationObserver cancel_observer(
649 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20650 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56651 browsers_.push_back(CreateBrowser(browser()->profile()));
652 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
653 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46654 DisableHangMonitor(browsers_[1]);
oshima82f72482014-10-24 14:14:32655 ASSERT_NO_FATAL_FAILURE(AcceptClose());
656 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56657 cancel_observer.Wait();
658 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
659 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
660 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
661
662 // Allow shutdown for both beforeunload dialogs.
663 RepeatedNotificationObserver close_observer(
664 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20665 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32666 ASSERT_NO_FATAL_FAILURE(AcceptClose());
667 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56668 close_observer.Wait();
669 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37670 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56671}
672
673// Test that tabs added during shutdown are closed.
[email protected]2e9d79f2013-08-16 05:45:56674IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46675 TestAddTabDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56676 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56677 browsers_.push_back(CreateBrowser(browser()->profile()));
678 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
679 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
680 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
681 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46682 DisableHangMonitor(browsers_[0]);
683 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56684
685 RepeatedNotificationObserver close_observer(
686 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20687 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32688 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56689 AddBlankTabAndShow(browsers_[0]);
690 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32691 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56692 close_observer.Wait();
693 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37694 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56695}
696
697// Test that tabs created during shutdown with beforeunload handlers can cancel
698// the shutdown.
metaflow812bb8542016-05-24 21:10:46699
[email protected]2e9d79f2013-08-16 05:45:56700IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46701 TestAddTabWithBeforeUnloadDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56702 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56703 browsers_.push_back(CreateBrowser(browser()->profile()));
704 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
705 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
706 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
707 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46708 DisableHangMonitor(browsers_[0]);
709 DisableHangMonitor(browsers_[1]);
710
[email protected]2e9d79f2013-08-16 05:45:56711 RepeatedNotificationObserver cancel_observer(
712 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20713 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32714 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56715 AddBlankTabAndShow(browsers_[0]);
716 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
717 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
718 AddBlankTabAndShow(browsers_[1]);
719 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
720 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46721 DisableHangMonitor(browsers_[0]);
722 DisableHangMonitor(browsers_[1]);
oshima82f72482014-10-24 14:14:32723 ASSERT_NO_FATAL_FAILURE(AcceptClose());
724 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56725 cancel_observer.Wait();
726 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
727 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
728 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
729
730 RepeatedNotificationObserver close_observer(
731 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20732 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32733 ASSERT_NO_FATAL_FAILURE(AcceptClose());
734 ASSERT_NO_FATAL_FAILURE(AcceptClose());
735 ASSERT_NO_FATAL_FAILURE(AcceptClose());
736 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56737
738 close_observer.Wait();
739 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37740 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56741}
742
rogermf18929c2017-04-19 19:59:33743// TODO(crbug/713201):
744// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
745#if defined(OS_MACOSX)
746#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
747#else
748#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
749#endif
750
[email protected]2e9d79f2013-08-16 05:45:56751IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33752 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40753 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
754 // And it is difficult to fix this issue without fixing that one.
755 if (GetParam())
756 return;
757
758 ASSERT_TRUE(embedded_test_server()->Start());
759 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
760 browser(), embedded_test_server()->GetURL("/title1.html")));
761
762 // Open second window.
763 ui_test_utils::NavigateToURLWithDisposition(
764 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
765 WindowOpenDisposition::NEW_WINDOW,
766 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
767 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
768 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
769 ? BrowserList::GetInstance()->get(0)
770 : BrowserList::GetInstance()->get(1);
771 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
772
773 // Let's work with second window only.
774 // This page has beforeunload handler already.
775 EXPECT_TRUE(browser2->tab_strip_model()
776 ->GetWebContentsAt(0)
777 ->NeedToFireBeforeUnload());
778 // This page doesn't have beforeunload handler. Yet.
779 ui_test_utils::NavigateToURLWithDisposition(
780 browser2, embedded_test_server()->GetURL("/title2.html"),
781 WindowOpenDisposition::NEW_FOREGROUND_TAB,
782 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
783 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
784 EXPECT_FALSE(browser2->tab_strip_model()
785 ->GetWebContentsAt(1)
786 ->NeedToFireBeforeUnload());
787 EXPECT_EQ(2, browser2->tab_strip_model()->count());
788
789 DisableHangMonitor(browser2);
790
791 // The test.
792
793 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
794 content::WindowedNotificationObserver observer(
795 chrome::NOTIFICATION_BROWSER_CLOSED,
796 content::NotificationService::AllSources());
797 chrome::CloseWindow(browser2);
798 // Just to be sure CloseWindow doesn't have asynchronous tasks
799 // that could have an impact.
800 content::RunAllPendingInMessageLoop();
801
802 // Closing browser shouldn't happen because of beforeunload handler.
803 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
804 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
805 // yet.
806 ASSERT_TRUE(content::ExecuteScript(
807 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(),
808 "window.addEventListener('beforeunload', "
809 "function(event) { event.returnValue = 'Foo'; });"));
810 EXPECT_TRUE(browser2->tab_strip_model()
811 ->GetWebContentsAt(1)
812 ->NeedToFireBeforeUnload());
813 // Accept closing the first tab.
814 ASSERT_NO_FATAL_FAILURE(AcceptClose());
815 // Just to be sure accepting a dialog doesn't have asynchronous tasks
816 // that could have an impact.
817 content::RunAllPendingInMessageLoop();
818 // It shouldn't close the whole window/browser.
819 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
820 EXPECT_EQ(2, browser2->tab_strip_model()->count());
821 // Accept closing the second tab.
822 ASSERT_NO_FATAL_FAILURE(AcceptClose());
823 observer.Wait();
824 // Now the second window/browser should be closed.
825 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
826 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
827 EXPECT_EQ(1u, restore_observer.changes_count());
828
829 // Restore the closed browser.
830 content::WindowedNotificationObserver open_window_observer(
831 chrome::NOTIFICATION_BROWSER_OPENED,
832 content::NotificationService::AllSources());
833 chrome::OpenWindowWithRestoredTabs(browser()->profile());
834 open_window_observer.Wait();
835 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
836 browser2 = BrowserList::GetInstance()->get(0) != browser()
837 ? BrowserList::GetInstance()->get(0)
838 : BrowserList::GetInstance()->get(1);
839
840 // Check the restored browser contents.
841 EXPECT_EQ(2, browser2->tab_strip_model()->count());
842 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
843 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
844 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
845 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
846}
847
mkolom65a0b3c2017-03-02 06:11:40848IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42849 TestCloseTabDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56850 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56851 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
852 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46853 DisableHangMonitor(browsers_[0]);
854
[email protected]2e9d79f2013-08-16 05:45:56855 RepeatedNotificationObserver cancel_observer(
856 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20857 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56858
859 browsers_.push_back(CreateBrowser(browser()->profile()));
860 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
861 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46862 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56863 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32864 ASSERT_NO_FATAL_FAILURE(CancelClose());
865 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56866 cancel_observer.Wait();
867 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
868 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
869 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
870
871 RepeatedNotificationObserver close_observer(
872 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20873 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56874 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32875 ASSERT_NO_FATAL_FAILURE(AcceptClose());
876 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56877
878 close_observer.Wait();
879 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37880 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56881}
882
[email protected]2e9d79f2013-08-16 05:45:56883IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46884 TestOpenAndCloseWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56885 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56886 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
887 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46888 DisableHangMonitor(browsers_[0]);
889
[email protected]2e9d79f2013-08-16 05:45:56890 RepeatedNotificationObserver cancel_observer(
891 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20892 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56893
894 browsers_.push_back(CreateBrowser(browser()->profile()));
895 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
896 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46897 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56898 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32899 ASSERT_NO_FATAL_FAILURE(CancelClose());
900 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56901 cancel_observer.Wait();
902 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
903 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
904 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
905
906 RepeatedNotificationObserver close_observer(
907 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20908 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56909 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32910 ASSERT_NO_FATAL_FAILURE(AcceptClose());
911 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56912
913 close_observer.Wait();
914 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37915 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56916}
917
918IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
919 TestCloseWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56920 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56921 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
922 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
923 browsers_.push_back(CreateBrowser(browser()->profile()));
924 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
925 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46926 DisableHangMonitor(browsers_[0]);
927 DisableHangMonitor(browsers_[1]);
928
[email protected]2e9d79f2013-08-16 05:45:56929 RepeatedNotificationObserver cancel_observer(
930 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20931 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56932
933 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32934 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56935 cancel_observer.Wait();
936 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
937 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
938 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
939
940 RepeatedNotificationObserver close_observer(
941 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20942 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56943 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32944 ASSERT_NO_FATAL_FAILURE(AcceptClose());
945 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56946
947 close_observer.Wait();
948 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37949 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56950}
951
[email protected]0ed53872014-05-10 05:51:05952INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
953 BrowserCloseManagerBrowserTest,
954 testing::Bool());
955
956class BrowserCloseManagerWithDownloadsBrowserTest :
957 public BrowserCloseManagerBrowserTest {
958 public:
959 BrowserCloseManagerWithDownloadsBrowserTest() {}
960 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
961
Daniel Chenga542fca2014-10-21 09:51:29962 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05963 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
964 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
965 }
966
[email protected]0ed53872014-05-10 05:51:05967 void SetDownloadPathForProfile(Profile* profile) {
968 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
969 download_prefs->SetDownloadPath(download_path());
970 }
971
972 const base::FilePath& download_path() const {
vabr0c237ae2016-09-14 09:24:28973 return scoped_download_directory_.GetPath();
[email protected]0ed53872014-05-10 05:51:05974 }
975
976 private:
977 base::ScopedTempDir scoped_download_directory_;
978};
979
jackhouae8e6e52015-05-29 06:36:46980// Mac has its own in-progress download prompt in app_controller_mac.mm, so
981// BrowserCloseManager should simply close all browsers. If there are no
982// browsers, it should not crash.
983#if defined(OS_MACOSX)
984IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
985 TestWithDownloads) {
svaldeza01f7d92015-11-18 17:47:56986 ASSERT_TRUE(embedded_test_server()->Start());
jackhouae8e6e52015-05-29 06:36:46987 SetDownloadPathForProfile(browser()->profile());
988 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
989
990 RepeatedNotificationObserver close_observer(
991 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
992
993 TestBrowserCloseManager::AttemptClose(
994 TestBrowserCloseManager::NO_USER_CHOICE);
995 close_observer.Wait();
996 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37997 EXPECT_TRUE(BrowserList::GetInstance()->empty());
jackhouae8e6e52015-05-29 06:36:46998 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
999
1000 // Attempting to close again should not crash.
1001 TestBrowserCloseManager::AttemptClose(
1002 TestBrowserCloseManager::NO_USER_CHOICE);
1003}
1004#else // defined(OS_MACOSX)
1005
[email protected]422a7d12013-10-21 12:10:421006// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:051007IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:421008 TestWithDangerousUrlDownload) {
svaldeza01f7d92015-11-18 17:47:561009 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051010 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:421011
1012 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:351013 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:421014 new TestDownloadManagerDelegate(browser()->profile()));
dcheng383ba8a2014-10-16 23:55:191015 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091016 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421017
1018 // Run a dangerous download, but the user doesn't make a decision.
1019 // This .swf normally would be categorized as DANGEROUS_FILE, but
1020 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
tfarina5e7b57232015-10-17 23:37:401021 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(
1022 "downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421023 content::DownloadTestObserverInterrupted observer(
1024 content::BrowserContext::GetDownloadManager(browser()->profile()),
1025 1,
1026 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1027 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191028 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421029 ui_test_utils::BROWSER_TEST_NONE);
1030 observer.WaitForFinished();
1031
1032 // Check that the download manager has the expected state.
1033 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1034 browser()->profile())->InProgressCount());
1035 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1036 browser()->profile())->NonMaliciousInProgressCount());
1037
1038 // Close the browser with no user action.
1039 RepeatedNotificationObserver close_observer(
1040 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1041 TestBrowserCloseManager::AttemptClose(
1042 TestBrowserCloseManager::NO_USER_CHOICE);
1043 close_observer.Wait();
1044 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371045 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421046}
1047
[email protected]edfca702013-08-16 08:58:141048// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:051049IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1050 TestWithDownloads) {
svaldeza01f7d92015-11-18 17:47:561051 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051052 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141053 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1054 content::TestNavigationObserver navigation_observer(
1055 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1056 TestBrowserCloseManager::AttemptClose(
1057 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1058 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1059 navigation_observer.Wait();
1060 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1061 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1062
1063 RepeatedNotificationObserver close_observer(
1064 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1065
1066 TestBrowserCloseManager::AttemptClose(
1067 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1068 close_observer.Wait();
1069 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371070 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381071 if (browser_defaults::kBrowserAliveWithNoWindows)
1072 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
1073 else
1074 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141075}
1076
sammccd347712015-03-13 22:02:321077// Test shutdown with a download in progress in an off-the-record profile.
1078IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1079 TestWithOffTheRecordDownloads) {
svaldeza01f7d92015-11-18 17:47:561080 ASSERT_TRUE(embedded_test_server()->Start());
sammccd347712015-03-13 22:02:321081 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1082 SetDownloadPathForProfile(otr_profile);
1083 Browser* otr_browser = CreateBrowser(otr_profile);
1084 {
1085 RepeatedNotificationObserver close_observer(
1086 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1087 browser()->window()->Close();
1088 close_observer.Wait();
1089 }
1090 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1091 content::TestNavigationObserver navigation_observer(
1092 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1093 TestBrowserCloseManager::AttemptClose(
1094 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1095 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1096 navigation_observer.Wait();
1097 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1098 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1099
1100 RepeatedNotificationObserver close_observer(
1101 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1102
1103 TestBrowserCloseManager::AttemptClose(
1104 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1105 close_observer.Wait();
1106 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371107 EXPECT_TRUE(BrowserList::GetInstance()->empty());
sammccd347712015-03-13 22:02:321108 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
1109}
1110
Asanka Herathd1341dc2017-04-19 18:28:391111// Test shutdown with a download in progress in a regular profile an inconito
1112// browser is opened and closed. While there are active downloads, closing the
1113// incognito window shouldn't block on the active downloads which belong to the
1114// parent profile.
1115IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1116 TestWithOffTheRecordWindowAndRegularDownload) {
1117 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1118 SetDownloadPathForProfile(otr_profile);
1119 Browser* otr_browser = CreateBrowser(otr_profile);
1120 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1121
1122 content::TestNavigationObserver navigation_observer(
1123 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1124 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1125 navigation_observer.Wait();
1126
1127 int num_downloads_blocking = 0;
1128 ASSERT_EQ(
1129 Browser::DOWNLOAD_CLOSE_OK,
1130 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1131 ASSERT_EQ(0, num_downloads_blocking);
1132
1133 {
1134 RepeatedNotificationObserver close_observer(
1135 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1136 otr_browser->window()->Close();
1137 close_observer.Wait();
1138 }
1139
1140 ASSERT_EQ(
1141 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1142 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1143 ASSERT_EQ(1, num_downloads_blocking);
1144
1145 {
1146 RepeatedNotificationObserver close_observer(
1147 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1148 TestBrowserCloseManager::AttemptClose(
1149 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1150 close_observer.Wait();
1151 }
1152
1153 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1154 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1155 if (browser_defaults::kBrowserAliveWithNoWindows)
1156 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
1157 else
1158 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
1159}
1160
[email protected]edfca702013-08-16 08:58:141161// Test shutdown with a download in progress from one profile, where the only
1162// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:051163IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:141164 TestWithDownloadsFromDifferentProfiles) {
1165 ProfileManager* profile_manager = g_browser_process->profile_manager();
1166 base::FilePath path =
1167 profile_manager->user_data_dir().AppendASCII("test_profile");
1168 if (!base::PathExists(path))
[email protected]426d1c92013-12-03 20:08:541169 ASSERT_TRUE(base::CreateDirectory(path));
[email protected]edfca702013-08-16 08:58:141170 Profile* other_profile =
1171 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1172 profile_manager->RegisterTestingProfile(other_profile, true, false);
1173 Browser* other_profile_browser = CreateBrowser(other_profile);
1174
svaldeza01f7d92015-11-18 17:47:561175 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051176 SetDownloadPathForProfile(browser()->profile());
1177 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:141178 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1179 {
1180 RepeatedNotificationObserver close_observer(
1181 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1182 browser()->window()->Close();
1183 close_observer.Wait();
1184 }
1185
1186 // When the shutdown is cancelled, the downloads page should be opened in a
1187 // browser for that profile. Because there are no browsers for that profile, a
1188 // new browser should be opened.
1189 ui_test_utils::BrowserAddedObserver new_browser_observer;
1190 TestBrowserCloseManager::AttemptClose(
1191 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1192 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1193 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1194 EXPECT_EQ(
1195 GURL(chrome::kChromeUIDownloadsURL),
1196 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1197 EXPECT_EQ(GURL("about:blank"),
1198 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1199 ->GetURL());
1200
1201 RepeatedNotificationObserver close_observer(
1202 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1203 TestBrowserCloseManager::AttemptClose(
1204 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1205 close_observer.Wait();
1206 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371207 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381208 if (browser_defaults::kBrowserAliveWithNoWindows)
1209 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
1210 else
1211 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141212}
1213
1214// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]0ed53872014-05-10 05:51:051215IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
metaflow812bb8542016-05-24 21:10:461216 TestBeforeUnloadAndDownloads) {
svaldeza01f7d92015-11-18 17:47:561217 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051218 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141219 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1220 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1221 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:461222 DisableHangMonitor(browser());
[email protected]edfca702013-08-16 08:58:141223
1224 content::WindowedNotificationObserver cancel_observer(
1225 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1226 content::NotificationService::AllSources());
1227 TestBrowserCloseManager::AttemptClose(
1228 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321229 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141230 cancel_observer.Wait();
1231 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1232
1233 RepeatedNotificationObserver close_observer(
1234 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1235 TestBrowserCloseManager::AttemptClose(
1236 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321237 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141238 close_observer.Wait();
1239 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371240 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141241}
1242
jackhouae8e6e52015-05-29 06:36:461243#endif // defined(OS_MACOSX)
1244
[email protected]0ed53872014-05-10 05:51:051245INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
1246 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561247 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201248
1249class BrowserCloseManagerWithBackgroundModeBrowserTest
1250 : public BrowserCloseManagerBrowserTest {
1251 public:
1252 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1253
Daniel Chenga542fca2014-10-21 09:51:291254 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201255 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1256 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351257 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201258 }
1259
1260 bool IsBackgroundModeSuspended() {
1261 return static_cast<FakeBackgroundModeManager*>(
1262 g_browser_process->background_mode_manager())
1263 ->IsBackgroundModeSuspended();
1264 }
1265
1266 private:
1267 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1268};
1269
1270// Check that background mode is suspended when closing all browsers unless we
1271// are quitting and that background mode is resumed when a new browser window is
1272// opened.
1273IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1274 CloseAllBrowsersWithBackgroundMode) {
1275 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351276 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201277 Profile* profile = browser()->profile();
1278 {
1279 RepeatedNotificationObserver close_observer(
1280 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381281 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1282 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201283 chrome::CloseAllBrowsers();
1284 close_observer.Wait();
1285 }
1286 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371287 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201288 EXPECT_TRUE(IsBackgroundModeSuspended());
1289
1290 // Background mode should be resumed when a new browser window is opened.
1291 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441292 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201293 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381294 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201295 EXPECT_FALSE(IsBackgroundModeSuspended());
1296 RepeatedNotificationObserver close_observer(
1297 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1298
1299 // Background mode should not be suspended when quitting.
1300 chrome::CloseAllBrowsersAndQuit();
1301 close_observer.Wait();
1302 EXPECT_TRUE(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());
[email protected]0c95faf42013-10-28 06:27:201305}
1306
1307// Check that closing the last browser window individually does not affect
1308// background mode.
1309IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161310 DISABLED_CloseSingleBrowserWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201311 RepeatedNotificationObserver close_observer(
1312 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1313 EXPECT_FALSE(IsBackgroundModeSuspended());
1314 browser()->window()->Close();
1315 close_observer.Wait();
1316 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371317 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201318 EXPECT_FALSE(IsBackgroundModeSuspended());
1319}
1320
1321// Check that closing all browsers with no browser windows open suspends
1322// background mode but does not cause Chrome to quit.
1323IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031324 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201325 RepeatedNotificationObserver close_observer(
1326 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1327 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381328 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1329 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201330 browser()->window()->Close();
1331 close_observer.Wait();
1332 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371333 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201334 EXPECT_FALSE(IsBackgroundModeSuspended());
1335
1336 chrome::CloseAllBrowsers();
1337 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371338 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201339 EXPECT_TRUE(IsBackgroundModeSuspended());
1340}
1341
1342INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1343 BrowserCloseManagerWithBackgroundModeBrowserTest,
1344 testing::Bool());