blob: 727be0799a36a05ce4bca645b8a9757ca2086a05 [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"
42#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
wafflesb63234052015-08-07 02:44:02294// 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
300IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
301 MAYBE_PRE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56302 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);
[email protected]0c95faf42013-10-28 06:27:20310 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32311 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56312 cancel_observer.Wait();
313 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
314
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));
oshima82f72482014-10-24 14:14:32324 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56325 navigation_observer.Wait();
326
327 RepeatedNotificationObserver close_observer(
328 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20329 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56330 close_observer.Wait();
331 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
332 EXPECT_TRUE(chrome::BrowserIterator().done());
333}
334
335// Test that the tab closed after the aborted shutdown attempt is not re-opened
336// when restoring the session.
wafflesb63234052015-08-07 02:44:02337// 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
343IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
344 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56345 // 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());
351}
352
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.
[email protected]40cd13a2013-10-24 13:42:01355IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
[email protected]2e9d79f2013-08-16 05:45:56356 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")));
362
363 // Cancel shutdown on the first beforeunload event.
364 {
365 RepeatedNotificationObserver cancel_observer(
366 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20367 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32368 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56369 cancel_observer.Wait();
370 }
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());
374
375 // Cancel shutdown on the second beforeunload event.
376 {
377 RepeatedNotificationObserver cancel_observer(
378 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20379 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32380 ASSERT_NO_FATAL_FAILURE(AcceptClose());
381 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56382 cancel_observer.Wait();
383 }
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());
387
388 // Allow shutdown for both beforeunload events.
389 RepeatedNotificationObserver close_observer(
390 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20391 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32392 ASSERT_NO_FATAL_FAILURE(AcceptClose());
393 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56394 close_observer.Wait();
395 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
396 EXPECT_TRUE(chrome::BrowserIterator().done());
397}
398
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.
[email protected]52656e02013-08-20 15:50:13402// Test is flaky on windows, disabled. See http://crbug.com/276366
wafflesb63234052015-08-07 02:44:02403// Test is flaky on Mac. See http://crbug.com/517687.
404#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]52656e02013-08-20 15:50:13405#define MAYBE_TestHangInBeforeUnloadMultipleTabs \
406 DISABLED_TestHangInBeforeUnloadMultipleTabs
407#else
408#define MAYBE_TestHangInBeforeUnloadMultipleTabs \
409 TestHangInBeforeUnloadMultipleTabs
410#endif
[email protected]2e9d79f2013-08-16 05:45:56411IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]52656e02013-08-20 15:50:13412 MAYBE_TestHangInBeforeUnloadMultipleTabs) {
[email protected]2e9d79f2013-08-16 05:45:56413 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")));
422
423 RepeatedNotificationObserver cancel_observer(
424 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20425 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32426 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56427 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());
431
432 RepeatedNotificationObserver close_observer(
433 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20434 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32435 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56436 close_observer.Wait();
437 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
438 EXPECT_TRUE(chrome::BrowserIterator().done());
439}
440
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.
444IN_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")));
455
456 RepeatedNotificationObserver cancel_observer(
457 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20458 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32459 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56460 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());
466
467 RepeatedNotificationObserver close_observer(
468 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20469 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32470 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56471 close_observer.Wait();
472 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
473 EXPECT_TRUE(chrome::BrowserIterator().done());
474}
475
476// Test that a window created during shutdown is closed.
477IN_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")));
482
483 RepeatedNotificationObserver close_observer(
484 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20485 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56486 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32487 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56488 close_observer.Wait();
489 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
490 EXPECT_TRUE(chrome::BrowserIterator().done());
491}
492
493// Test that a window created during shutdown with a beforeunload handler can
494// cancel the shutdown.
495IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01496 TestAddWindowWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56497 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
498 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
499 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
500
501 RepeatedNotificationObserver cancel_observer(
502 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20503 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56504 browsers_.push_back(CreateBrowser(browser()->profile()));
505 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
506 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
oshima82f72482014-10-24 14:14:32507 ASSERT_NO_FATAL_FAILURE(AcceptClose());
508 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56509 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());
513
514 // Allow shutdown for both beforeunload dialogs.
515 RepeatedNotificationObserver close_observer(
516 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20517 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32518 ASSERT_NO_FATAL_FAILURE(AcceptClose());
519 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56520 close_observer.Wait();
521 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
522 EXPECT_TRUE(chrome::BrowserIterator().done());
523}
524
525// Test that tabs added during shutdown are closed.
526IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
527 TestAddTabDuringShutdown) {
528 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
529 browsers_.push_back(CreateBrowser(browser()->profile()));
530 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
531 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
532 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
533 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
534
535 RepeatedNotificationObserver close_observer(
536 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20537 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32538 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56539 AddBlankTabAndShow(browsers_[0]);
540 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32541 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56542 close_observer.Wait();
543 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
544 EXPECT_TRUE(chrome::BrowserIterator().done());
545}
546
547// Test that tabs created during shutdown with beforeunload handlers can cancel
548// the shutdown.
549IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01550 TestAddTabWithBeforeUnloadDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56551 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
552 browsers_.push_back(CreateBrowser(browser()->profile()));
553 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
554 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
555 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
556 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
557 RepeatedNotificationObserver cancel_observer(
558 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20559 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32560 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56561 AddBlankTabAndShow(browsers_[0]);
562 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
563 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
564 AddBlankTabAndShow(browsers_[1]);
565 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
566 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
oshima82f72482014-10-24 14:14:32567 ASSERT_NO_FATAL_FAILURE(AcceptClose());
568 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56569 cancel_observer.Wait();
570 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
571 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
572 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
573
574 RepeatedNotificationObserver close_observer(
575 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20576 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32577 ASSERT_NO_FATAL_FAILURE(AcceptClose());
578 ASSERT_NO_FATAL_FAILURE(AcceptClose());
579 ASSERT_NO_FATAL_FAILURE(AcceptClose());
580 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56581
582 close_observer.Wait();
583 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
584 EXPECT_TRUE(chrome::BrowserIterator().done());
585}
586
587IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
588 TestCloseTabDuringShutdown) {
589 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
590 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
591 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
592 RepeatedNotificationObserver cancel_observer(
593 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20594 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56595
596 browsers_.push_back(CreateBrowser(browser()->profile()));
597 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
598 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
599 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32600 ASSERT_NO_FATAL_FAILURE(CancelClose());
601 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56602 cancel_observer.Wait();
603 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
604 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
605 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
606
607 RepeatedNotificationObserver close_observer(
608 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20609 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56610 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32611 ASSERT_NO_FATAL_FAILURE(AcceptClose());
612 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56613
614 close_observer.Wait();
615 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
616 EXPECT_TRUE(chrome::BrowserIterator().done());
617}
618
avi848c4dc2015-06-11 18:59:40619// Test is flaky on Windows and Mac. See http://crbug.com/276366.
620#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]8376e172013-10-03 08:42:49621#define MAYBE_TestOpenAndCloseWindowDuringShutdown \
622 DISABLED_TestOpenAndCloseWindowDuringShutdown
623#else
624#define MAYBE_TestOpenAndCloseWindowDuringShutdown \
625 TestOpenAndCloseWindowDuringShutdown
626#endif
[email protected]2e9d79f2013-08-16 05:45:56627IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]8376e172013-10-03 08:42:49628 MAYBE_TestOpenAndCloseWindowDuringShutdown) {
[email protected]2e9d79f2013-08-16 05:45:56629 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
630 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
631 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
632 RepeatedNotificationObserver cancel_observer(
633 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20634 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56635
636 browsers_.push_back(CreateBrowser(browser()->profile()));
637 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
638 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
639 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32640 ASSERT_NO_FATAL_FAILURE(CancelClose());
641 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56642 cancel_observer.Wait();
643 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
644 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
645 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
646
647 RepeatedNotificationObserver close_observer(
648 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20649 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56650 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32651 ASSERT_NO_FATAL_FAILURE(AcceptClose());
652 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56653
654 close_observer.Wait();
655 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
656 EXPECT_TRUE(chrome::BrowserIterator().done());
657}
658
659IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
660 TestCloseWindowDuringShutdown) {
661 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
662 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
663 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
664 browsers_.push_back(CreateBrowser(browser()->profile()));
665 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
666 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
667 RepeatedNotificationObserver cancel_observer(
668 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20669 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56670
671 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32672 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56673 cancel_observer.Wait();
674 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
675 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
676 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
677
678 RepeatedNotificationObserver close_observer(
679 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20680 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56681 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32682 ASSERT_NO_FATAL_FAILURE(AcceptClose());
683 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56684
685 close_observer.Wait();
686 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
687 EXPECT_TRUE(chrome::BrowserIterator().done());
688}
689
[email protected]0ed53872014-05-10 05:51:05690INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
691 BrowserCloseManagerBrowserTest,
692 testing::Bool());
693
694class BrowserCloseManagerWithDownloadsBrowserTest :
695 public BrowserCloseManagerBrowserTest {
696 public:
697 BrowserCloseManagerWithDownloadsBrowserTest() {}
698 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
699
Daniel Chenga542fca2014-10-21 09:51:29700 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05701 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
702 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
703 }
704
705 void SetDownloadPathForProfile(Profile* profile) {
706 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
707 download_prefs->SetDownloadPath(download_path());
708 }
709
710 const base::FilePath& download_path() const {
711 return scoped_download_directory_.path();
712 }
713
714 private:
715 base::ScopedTempDir scoped_download_directory_;
716};
717
jackhouae8e6e52015-05-29 06:36:46718// Mac has its own in-progress download prompt in app_controller_mac.mm, so
719// BrowserCloseManager should simply close all browsers. If there are no
720// browsers, it should not crash.
721#if defined(OS_MACOSX)
722IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
723 TestWithDownloads) {
724 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
725 SetDownloadPathForProfile(browser()->profile());
726 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
727
728 RepeatedNotificationObserver close_observer(
729 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
730
731 TestBrowserCloseManager::AttemptClose(
732 TestBrowserCloseManager::NO_USER_CHOICE);
733 close_observer.Wait();
734 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
735 EXPECT_TRUE(chrome::BrowserIterator().done());
736 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
737
738 // Attempting to close again should not crash.
739 TestBrowserCloseManager::AttemptClose(
740 TestBrowserCloseManager::NO_USER_CHOICE);
741}
742#else // defined(OS_MACOSX)
743
[email protected]422a7d12013-10-21 12:10:42744// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:05745IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:42746 TestWithDangerousUrlDownload) {
747 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05748 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:42749
750 // Set up the fake delegate that forces the download to be malicious.
[email protected]861b4d0a2013-12-13 20:10:41751 scoped_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:42752 new TestDownloadManagerDelegate(browser()->profile()));
dcheng383ba8a2014-10-16 23:55:19753 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
754 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass());
[email protected]422a7d12013-10-21 12:10:42755
756 // Run a dangerous download, but the user doesn't make a decision.
757 // This .swf normally would be categorized as DANGEROUS_FILE, but
758 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
759 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
xunjieli0332c192014-09-10 23:23:31760 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
[email protected]422a7d12013-10-21 12:10:42761 content::DownloadTestObserverInterrupted observer(
762 content::BrowserContext::GetDownloadManager(browser()->profile()),
763 1,
764 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
765 ui_test_utils::NavigateToURLWithDisposition(
766 browser(),
767 GURL(download_url),
768 NEW_BACKGROUND_TAB,
769 ui_test_utils::BROWSER_TEST_NONE);
770 observer.WaitForFinished();
771
772 // Check that the download manager has the expected state.
773 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
774 browser()->profile())->InProgressCount());
775 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
776 browser()->profile())->NonMaliciousInProgressCount());
777
778 // Close the browser with no user action.
779 RepeatedNotificationObserver close_observer(
780 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
781 TestBrowserCloseManager::AttemptClose(
782 TestBrowserCloseManager::NO_USER_CHOICE);
783 close_observer.Wait();
784 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
785 EXPECT_TRUE(chrome::BrowserIterator().done());
786}
787
[email protected]edfca702013-08-16 08:58:14788// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:05789IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
790 TestWithDownloads) {
[email protected]edfca702013-08-16 08:58:14791 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05792 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:14793 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
794 content::TestNavigationObserver navigation_observer(
795 browser()->tab_strip_model()->GetActiveWebContents(), 1);
796 TestBrowserCloseManager::AttemptClose(
797 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
798 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
799 navigation_observer.Wait();
800 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
801 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
802
803 RepeatedNotificationObserver close_observer(
804 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
805
806 TestBrowserCloseManager::AttemptClose(
807 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
808 close_observer.Wait();
809 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
810 EXPECT_TRUE(chrome::BrowserIterator().done());
[email protected]5fec4702013-11-20 04:37:38811 if (browser_defaults::kBrowserAliveWithNoWindows)
812 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
813 else
814 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:14815}
816
sammccd347712015-03-13 22:02:32817// Test shutdown with a download in progress in an off-the-record profile.
818IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
819 TestWithOffTheRecordDownloads) {
820 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
821 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
822 SetDownloadPathForProfile(otr_profile);
823 Browser* otr_browser = CreateBrowser(otr_profile);
824 {
825 RepeatedNotificationObserver close_observer(
826 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
827 browser()->window()->Close();
828 close_observer.Wait();
829 }
830 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
831 content::TestNavigationObserver navigation_observer(
832 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
833 TestBrowserCloseManager::AttemptClose(
834 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
835 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
836 navigation_observer.Wait();
837 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
838 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
839
840 RepeatedNotificationObserver close_observer(
841 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
842
843 TestBrowserCloseManager::AttemptClose(
844 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
845 close_observer.Wait();
846 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
847 EXPECT_TRUE(chrome::BrowserIterator().done());
848 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
849}
850
[email protected]edfca702013-08-16 08:58:14851// Test shutdown with a download in progress from one profile, where the only
852// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:05853IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:14854 TestWithDownloadsFromDifferentProfiles) {
855 ProfileManager* profile_manager = g_browser_process->profile_manager();
856 base::FilePath path =
857 profile_manager->user_data_dir().AppendASCII("test_profile");
858 if (!base::PathExists(path))
[email protected]426d1c92013-12-03 20:08:54859 ASSERT_TRUE(base::CreateDirectory(path));
[email protected]edfca702013-08-16 08:58:14860 Profile* other_profile =
861 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
862 profile_manager->RegisterTestingProfile(other_profile, true, false);
863 Browser* other_profile_browser = CreateBrowser(other_profile);
864
865 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05866 SetDownloadPathForProfile(browser()->profile());
867 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:14868 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
869 {
870 RepeatedNotificationObserver close_observer(
871 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
872 browser()->window()->Close();
873 close_observer.Wait();
874 }
875
876 // When the shutdown is cancelled, the downloads page should be opened in a
877 // browser for that profile. Because there are no browsers for that profile, a
878 // new browser should be opened.
879 ui_test_utils::BrowserAddedObserver new_browser_observer;
880 TestBrowserCloseManager::AttemptClose(
881 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
882 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
883 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
884 EXPECT_EQ(
885 GURL(chrome::kChromeUIDownloadsURL),
886 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
887 EXPECT_EQ(GURL("about:blank"),
888 other_profile_browser->tab_strip_model()->GetActiveWebContents()
889 ->GetURL());
890
891 RepeatedNotificationObserver close_observer(
892 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
893 TestBrowserCloseManager::AttemptClose(
894 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
895 close_observer.Wait();
896 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
897 EXPECT_TRUE(chrome::BrowserIterator().done());
[email protected]5fec4702013-11-20 04:37:38898 if (browser_defaults::kBrowserAliveWithNoWindows)
899 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
900 else
901 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:14902}
903
904// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]4c23057a2013-11-06 18:29:52905// Disabled, see http://crbug.com/315754.
[email protected]0ed53872014-05-10 05:51:05906IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]4c23057a2013-11-06 18:29:52907 DISABLED_TestBeforeUnloadAndDownloads) {
[email protected]edfca702013-08-16 08:58:14908 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]0ed53872014-05-10 05:51:05909 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:14910 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
911 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
912 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
913
914 content::WindowedNotificationObserver cancel_observer(
915 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
916 content::NotificationService::AllSources());
917 TestBrowserCloseManager::AttemptClose(
918 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:32919 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:14920 cancel_observer.Wait();
921 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
922
923 RepeatedNotificationObserver close_observer(
924 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
925 TestBrowserCloseManager::AttemptClose(
926 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:32927 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:14928 close_observer.Wait();
929 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
930 EXPECT_TRUE(chrome::BrowserIterator().done());
931}
932
jackhouae8e6e52015-05-29 06:36:46933#endif // defined(OS_MACOSX)
934
[email protected]0ed53872014-05-10 05:51:05935INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
936 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:56937 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:20938
939class BrowserCloseManagerWithBackgroundModeBrowserTest
940 : public BrowserCloseManagerBrowserTest {
941 public:
942 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
943
Daniel Chenga542fca2014-10-21 09:51:29944 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:20945 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
946 g_browser_process->set_background_mode_manager_for_test(
947 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
948 }
949
950 bool IsBackgroundModeSuspended() {
951 return static_cast<FakeBackgroundModeManager*>(
952 g_browser_process->background_mode_manager())
953 ->IsBackgroundModeSuspended();
954 }
955
956 private:
957 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
958};
959
960// Check that background mode is suspended when closing all browsers unless we
961// are quitting and that background mode is resumed when a new browser window is
962// opened.
963IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
964 CloseAllBrowsersWithBackgroundMode) {
965 EXPECT_FALSE(IsBackgroundModeSuspended());
966 Profile* profile = browser()->profile();
967 {
968 RepeatedNotificationObserver close_observer(
969 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]9a718b22014-02-25 15:17:27970 chrome::IncrementKeepAliveCount();
[email protected]0c95faf42013-10-28 06:27:20971 chrome::CloseAllBrowsers();
972 close_observer.Wait();
973 }
974 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
975 EXPECT_TRUE(chrome::BrowserIterator().done());
976 EXPECT_TRUE(IsBackgroundModeSuspended());
977
978 // Background mode should be resumed when a new browser window is opened.
979 ui_test_utils::BrowserAddedObserver new_browser_observer;
980 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
981 new_browser_observer.WaitForSingleNewBrowser();
[email protected]9a718b22014-02-25 15:17:27982 chrome::DecrementKeepAliveCount();
[email protected]0c95faf42013-10-28 06:27:20983 EXPECT_FALSE(IsBackgroundModeSuspended());
984 RepeatedNotificationObserver close_observer(
985 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
986
987 // Background mode should not be suspended when quitting.
988 chrome::CloseAllBrowsersAndQuit();
989 close_observer.Wait();
990 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
991 EXPECT_TRUE(chrome::BrowserIterator().done());
992 EXPECT_FALSE(IsBackgroundModeSuspended());
993
994}
995
996// Check that closing the last browser window individually does not affect
997// background mode.
998IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
999 CloseSingleBrowserWithBackgroundMode) {
1000 RepeatedNotificationObserver close_observer(
1001 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1002 EXPECT_FALSE(IsBackgroundModeSuspended());
1003 browser()->window()->Close();
1004 close_observer.Wait();
1005 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1006 EXPECT_TRUE(chrome::BrowserIterator().done());
1007 EXPECT_FALSE(IsBackgroundModeSuspended());
1008}
1009
1010// Check that closing all browsers with no browser windows open suspends
1011// background mode but does not cause Chrome to quit.
1012IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1013 CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
1014 RepeatedNotificationObserver close_observer(
1015 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1016 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]9a718b22014-02-25 15:17:271017 chrome::IncrementKeepAliveCount();
[email protected]0c95faf42013-10-28 06:27:201018 browser()->window()->Close();
1019 close_observer.Wait();
1020 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1021 EXPECT_TRUE(chrome::BrowserIterator().done());
1022 EXPECT_FALSE(IsBackgroundModeSuspended());
1023
1024 chrome::CloseAllBrowsers();
1025 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1026 EXPECT_TRUE(chrome::BrowserIterator().done());
1027 EXPECT_TRUE(IsBackgroundModeSuspended());
1028}
1029
1030INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1031 BrowserCloseManagerWithBackgroundModeBrowserTest,
1032 testing::Bool());