Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / lifetime / browser_close_manager_browsertest.cc
blob0eab958f094ee2fa911eb3588012cdc5f09d11cb
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 // Disable new downloads UI as it is very very slow. https://crbug.com/526577
708 // TODO(dbeam): remove this once the downloads UI is not slow.
709 void SetUpCommandLine(base::CommandLine* command_line) override {
710 BrowserCloseManagerBrowserTest::SetUpCommandLine(command_line);
711 command_line->AppendSwitch(switches::kDisableMaterialDesignDownloads);
714 void SetDownloadPathForProfile(Profile* profile) {
715 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
716 download_prefs->SetDownloadPath(download_path());
719 const base::FilePath& download_path() const {
720 return scoped_download_directory_.path();
723 private:
724 base::ScopedTempDir scoped_download_directory_;
727 // Mac has its own in-progress download prompt in app_controller_mac.mm, so
728 // BrowserCloseManager should simply close all browsers. If there are no
729 // browsers, it should not crash.
730 #if defined(OS_MACOSX)
731 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
732 TestWithDownloads) {
733 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
734 SetDownloadPathForProfile(browser()->profile());
735 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
737 RepeatedNotificationObserver close_observer(
738 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
740 TestBrowserCloseManager::AttemptClose(
741 TestBrowserCloseManager::NO_USER_CHOICE);
742 close_observer.Wait();
743 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
744 EXPECT_TRUE(chrome::BrowserIterator().done());
745 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
747 // Attempting to close again should not crash.
748 TestBrowserCloseManager::AttemptClose(
749 TestBrowserCloseManager::NO_USER_CHOICE);
751 #else // defined(OS_MACOSX)
753 // Test shutdown with a DANGEROUS_URL download undecided.
754 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
755 TestWithDangerousUrlDownload) {
756 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
757 SetDownloadPathForProfile(browser()->profile());
759 // Set up the fake delegate that forces the download to be malicious.
760 scoped_ptr<TestDownloadManagerDelegate> test_delegate(
761 new TestDownloadManagerDelegate(browser()->profile()));
762 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
763 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass());
765 // Run a dangerous download, but the user doesn't make a decision.
766 // This .swf normally would be categorized as DANGEROUS_FILE, but
767 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
768 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
769 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
770 content::DownloadTestObserverInterrupted observer(
771 content::BrowserContext::GetDownloadManager(browser()->profile()),
773 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
774 ui_test_utils::NavigateToURLWithDisposition(
775 browser(),
776 GURL(download_url),
777 NEW_BACKGROUND_TAB,
778 ui_test_utils::BROWSER_TEST_NONE);
779 observer.WaitForFinished();
781 // Check that the download manager has the expected state.
782 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
783 browser()->profile())->InProgressCount());
784 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
785 browser()->profile())->NonMaliciousInProgressCount());
787 // Close the browser with no user action.
788 RepeatedNotificationObserver close_observer(
789 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
790 TestBrowserCloseManager::AttemptClose(
791 TestBrowserCloseManager::NO_USER_CHOICE);
792 close_observer.Wait();
793 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
794 EXPECT_TRUE(chrome::BrowserIterator().done());
797 // Test shutdown with a download in progress.
798 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
799 TestWithDownloads) {
800 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
801 SetDownloadPathForProfile(browser()->profile());
802 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
803 content::TestNavigationObserver navigation_observer(
804 browser()->tab_strip_model()->GetActiveWebContents(), 1);
805 TestBrowserCloseManager::AttemptClose(
806 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
807 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
808 navigation_observer.Wait();
809 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
810 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
812 RepeatedNotificationObserver close_observer(
813 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
815 TestBrowserCloseManager::AttemptClose(
816 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
817 close_observer.Wait();
818 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
819 EXPECT_TRUE(chrome::BrowserIterator().done());
820 if (browser_defaults::kBrowserAliveWithNoWindows)
821 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
822 else
823 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
826 // Test shutdown with a download in progress in an off-the-record profile.
827 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
828 TestWithOffTheRecordDownloads) {
829 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
830 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
831 SetDownloadPathForProfile(otr_profile);
832 Browser* otr_browser = CreateBrowser(otr_profile);
834 RepeatedNotificationObserver close_observer(
835 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
836 browser()->window()->Close();
837 close_observer.Wait();
839 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
840 content::TestNavigationObserver navigation_observer(
841 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
842 TestBrowserCloseManager::AttemptClose(
843 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
844 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
845 navigation_observer.Wait();
846 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
847 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
849 RepeatedNotificationObserver close_observer(
850 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
852 TestBrowserCloseManager::AttemptClose(
853 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
854 close_observer.Wait();
855 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
856 EXPECT_TRUE(chrome::BrowserIterator().done());
857 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
860 // Test shutdown with a download in progress from one profile, where the only
861 // open windows are for another profile.
862 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
863 TestWithDownloadsFromDifferentProfiles) {
864 ProfileManager* profile_manager = g_browser_process->profile_manager();
865 base::FilePath path =
866 profile_manager->user_data_dir().AppendASCII("test_profile");
867 if (!base::PathExists(path))
868 ASSERT_TRUE(base::CreateDirectory(path));
869 Profile* other_profile =
870 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
871 profile_manager->RegisterTestingProfile(other_profile, true, false);
872 Browser* other_profile_browser = CreateBrowser(other_profile);
874 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
875 SetDownloadPathForProfile(browser()->profile());
876 SetDownloadPathForProfile(other_profile);
877 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
879 RepeatedNotificationObserver close_observer(
880 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
881 browser()->window()->Close();
882 close_observer.Wait();
885 // When the shutdown is cancelled, the downloads page should be opened in a
886 // browser for that profile. Because there are no browsers for that profile, a
887 // new browser should be opened.
888 ui_test_utils::BrowserAddedObserver new_browser_observer;
889 TestBrowserCloseManager::AttemptClose(
890 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
891 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
892 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
893 EXPECT_EQ(
894 GURL(chrome::kChromeUIDownloadsURL),
895 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
896 EXPECT_EQ(GURL("about:blank"),
897 other_profile_browser->tab_strip_model()->GetActiveWebContents()
898 ->GetURL());
900 RepeatedNotificationObserver close_observer(
901 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
902 TestBrowserCloseManager::AttemptClose(
903 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
904 close_observer.Wait();
905 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
906 EXPECT_TRUE(chrome::BrowserIterator().done());
907 if (browser_defaults::kBrowserAliveWithNoWindows)
908 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
909 else
910 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
913 // Test shutdown with downloads in progress and beforeunload handlers.
914 // Disabled, see http://crbug.com/315754.
915 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
916 DISABLED_TestBeforeUnloadAndDownloads) {
917 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
918 SetDownloadPathForProfile(browser()->profile());
919 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
920 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
921 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
923 content::WindowedNotificationObserver cancel_observer(
924 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
925 content::NotificationService::AllSources());
926 TestBrowserCloseManager::AttemptClose(
927 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
928 ASSERT_NO_FATAL_FAILURE(AcceptClose());
929 cancel_observer.Wait();
930 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
932 RepeatedNotificationObserver close_observer(
933 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
934 TestBrowserCloseManager::AttemptClose(
935 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
936 ASSERT_NO_FATAL_FAILURE(AcceptClose());
937 close_observer.Wait();
938 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
939 EXPECT_TRUE(chrome::BrowserIterator().done());
942 #endif // defined(OS_MACOSX)
944 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
945 BrowserCloseManagerWithDownloadsBrowserTest,
946 testing::Bool());
948 class BrowserCloseManagerWithBackgroundModeBrowserTest
949 : public BrowserCloseManagerBrowserTest {
950 public:
951 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
953 void SetUpOnMainThread() override {
954 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
955 g_browser_process->set_background_mode_manager_for_test(
956 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
959 bool IsBackgroundModeSuspended() {
960 return static_cast<FakeBackgroundModeManager*>(
961 g_browser_process->background_mode_manager())
962 ->IsBackgroundModeSuspended();
965 private:
966 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
969 // Check that background mode is suspended when closing all browsers unless we
970 // are quitting and that background mode is resumed when a new browser window is
971 // opened.
972 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
973 CloseAllBrowsersWithBackgroundMode) {
974 EXPECT_FALSE(IsBackgroundModeSuspended());
975 Profile* profile = browser()->profile();
977 RepeatedNotificationObserver close_observer(
978 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
979 chrome::IncrementKeepAliveCount();
980 chrome::CloseAllBrowsers();
981 close_observer.Wait();
983 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
984 EXPECT_TRUE(chrome::BrowserIterator().done());
985 EXPECT_TRUE(IsBackgroundModeSuspended());
987 // Background mode should be resumed when a new browser window is opened.
988 ui_test_utils::BrowserAddedObserver new_browser_observer;
989 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
990 new_browser_observer.WaitForSingleNewBrowser();
991 chrome::DecrementKeepAliveCount();
992 EXPECT_FALSE(IsBackgroundModeSuspended());
993 RepeatedNotificationObserver close_observer(
994 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
996 // Background mode should not be suspended when quitting.
997 chrome::CloseAllBrowsersAndQuit();
998 close_observer.Wait();
999 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1000 EXPECT_TRUE(chrome::BrowserIterator().done());
1001 EXPECT_FALSE(IsBackgroundModeSuspended());
1005 // Check that closing the last browser window individually does not affect
1006 // background mode.
1007 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1008 CloseSingleBrowserWithBackgroundMode) {
1009 RepeatedNotificationObserver close_observer(
1010 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1011 EXPECT_FALSE(IsBackgroundModeSuspended());
1012 browser()->window()->Close();
1013 close_observer.Wait();
1014 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1015 EXPECT_TRUE(chrome::BrowserIterator().done());
1016 EXPECT_FALSE(IsBackgroundModeSuspended());
1019 // Check that closing all browsers with no browser windows open suspends
1020 // background mode but does not cause Chrome to quit.
1021 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1022 CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
1023 RepeatedNotificationObserver close_observer(
1024 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1025 EXPECT_FALSE(IsBackgroundModeSuspended());
1026 chrome::IncrementKeepAliveCount();
1027 browser()->window()->Close();
1028 close_observer.Wait();
1029 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1030 EXPECT_TRUE(chrome::BrowserIterator().done());
1031 EXPECT_FALSE(IsBackgroundModeSuspended());
1033 chrome::CloseAllBrowsers();
1034 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1035 EXPECT_TRUE(chrome::BrowserIterator().done());
1036 EXPECT_TRUE(IsBackgroundModeSuspended());
1039 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1040 BrowserCloseManagerWithBackgroundModeBrowserTest,
1041 testing::Bool());