Rename vector icon header files.
[chromium-blink-merge.git] / chrome / browser / lifetime / browser_close_manager_browsertest.cc
blobbbbddd9e32fa90e7c84bc64d349d9d479bbed09d
1 // 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.
5 #include <vector>
7 #include "base/command_line.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "chrome/browser/background/background_mode_manager.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/browser_shutdown.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/defaults.h"
14 #include "chrome/browser/download/chrome_download_manager_delegate.h"
15 #include "chrome/browser/download/download_prefs.h"
16 #include "chrome/browser/download/download_service.h"
17 #include "chrome/browser/download/download_service_factory.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/lifetime/browser_close_manager.h"
20 #include "chrome/browser/net/url_request_mock_util.h"
21 #include "chrome/browser/prefs/session_startup_pref.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_commands.h"
26 #include "chrome/browser/ui/browser_iterator.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/app_modal/javascript_app_modal_dialog.h"
34 #include "components/app_modal/native_app_modal_dialog.h"
35 #include "content/public/browser/browser_context.h"
36 #include "content/public/browser/download_item.h"
37 #include "content/public/browser/download_manager.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/test/download_test_observer.h"
41 #include "content/public/test/test_navigation_observer.h"
42 #include "net/test/embedded_test_server/embedded_test_server.h"
43 #include "net/test/url_request/url_request_mock_http_job.h"
44 #include "net/test/url_request/url_request_slow_download_job.h"
46 #if defined(OS_CHROMEOS)
47 #include "chromeos/chromeos_switches.h"
48 #endif
50 namespace {
52 app_modal::NativeAppModalDialog* GetNextDialog() {
53 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
54 EXPECT_TRUE(dialog->IsJavaScriptModalDialog());
55 app_modal::JavaScriptAppModalDialog* js_dialog =
56 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
57 CHECK(js_dialog->native_dialog());
58 return js_dialog->native_dialog();
61 void AcceptClose() {
62 GetNextDialog()->AcceptAppModalDialog();
65 void CancelClose() {
66 GetNextDialog()->CancelAppModalDialog();
69 class RepeatedNotificationObserver : public content::NotificationObserver {
70 public:
71 explicit RepeatedNotificationObserver(int type, int count)
72 : num_outstanding_(count), running_(false) {
73 registrar_.Add(this, type, content::NotificationService::AllSources());
76 void Observe(int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) override {
79 ASSERT_GT(num_outstanding_, 0);
80 if (!--num_outstanding_ && running_) {
81 content::BrowserThread::PostTask(
82 content::BrowserThread::UI, FROM_HERE, run_loop_.QuitClosure());
86 void Wait() {
87 if (num_outstanding_ <= 0)
88 return;
90 running_ = true;
91 run_loop_.Run();
92 running_ = false;
95 private:
96 int num_outstanding_;
97 content::NotificationRegistrar registrar_;
98 bool running_;
99 base::RunLoop run_loop_;
101 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
104 class TestBrowserCloseManager : public BrowserCloseManager {
105 public:
106 enum UserChoice {
107 USER_CHOICE_USER_CANCELS_CLOSE,
108 USER_CHOICE_USER_ALLOWS_CLOSE,
109 NO_USER_CHOICE
112 static void AttemptClose(UserChoice user_choice) {
113 scoped_refptr<BrowserCloseManager> browser_close_manager =
114 new TestBrowserCloseManager(user_choice);
115 browser_shutdown::SetTryingToQuit(true);
116 browser_close_manager->StartClosingBrowsers();
119 protected:
120 ~TestBrowserCloseManager() override {}
122 void ConfirmCloseWithPendingDownloads(
123 int download_count,
124 const base::Callback<void(bool)>& callback) override {
125 EXPECT_NE(NO_USER_CHOICE, user_choice_);
126 switch (user_choice_) {
127 case NO_USER_CHOICE:
128 case USER_CHOICE_USER_CANCELS_CLOSE: {
129 callback.Run(false);
130 break;
132 case USER_CHOICE_USER_ALLOWS_CLOSE: {
133 callback.Run(true);
134 break;
139 private:
140 explicit TestBrowserCloseManager(UserChoice user_choice)
141 : user_choice_(user_choice) {}
143 UserChoice user_choice_;
145 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
148 class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
149 public:
150 explicit TestDownloadManagerDelegate(Profile* profile)
151 : ChromeDownloadManagerDelegate(profile) {
152 GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1);
154 ~TestDownloadManagerDelegate() override {}
156 bool DetermineDownloadTarget(
157 content::DownloadItem* item,
158 const content::DownloadTargetCallback& callback) override {
159 content::DownloadTargetCallback dangerous_callback =
160 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
161 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
162 item, dangerous_callback);
165 static void SetDangerous(
166 const content::DownloadTargetCallback& callback,
167 const base::FilePath& target_path,
168 content::DownloadItem::TargetDisposition disp,
169 content::DownloadDangerType danger_type,
170 const base::FilePath& intermediate_path) {
171 callback.Run(target_path,
172 disp,
173 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
174 intermediate_path);
178 class FakeBackgroundModeManager : public BackgroundModeManager {
179 public:
180 FakeBackgroundModeManager()
181 : BackgroundModeManager(
182 *base::CommandLine::ForCurrentProcess(),
183 &g_browser_process->profile_manager()->GetProfileInfoCache()),
184 suspended_(false) {}
186 void SuspendBackgroundMode() override {
187 BackgroundModeManager::SuspendBackgroundMode();
188 suspended_ = true;
191 void ResumeBackgroundMode() override {
192 BackgroundModeManager::ResumeBackgroundMode();
193 suspended_ = false;
196 bool IsBackgroundModeSuspended() {
197 return suspended_;
200 private:
201 bool suspended_;
203 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
206 } // namespace
208 class BrowserCloseManagerBrowserTest
209 : public InProcessBrowserTest,
210 public testing::WithParamInterface<bool> {
211 protected:
212 void SetUpOnMainThread() override {
213 InProcessBrowserTest::SetUpOnMainThread();
214 SessionStartupPref::SetStartupPref(
215 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
216 browsers_.push_back(browser());
217 content::BrowserThread::PostTask(
218 content::BrowserThread::IO,
219 FROM_HERE,
220 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
223 void SetUpCommandLine(base::CommandLine* command_line) override {
224 if (GetParam())
225 command_line->AppendSwitch(switches::kEnableFastUnload);
226 #if defined(OS_CHROMEOS)
227 command_line->AppendSwitch(
228 chromeos::switches::kIgnoreUserProfileMappingForTests);
229 #endif
232 void CreateStalledDownload(Browser* browser) {
233 content::DownloadTestObserverInProgress observer(
234 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
235 ui_test_utils::NavigateToURLWithDisposition(
236 browser,
237 GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl),
238 NEW_BACKGROUND_TAB,
239 ui_test_utils::BROWSER_TEST_NONE);
240 observer.WaitForFinished();
241 EXPECT_EQ(
242 1UL,
243 observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS));
246 std::vector<Browser*> browsers_;
249 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
250 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
251 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
252 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
253 RepeatedNotificationObserver cancel_observer(
254 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
255 chrome::CloseAllBrowsersAndQuit();
256 ASSERT_NO_FATAL_FAILURE(CancelClose());
257 cancel_observer.Wait();
258 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
259 EXPECT_EQ(1, browser()->tab_strip_model()->count());
261 RepeatedNotificationObserver close_observer(
262 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
263 chrome::CloseAllBrowsersAndQuit();
264 ASSERT_NO_FATAL_FAILURE(AcceptClose());
265 close_observer.Wait();
266 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
267 EXPECT_TRUE(chrome::BrowserIterator().done());
270 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
271 TestShutdownMoreThanOnce) {
272 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
273 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
274 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
275 RepeatedNotificationObserver cancel_observer(
276 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
277 chrome::CloseAllBrowsersAndQuit();
278 chrome::CloseAllBrowsersAndQuit();
279 ASSERT_NO_FATAL_FAILURE(CancelClose());
280 cancel_observer.Wait();
281 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
282 EXPECT_EQ(1, browser()->tab_strip_model()->count());
284 RepeatedNotificationObserver close_observer(
285 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
286 chrome::CloseAllBrowsersAndQuit();
287 chrome::CloseAllBrowsersAndQuit();
288 ASSERT_NO_FATAL_FAILURE(AcceptClose());
289 close_observer.Wait();
290 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
291 EXPECT_TRUE(chrome::BrowserIterator().done());
294 // Test is flaky on Mac. http://crbug.com/517687
295 #if defined(OS_MACOSX)
296 #define MAYBE_PRE_TestSessionRestore DISABLED_PRE_TestSessionRestore
297 #else
298 #define MAYBE_PRE_TestSessionRestore DISABLED_PRE_TestSessionRestore
299 #endif
300 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
301 MAYBE_PRE_TestSessionRestore) {
302 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
303 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
304 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
305 AddBlankTabAndShow(browser());
306 ASSERT_NO_FATAL_FAILURE(
307 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
308 RepeatedNotificationObserver cancel_observer(
309 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
310 chrome::CloseAllBrowsersAndQuit();
311 ASSERT_NO_FATAL_FAILURE(CancelClose());
312 cancel_observer.Wait();
313 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
315 browser()->tab_strip_model()
316 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
317 content::TestNavigationObserver navigation_observer(
318 browser()->tab_strip_model()->GetActiveWebContents(), 1);
319 ASSERT_NO_FATAL_FAILURE(
320 NavigateToURLWithDisposition(browser(),
321 GURL(chrome::kChromeUIVersionURL),
322 CURRENT_TAB,
323 ui_test_utils::BROWSER_TEST_NONE));
324 ASSERT_NO_FATAL_FAILURE(AcceptClose());
325 navigation_observer.Wait();
327 RepeatedNotificationObserver close_observer(
328 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
329 chrome::CloseAllBrowsersAndQuit();
330 close_observer.Wait();
331 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
332 EXPECT_TRUE(chrome::BrowserIterator().done());
335 // Test that the tab closed after the aborted shutdown attempt is not re-opened
336 // when restoring the session.
337 // Test is flaky on Mac. http://crbug.com/517687
338 #if defined(OS_MACOSX)
339 #define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
340 #else
341 #define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
342 #endif
343 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
344 MAYBE_TestSessionRestore) {
345 // The testing framework launches Chrome with about:blank as args.
346 EXPECT_EQ(2, browser()->tab_strip_model()->count());
347 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
348 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
349 EXPECT_EQ(GURL("about:blank"),
350 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
353 // Test that browser windows are only closed if all browsers are ready to close
354 // and that all beforeunload dialogs are shown again after a cancel.
355 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
356 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
357 browsers_.push_back(CreateBrowser(browser()->profile()));
358 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
359 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
360 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
361 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
363 // Cancel shutdown on the first beforeunload event.
365 RepeatedNotificationObserver cancel_observer(
366 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
367 chrome::CloseAllBrowsersAndQuit();
368 ASSERT_NO_FATAL_FAILURE(CancelClose());
369 cancel_observer.Wait();
371 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
372 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
373 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
375 // Cancel shutdown on the second beforeunload event.
377 RepeatedNotificationObserver cancel_observer(
378 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
379 chrome::CloseAllBrowsersAndQuit();
380 ASSERT_NO_FATAL_FAILURE(AcceptClose());
381 ASSERT_NO_FATAL_FAILURE(CancelClose());
382 cancel_observer.Wait();
384 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
385 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
386 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
388 // Allow shutdown for both beforeunload events.
389 RepeatedNotificationObserver close_observer(
390 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
391 chrome::CloseAllBrowsersAndQuit();
392 ASSERT_NO_FATAL_FAILURE(AcceptClose());
393 ASSERT_NO_FATAL_FAILURE(AcceptClose());
394 close_observer.Wait();
395 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
396 EXPECT_TRUE(chrome::BrowserIterator().done());
399 // Test that tabs in the same window with a beforeunload event that hangs are
400 // treated the same as the user accepting the close, but do not close the tab
401 // early.
402 // Test is flaky on windows, disabled. See http://crbug.com/276366
403 // Test is flaky on Mac. See http://crbug.com/517687.
404 #if defined(OS_WIN) || defined(OS_MACOSX)
405 #define MAYBE_TestHangInBeforeUnloadMultipleTabs \
406 DISABLED_TestHangInBeforeUnloadMultipleTabs
407 #else
408 #define MAYBE_TestHangInBeforeUnloadMultipleTabs \
409 TestHangInBeforeUnloadMultipleTabs
410 #endif
411 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
412 MAYBE_TestHangInBeforeUnloadMultipleTabs) {
413 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
414 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
415 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
416 AddBlankTabAndShow(browsers_[0]);
417 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
418 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
419 AddBlankTabAndShow(browsers_[0]);
420 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
421 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
423 RepeatedNotificationObserver cancel_observer(
424 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
425 chrome::CloseAllBrowsersAndQuit();
426 ASSERT_NO_FATAL_FAILURE(CancelClose());
427 cancel_observer.Wait();
428 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
429 // All tabs should still be open.
430 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
432 RepeatedNotificationObserver close_observer(
433 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
434 chrome::CloseAllBrowsersAndQuit();
435 ASSERT_NO_FATAL_FAILURE(AcceptClose());
436 close_observer.Wait();
437 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
438 EXPECT_TRUE(chrome::BrowserIterator().done());
441 // Test that tabs in different windows with a beforeunload event that hangs are
442 // treated the same as the user accepting the close, but do not close the tab
443 // early.
444 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
445 TestHangInBeforeUnloadMultipleWindows) {
446 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
447 browsers_.push_back(CreateBrowser(browser()->profile()));
448 browsers_.push_back(CreateBrowser(browser()->profile()));
449 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
450 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
451 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
452 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
453 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
454 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
456 RepeatedNotificationObserver cancel_observer(
457 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
458 chrome::CloseAllBrowsersAndQuit();
459 ASSERT_NO_FATAL_FAILURE(CancelClose());
460 cancel_observer.Wait();
461 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
462 // All windows should still be open.
463 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
464 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
465 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
467 RepeatedNotificationObserver close_observer(
468 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
469 chrome::CloseAllBrowsersAndQuit();
470 ASSERT_NO_FATAL_FAILURE(AcceptClose());
471 close_observer.Wait();
472 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
473 EXPECT_TRUE(chrome::BrowserIterator().done());
476 // Test that a window created during shutdown is closed.
477 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
478 TestAddWindowDuringShutdown) {
479 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
480 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
481 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
483 RepeatedNotificationObserver close_observer(
484 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
485 chrome::CloseAllBrowsersAndQuit();
486 browsers_.push_back(CreateBrowser(browser()->profile()));
487 ASSERT_NO_FATAL_FAILURE(AcceptClose());
488 close_observer.Wait();
489 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
490 EXPECT_TRUE(chrome::BrowserIterator().done());
493 // Test that a window created during shutdown with a beforeunload handler can
494 // cancel the shutdown.
495 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
496 TestAddWindowWithBeforeUnloadDuringShutdown) {
497 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
498 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
499 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
501 RepeatedNotificationObserver cancel_observer(
502 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
503 chrome::CloseAllBrowsersAndQuit();
504 browsers_.push_back(CreateBrowser(browser()->profile()));
505 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
506 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
507 ASSERT_NO_FATAL_FAILURE(AcceptClose());
508 ASSERT_NO_FATAL_FAILURE(CancelClose());
509 cancel_observer.Wait();
510 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
511 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
512 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
514 // Allow shutdown for both beforeunload dialogs.
515 RepeatedNotificationObserver close_observer(
516 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
517 chrome::CloseAllBrowsersAndQuit();
518 ASSERT_NO_FATAL_FAILURE(AcceptClose());
519 ASSERT_NO_FATAL_FAILURE(AcceptClose());
520 close_observer.Wait();
521 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
522 EXPECT_TRUE(chrome::BrowserIterator().done());
525 // Test that tabs added during shutdown are closed.
526 // Disabled for being flaky tests: crbug.com/519646
527 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
528 DISABLED_TestAddTabDuringShutdown) {
529 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
530 browsers_.push_back(CreateBrowser(browser()->profile()));
531 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
532 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
533 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
534 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
536 RepeatedNotificationObserver close_observer(
537 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
538 chrome::CloseAllBrowsersAndQuit();
539 ASSERT_NO_FATAL_FAILURE(AcceptClose());
540 AddBlankTabAndShow(browsers_[0]);
541 AddBlankTabAndShow(browsers_[1]);
542 ASSERT_NO_FATAL_FAILURE(AcceptClose());
543 close_observer.Wait();
544 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
545 EXPECT_TRUE(chrome::BrowserIterator().done());
548 // Test that tabs created during shutdown with beforeunload handlers can cancel
549 // the shutdown.
550 // Disabled for being flaky tests: crbug.com/519646
551 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
552 DISABLED_TestAddTabWithBeforeUnloadDuringShutdown) {
553 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
554 browsers_.push_back(CreateBrowser(browser()->profile()));
555 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
556 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
557 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
558 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
559 RepeatedNotificationObserver cancel_observer(
560 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
561 chrome::CloseAllBrowsersAndQuit();
562 ASSERT_NO_FATAL_FAILURE(AcceptClose());
563 AddBlankTabAndShow(browsers_[0]);
564 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
565 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
566 AddBlankTabAndShow(browsers_[1]);
567 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
568 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
569 ASSERT_NO_FATAL_FAILURE(AcceptClose());
570 ASSERT_NO_FATAL_FAILURE(CancelClose());
571 cancel_observer.Wait();
572 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
573 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
574 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
576 RepeatedNotificationObserver close_observer(
577 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
578 chrome::CloseAllBrowsersAndQuit();
579 ASSERT_NO_FATAL_FAILURE(AcceptClose());
580 ASSERT_NO_FATAL_FAILURE(AcceptClose());
581 ASSERT_NO_FATAL_FAILURE(AcceptClose());
582 ASSERT_NO_FATAL_FAILURE(AcceptClose());
584 close_observer.Wait();
585 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
586 EXPECT_TRUE(chrome::BrowserIterator().done());
589 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
590 TestCloseTabDuringShutdown) {
591 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
592 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
593 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
594 RepeatedNotificationObserver cancel_observer(
595 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
596 chrome::CloseAllBrowsersAndQuit();
598 browsers_.push_back(CreateBrowser(browser()->profile()));
599 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
600 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
601 browsers_[1]->tab_strip_model()->CloseAllTabs();
602 ASSERT_NO_FATAL_FAILURE(CancelClose());
603 ASSERT_NO_FATAL_FAILURE(CancelClose());
604 cancel_observer.Wait();
605 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
606 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
607 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
609 RepeatedNotificationObserver close_observer(
610 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
611 chrome::CloseAllBrowsersAndQuit();
612 browsers_[1]->tab_strip_model()->CloseAllTabs();
613 ASSERT_NO_FATAL_FAILURE(AcceptClose());
614 ASSERT_NO_FATAL_FAILURE(AcceptClose());
616 close_observer.Wait();
617 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
618 EXPECT_TRUE(chrome::BrowserIterator().done());
621 // Test is flaky on Windows and Mac. See http://crbug.com/276366.
622 #if defined(OS_WIN) || defined(OS_MACOSX)
623 #define MAYBE_TestOpenAndCloseWindowDuringShutdown \
624 DISABLED_TestOpenAndCloseWindowDuringShutdown
625 #else
626 #define MAYBE_TestOpenAndCloseWindowDuringShutdown \
627 TestOpenAndCloseWindowDuringShutdown
628 #endif
629 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
630 MAYBE_TestOpenAndCloseWindowDuringShutdown) {
631 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
632 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
633 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
634 RepeatedNotificationObserver cancel_observer(
635 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
636 chrome::CloseAllBrowsersAndQuit();
638 browsers_.push_back(CreateBrowser(browser()->profile()));
639 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
640 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
641 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
642 ASSERT_NO_FATAL_FAILURE(CancelClose());
643 ASSERT_NO_FATAL_FAILURE(CancelClose());
644 cancel_observer.Wait();
645 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
646 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
647 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
649 RepeatedNotificationObserver close_observer(
650 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
651 chrome::CloseAllBrowsersAndQuit();
652 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
653 ASSERT_NO_FATAL_FAILURE(AcceptClose());
654 ASSERT_NO_FATAL_FAILURE(AcceptClose());
656 close_observer.Wait();
657 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
658 EXPECT_TRUE(chrome::BrowserIterator().done());
661 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
662 TestCloseWindowDuringShutdown) {
663 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
664 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
665 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
666 browsers_.push_back(CreateBrowser(browser()->profile()));
667 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
668 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
669 RepeatedNotificationObserver cancel_observer(
670 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
671 chrome::CloseAllBrowsersAndQuit();
673 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
674 ASSERT_NO_FATAL_FAILURE(CancelClose());
675 cancel_observer.Wait();
676 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
677 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
678 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
680 RepeatedNotificationObserver close_observer(
681 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
682 chrome::CloseAllBrowsersAndQuit();
683 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
684 ASSERT_NO_FATAL_FAILURE(AcceptClose());
685 ASSERT_NO_FATAL_FAILURE(AcceptClose());
687 close_observer.Wait();
688 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
689 EXPECT_TRUE(chrome::BrowserIterator().done());
692 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
693 BrowserCloseManagerBrowserTest,
694 testing::Bool());
696 class BrowserCloseManagerWithDownloadsBrowserTest :
697 public BrowserCloseManagerBrowserTest {
698 public:
699 BrowserCloseManagerWithDownloadsBrowserTest() {}
700 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
702 void SetUpOnMainThread() override {
703 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
704 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
707 void SetDownloadPathForProfile(Profile* profile) {
708 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
709 download_prefs->SetDownloadPath(download_path());
712 const base::FilePath& download_path() const {
713 return scoped_download_directory_.path();
716 private:
717 base::ScopedTempDir scoped_download_directory_;
720 // Mac has its own in-progress download prompt in app_controller_mac.mm, so
721 // BrowserCloseManager should simply close all browsers. If there are no
722 // browsers, it should not crash.
723 #if defined(OS_MACOSX)
724 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
725 TestWithDownloads) {
726 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
727 SetDownloadPathForProfile(browser()->profile());
728 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
730 RepeatedNotificationObserver close_observer(
731 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
733 TestBrowserCloseManager::AttemptClose(
734 TestBrowserCloseManager::NO_USER_CHOICE);
735 close_observer.Wait();
736 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
737 EXPECT_TRUE(chrome::BrowserIterator().done());
738 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
740 // Attempting to close again should not crash.
741 TestBrowserCloseManager::AttemptClose(
742 TestBrowserCloseManager::NO_USER_CHOICE);
744 #else // defined(OS_MACOSX)
746 // Test shutdown with a DANGEROUS_URL download undecided.
747 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
748 TestWithDangerousUrlDownload) {
749 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
750 SetDownloadPathForProfile(browser()->profile());
752 // Set up the fake delegate that forces the download to be malicious.
753 scoped_ptr<TestDownloadManagerDelegate> test_delegate(
754 new TestDownloadManagerDelegate(browser()->profile()));
755 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
756 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass());
758 // Run a dangerous download, but the user doesn't make a decision.
759 // This .swf normally would be categorized as DANGEROUS_FILE, but
760 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
761 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
762 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
763 content::DownloadTestObserverInterrupted observer(
764 content::BrowserContext::GetDownloadManager(browser()->profile()),
766 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
767 ui_test_utils::NavigateToURLWithDisposition(
768 browser(),
769 GURL(download_url),
770 NEW_BACKGROUND_TAB,
771 ui_test_utils::BROWSER_TEST_NONE);
772 observer.WaitForFinished();
774 // Check that the download manager has the expected state.
775 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
776 browser()->profile())->InProgressCount());
777 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
778 browser()->profile())->NonMaliciousInProgressCount());
780 // Close the browser with no user action.
781 RepeatedNotificationObserver close_observer(
782 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
783 TestBrowserCloseManager::AttemptClose(
784 TestBrowserCloseManager::NO_USER_CHOICE);
785 close_observer.Wait();
786 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
787 EXPECT_TRUE(chrome::BrowserIterator().done());
790 // Test shutdown with a download in progress.
791 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
792 TestWithDownloads) {
793 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
794 SetDownloadPathForProfile(browser()->profile());
795 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
796 content::TestNavigationObserver navigation_observer(
797 browser()->tab_strip_model()->GetActiveWebContents(), 1);
798 TestBrowserCloseManager::AttemptClose(
799 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
800 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
801 navigation_observer.Wait();
802 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
803 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
805 RepeatedNotificationObserver close_observer(
806 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
808 TestBrowserCloseManager::AttemptClose(
809 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
810 close_observer.Wait();
811 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
812 EXPECT_TRUE(chrome::BrowserIterator().done());
813 if (browser_defaults::kBrowserAliveWithNoWindows)
814 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
815 else
816 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
819 // Test shutdown with a download in progress in an off-the-record profile.
820 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
821 TestWithOffTheRecordDownloads) {
822 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
823 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
824 SetDownloadPathForProfile(otr_profile);
825 Browser* otr_browser = CreateBrowser(otr_profile);
827 RepeatedNotificationObserver close_observer(
828 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
829 browser()->window()->Close();
830 close_observer.Wait();
832 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
833 content::TestNavigationObserver navigation_observer(
834 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
835 TestBrowserCloseManager::AttemptClose(
836 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
837 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
838 navigation_observer.Wait();
839 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
840 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
842 RepeatedNotificationObserver close_observer(
843 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
845 TestBrowserCloseManager::AttemptClose(
846 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
847 close_observer.Wait();
848 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
849 EXPECT_TRUE(chrome::BrowserIterator().done());
850 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
853 // Test shutdown with a download in progress from one profile, where the only
854 // open windows are for another profile.
855 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
856 TestWithDownloadsFromDifferentProfiles) {
857 ProfileManager* profile_manager = g_browser_process->profile_manager();
858 base::FilePath path =
859 profile_manager->user_data_dir().AppendASCII("test_profile");
860 if (!base::PathExists(path))
861 ASSERT_TRUE(base::CreateDirectory(path));
862 Profile* other_profile =
863 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
864 profile_manager->RegisterTestingProfile(other_profile, true, false);
865 Browser* other_profile_browser = CreateBrowser(other_profile);
867 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
868 SetDownloadPathForProfile(browser()->profile());
869 SetDownloadPathForProfile(other_profile);
870 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
872 RepeatedNotificationObserver close_observer(
873 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
874 browser()->window()->Close();
875 close_observer.Wait();
878 // When the shutdown is cancelled, the downloads page should be opened in a
879 // browser for that profile. Because there are no browsers for that profile, a
880 // new browser should be opened.
881 ui_test_utils::BrowserAddedObserver new_browser_observer;
882 TestBrowserCloseManager::AttemptClose(
883 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
884 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
885 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
886 EXPECT_EQ(
887 GURL(chrome::kChromeUIDownloadsURL),
888 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
889 EXPECT_EQ(GURL("about:blank"),
890 other_profile_browser->tab_strip_model()->GetActiveWebContents()
891 ->GetURL());
893 RepeatedNotificationObserver close_observer(
894 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
895 TestBrowserCloseManager::AttemptClose(
896 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
897 close_observer.Wait();
898 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
899 EXPECT_TRUE(chrome::BrowserIterator().done());
900 if (browser_defaults::kBrowserAliveWithNoWindows)
901 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
902 else
903 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
906 // Test shutdown with downloads in progress and beforeunload handlers.
907 // Disabled, see http://crbug.com/315754.
908 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
909 DISABLED_TestBeforeUnloadAndDownloads) {
910 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
911 SetDownloadPathForProfile(browser()->profile());
912 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
913 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
914 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
916 content::WindowedNotificationObserver cancel_observer(
917 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
918 content::NotificationService::AllSources());
919 TestBrowserCloseManager::AttemptClose(
920 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
921 ASSERT_NO_FATAL_FAILURE(AcceptClose());
922 cancel_observer.Wait();
923 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
925 RepeatedNotificationObserver close_observer(
926 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
927 TestBrowserCloseManager::AttemptClose(
928 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
929 ASSERT_NO_FATAL_FAILURE(AcceptClose());
930 close_observer.Wait();
931 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
932 EXPECT_TRUE(chrome::BrowserIterator().done());
935 #endif // defined(OS_MACOSX)
937 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
938 BrowserCloseManagerWithDownloadsBrowserTest,
939 testing::Bool());
941 class BrowserCloseManagerWithBackgroundModeBrowserTest
942 : public BrowserCloseManagerBrowserTest {
943 public:
944 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
946 void SetUpOnMainThread() override {
947 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
948 g_browser_process->set_background_mode_manager_for_test(
949 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
952 bool IsBackgroundModeSuspended() {
953 return static_cast<FakeBackgroundModeManager*>(
954 g_browser_process->background_mode_manager())
955 ->IsBackgroundModeSuspended();
958 private:
959 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
962 // Check that background mode is suspended when closing all browsers unless we
963 // are quitting and that background mode is resumed when a new browser window is
964 // opened.
965 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
966 CloseAllBrowsersWithBackgroundMode) {
967 EXPECT_FALSE(IsBackgroundModeSuspended());
968 Profile* profile = browser()->profile();
970 RepeatedNotificationObserver close_observer(
971 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
972 chrome::IncrementKeepAliveCount();
973 chrome::CloseAllBrowsers();
974 close_observer.Wait();
976 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
977 EXPECT_TRUE(chrome::BrowserIterator().done());
978 EXPECT_TRUE(IsBackgroundModeSuspended());
980 // Background mode should be resumed when a new browser window is opened.
981 ui_test_utils::BrowserAddedObserver new_browser_observer;
982 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
983 new_browser_observer.WaitForSingleNewBrowser();
984 chrome::DecrementKeepAliveCount();
985 EXPECT_FALSE(IsBackgroundModeSuspended());
986 RepeatedNotificationObserver close_observer(
987 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
989 // Background mode should not be suspended when quitting.
990 chrome::CloseAllBrowsersAndQuit();
991 close_observer.Wait();
992 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
993 EXPECT_TRUE(chrome::BrowserIterator().done());
994 EXPECT_FALSE(IsBackgroundModeSuspended());
998 // Check that closing the last browser window individually does not affect
999 // background mode.
1000 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1001 CloseSingleBrowserWithBackgroundMode) {
1002 RepeatedNotificationObserver close_observer(
1003 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1004 EXPECT_FALSE(IsBackgroundModeSuspended());
1005 browser()->window()->Close();
1006 close_observer.Wait();
1007 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1008 EXPECT_TRUE(chrome::BrowserIterator().done());
1009 EXPECT_FALSE(IsBackgroundModeSuspended());
1012 // Check that closing all browsers with no browser windows open suspends
1013 // background mode but does not cause Chrome to quit.
1014 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1015 CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
1016 RepeatedNotificationObserver close_observer(
1017 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1018 EXPECT_FALSE(IsBackgroundModeSuspended());
1019 chrome::IncrementKeepAliveCount();
1020 browser()->window()->Close();
1021 close_observer.Wait();
1022 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1023 EXPECT_TRUE(chrome::BrowserIterator().done());
1024 EXPECT_FALSE(IsBackgroundModeSuspended());
1026 chrome::CloseAllBrowsers();
1027 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1028 EXPECT_TRUE(chrome::BrowserIterator().done());
1029 EXPECT_TRUE(IsBackgroundModeSuspended());
1032 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1033 BrowserCloseManagerWithBackgroundModeBrowserTest,
1034 testing::Bool());