Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / lifetime / browser_close_manager_browsertest.cc
blobac98995a264dcfa42564f844f6a3e9262e3e7be1
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 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, PRE_TestSessionRestore) {
295 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
296 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
297 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
298 AddBlankTabAndShow(browser());
299 ASSERT_NO_FATAL_FAILURE(
300 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
301 RepeatedNotificationObserver cancel_observer(
302 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
303 chrome::CloseAllBrowsersAndQuit();
304 ASSERT_NO_FATAL_FAILURE(CancelClose());
305 cancel_observer.Wait();
306 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
308 browser()->tab_strip_model()
309 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
310 content::TestNavigationObserver navigation_observer(
311 browser()->tab_strip_model()->GetActiveWebContents(), 1);
312 ASSERT_NO_FATAL_FAILURE(
313 NavigateToURLWithDisposition(browser(),
314 GURL(chrome::kChromeUIVersionURL),
315 CURRENT_TAB,
316 ui_test_utils::BROWSER_TEST_NONE));
317 ASSERT_NO_FATAL_FAILURE(AcceptClose());
318 navigation_observer.Wait();
320 RepeatedNotificationObserver close_observer(
321 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
322 chrome::CloseAllBrowsersAndQuit();
323 close_observer.Wait();
324 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
325 EXPECT_TRUE(chrome::BrowserIterator().done());
328 // Test that the tab closed after the aborted shutdown attempt is not re-opened
329 // when restoring the session.
330 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSessionRestore) {
331 // The testing framework launches Chrome with about:blank as args.
332 EXPECT_EQ(2, browser()->tab_strip_model()->count());
333 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
334 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
335 EXPECT_EQ(GURL("about:blank"),
336 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
339 // Test that browser windows are only closed if all browsers are ready to close
340 // and that all beforeunload dialogs are shown again after a cancel.
341 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
342 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
343 browsers_.push_back(CreateBrowser(browser()->profile()));
344 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
345 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
346 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
347 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
349 // Cancel shutdown on the first beforeunload event.
351 RepeatedNotificationObserver cancel_observer(
352 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
353 chrome::CloseAllBrowsersAndQuit();
354 ASSERT_NO_FATAL_FAILURE(CancelClose());
355 cancel_observer.Wait();
357 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
358 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
359 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
361 // Cancel shutdown on the second beforeunload event.
363 RepeatedNotificationObserver cancel_observer(
364 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
365 chrome::CloseAllBrowsersAndQuit();
366 ASSERT_NO_FATAL_FAILURE(AcceptClose());
367 ASSERT_NO_FATAL_FAILURE(CancelClose());
368 cancel_observer.Wait();
370 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
371 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
372 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
374 // Allow shutdown for both beforeunload events.
375 RepeatedNotificationObserver close_observer(
376 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
377 chrome::CloseAllBrowsersAndQuit();
378 ASSERT_NO_FATAL_FAILURE(AcceptClose());
379 ASSERT_NO_FATAL_FAILURE(AcceptClose());
380 close_observer.Wait();
381 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
382 EXPECT_TRUE(chrome::BrowserIterator().done());
385 // Test that tabs in the same window with a beforeunload event that hangs are
386 // treated the same as the user accepting the close, but do not close the tab
387 // early.
388 // Test is flaky on windows, disabled. See http://crbug.com/276366
389 #if defined(OS_WIN)
390 #define MAYBE_TestHangInBeforeUnloadMultipleTabs \
391 DISABLED_TestHangInBeforeUnloadMultipleTabs
392 #else
393 #define MAYBE_TestHangInBeforeUnloadMultipleTabs \
394 TestHangInBeforeUnloadMultipleTabs
395 #endif
396 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
397 MAYBE_TestHangInBeforeUnloadMultipleTabs) {
398 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
399 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
400 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
401 AddBlankTabAndShow(browsers_[0]);
402 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
403 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
404 AddBlankTabAndShow(browsers_[0]);
405 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
406 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
408 RepeatedNotificationObserver cancel_observer(
409 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
410 chrome::CloseAllBrowsersAndQuit();
411 ASSERT_NO_FATAL_FAILURE(CancelClose());
412 cancel_observer.Wait();
413 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
414 // All tabs should still be open.
415 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
417 RepeatedNotificationObserver close_observer(
418 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
419 chrome::CloseAllBrowsersAndQuit();
420 ASSERT_NO_FATAL_FAILURE(AcceptClose());
421 close_observer.Wait();
422 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
423 EXPECT_TRUE(chrome::BrowserIterator().done());
426 // Test that tabs in different windows with a beforeunload event that hangs are
427 // treated the same as the user accepting the close, but do not close the tab
428 // early.
429 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
430 TestHangInBeforeUnloadMultipleWindows) {
431 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
432 browsers_.push_back(CreateBrowser(browser()->profile()));
433 browsers_.push_back(CreateBrowser(browser()->profile()));
434 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
435 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
436 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
437 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
438 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
439 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
441 RepeatedNotificationObserver cancel_observer(
442 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
443 chrome::CloseAllBrowsersAndQuit();
444 ASSERT_NO_FATAL_FAILURE(CancelClose());
445 cancel_observer.Wait();
446 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
447 // All windows should still be open.
448 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
449 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
450 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
452 RepeatedNotificationObserver close_observer(
453 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
454 chrome::CloseAllBrowsersAndQuit();
455 ASSERT_NO_FATAL_FAILURE(AcceptClose());
456 close_observer.Wait();
457 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
458 EXPECT_TRUE(chrome::BrowserIterator().done());
461 // Test that a window created during shutdown is closed.
462 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
463 TestAddWindowDuringShutdown) {
464 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
465 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
466 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
468 RepeatedNotificationObserver close_observer(
469 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
470 chrome::CloseAllBrowsersAndQuit();
471 browsers_.push_back(CreateBrowser(browser()->profile()));
472 ASSERT_NO_FATAL_FAILURE(AcceptClose());
473 close_observer.Wait();
474 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
475 EXPECT_TRUE(chrome::BrowserIterator().done());
478 // Test that a window created during shutdown with a beforeunload handler can
479 // cancel the shutdown.
480 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
481 TestAddWindowWithBeforeUnloadDuringShutdown) {
482 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
483 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
484 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
486 RepeatedNotificationObserver cancel_observer(
487 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
488 chrome::CloseAllBrowsersAndQuit();
489 browsers_.push_back(CreateBrowser(browser()->profile()));
490 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
491 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
492 ASSERT_NO_FATAL_FAILURE(AcceptClose());
493 ASSERT_NO_FATAL_FAILURE(CancelClose());
494 cancel_observer.Wait();
495 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
496 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
497 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
499 // Allow shutdown for both beforeunload dialogs.
500 RepeatedNotificationObserver close_observer(
501 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
502 chrome::CloseAllBrowsersAndQuit();
503 ASSERT_NO_FATAL_FAILURE(AcceptClose());
504 ASSERT_NO_FATAL_FAILURE(AcceptClose());
505 close_observer.Wait();
506 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
507 EXPECT_TRUE(chrome::BrowserIterator().done());
510 // Test that tabs added during shutdown are closed.
511 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
512 TestAddTabDuringShutdown) {
513 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
514 browsers_.push_back(CreateBrowser(browser()->profile()));
515 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
516 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
517 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
518 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
520 RepeatedNotificationObserver close_observer(
521 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
522 chrome::CloseAllBrowsersAndQuit();
523 ASSERT_NO_FATAL_FAILURE(AcceptClose());
524 AddBlankTabAndShow(browsers_[0]);
525 AddBlankTabAndShow(browsers_[1]);
526 ASSERT_NO_FATAL_FAILURE(AcceptClose());
527 close_observer.Wait();
528 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
529 EXPECT_TRUE(chrome::BrowserIterator().done());
532 // Test that tabs created during shutdown with beforeunload handlers can cancel
533 // the shutdown.
534 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
535 TestAddTabWithBeforeUnloadDuringShutdown) {
536 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
537 browsers_.push_back(CreateBrowser(browser()->profile()));
538 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
539 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
540 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
541 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
542 RepeatedNotificationObserver cancel_observer(
543 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
544 chrome::CloseAllBrowsersAndQuit();
545 ASSERT_NO_FATAL_FAILURE(AcceptClose());
546 AddBlankTabAndShow(browsers_[0]);
547 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
548 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
549 AddBlankTabAndShow(browsers_[1]);
550 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
551 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
552 ASSERT_NO_FATAL_FAILURE(AcceptClose());
553 ASSERT_NO_FATAL_FAILURE(CancelClose());
554 cancel_observer.Wait();
555 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
556 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
557 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
559 RepeatedNotificationObserver close_observer(
560 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
561 chrome::CloseAllBrowsersAndQuit();
562 ASSERT_NO_FATAL_FAILURE(AcceptClose());
563 ASSERT_NO_FATAL_FAILURE(AcceptClose());
564 ASSERT_NO_FATAL_FAILURE(AcceptClose());
565 ASSERT_NO_FATAL_FAILURE(AcceptClose());
567 close_observer.Wait();
568 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
569 EXPECT_TRUE(chrome::BrowserIterator().done());
572 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
573 TestCloseTabDuringShutdown) {
574 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
575 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
576 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
577 RepeatedNotificationObserver cancel_observer(
578 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
579 chrome::CloseAllBrowsersAndQuit();
581 browsers_.push_back(CreateBrowser(browser()->profile()));
582 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
583 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
584 browsers_[1]->tab_strip_model()->CloseAllTabs();
585 ASSERT_NO_FATAL_FAILURE(CancelClose());
586 ASSERT_NO_FATAL_FAILURE(CancelClose());
587 cancel_observer.Wait();
588 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
589 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
590 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
592 RepeatedNotificationObserver close_observer(
593 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
594 chrome::CloseAllBrowsersAndQuit();
595 browsers_[1]->tab_strip_model()->CloseAllTabs();
596 ASSERT_NO_FATAL_FAILURE(AcceptClose());
597 ASSERT_NO_FATAL_FAILURE(AcceptClose());
599 close_observer.Wait();
600 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
601 EXPECT_TRUE(chrome::BrowserIterator().done());
604 // Test is flaky on Windows and Mac. See http://crbug.com/276366.
605 #if defined(OS_WIN) || defined(OS_MACOSX)
606 #define MAYBE_TestOpenAndCloseWindowDuringShutdown \
607 DISABLED_TestOpenAndCloseWindowDuringShutdown
608 #else
609 #define MAYBE_TestOpenAndCloseWindowDuringShutdown \
610 TestOpenAndCloseWindowDuringShutdown
611 #endif
612 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
613 MAYBE_TestOpenAndCloseWindowDuringShutdown) {
614 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
615 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
616 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
617 RepeatedNotificationObserver cancel_observer(
618 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
619 chrome::CloseAllBrowsersAndQuit();
621 browsers_.push_back(CreateBrowser(browser()->profile()));
622 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
623 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
624 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
625 ASSERT_NO_FATAL_FAILURE(CancelClose());
626 ASSERT_NO_FATAL_FAILURE(CancelClose());
627 cancel_observer.Wait();
628 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
629 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
630 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
632 RepeatedNotificationObserver close_observer(
633 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
634 chrome::CloseAllBrowsersAndQuit();
635 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
636 ASSERT_NO_FATAL_FAILURE(AcceptClose());
637 ASSERT_NO_FATAL_FAILURE(AcceptClose());
639 close_observer.Wait();
640 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
641 EXPECT_TRUE(chrome::BrowserIterator().done());
644 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
645 TestCloseWindowDuringShutdown) {
646 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
647 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
648 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
649 browsers_.push_back(CreateBrowser(browser()->profile()));
650 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
651 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
652 RepeatedNotificationObserver cancel_observer(
653 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
654 chrome::CloseAllBrowsersAndQuit();
656 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
657 ASSERT_NO_FATAL_FAILURE(CancelClose());
658 cancel_observer.Wait();
659 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
660 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
661 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
663 RepeatedNotificationObserver close_observer(
664 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
665 chrome::CloseAllBrowsersAndQuit();
666 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
667 ASSERT_NO_FATAL_FAILURE(AcceptClose());
668 ASSERT_NO_FATAL_FAILURE(AcceptClose());
670 close_observer.Wait();
671 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
672 EXPECT_TRUE(chrome::BrowserIterator().done());
675 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
676 BrowserCloseManagerBrowserTest,
677 testing::Bool());
679 class BrowserCloseManagerWithDownloadsBrowserTest :
680 public BrowserCloseManagerBrowserTest {
681 public:
682 BrowserCloseManagerWithDownloadsBrowserTest() {}
683 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
685 void SetUpOnMainThread() override {
686 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
687 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
690 void SetDownloadPathForProfile(Profile* profile) {
691 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
692 download_prefs->SetDownloadPath(download_path());
695 const base::FilePath& download_path() const {
696 return scoped_download_directory_.path();
699 private:
700 base::ScopedTempDir scoped_download_directory_;
703 // Mac has its own in-progress download prompt in app_controller_mac.mm, so
704 // BrowserCloseManager should simply close all browsers. If there are no
705 // browsers, it should not crash.
706 #if defined(OS_MACOSX)
707 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
708 TestWithDownloads) {
709 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
710 SetDownloadPathForProfile(browser()->profile());
711 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
713 RepeatedNotificationObserver close_observer(
714 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
716 TestBrowserCloseManager::AttemptClose(
717 TestBrowserCloseManager::NO_USER_CHOICE);
718 close_observer.Wait();
719 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
720 EXPECT_TRUE(chrome::BrowserIterator().done());
721 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
723 // Attempting to close again should not crash.
724 TestBrowserCloseManager::AttemptClose(
725 TestBrowserCloseManager::NO_USER_CHOICE);
727 #else // defined(OS_MACOSX)
729 // Test shutdown with a DANGEROUS_URL download undecided.
730 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
731 TestWithDangerousUrlDownload) {
732 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
733 SetDownloadPathForProfile(browser()->profile());
735 // Set up the fake delegate that forces the download to be malicious.
736 scoped_ptr<TestDownloadManagerDelegate> test_delegate(
737 new TestDownloadManagerDelegate(browser()->profile()));
738 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
739 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass());
741 // Run a dangerous download, but the user doesn't make a decision.
742 // This .swf normally would be categorized as DANGEROUS_FILE, but
743 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
744 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
745 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
746 content::DownloadTestObserverInterrupted observer(
747 content::BrowserContext::GetDownloadManager(browser()->profile()),
749 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
750 ui_test_utils::NavigateToURLWithDisposition(
751 browser(),
752 GURL(download_url),
753 NEW_BACKGROUND_TAB,
754 ui_test_utils::BROWSER_TEST_NONE);
755 observer.WaitForFinished();
757 // Check that the download manager has the expected state.
758 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
759 browser()->profile())->InProgressCount());
760 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
761 browser()->profile())->NonMaliciousInProgressCount());
763 // Close the browser with no user action.
764 RepeatedNotificationObserver close_observer(
765 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
766 TestBrowserCloseManager::AttemptClose(
767 TestBrowserCloseManager::NO_USER_CHOICE);
768 close_observer.Wait();
769 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
770 EXPECT_TRUE(chrome::BrowserIterator().done());
773 // Test shutdown with a download in progress.
774 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
775 TestWithDownloads) {
776 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
777 SetDownloadPathForProfile(browser()->profile());
778 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
779 content::TestNavigationObserver navigation_observer(
780 browser()->tab_strip_model()->GetActiveWebContents(), 1);
781 TestBrowserCloseManager::AttemptClose(
782 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
783 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
784 navigation_observer.Wait();
785 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
786 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
788 RepeatedNotificationObserver close_observer(
789 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
791 TestBrowserCloseManager::AttemptClose(
792 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
793 close_observer.Wait();
794 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
795 EXPECT_TRUE(chrome::BrowserIterator().done());
796 if (browser_defaults::kBrowserAliveWithNoWindows)
797 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
798 else
799 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
802 // Test shutdown with a download in progress in an off-the-record profile.
803 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
804 TestWithOffTheRecordDownloads) {
805 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
806 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
807 SetDownloadPathForProfile(otr_profile);
808 Browser* otr_browser = CreateBrowser(otr_profile);
810 RepeatedNotificationObserver close_observer(
811 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
812 browser()->window()->Close();
813 close_observer.Wait();
815 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
816 content::TestNavigationObserver navigation_observer(
817 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
818 TestBrowserCloseManager::AttemptClose(
819 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
820 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
821 navigation_observer.Wait();
822 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
823 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
825 RepeatedNotificationObserver close_observer(
826 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
828 TestBrowserCloseManager::AttemptClose(
829 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
830 close_observer.Wait();
831 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
832 EXPECT_TRUE(chrome::BrowserIterator().done());
833 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
836 // Test shutdown with a download in progress from one profile, where the only
837 // open windows are for another profile.
838 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
839 TestWithDownloadsFromDifferentProfiles) {
840 ProfileManager* profile_manager = g_browser_process->profile_manager();
841 base::FilePath path =
842 profile_manager->user_data_dir().AppendASCII("test_profile");
843 if (!base::PathExists(path))
844 ASSERT_TRUE(base::CreateDirectory(path));
845 Profile* other_profile =
846 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
847 profile_manager->RegisterTestingProfile(other_profile, true, false);
848 Browser* other_profile_browser = CreateBrowser(other_profile);
850 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
851 SetDownloadPathForProfile(browser()->profile());
852 SetDownloadPathForProfile(other_profile);
853 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
855 RepeatedNotificationObserver close_observer(
856 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
857 browser()->window()->Close();
858 close_observer.Wait();
861 // When the shutdown is cancelled, the downloads page should be opened in a
862 // browser for that profile. Because there are no browsers for that profile, a
863 // new browser should be opened.
864 ui_test_utils::BrowserAddedObserver new_browser_observer;
865 TestBrowserCloseManager::AttemptClose(
866 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
867 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
868 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
869 EXPECT_EQ(
870 GURL(chrome::kChromeUIDownloadsURL),
871 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
872 EXPECT_EQ(GURL("about:blank"),
873 other_profile_browser->tab_strip_model()->GetActiveWebContents()
874 ->GetURL());
876 RepeatedNotificationObserver close_observer(
877 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
878 TestBrowserCloseManager::AttemptClose(
879 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
880 close_observer.Wait();
881 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
882 EXPECT_TRUE(chrome::BrowserIterator().done());
883 if (browser_defaults::kBrowserAliveWithNoWindows)
884 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
885 else
886 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
889 // Test shutdown with downloads in progress and beforeunload handlers.
890 // Disabled, see http://crbug.com/315754.
891 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
892 DISABLED_TestBeforeUnloadAndDownloads) {
893 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
894 SetDownloadPathForProfile(browser()->profile());
895 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
896 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
897 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
899 content::WindowedNotificationObserver cancel_observer(
900 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
901 content::NotificationService::AllSources());
902 TestBrowserCloseManager::AttemptClose(
903 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
904 ASSERT_NO_FATAL_FAILURE(AcceptClose());
905 cancel_observer.Wait();
906 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
908 RepeatedNotificationObserver close_observer(
909 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
910 TestBrowserCloseManager::AttemptClose(
911 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
912 ASSERT_NO_FATAL_FAILURE(AcceptClose());
913 close_observer.Wait();
914 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
915 EXPECT_TRUE(chrome::BrowserIterator().done());
918 #endif // defined(OS_MACOSX)
920 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
921 BrowserCloseManagerWithDownloadsBrowserTest,
922 testing::Bool());
924 class BrowserCloseManagerWithBackgroundModeBrowserTest
925 : public BrowserCloseManagerBrowserTest {
926 public:
927 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
929 void SetUpOnMainThread() override {
930 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
931 g_browser_process->set_background_mode_manager_for_test(
932 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
935 bool IsBackgroundModeSuspended() {
936 return static_cast<FakeBackgroundModeManager*>(
937 g_browser_process->background_mode_manager())
938 ->IsBackgroundModeSuspended();
941 private:
942 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
945 // Check that background mode is suspended when closing all browsers unless we
946 // are quitting and that background mode is resumed when a new browser window is
947 // opened.
948 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
949 CloseAllBrowsersWithBackgroundMode) {
950 EXPECT_FALSE(IsBackgroundModeSuspended());
951 Profile* profile = browser()->profile();
953 RepeatedNotificationObserver close_observer(
954 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
955 chrome::IncrementKeepAliveCount();
956 chrome::CloseAllBrowsers();
957 close_observer.Wait();
959 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
960 EXPECT_TRUE(chrome::BrowserIterator().done());
961 EXPECT_TRUE(IsBackgroundModeSuspended());
963 // Background mode should be resumed when a new browser window is opened.
964 ui_test_utils::BrowserAddedObserver new_browser_observer;
965 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
966 new_browser_observer.WaitForSingleNewBrowser();
967 chrome::DecrementKeepAliveCount();
968 EXPECT_FALSE(IsBackgroundModeSuspended());
969 RepeatedNotificationObserver close_observer(
970 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
972 // Background mode should not be suspended when quitting.
973 chrome::CloseAllBrowsersAndQuit();
974 close_observer.Wait();
975 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
976 EXPECT_TRUE(chrome::BrowserIterator().done());
977 EXPECT_FALSE(IsBackgroundModeSuspended());
981 // Check that closing the last browser window individually does not affect
982 // background mode.
983 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
984 CloseSingleBrowserWithBackgroundMode) {
985 RepeatedNotificationObserver close_observer(
986 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
987 EXPECT_FALSE(IsBackgroundModeSuspended());
988 browser()->window()->Close();
989 close_observer.Wait();
990 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
991 EXPECT_TRUE(chrome::BrowserIterator().done());
992 EXPECT_FALSE(IsBackgroundModeSuspended());
995 // Check that closing all browsers with no browser windows open suspends
996 // background mode but does not cause Chrome to quit.
997 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
998 CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
999 RepeatedNotificationObserver close_observer(
1000 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1001 EXPECT_FALSE(IsBackgroundModeSuspended());
1002 chrome::IncrementKeepAliveCount();
1003 browser()->window()->Close();
1004 close_observer.Wait();
1005 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1006 EXPECT_TRUE(chrome::BrowserIterator().done());
1007 EXPECT_FALSE(IsBackgroundModeSuspended());
1009 chrome::CloseAllBrowsers();
1010 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1011 EXPECT_TRUE(chrome::BrowserIterator().done());
1012 EXPECT_TRUE(IsBackgroundModeSuspended());
1015 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1016 BrowserCloseManagerWithBackgroundModeBrowserTest,
1017 testing::Bool());