blob: 4b9e34d449147022574196db852454c3cc064cd3 [file] [log] [blame]
[email protected]2e9d79f2013-08-16 05:45:561// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <vector>
6
7#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:058#include "base/files/scoped_temp_dir.h"
[email protected]0c95faf42013-10-28 06:27:209#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1410#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5611#include "chrome/browser/browser_shutdown.h"
12#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3813#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4214#include "chrome/browser/download/chrome_download_manager_delegate.h"
[email protected]0ed53872014-05-10 05:51:0515#include "chrome/browser/download/download_prefs.h"
[email protected]422a7d12013-10-21 12:10:4216#include "chrome/browser/download/download_service.h"
17#include "chrome/browser/download/download_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5618#include "chrome/browser/lifetime/application_lifetime.h"
19#include "chrome/browser/lifetime/browser_close_manager.h"
[email protected]edfca702013-08-16 08:58:1420#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5621#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1422#include "chrome/browser/profiles/profile.h"
23#include "chrome/browser/profiles/profile_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5624#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2025#include "chrome/browser/ui/browser_commands.h"
[email protected]2e9d79f2013-08-16 05:45:5626#include "chrome/browser/ui/browser_iterator.h"
[email protected]edfca702013-08-16 08:58:1427#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5628#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"
oshimaf65398422014-11-18 23:30:4233#include "components/app_modal/javascript_app_modal_dialog.h"
34#include "components/app_modal/native_app_modal_dialog.h"
[email protected]edfca702013-08-16 08:58:1435#include "content/public/browser/browser_context.h"
36#include "content/public/browser/download_item.h"
37#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5638#include "content/public/browser/notification_service.h"
39#include "content/public/browser/web_contents.h"
[email protected]edfca702013-08-16 08:58:1440#include "content/public/test/download_test_observer.h"
[email protected]2e9d79f2013-08-16 05:45:5641#include "content/public/test/test_navigation_observer.h"
[email protected]2e9d79f2013-08-16 05:45:5642#include "net/test/embedded_test_server/embedded_test_server.h"
xunjieli0332c192014-09-10 23:23:3143#include "net/test/url_request/url_request_mock_http_job.h"
jam8e45cd72015-01-20 16:33:4444#include "net/test/url_request/url_request_slow_download_job.h"
[email protected]2e9d79f2013-08-16 05:45:5645
[email protected]f9357a442014-05-15 18:44:0746#if defined(OS_CHROMEOS)
47#include "chromeos/chromeos_switches.h"
48#endif
49
[email protected]2e9d79f2013-08-16 05:45:5650namespace {
51
oshima0929be2a2014-11-19 22:21:0352app_modal::NativeAppModalDialog* GetNextDialog() {
53 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
oshima82f72482014-10-24 14:14:3254 EXPECT_TRUE(dialog->IsJavaScriptModalDialog());
oshima0929be2a2014-11-19 22:21:0355 app_modal::JavaScriptAppModalDialog* js_dialog =
56 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
oshima82f72482014-10-24 14:14:3257 CHECK(js_dialog->native_dialog());
58 return js_dialog->native_dialog();
59}
[email protected]2e9d79f2013-08-16 05:45:5660
oshima82f72482014-10-24 14:14:3261void AcceptClose() {
62 GetNextDialog()->AcceptAppModalDialog();
63}
[email protected]2e9d79f2013-08-16 05:45:5664
oshima82f72482014-10-24 14:14:3265void CancelClose() {
66 GetNextDialog()->CancelAppModalDialog();
67}
[email protected]2e9d79f2013-08-16 05:45:5668
69class 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());
74 }
75
Daniel Chenga542fca2014-10-21 09:51:2976 void Observe(int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) override {
[email protected]2e9d79f2013-08-16 05:45:5679 ASSERT_GT(num_outstanding_, 0);
80 if (!--num_outstanding_ && running_) {
81 content::BrowserThread::PostTask(
82 content::BrowserThread::UI, FROM_HERE, run_loop_.QuitClosure());
83 }
84 }
85
86 void Wait() {
87 if (num_outstanding_ <= 0)
88 return;
89
90 running_ = true;
91 run_loop_.Run();
92 running_ = false;
93 }
94
95 private:
96 int num_outstanding_;
97 content::NotificationRegistrar registrar_;
98 bool running_;
99 base::RunLoop run_loop_;
100
101 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
102};
103
[email protected]edfca702013-08-16 08:58:14104class TestBrowserCloseManager : public BrowserCloseManager {
105 public:
106 enum UserChoice {
107 USER_CHOICE_USER_CANCELS_CLOSE,
108 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42109 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14110 };
111
112 static void AttemptClose(UserChoice user_choice) {
113 scoped_refptr<BrowserCloseManager> browser_close_manager =
114 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20115 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14116 browser_close_manager->StartClosingBrowsers();
117 }
118
119 protected:
Daniel Chenga542fca2014-10-21 09:51:29120 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14121
Daniel Chenga542fca2014-10-21 09:51:29122 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14123 int download_count,
mostynb2b52d1db2014-10-07 02:47:17124 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42125 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14126 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42127 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14128 case USER_CHOICE_USER_CANCELS_CLOSE: {
129 callback.Run(false);
130 break;
131 }
132 case USER_CHOICE_USER_ALLOWS_CLOSE: {
133 callback.Run(true);
134 break;
135 }
136 }
137 }
138
139 private:
140 explicit TestBrowserCloseManager(UserChoice user_choice)
141 : user_choice_(user_choice) {}
142
143 UserChoice user_choice_;
144
145 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
146};
147
[email protected]422a7d12013-10-21 12:10:42148class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
149 public:
150 explicit TestDownloadManagerDelegate(Profile* profile)
151 : ChromeDownloadManagerDelegate(profile) {
[email protected]861b4d0a2013-12-13 20:10:41152 GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42153 }
Daniel Chenga542fca2014-10-21 09:51:29154 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42155
Daniel Chenga542fca2014-10-21 09:51:29156 bool DetermineDownloadTarget(
[email protected]422a7d12013-10-21 12:10:42157 content::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17158 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42159 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41160 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42161 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
162 item, dangerous_callback);
163 }
164
[email protected]861b4d0a2013-12-13 20:10:41165 static void SetDangerous(
[email protected]422a7d12013-10-21 12:10:42166 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);
175 }
[email protected]422a7d12013-10-21 12:10:42176};
177
[email protected]0c95faf42013-10-28 06:27:20178class FakeBackgroundModeManager : public BackgroundModeManager {
179 public:
180 FakeBackgroundModeManager()
181 : BackgroundModeManager(
gab94474812015-01-08 20:04:36182 *base::CommandLine::ForCurrentProcess(),
[email protected]0c95faf42013-10-28 06:27:20183 &g_browser_process->profile_manager()->GetProfileInfoCache()),
184 suspended_(false) {}
185
Daniel Chenga542fca2014-10-21 09:51:29186 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20187 BackgroundModeManager::SuspendBackgroundMode();
188 suspended_ = true;
189 }
190
Daniel Chenga542fca2014-10-21 09:51:29191 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20192 BackgroundModeManager::ResumeBackgroundMode();
193 suspended_ = false;
194 }
195
196 bool IsBackgroundModeSuspended() {
197 return suspended_;
198 }
199
200 private:
201 bool suspended_;
202
203 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
204};
205
[email protected]2e9d79f2013-08-16 05:45:56206} // namespace
207
208class BrowserCloseManagerBrowserTest
209 : public InProcessBrowserTest,
210 public testing::WithParamInterface<bool> {
211 protected:
Daniel Chenga542fca2014-10-21 09:51:29212 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56213 InProcessBrowserTest::SetUpOnMainThread();
214 SessionStartupPref::SetStartupPref(
215 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
216 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14217 content::BrowserThread::PostTask(
218 content::BrowserThread::IO,
219 FROM_HERE,
220 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
[email protected]2e9d79f2013-08-16 05:45:56221 }
222
avi556c05022014-12-22 23:31:43223 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56224 if (GetParam())
225 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07226#if defined(OS_CHROMEOS)
227 command_line->AppendSwitch(
228 chromeos::switches::kIgnoreUserProfileMappingForTests);
229#endif
[email protected]2e9d79f2013-08-16 05:45:56230 }
231
[email protected]edfca702013-08-16 08:58:14232 void CreateStalledDownload(Browser* browser) {
233 content::DownloadTestObserverInProgress observer(
234 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
235 ui_test_utils::NavigateToURLWithDisposition(
236 browser,
jam8e45cd72015-01-20 16:33:44237 GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl),
[email protected]edfca702013-08-16 08:58:14238 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));
244 }
245
[email protected]2e9d79f2013-08-16 05:45:56246 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56247};
248
249IN_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);
[email protected]0c95faf42013-10-28 06:27:20255 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32256 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56257 cancel_observer.Wait();
258 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
259 EXPECT_EQ(1, browser()->tab_strip_model()->count());
260
261 RepeatedNotificationObserver close_observer(
262 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20263 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32264 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56265 close_observer.Wait();
266 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
267 EXPECT_TRUE(chrome::BrowserIterator().done());
268}
269
270IN_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);
[email protected]0c95faf42013-10-28 06:27:20277 chrome::CloseAllBrowsersAndQuit();
278 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32279 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56280 cancel_observer.Wait();
281 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
282 EXPECT_EQ(1, browser()->tab_strip_model()->count());
283
284 RepeatedNotificationObserver close_observer(
285 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20286 chrome::CloseAllBrowsersAndQuit();
287 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32288 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56289 close_observer.Wait();
290 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
291 EXPECT_TRUE(chrome::BrowserIterator().done());
292}
293
294IN_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);
[email protected]0c95faf42013-10-28 06:27:20303 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32304 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56305 cancel_observer.Wait();
306 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
307
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));
oshima82f72482014-10-24 14:14:32317 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56318 navigation_observer.Wait();
319
320 RepeatedNotificationObserver close_observer(
321 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20322 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56323 close_observer.Wait();
324 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
325 EXPECT_TRUE(chrome::BrowserIterator().done());
326}
327
328// Test that the tab closed after the aborted shutdown attempt is not re-opened
329// when restoring the session.
330IN_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());
337}
338
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.
[email protected]40cd13a2013-10-24 13:42:01341IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56342 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")));
348
349 // Cancel shutdown on the first beforeunload event.
350 {
351 RepeatedNotificationObserver cancel_observer(
352 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20353 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32354 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56355 cancel_observer.Wait();
356 }
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());
360
361 // Cancel shutdown on the second beforeunload event.
362 {
363 RepeatedNotificationObserver cancel_observer(
364 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20365 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32366 ASSERT_NO_FATAL_FAILURE(AcceptClose());
367 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56368 cancel_observer.Wait();
369 }
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());
373
374 // Allow shutdown for both beforeunload events.
375 RepeatedNotificationObserver close_observer(
376 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20377 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32378 ASSERT_NO_FATAL_FAILURE(AcceptClose());
379 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56380 close_observer.Wait();
381 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
382 EXPECT_TRUE(chrome::BrowserIterator().done());
383}
384
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.
[email protected]52656e02013-08-20 15:50:13388// 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
[email protected]2e9d79f2013-08-16 05:45:56396IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]52656e02013-08-20 15:50:13397 MAYBE_TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56398 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")));
407
408 RepeatedNotificationObserver cancel_observer(
409 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20410 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32411 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56412 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());
416
417 RepeatedNotificationObserver close_observer(
418 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20419 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32420 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56421 close_observer.Wait();
422 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
423 EXPECT_TRUE(chrome::BrowserIterator().done());
424}
425
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.
429IN_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")));
440
441 RepeatedNotificationObserver cancel_observer(
442 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20443 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32444 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56445 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());
451
452 RepeatedNotificationObserver close_observer(
453 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20454 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32455 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56456 close_observer.Wait();
457 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
458 EXPECT_TRUE(chrome::BrowserIterator().done());
459}
460
461// Test that a window created during shutdown is closed.
462IN_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")));
467
468 RepeatedNotificationObserver close_observer(
469 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20470 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56471 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32472 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56473 close_observer.Wait();
474 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
475 EXPECT_TRUE(chrome::BrowserIterator().done());
476}
477
478// Test that a window created during shutdown with a beforeunload handler can
479// cancel the shutdown.
480IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01481 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56482 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
483 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
484 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
485
486 RepeatedNotificationObserver cancel_observer(
487 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20488 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56489 browsers_.push_back(CreateBrowser(browser()->profile()));
490 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
491 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
oshima82f72482014-10-24 14:14:32492 ASSERT_NO_FATAL_FAILURE(AcceptClose());
493 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56494 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());
498
499 // Allow shutdown for both beforeunload dialogs.
500 RepeatedNotificationObserver close_observer(
501 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20502 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32503 ASSERT_NO_FATAL_FAILURE(AcceptClose());
504 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56505 close_observer.Wait();
506 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
507 EXPECT_TRUE(chrome::BrowserIterator().done());
508}
509
510// Test that tabs added during shutdown are closed.
511IN_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")));
519
520 RepeatedNotificationObserver close_observer(
521 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20522 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32523 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56524 AddBlankTabAndShow(browsers_[0]);
525 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32526 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56527 close_observer.Wait();
528 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
529 EXPECT_TRUE(chrome::BrowserIterator().done());
530}
531
532// Test that tabs created during shutdown with beforeunload handlers can cancel
533// the shutdown.
534IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01535 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56536 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);
[email protected]0c95faf42013-10-28 06:27:20544 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32545 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56546 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")));
oshima82f72482014-10-24 14:14:32552 ASSERT_NO_FATAL_FAILURE(AcceptClose());
553 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56554 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());
558
559 RepeatedNotificationObserver close_observer(
560 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20561 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32562 ASSERT_NO_FATAL_FAILURE(AcceptClose());
563 ASSERT_NO_FATAL_FAILURE(AcceptClose());
564 ASSERT_NO_FATAL_FAILURE(AcceptClose());
565 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56566
567 close_observer.Wait();
568 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
569 EXPECT_TRUE(chrome::BrowserIterator().done());
570}
571
572IN_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);
[email protected]0c95faf42013-10-28 06:27:20579 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56580
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();
oshima82f72482014-10-24 14:14:32585 ASSERT_NO_FATAL_FAILURE(CancelClose());
586 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56587 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());
591
592 RepeatedNotificationObserver close_observer(
593 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20594 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56595 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32596 ASSERT_NO_FATAL_FAILURE(AcceptClose());
597 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56598
599 close_observer.Wait();
600 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
601 EXPECT_TRUE(chrome::BrowserIterator().done());
602}
603
[email protected]8376e172013-10-03 08:42:49604// Test is flaky on windows, disabled. See http://crbug.com/276366
605#if defined(OS_WIN)
606#define MAYBE_TestOpenAndCloseWindowDuringShutdown \
607 DISABLED_TestOpenAndCloseWindowDuringShutdown
608#else
609#define MAYBE_TestOpenAndCloseWindowDuringShutdown \
610 TestOpenAndCloseWindowDuringShutdown
611#endif
[email protected]2e9d79f2013-08-16 05:45:56612IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]8376e172013-10-03 08:42:49613 MAYBE_TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56614 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);
[email protected]0c95faf42013-10-28 06:27:20619 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56620
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());
oshima82f72482014-10-24 14:14:32625 ASSERT_NO_FATAL_FAILURE(CancelClose());
626 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56627 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());
631
632 RepeatedNotificationObserver close_observer(
633 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20634 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56635 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32636 ASSERT_NO_FATAL_FAILURE(AcceptClose());
637 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56638
639 close_observer.Wait();
640 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
641 EXPECT_TRUE(chrome::BrowserIterator().done());
642}
643
644IN_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);
[email protected]0c95faf42013-10-28 06:27:20654 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56655
656 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32657 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56658 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());
662
663 RepeatedNotificationObserver close_observer(
664 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20665 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56666 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32667 ASSERT_NO_FATAL_FAILURE(AcceptClose());
668 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56669
670 close_observer.Wait();
671 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
672 EXPECT_TRUE(chrome::BrowserIterator().done());
673}
674
[email protected]0ed53872014-05-10 05:51:05675INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
676 BrowserCloseManagerBrowserTest,
677 testing::Bool());
678
679class BrowserCloseManagerWithDownloadsBrowserTest :
680 public BrowserCloseManagerBrowserTest {
681 public:
682 BrowserCloseManagerWithDownloadsBrowserTest() {}
683 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
684
Daniel Chenga542fca2014-10-21 09:51:29685 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05686 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
687 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
688 }
689
690 void SetDownloadPathForProfile(Profile* profile) {
691 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
692 download_prefs->SetDownloadPath(download_path());
693 }
694
695 const base::FilePath& download_path() const {
696 return scoped_download_directory_.path();
697 }
698
699 private:
700 base::ScopedTempDir scoped_download_directory_;
701};
702
[email protected]422a7d12013-10-21 12:10:42703// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:05704IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:42705 TestWithDangerousUrlDownload) {
706 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05707 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:42708
709 // Set up the fake delegate that forces the download to be malicious.
[email protected]861b4d0a2013-12-13 20:10:41710 scoped_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42711 new TestDownloadManagerDelegate(browser()->profile()));
dcheng383ba8a2014-10-16 23:55:19712 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
713 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass());
[email protected]422a7d12013-10-21 12:10:42714
715 // Run a dangerous download, but the user doesn't make a decision.
716 // This .swf normally would be categorized as DANGEROUS_FILE, but
717 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
718 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
xunjieli0332c192014-09-10 23:23:31719 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
[email protected]422a7d12013-10-21 12:10:42720 content::DownloadTestObserverInterrupted observer(
721 content::BrowserContext::GetDownloadManager(browser()->profile()),
722 1,
723 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
724 ui_test_utils::NavigateToURLWithDisposition(
725 browser(),
726 GURL(download_url),
727 NEW_BACKGROUND_TAB,
728 ui_test_utils::BROWSER_TEST_NONE);
729 observer.WaitForFinished();
730
731 // Check that the download manager has the expected state.
732 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
733 browser()->profile())->InProgressCount());
734 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
735 browser()->profile())->NonMaliciousInProgressCount());
736
737 // Close the browser with no user action.
738 RepeatedNotificationObserver close_observer(
739 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}
746
[email protected]edfca702013-08-16 08:58:14747// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:05748IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
749 TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:14750 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05751 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:14752 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
753 content::TestNavigationObserver navigation_observer(
754 browser()->tab_strip_model()->GetActiveWebContents(), 1);
755 TestBrowserCloseManager::AttemptClose(
756 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
757 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
758 navigation_observer.Wait();
759 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
760 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
761
762 RepeatedNotificationObserver close_observer(
763 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
764
765 TestBrowserCloseManager::AttemptClose(
766 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
767 close_observer.Wait();
768 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
769 EXPECT_TRUE(chrome::BrowserIterator().done());
[email protected]5fec4702013-11-20 04:37:38770 if (browser_defaults::kBrowserAliveWithNoWindows)
771 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
772 else
773 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:14774}
775
776// Test shutdown with a download in progress from one profile, where the only
777// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:05778IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:14779 TestWithDownloadsFromDifferentProfiles) {
780 ProfileManager* profile_manager = g_browser_process->profile_manager();
781 base::FilePath path =
782 profile_manager->user_data_dir().AppendASCII("test_profile");
783 if (!base::PathExists(path))
[email protected]426d1c92013-12-03 20:08:54784 ASSERT_TRUE(base::CreateDirectory(path));
[email protected]edfca702013-08-16 08:58:14785 Profile* other_profile =
786 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
787 profile_manager->RegisterTestingProfile(other_profile, true, false);
788 Browser* other_profile_browser = CreateBrowser(other_profile);
789
790 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05791 SetDownloadPathForProfile(browser()->profile());
792 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:14793 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
794 {
795 RepeatedNotificationObserver close_observer(
796 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
797 browser()->window()->Close();
798 close_observer.Wait();
799 }
800
801 // When the shutdown is cancelled, the downloads page should be opened in a
802 // browser for that profile. Because there are no browsers for that profile, a
803 // new browser should be opened.
804 ui_test_utils::BrowserAddedObserver new_browser_observer;
805 TestBrowserCloseManager::AttemptClose(
806 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
807 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
808 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
809 EXPECT_EQ(
810 GURL(chrome::kChromeUIDownloadsURL),
811 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
812 EXPECT_EQ(GURL("about:blank"),
813 other_profile_browser->tab_strip_model()->GetActiveWebContents()
814 ->GetURL());
815
816 RepeatedNotificationObserver close_observer(
817 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
818 TestBrowserCloseManager::AttemptClose(
819 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
820 close_observer.Wait();
821 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
822 EXPECT_TRUE(chrome::BrowserIterator().done());
[email protected]5fec4702013-11-20 04:37:38823 if (browser_defaults::kBrowserAliveWithNoWindows)
824 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
825 else
826 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:14827}
828
829// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]4c23057a2013-11-06 18:29:52830// Disabled, see http://crbug.com/315754.
[email protected]0ed53872014-05-10 05:51:05831IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]4c23057a2013-11-06 18:29:52832 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:14833 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05834 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:14835 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
836 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
837 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
838
839 content::WindowedNotificationObserver cancel_observer(
840 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
841 content::NotificationService::AllSources());
842 TestBrowserCloseManager::AttemptClose(
843 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:32844 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:14845 cancel_observer.Wait();
846 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
847
848 RepeatedNotificationObserver close_observer(
849 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
850 TestBrowserCloseManager::AttemptClose(
851 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:32852 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:14853 close_observer.Wait();
854 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
855 EXPECT_TRUE(chrome::BrowserIterator().done());
856}
857
[email protected]0ed53872014-05-10 05:51:05858INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
859 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56860 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:20861
862class BrowserCloseManagerWithBackgroundModeBrowserTest
863 : public BrowserCloseManagerBrowserTest {
864 public:
865 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
866
Daniel Chenga542fca2014-10-21 09:51:29867 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:20868 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
869 g_browser_process->set_background_mode_manager_for_test(
870 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
871 }
872
873 bool IsBackgroundModeSuspended() {
874 return static_cast<FakeBackgroundModeManager*>(
875 g_browser_process->background_mode_manager())
876 ->IsBackgroundModeSuspended();
877 }
878
879 private:
880 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
881};
882
883// Check that background mode is suspended when closing all browsers unless we
884// are quitting and that background mode is resumed when a new browser window is
885// opened.
886IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
887 CloseAllBrowsersWithBackgroundMode) {
888 EXPECT_FALSE(IsBackgroundModeSuspended());
889 Profile* profile = browser()->profile();
890 {
891 RepeatedNotificationObserver close_observer(
892 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]9a718b22014-02-25 15:17:27893 chrome::IncrementKeepAliveCount();
[email protected]0c95faf42013-10-28 06:27:20894 chrome::CloseAllBrowsers();
895 close_observer.Wait();
896 }
897 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
898 EXPECT_TRUE(chrome::BrowserIterator().done());
899 EXPECT_TRUE(IsBackgroundModeSuspended());
900
901 // Background mode should be resumed when a new browser window is opened.
902 ui_test_utils::BrowserAddedObserver new_browser_observer;
903 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
904 new_browser_observer.WaitForSingleNewBrowser();
[email protected]9a718b22014-02-25 15:17:27905 chrome::DecrementKeepAliveCount();
[email protected]0c95faf42013-10-28 06:27:20906 EXPECT_FALSE(IsBackgroundModeSuspended());
907 RepeatedNotificationObserver close_observer(
908 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
909
910 // Background mode should not be suspended when quitting.
911 chrome::CloseAllBrowsersAndQuit();
912 close_observer.Wait();
913 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
914 EXPECT_TRUE(chrome::BrowserIterator().done());
915 EXPECT_FALSE(IsBackgroundModeSuspended());
916
917}
918
919// Check that closing the last browser window individually does not affect
920// background mode.
921IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
922 CloseSingleBrowserWithBackgroundMode) {
923 RepeatedNotificationObserver close_observer(
924 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
925 EXPECT_FALSE(IsBackgroundModeSuspended());
926 browser()->window()->Close();
927 close_observer.Wait();
928 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
929 EXPECT_TRUE(chrome::BrowserIterator().done());
930 EXPECT_FALSE(IsBackgroundModeSuspended());
931}
932
933// Check that closing all browsers with no browser windows open suspends
934// background mode but does not cause Chrome to quit.
935IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
936 CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
937 RepeatedNotificationObserver close_observer(
938 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
939 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]9a718b22014-02-25 15:17:27940 chrome::IncrementKeepAliveCount();
[email protected]0c95faf42013-10-28 06:27:20941 browser()->window()->Close();
942 close_observer.Wait();
943 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
944 EXPECT_TRUE(chrome::BrowserIterator().done());
945 EXPECT_FALSE(IsBackgroundModeSuspended());
946
947 chrome::CloseAllBrowsers();
948 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
949 EXPECT_TRUE(chrome::BrowserIterator().done());
950 EXPECT_TRUE(IsBackgroundModeSuspended());
951}
952
953INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
954 BrowserCloseManagerWithBackgroundModeBrowserTest,
955 testing::Bool());