Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / lifetime / browser_close_manager_browsertest.cc
blob727be0799a36a05ce4bca645b8a9757ca2086a05
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 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
527 TestAddTabDuringShutdown) {
528 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
529 browsers_.push_back(CreateBrowser(browser()->profile()));
530 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
531 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
532 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
533 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
535 RepeatedNotificationObserver close_observer(
536 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
537 chrome::CloseAllBrowsersAndQuit();
538 ASSERT_NO_FATAL_FAILURE(AcceptClose());
539 AddBlankTabAndShow(browsers_[0]);
540 AddBlankTabAndShow(browsers_[1]);
541 ASSERT_NO_FATAL_FAILURE(AcceptClose());
542 close_observer.Wait();
543 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
544 EXPECT_TRUE(chrome::BrowserIterator().done());
547 // Test that tabs created during shutdown with beforeunload handlers can cancel
548 // the shutdown.
549 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
550 TestAddTabWithBeforeUnloadDuringShutdown) {
551 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
552 browsers_.push_back(CreateBrowser(browser()->profile()));
553 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
554 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
555 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
556 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
557 RepeatedNotificationObserver cancel_observer(
558 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
559 chrome::CloseAllBrowsersAndQuit();
560 ASSERT_NO_FATAL_FAILURE(AcceptClose());
561 AddBlankTabAndShow(browsers_[0]);
562 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
563 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
564 AddBlankTabAndShow(browsers_[1]);
565 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
566 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
567 ASSERT_NO_FATAL_FAILURE(AcceptClose());
568 ASSERT_NO_FATAL_FAILURE(CancelClose());
569 cancel_observer.Wait();
570 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
571 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
572 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
574 RepeatedNotificationObserver close_observer(
575 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
576 chrome::CloseAllBrowsersAndQuit();
577 ASSERT_NO_FATAL_FAILURE(AcceptClose());
578 ASSERT_NO_FATAL_FAILURE(AcceptClose());
579 ASSERT_NO_FATAL_FAILURE(AcceptClose());
580 ASSERT_NO_FATAL_FAILURE(AcceptClose());
582 close_observer.Wait();
583 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
584 EXPECT_TRUE(chrome::BrowserIterator().done());
587 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
588 TestCloseTabDuringShutdown) {
589 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
590 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
591 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
592 RepeatedNotificationObserver cancel_observer(
593 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
594 chrome::CloseAllBrowsersAndQuit();
596 browsers_.push_back(CreateBrowser(browser()->profile()));
597 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
598 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
599 browsers_[1]->tab_strip_model()->CloseAllTabs();
600 ASSERT_NO_FATAL_FAILURE(CancelClose());
601 ASSERT_NO_FATAL_FAILURE(CancelClose());
602 cancel_observer.Wait();
603 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
604 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
605 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
607 RepeatedNotificationObserver close_observer(
608 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
609 chrome::CloseAllBrowsersAndQuit();
610 browsers_[1]->tab_strip_model()->CloseAllTabs();
611 ASSERT_NO_FATAL_FAILURE(AcceptClose());
612 ASSERT_NO_FATAL_FAILURE(AcceptClose());
614 close_observer.Wait();
615 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
616 EXPECT_TRUE(chrome::BrowserIterator().done());
619 // Test is flaky on Windows and Mac. See http://crbug.com/276366.
620 #if defined(OS_WIN) || defined(OS_MACOSX)
621 #define MAYBE_TestOpenAndCloseWindowDuringShutdown \
622 DISABLED_TestOpenAndCloseWindowDuringShutdown
623 #else
624 #define MAYBE_TestOpenAndCloseWindowDuringShutdown \
625 TestOpenAndCloseWindowDuringShutdown
626 #endif
627 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
628 MAYBE_TestOpenAndCloseWindowDuringShutdown) {
629 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
630 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
631 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
632 RepeatedNotificationObserver cancel_observer(
633 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
634 chrome::CloseAllBrowsersAndQuit();
636 browsers_.push_back(CreateBrowser(browser()->profile()));
637 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
638 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
639 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
640 ASSERT_NO_FATAL_FAILURE(CancelClose());
641 ASSERT_NO_FATAL_FAILURE(CancelClose());
642 cancel_observer.Wait();
643 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
644 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
645 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
647 RepeatedNotificationObserver close_observer(
648 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
649 chrome::CloseAllBrowsersAndQuit();
650 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
651 ASSERT_NO_FATAL_FAILURE(AcceptClose());
652 ASSERT_NO_FATAL_FAILURE(AcceptClose());
654 close_observer.Wait();
655 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
656 EXPECT_TRUE(chrome::BrowserIterator().done());
659 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
660 TestCloseWindowDuringShutdown) {
661 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
662 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
663 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
664 browsers_.push_back(CreateBrowser(browser()->profile()));
665 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
666 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
667 RepeatedNotificationObserver cancel_observer(
668 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
669 chrome::CloseAllBrowsersAndQuit();
671 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
672 ASSERT_NO_FATAL_FAILURE(CancelClose());
673 cancel_observer.Wait();
674 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
675 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
676 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
678 RepeatedNotificationObserver close_observer(
679 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
680 chrome::CloseAllBrowsersAndQuit();
681 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
682 ASSERT_NO_FATAL_FAILURE(AcceptClose());
683 ASSERT_NO_FATAL_FAILURE(AcceptClose());
685 close_observer.Wait();
686 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
687 EXPECT_TRUE(chrome::BrowserIterator().done());
690 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
691 BrowserCloseManagerBrowserTest,
692 testing::Bool());
694 class BrowserCloseManagerWithDownloadsBrowserTest :
695 public BrowserCloseManagerBrowserTest {
696 public:
697 BrowserCloseManagerWithDownloadsBrowserTest() {}
698 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
700 void SetUpOnMainThread() override {
701 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
702 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
705 void SetDownloadPathForProfile(Profile* profile) {
706 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
707 download_prefs->SetDownloadPath(download_path());
710 const base::FilePath& download_path() const {
711 return scoped_download_directory_.path();
714 private:
715 base::ScopedTempDir scoped_download_directory_;
718 // Mac has its own in-progress download prompt in app_controller_mac.mm, so
719 // BrowserCloseManager should simply close all browsers. If there are no
720 // browsers, it should not crash.
721 #if defined(OS_MACOSX)
722 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
723 TestWithDownloads) {
724 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
725 SetDownloadPathForProfile(browser()->profile());
726 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
728 RepeatedNotificationObserver close_observer(
729 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
731 TestBrowserCloseManager::AttemptClose(
732 TestBrowserCloseManager::NO_USER_CHOICE);
733 close_observer.Wait();
734 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
735 EXPECT_TRUE(chrome::BrowserIterator().done());
736 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
738 // Attempting to close again should not crash.
739 TestBrowserCloseManager::AttemptClose(
740 TestBrowserCloseManager::NO_USER_CHOICE);
742 #else // defined(OS_MACOSX)
744 // Test shutdown with a DANGEROUS_URL download undecided.
745 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
746 TestWithDangerousUrlDownload) {
747 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
748 SetDownloadPathForProfile(browser()->profile());
750 // Set up the fake delegate that forces the download to be malicious.
751 scoped_ptr<TestDownloadManagerDelegate> test_delegate(
752 new TestDownloadManagerDelegate(browser()->profile()));
753 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
754 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass());
756 // Run a dangerous download, but the user doesn't make a decision.
757 // This .swf normally would be categorized as DANGEROUS_FILE, but
758 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
759 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
760 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
761 content::DownloadTestObserverInterrupted observer(
762 content::BrowserContext::GetDownloadManager(browser()->profile()),
764 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
765 ui_test_utils::NavigateToURLWithDisposition(
766 browser(),
767 GURL(download_url),
768 NEW_BACKGROUND_TAB,
769 ui_test_utils::BROWSER_TEST_NONE);
770 observer.WaitForFinished();
772 // Check that the download manager has the expected state.
773 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
774 browser()->profile())->InProgressCount());
775 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
776 browser()->profile())->NonMaliciousInProgressCount());
778 // Close the browser with no user action.
779 RepeatedNotificationObserver close_observer(
780 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
781 TestBrowserCloseManager::AttemptClose(
782 TestBrowserCloseManager::NO_USER_CHOICE);
783 close_observer.Wait();
784 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
785 EXPECT_TRUE(chrome::BrowserIterator().done());
788 // Test shutdown with a download in progress.
789 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
790 TestWithDownloads) {
791 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
792 SetDownloadPathForProfile(browser()->profile());
793 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
794 content::TestNavigationObserver navigation_observer(
795 browser()->tab_strip_model()->GetActiveWebContents(), 1);
796 TestBrowserCloseManager::AttemptClose(
797 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
798 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
799 navigation_observer.Wait();
800 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
801 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
803 RepeatedNotificationObserver close_observer(
804 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
806 TestBrowserCloseManager::AttemptClose(
807 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
808 close_observer.Wait();
809 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
810 EXPECT_TRUE(chrome::BrowserIterator().done());
811 if (browser_defaults::kBrowserAliveWithNoWindows)
812 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
813 else
814 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
817 // Test shutdown with a download in progress in an off-the-record profile.
818 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
819 TestWithOffTheRecordDownloads) {
820 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
821 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
822 SetDownloadPathForProfile(otr_profile);
823 Browser* otr_browser = CreateBrowser(otr_profile);
825 RepeatedNotificationObserver close_observer(
826 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
827 browser()->window()->Close();
828 close_observer.Wait();
830 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
831 content::TestNavigationObserver navigation_observer(
832 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
833 TestBrowserCloseManager::AttemptClose(
834 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
835 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
836 navigation_observer.Wait();
837 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
838 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
840 RepeatedNotificationObserver close_observer(
841 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
843 TestBrowserCloseManager::AttemptClose(
844 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
845 close_observer.Wait();
846 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
847 EXPECT_TRUE(chrome::BrowserIterator().done());
848 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
851 // Test shutdown with a download in progress from one profile, where the only
852 // open windows are for another profile.
853 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
854 TestWithDownloadsFromDifferentProfiles) {
855 ProfileManager* profile_manager = g_browser_process->profile_manager();
856 base::FilePath path =
857 profile_manager->user_data_dir().AppendASCII("test_profile");
858 if (!base::PathExists(path))
859 ASSERT_TRUE(base::CreateDirectory(path));
860 Profile* other_profile =
861 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
862 profile_manager->RegisterTestingProfile(other_profile, true, false);
863 Browser* other_profile_browser = CreateBrowser(other_profile);
865 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
866 SetDownloadPathForProfile(browser()->profile());
867 SetDownloadPathForProfile(other_profile);
868 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
870 RepeatedNotificationObserver close_observer(
871 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
872 browser()->window()->Close();
873 close_observer.Wait();
876 // When the shutdown is cancelled, the downloads page should be opened in a
877 // browser for that profile. Because there are no browsers for that profile, a
878 // new browser should be opened.
879 ui_test_utils::BrowserAddedObserver new_browser_observer;
880 TestBrowserCloseManager::AttemptClose(
881 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
882 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
883 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
884 EXPECT_EQ(
885 GURL(chrome::kChromeUIDownloadsURL),
886 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
887 EXPECT_EQ(GURL("about:blank"),
888 other_profile_browser->tab_strip_model()->GetActiveWebContents()
889 ->GetURL());
891 RepeatedNotificationObserver close_observer(
892 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
893 TestBrowserCloseManager::AttemptClose(
894 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
895 close_observer.Wait();
896 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
897 EXPECT_TRUE(chrome::BrowserIterator().done());
898 if (browser_defaults::kBrowserAliveWithNoWindows)
899 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
900 else
901 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
904 // Test shutdown with downloads in progress and beforeunload handlers.
905 // Disabled, see http://crbug.com/315754.
906 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
907 DISABLED_TestBeforeUnloadAndDownloads) {
908 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
909 SetDownloadPathForProfile(browser()->profile());
910 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
911 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
912 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
914 content::WindowedNotificationObserver cancel_observer(
915 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
916 content::NotificationService::AllSources());
917 TestBrowserCloseManager::AttemptClose(
918 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
919 ASSERT_NO_FATAL_FAILURE(AcceptClose());
920 cancel_observer.Wait();
921 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
923 RepeatedNotificationObserver close_observer(
924 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
925 TestBrowserCloseManager::AttemptClose(
926 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
927 ASSERT_NO_FATAL_FAILURE(AcceptClose());
928 close_observer.Wait();
929 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
930 EXPECT_TRUE(chrome::BrowserIterator().done());
933 #endif // defined(OS_MACOSX)
935 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
936 BrowserCloseManagerWithDownloadsBrowserTest,
937 testing::Bool());
939 class BrowserCloseManagerWithBackgroundModeBrowserTest
940 : public BrowserCloseManagerBrowserTest {
941 public:
942 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
944 void SetUpOnMainThread() override {
945 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
946 g_browser_process->set_background_mode_manager_for_test(
947 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
950 bool IsBackgroundModeSuspended() {
951 return static_cast<FakeBackgroundModeManager*>(
952 g_browser_process->background_mode_manager())
953 ->IsBackgroundModeSuspended();
956 private:
957 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
960 // Check that background mode is suspended when closing all browsers unless we
961 // are quitting and that background mode is resumed when a new browser window is
962 // opened.
963 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
964 CloseAllBrowsersWithBackgroundMode) {
965 EXPECT_FALSE(IsBackgroundModeSuspended());
966 Profile* profile = browser()->profile();
968 RepeatedNotificationObserver close_observer(
969 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
970 chrome::IncrementKeepAliveCount();
971 chrome::CloseAllBrowsers();
972 close_observer.Wait();
974 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
975 EXPECT_TRUE(chrome::BrowserIterator().done());
976 EXPECT_TRUE(IsBackgroundModeSuspended());
978 // Background mode should be resumed when a new browser window is opened.
979 ui_test_utils::BrowserAddedObserver new_browser_observer;
980 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
981 new_browser_observer.WaitForSingleNewBrowser();
982 chrome::DecrementKeepAliveCount();
983 EXPECT_FALSE(IsBackgroundModeSuspended());
984 RepeatedNotificationObserver close_observer(
985 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
987 // Background mode should not be suspended when quitting.
988 chrome::CloseAllBrowsersAndQuit();
989 close_observer.Wait();
990 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
991 EXPECT_TRUE(chrome::BrowserIterator().done());
992 EXPECT_FALSE(IsBackgroundModeSuspended());
996 // Check that closing the last browser window individually does not affect
997 // background mode.
998 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
999 CloseSingleBrowserWithBackgroundMode) {
1000 RepeatedNotificationObserver close_observer(
1001 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1002 EXPECT_FALSE(IsBackgroundModeSuspended());
1003 browser()->window()->Close();
1004 close_observer.Wait();
1005 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1006 EXPECT_TRUE(chrome::BrowserIterator().done());
1007 EXPECT_FALSE(IsBackgroundModeSuspended());
1010 // Check that closing all browsers with no browser windows open suspends
1011 // background mode but does not cause Chrome to quit.
1012 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1013 CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
1014 RepeatedNotificationObserver close_observer(
1015 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1016 EXPECT_FALSE(IsBackgroundModeSuspended());
1017 chrome::IncrementKeepAliveCount();
1018 browser()->window()->Close();
1019 close_observer.Wait();
1020 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1021 EXPECT_TRUE(chrome::BrowserIterator().done());
1022 EXPECT_FALSE(IsBackgroundModeSuspended());
1024 chrome::CloseAllBrowsers();
1025 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1026 EXPECT_TRUE(chrome::BrowserIterator().done());
1027 EXPECT_TRUE(IsBackgroundModeSuspended());
1030 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1031 BrowserCloseManagerWithBackgroundModeBrowserTest,
1032 testing::Bool());