blob: f0d21f86a943ef963f5d64ce36c29ac4eacd7c4f [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
dchenge73d8520c2015-12-27 01:19:095#include "chrome/browser/lifetime/browser_close_manager.h"
6
7#include <utility>
[email protected]2e9d79f2013-08-16 05:45:568#include <vector>
9
10#include "base/command_line.h"
[email protected]0ed53872014-05-10 05:51:0511#include "base/files/scoped_temp_dir.h"
avi6846aef2015-12-26 01:09:3812#include "base/macros.h"
jam3f2d3932017-04-26 20:28:5113#include "base/threading/thread_restrictions.h"
avi6846aef2015-12-26 01:09:3814#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2015#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1416#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5617#include "chrome/browser/browser_shutdown.h"
18#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3819#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4220#include "chrome/browser/download/chrome_download_manager_delegate.h"
peterccb33e82017-05-02 19:00:4421#include "chrome/browser/download/download_core_service.h"
22#include "chrome/browser/download/download_core_service_factory.h"
[email protected]0ed53872014-05-10 05:51:0523#include "chrome/browser/download/download_prefs.h"
[email protected]2e9d79f2013-08-16 05:45:5624#include "chrome/browser/lifetime/application_lifetime.h"
dgn02377782016-03-12 00:58:3825#include "chrome/browser/lifetime/keep_alive_types.h"
26#include "chrome/browser/lifetime/scoped_keep_alive.h"
[email protected]edfca702013-08-16 08:58:1427#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5628#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1429#include "chrome/browser/profiles/profile.h"
30#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4031#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5632#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2033#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3734#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1435#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5636#include "chrome/browser/ui/tabs/tab_strip_model.h"
37#include "chrome/common/chrome_switches.h"
38#include "chrome/common/url_constants.h"
39#include "chrome/test/base/in_process_browser_test.h"
40#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4241#include "components/app_modal/javascript_app_modal_dialog.h"
42#include "components/app_modal/native_app_modal_dialog.h"
mkolom65a0b3c2017-03-02 06:11:4043#include "components/sessions/core/tab_restore_service.h"
44#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1445#include "content/public/browser/browser_context.h"
46#include "content/public/browser/download_item.h"
47#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5648#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3449#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4650#include "content/public/browser/render_view_host.h"
51#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5652#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4053#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1454#include "content/public/test/download_test_observer.h"
[email protected]2e9d79f2013-08-16 05:45:5655#include "content/public/test/test_navigation_observer.h"
56#include "net/test/embedded_test_server/embedded_test_server.h"
xunjieli0332c192014-09-10 23:23:3157#include "net/test/url_request/url_request_mock_http_job.h"
jam8e45cd72015-01-20 16:33:4458#include "net/test/url_request/url_request_slow_download_job.h"
[email protected]2e9d79f2013-08-16 05:45:5659
[email protected]f9357a442014-05-15 18:44:0760#if defined(OS_CHROMEOS)
61#include "chromeos/chromeos_switches.h"
62#endif
63
[email protected]2e9d79f2013-08-16 05:45:5664namespace {
65
oshima0929be2a2014-11-19 22:21:0366app_modal::NativeAppModalDialog* GetNextDialog() {
avi373e72a2017-05-26 20:33:5267 app_modal::JavaScriptAppModalDialog* dialog =
68 ui_test_utils::WaitForAppModalDialog();
69 CHECK(dialog->native_dialog());
70 return dialog->native_dialog();
oshima82f72482014-10-24 14:14:3271}
[email protected]2e9d79f2013-08-16 05:45:5672
avi336125f72017-05-06 22:25:4073// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4674// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3275void AcceptClose() {
76 GetNextDialog()->AcceptAppModalDialog();
77}
[email protected]2e9d79f2013-08-16 05:45:5678
avi336125f72017-05-06 22:25:4079// Note: call |PrepareForDialog| on the relevant WebContents or Browser before
metaflow812bb8542016-05-24 21:10:4680// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3281void CancelClose() {
82 GetNextDialog()->CancelAppModalDialog();
83}
[email protected]2e9d79f2013-08-16 05:45:5684
Scott Violet1f106b582017-07-12 15:49:5885class RepeatedNotificationObserver : public content::NotificationObserver {
86 public:
87 explicit RepeatedNotificationObserver(int type, int count)
88 : num_outstanding_(count), running_(false) {
89 registrar_.Add(this, type, content::NotificationService::AllSources());
90 }
91
92 void Observe(int type,
93 const content::NotificationSource& source,
94 const content::NotificationDetails& details) override {
95 ASSERT_GT(num_outstanding_, 0);
96 if (!--num_outstanding_ && running_) {
97 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
98 run_loop_.QuitClosure());
99 }
100 }
101
102 void Wait() {
103 if (num_outstanding_ <= 0)
104 return;
105
106 running_ = true;
107 run_loop_.Run();
108 running_ = false;
109 }
110
111 private:
112 int num_outstanding_;
113 content::NotificationRegistrar registrar_;
114 bool running_;
115 base::RunLoop run_loop_;
116
117 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
118};
119
mkolom65a0b3c2017-03-02 06:11:40120class TabRestoreServiceChangesObserver
121 : public sessions::TabRestoreServiceObserver {
122 public:
123 explicit TabRestoreServiceChangesObserver(Profile* profile)
124 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
125 if (service_)
126 service_->AddObserver(this);
127 }
128
129 ~TabRestoreServiceChangesObserver() override {
130 if (service_)
131 service_->RemoveObserver(this);
132 }
133
134 size_t changes_count() const { return changes_count_; }
135
136 private:
137 // sessions::TabRestoreServiceObserver:
138 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
139 changes_count_++;
140 }
141
142 // sessions::TabRestoreServiceObserver:
143 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
144 service_ = nullptr;
145 }
146
147 sessions::TabRestoreService* service_ = nullptr;
148 size_t changes_count_ = 0;
149
150 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
151};
152
[email protected]edfca702013-08-16 08:58:14153class TestBrowserCloseManager : public BrowserCloseManager {
154 public:
155 enum UserChoice {
156 USER_CHOICE_USER_CANCELS_CLOSE,
157 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42158 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14159 };
160
161 static void AttemptClose(UserChoice user_choice) {
162 scoped_refptr<BrowserCloseManager> browser_close_manager =
163 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20164 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14165 browser_close_manager->StartClosingBrowsers();
166 }
167
168 protected:
Daniel Chenga542fca2014-10-21 09:51:29169 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14170
Daniel Chenga542fca2014-10-21 09:51:29171 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14172 int download_count,
mostynb2b52d1db2014-10-07 02:47:17173 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42174 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14175 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42176 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14177 case USER_CHOICE_USER_CANCELS_CLOSE: {
178 callback.Run(false);
179 break;
180 }
181 case USER_CHOICE_USER_ALLOWS_CLOSE: {
182 callback.Run(true);
183 break;
184 }
185 }
186 }
187
188 private:
189 explicit TestBrowserCloseManager(UserChoice user_choice)
190 : user_choice_(user_choice) {}
191
192 UserChoice user_choice_;
193
194 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
195};
196
[email protected]422a7d12013-10-21 12:10:42197class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
198 public:
199 explicit TestDownloadManagerDelegate(Profile* profile)
200 : ChromeDownloadManagerDelegate(profile) {
[email protected]861b4d0a2013-12-13 20:10:41201 GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42202 }
Daniel Chenga542fca2014-10-21 09:51:29203 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42204
Daniel Chenga542fca2014-10-21 09:51:29205 bool DetermineDownloadTarget(
[email protected]422a7d12013-10-21 12:10:42206 content::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17207 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42208 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41209 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42210 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
211 item, dangerous_callback);
212 }
213
Asanka Herath1ba0e9f2017-04-03 18:48:53214 static void SetDangerous(const content::DownloadTargetCallback& callback,
215 const base::FilePath& target_path,
216 content::DownloadItem::TargetDisposition disp,
217 content::DownloadDangerType danger_type,
218 const base::FilePath& intermediate_path,
219 content::DownloadInterruptReason reason) {
220 callback.Run(target_path, disp, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
221 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42222 }
[email protected]422a7d12013-10-21 12:10:42223};
224
[email protected]0c95faf42013-10-28 06:27:20225class FakeBackgroundModeManager : public BackgroundModeManager {
226 public:
227 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54228 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
229 &g_browser_process->profile_manager()
230 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20231 suspended_(false) {}
232
Daniel Chenga542fca2014-10-21 09:51:29233 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20234 BackgroundModeManager::SuspendBackgroundMode();
235 suspended_ = true;
236 }
237
Daniel Chenga542fca2014-10-21 09:51:29238 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20239 BackgroundModeManager::ResumeBackgroundMode();
240 suspended_ = false;
241 }
242
243 bool IsBackgroundModeSuspended() {
244 return suspended_;
245 }
246
247 private:
248 bool suspended_;
249
250 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
251};
252
[email protected]2e9d79f2013-08-16 05:45:56253} // namespace
254
255class BrowserCloseManagerBrowserTest
256 : public InProcessBrowserTest,
257 public testing::WithParamInterface<bool> {
258 protected:
Daniel Chenga542fca2014-10-21 09:51:29259 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56260 SessionStartupPref::SetStartupPref(
261 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
262 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14263 content::BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33264 content::BrowserThread::IO, FROM_HERE,
265 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
[email protected]2e9d79f2013-08-16 05:45:56266 }
267
avi556c05022014-12-22 23:31:43268 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56269 if (GetParam())
270 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07271#if defined(OS_CHROMEOS)
272 command_line->AppendSwitch(
273 chromeos::switches::kIgnoreUserProfileMappingForTests);
274#endif
[email protected]2e9d79f2013-08-16 05:45:56275 }
276
[email protected]edfca702013-08-16 08:58:14277 void CreateStalledDownload(Browser* browser) {
278 content::DownloadTestObserverInProgress observer(
279 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
280 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19281 browser, GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl),
282 WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14283 ui_test_utils::BROWSER_TEST_NONE);
284 observer.WaitForFinished();
285 EXPECT_EQ(
286 1UL,
287 observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS));
288 }
289
avi336125f72017-05-06 22:25:40290 void PrepareForDialog(content::WebContents* web_contents) {
291 content::PrepContentsForBeforeUnloadTest(web_contents);
metaflow812bb8542016-05-24 21:10:46292 }
293
avi336125f72017-05-06 22:25:40294 void PrepareForDialog(Browser* browser) {
metaflow812bb8542016-05-24 21:10:46295 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
avi336125f72017-05-06 22:25:40296 PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i));
metaflow812bb8542016-05-24 21:10:46297 }
298
[email protected]2e9d79f2013-08-16 05:45:56299 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56300};
301
302IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
svaldeza01f7d92015-11-18 17:47:56303 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56304 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
305 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40306 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46307
Scott Violet1f106b582017-07-12 15:49:58308 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56309 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 EXPECT_EQ(1, browser()->tab_strip_model()->count());
315
Scott Violet1f106b582017-07-12 15:49:58316 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56317 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20318 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32319 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56320 close_observer.Wait();
321 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37322 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56323}
324
325IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
326 TestShutdownMoreThanOnce) {
svaldeza01f7d92015-11-18 17:47:56327 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56328 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
329 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40330 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46331
Scott Violet1f106b582017-07-12 15:49:58332 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56333 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20334 chrome::CloseAllBrowsersAndQuit();
335 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32336 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56337 cancel_observer.Wait();
338 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
339 EXPECT_EQ(1, browser()->tab_strip_model()->count());
340
Scott Violet1f106b582017-07-12 15:49:58341 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56342 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20343 chrome::CloseAllBrowsersAndQuit();
344 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32345 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56346 close_observer.Wait();
347 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37348 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56349}
350
wafflesb63234052015-08-07 02:44:02351IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46352 PRE_TestSessionRestore) {
svaldeza01f7d92015-11-18 17:47:56353 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56354 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
355 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
356 AddBlankTabAndShow(browser());
357 ASSERT_NO_FATAL_FAILURE(
358 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
avi336125f72017-05-06 22:25:40359 PrepareForDialog(browser());
metaflow812bb8542016-05-24 21:10:46360
Scott Violet1f106b582017-07-12 15:49:58361 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56362 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20363 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32364 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56365 cancel_observer.Wait();
366 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
367
368 browser()->tab_strip_model()
369 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
370 content::TestNavigationObserver navigation_observer(
371 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19372 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
373 browser(), GURL(chrome::kChromeUIVersionURL),
374 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32375 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56376 navigation_observer.Wait();
377
Scott Violet1f106b582017-07-12 15:49:58378 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56379 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20380 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56381 close_observer.Wait();
382 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37383 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56384}
385
386// Test that the tab closed after the aborted shutdown attempt is not re-opened
387// when restoring the session.
benwells70cc93222017-06-29 05:42:36388// Flaky on Windows trybots, see https://crbug.com/737860.
389#if defined(OS_WIN)
390#define MAYBE_TestSessionRestore DISABLED_TestSessionRestore
391#else
392#define MAYBE_TestSessionRestore TestSessionRestore
393#endif
wafflesb63234052015-08-07 02:44:02394IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
benwells70cc93222017-06-29 05:42:36395 MAYBE_TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56396 // The testing framework launches Chrome with about:blank as args.
397 EXPECT_EQ(2, browser()->tab_strip_model()->count());
398 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
399 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
400 EXPECT_EQ(GURL("about:blank"),
401 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
402}
403
404// Test that browser windows are only closed if all browsers are ready to close
405// and that all beforeunload dialogs are shown again after a cancel.
[email protected]40cd13a2013-10-24 13:42:01406IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
svaldeza01f7d92015-11-18 17:47:56407 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56408 browsers_.push_back(CreateBrowser(browser()->profile()));
409 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
410 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
411 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
412 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40413 PrepareForDialog(browsers_[0]);
414 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56415
416 // Cancel shutdown on the first beforeunload event.
417 {
Scott Violet1f106b582017-07-12 15:49:58418 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56419 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20420 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32421 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56422 cancel_observer.Wait();
423 }
424 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
425 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
426 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
427
428 // Cancel shutdown on the second beforeunload event.
429 {
Scott Violet1f106b582017-07-12 15:49:58430 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56431 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20432 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32433 ASSERT_NO_FATAL_FAILURE(AcceptClose());
434 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56435 cancel_observer.Wait();
436 }
437 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
438 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
439 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
440
441 // Allow shutdown for both beforeunload events.
Scott Violet1f106b582017-07-12 15:49:58442 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56443 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20444 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32445 ASSERT_NO_FATAL_FAILURE(AcceptClose());
446 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56447 close_observer.Wait();
448 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37449 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56450}
451
452// Test that tabs in the same window with a beforeunload event that hangs are
453// treated the same as the user accepting the close, but do not close the tab
454// early.
455IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46456 TestHangInBeforeUnloadMultipleTabs) {
svaldeza01f7d92015-11-18 17:47:56457 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56458 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
459 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
460 AddBlankTabAndShow(browsers_[0]);
461 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
462 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
463 AddBlankTabAndShow(browsers_[0]);
464 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
465 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46466 // Disable the hang monitor in the tab that is not expected to hang, so that
467 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40468 PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56469
Scott Violet1f106b582017-07-12 15:49:58470 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56471 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20472 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32473 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56474 cancel_observer.Wait();
475 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
476 // All tabs should still be open.
477 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
478
Scott Violet1f106b582017-07-12 15:49:58479 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56480 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20481 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32482 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56483 close_observer.Wait();
484 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37485 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56486}
487
488// Test that tabs in different windows with a beforeunload event that hangs are
489// treated the same as the user accepting the close, but do not close the tab
490// early.
491IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
492 TestHangInBeforeUnloadMultipleWindows) {
svaldeza01f7d92015-11-18 17:47:56493 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56494 browsers_.push_back(CreateBrowser(browser()->profile()));
495 browsers_.push_back(CreateBrowser(browser()->profile()));
496 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
497 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
498 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
499 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
500 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
501 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46502 // Disable the hang monitor in the tab that is not expected to hang, so that
503 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40504 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56505
Scott Violet1f106b582017-07-12 15:49:58506 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56507 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20508 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32509 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56510 cancel_observer.Wait();
511 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
512 // All windows should still be open.
513 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
514 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
515 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
516
Scott Violet1f106b582017-07-12 15:49:58517 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56518 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20519 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32520 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56521 close_observer.Wait();
522 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37523 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56524}
525
metaflow73fc9552016-05-23 18:26:48526// Test that tabs that are slow to respond are not closed prematurely.
527// Regression for crbug.com/365052 caused some of tabs to be closed even if
528// user chose to cancel browser close.
metaflow73fc9552016-05-23 18:26:48529IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46530 TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48531 ASSERT_TRUE(embedded_test_server()->Start());
532 const int kTabCount = 5;
533 const int kResposiveTabIndex = 2;
534 // Create tab strip with all tabs except one responding after
535 // RenderViewHostImpl::kUnloadTimeoutMS.
536 // Minimum configuration is two slow tabs and then responsive tab.
537 // But we also want to check how slow tabs behave in tail.
538 for (int i = 0; i < kTabCount; i++) {
539 if (i)
540 AddBlankTabAndShow(browsers_[0]);
541 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
542 browsers_[0],
543 embedded_test_server()->GetURL((i == kResposiveTabIndex)
544 ? "/beforeunload.html"
545 : "/beforeunload_slow.html")));
546 }
metaflow812bb8542016-05-24 21:10:46547 // Disable the hang monitor in the tab that is not expected to hang, so that
548 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40549 PrepareForDialog(
metaflow812bb8542016-05-24 21:10:46550 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
551
Scott Violet1f106b582017-07-12 15:49:58552 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48553 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
554 chrome::CloseAllBrowsersAndQuit();
555 ASSERT_NO_FATAL_FAILURE(CancelClose());
556 cancel_observer.Wait();
557 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
558
559 // All tabs should still be open.
560 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
Scott Violet1f106b582017-07-12 15:49:58561 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48562 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
563
564 // Quit, this time accepting close confirmation dialog.
565 chrome::CloseAllBrowsersAndQuit();
566 ASSERT_NO_FATAL_FAILURE(AcceptClose());
567 close_observer.Wait();
568 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
569 EXPECT_TRUE(BrowserList::GetInstance()->empty());
570}
571
572// Test that tabs in different windows with a slow beforeunload event response
573// are treated the same as the user accepting the close, but do not close the
574// tab early.
575// Regression for crbug.com/365052 caused CHECK in tabstrip.
metaflow73fc9552016-05-23 18:26:48576IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46577 TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48578 ASSERT_TRUE(embedded_test_server()->Start());
579 const int kBrowserCount = 5;
580 const int kResposiveBrowserIndex = 2;
581 // Create multiple browsers with all tabs except one responding after
582 // RenderViewHostImpl::kUnloadTimeoutMS .
583 // Minimum configuration is just one browser with slow tab and then
584 // browser with responsive tab.
585 // But we also want to check how slow tabs behave in tail and make test
586 // more robust.
587 for (int i = 0; i < kBrowserCount; i++) {
588 if (i)
589 browsers_.push_back(CreateBrowser(browser()->profile()));
590 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
591 browsers_[i],
592 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
593 ? "/beforeunload.html"
594 : "/beforeunload_slow.html")));
595 }
metaflow812bb8542016-05-24 21:10:46596 // Disable the hang monitor in the tab that is not expected to hang, so that
597 // the dialog is guaranteed to show.
avi336125f72017-05-06 22:25:40598 PrepareForDialog(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48599
Scott Violet1f106b582017-07-12 15:49:58600 RepeatedNotificationObserver cancel_observer(
metaflow73fc9552016-05-23 18:26:48601 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
602 chrome::CloseAllBrowsersAndQuit();
603 ASSERT_NO_FATAL_FAILURE(CancelClose());
604 cancel_observer.Wait();
605 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
606
607 // All windows should still be open.
608 for (int i = 0; i < kBrowserCount; i++)
609 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
610
611 // Quit, this time accepting close confirmation dialog.
Scott Violet1f106b582017-07-12 15:49:58612 RepeatedNotificationObserver close_observer(
metaflow73fc9552016-05-23 18:26:48613 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
614 chrome::CloseAllBrowsersAndQuit();
615 ASSERT_NO_FATAL_FAILURE(AcceptClose());
616 close_observer.Wait();
617 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
618 EXPECT_TRUE(BrowserList::GetInstance()->empty());
619}
620
Jan Krcal9a23f222017-08-01 14:13:07621// Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081.
622#if defined(OS_WIN) && !defined(NDEBUG)
623#define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown
624#else
625#define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown
626#endif
627
[email protected]2e9d79f2013-08-16 05:45:56628// Test that a window created during shutdown is closed.
629IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
Jan Krcal9a23f222017-08-01 14:13:07630 MAYBE_TestAddWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56631 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56632 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
633 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40634 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56635
Scott Violet1f106b582017-07-12 15:49:58636 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56637 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20638 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56639 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32640 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56641 close_observer.Wait();
642 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37643 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56644}
645
646// Test that a window created during shutdown with a beforeunload handler can
647// cancel the shutdown.
648IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01649 TestAddWindowWithBeforeUnloadDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56650 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56651 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
652 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40653 PrepareForDialog(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56654
Scott Violet1f106b582017-07-12 15:49:58655 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56656 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20657 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56658 browsers_.push_back(CreateBrowser(browser()->profile()));
659 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
660 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40661 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32662 ASSERT_NO_FATAL_FAILURE(AcceptClose());
663 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56664 cancel_observer.Wait();
665 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
666 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
667 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
668
669 // Allow shutdown for both beforeunload dialogs.
Scott Violet1f106b582017-07-12 15:49:58670 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56671 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20672 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32673 ASSERT_NO_FATAL_FAILURE(AcceptClose());
674 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56675 close_observer.Wait();
676 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37677 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56678}
679
680// Test that tabs added during shutdown are closed.
681IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46682 TestAddTabDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56683 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56684 browsers_.push_back(CreateBrowser(browser()->profile()));
685 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
686 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
687 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
688 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40689 PrepareForDialog(browsers_[0]);
690 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56691
Scott Violet1f106b582017-07-12 15:49:58692 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56693 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20694 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32695 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56696 AddBlankTabAndShow(browsers_[0]);
697 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32698 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56699 close_observer.Wait();
700 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37701 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56702}
703
704// Test that tabs created during shutdown with beforeunload handlers can cancel
705// the shutdown.
metaflow812bb8542016-05-24 21:10:46706
[email protected]2e9d79f2013-08-16 05:45:56707IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46708 TestAddTabWithBeforeUnloadDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56709 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56710 browsers_.push_back(CreateBrowser(browser()->profile()));
711 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
712 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
713 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
714 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40715 PrepareForDialog(browsers_[0]);
716 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46717
Scott Violet1f106b582017-07-12 15:49:58718 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56719 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20720 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32721 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56722 AddBlankTabAndShow(browsers_[0]);
723 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
724 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
725 AddBlankTabAndShow(browsers_[1]);
726 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
727 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40728 PrepareForDialog(browsers_[0]);
729 PrepareForDialog(browsers_[1]);
oshima82f72482014-10-24 14:14:32730 ASSERT_NO_FATAL_FAILURE(AcceptClose());
731 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56732 cancel_observer.Wait();
733 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
734 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
735 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
736
Scott Violet1f106b582017-07-12 15:49:58737 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56738 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20739 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32740 ASSERT_NO_FATAL_FAILURE(AcceptClose());
741 ASSERT_NO_FATAL_FAILURE(AcceptClose());
742 ASSERT_NO_FATAL_FAILURE(AcceptClose());
743 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56744
745 close_observer.Wait();
746 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37747 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56748}
749
rogermf18929c2017-04-19 19:59:33750// TODO(crbug/713201):
751// BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac.
752#if defined(OS_MACOSX)
753#define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing
754#else
755#define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing
756#endif
757
[email protected]2e9d79f2013-08-16 05:45:56758IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
rogermf18929c2017-04-19 19:59:33759 MAYBE_AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40760 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
761 // And it is difficult to fix this issue without fixing that one.
762 if (GetParam())
763 return;
764
765 ASSERT_TRUE(embedded_test_server()->Start());
766 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
767 browser(), embedded_test_server()->GetURL("/title1.html")));
768
769 // Open second window.
770 ui_test_utils::NavigateToURLWithDisposition(
771 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
772 WindowOpenDisposition::NEW_WINDOW,
773 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
774 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
775 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
776 ? BrowserList::GetInstance()->get(0)
777 : BrowserList::GetInstance()->get(1);
778 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
779
780 // Let's work with second window only.
781 // This page has beforeunload handler already.
782 EXPECT_TRUE(browser2->tab_strip_model()
783 ->GetWebContentsAt(0)
784 ->NeedToFireBeforeUnload());
785 // This page doesn't have beforeunload handler. Yet.
786 ui_test_utils::NavigateToURLWithDisposition(
787 browser2, embedded_test_server()->GetURL("/title2.html"),
788 WindowOpenDisposition::NEW_FOREGROUND_TAB,
789 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
790 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
791 EXPECT_FALSE(browser2->tab_strip_model()
792 ->GetWebContentsAt(1)
793 ->NeedToFireBeforeUnload());
794 EXPECT_EQ(2, browser2->tab_strip_model()->count());
795
avi336125f72017-05-06 22:25:40796 PrepareForDialog(browser2);
mkolom65a0b3c2017-03-02 06:11:40797
798 // The test.
799
800 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
801 content::WindowedNotificationObserver observer(
802 chrome::NOTIFICATION_BROWSER_CLOSED,
803 content::NotificationService::AllSources());
804 chrome::CloseWindow(browser2);
805 // Just to be sure CloseWindow doesn't have asynchronous tasks
806 // that could have an impact.
807 content::RunAllPendingInMessageLoop();
808
809 // Closing browser shouldn't happen because of beforeunload handler.
810 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
811 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
812 // yet.
813 ASSERT_TRUE(content::ExecuteScript(
814 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(),
815 "window.addEventListener('beforeunload', "
816 "function(event) { event.returnValue = 'Foo'; });"));
817 EXPECT_TRUE(browser2->tab_strip_model()
818 ->GetWebContentsAt(1)
819 ->NeedToFireBeforeUnload());
820 // Accept closing the first tab.
821 ASSERT_NO_FATAL_FAILURE(AcceptClose());
822 // Just to be sure accepting a dialog doesn't have asynchronous tasks
823 // that could have an impact.
824 content::RunAllPendingInMessageLoop();
825 // It shouldn't close the whole window/browser.
826 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
827 EXPECT_EQ(2, browser2->tab_strip_model()->count());
828 // Accept closing the second tab.
829 ASSERT_NO_FATAL_FAILURE(AcceptClose());
830 observer.Wait();
831 // Now the second window/browser should be closed.
832 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
833 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
834 EXPECT_EQ(1u, restore_observer.changes_count());
835
836 // Restore the closed browser.
837 content::WindowedNotificationObserver open_window_observer(
838 chrome::NOTIFICATION_BROWSER_OPENED,
839 content::NotificationService::AllSources());
840 chrome::OpenWindowWithRestoredTabs(browser()->profile());
841 open_window_observer.Wait();
842 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
843 browser2 = BrowserList::GetInstance()->get(0) != browser()
844 ? BrowserList::GetInstance()->get(0)
845 : BrowserList::GetInstance()->get(1);
846
847 // Check the restored browser contents.
848 EXPECT_EQ(2, browser2->tab_strip_model()->count());
849 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
850 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
851 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
852 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
853}
854
855IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42856 TestCloseTabDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56857 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56858 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
859 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40860 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46861
Scott Violet1f106b582017-07-12 15:49:58862 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56863 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20864 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56865
866 browsers_.push_back(CreateBrowser(browser()->profile()));
867 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
868 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40869 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56870 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32871 ASSERT_NO_FATAL_FAILURE(CancelClose());
872 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56873 cancel_observer.Wait();
874 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
875 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
876 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
877
Scott Violet1f106b582017-07-12 15:49:58878 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56879 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20880 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56881 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32882 ASSERT_NO_FATAL_FAILURE(AcceptClose());
883 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56884
885 close_observer.Wait();
886 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37887 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56888}
889
890IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46891 TestOpenAndCloseWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56892 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56893 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
894 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40895 PrepareForDialog(browsers_[0]);
metaflow812bb8542016-05-24 21:10:46896
Scott Violet1f106b582017-07-12 15:49:58897 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56898 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20899 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56900
901 browsers_.push_back(CreateBrowser(browser()->profile()));
902 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
903 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40904 PrepareForDialog(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56905 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32906 ASSERT_NO_FATAL_FAILURE(CancelClose());
907 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56908 cancel_observer.Wait();
909 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
910 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
911 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
912
Scott Violet1f106b582017-07-12 15:49:58913 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56914 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20915 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56916 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32917 ASSERT_NO_FATAL_FAILURE(AcceptClose());
918 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56919
920 close_observer.Wait();
921 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37922 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56923}
924
925IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
926 TestCloseWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56927 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56928 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
929 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
930 browsers_.push_back(CreateBrowser(browser()->profile()));
931 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
932 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:40933 PrepareForDialog(browsers_[0]);
934 PrepareForDialog(browsers_[1]);
metaflow812bb8542016-05-24 21:10:46935
Scott Violet1f106b582017-07-12 15:49:58936 RepeatedNotificationObserver cancel_observer(
[email protected]2e9d79f2013-08-16 05:45:56937 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20938 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56939
940 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32941 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56942 cancel_observer.Wait();
943 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
944 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
945 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
946
Scott Violet1f106b582017-07-12 15:49:58947 RepeatedNotificationObserver close_observer(
[email protected]2e9d79f2013-08-16 05:45:56948 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20949 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56950 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32951 ASSERT_NO_FATAL_FAILURE(AcceptClose());
952 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56953
954 close_observer.Wait();
955 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37956 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56957}
958
[email protected]0ed53872014-05-10 05:51:05959INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
960 BrowserCloseManagerBrowserTest,
961 testing::Bool());
962
963class BrowserCloseManagerWithDownloadsBrowserTest :
964 public BrowserCloseManagerBrowserTest {
965 public:
966 BrowserCloseManagerWithDownloadsBrowserTest() {}
967 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
968
Daniel Chenga542fca2014-10-21 09:51:29969 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05970 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
971 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
972 }
973
974 void SetDownloadPathForProfile(Profile* profile) {
975 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
976 download_prefs->SetDownloadPath(download_path());
977 }
978
979 const base::FilePath& download_path() const {
vabr0c237ae2016-09-14 09:24:28980 return scoped_download_directory_.GetPath();
[email protected]0ed53872014-05-10 05:51:05981 }
982
983 private:
984 base::ScopedTempDir scoped_download_directory_;
985};
986
jackhouae8e6e52015-05-29 06:36:46987// Mac has its own in-progress download prompt in app_controller_mac.mm, so
988// BrowserCloseManager should simply close all browsers. If there are no
989// browsers, it should not crash.
990#if defined(OS_MACOSX)
991IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
992 TestWithDownloads) {
svaldeza01f7d92015-11-18 17:47:56993 ASSERT_TRUE(embedded_test_server()->Start());
jackhouae8e6e52015-05-29 06:36:46994 SetDownloadPathForProfile(browser()->profile());
995 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
996
Scott Violet1f106b582017-07-12 15:49:58997 RepeatedNotificationObserver close_observer(
jackhouae8e6e52015-05-29 06:36:46998 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
999
1000 TestBrowserCloseManager::AttemptClose(
1001 TestBrowserCloseManager::NO_USER_CHOICE);
1002 close_observer.Wait();
1003 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371004 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441005 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
jackhouae8e6e52015-05-29 06:36:461006
1007 // Attempting to close again should not crash.
1008 TestBrowserCloseManager::AttemptClose(
1009 TestBrowserCloseManager::NO_USER_CHOICE);
1010}
1011#else // defined(OS_MACOSX)
1012
[email protected]422a7d12013-10-21 12:10:421013// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:051014IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:421015 TestWithDangerousUrlDownload) {
svaldeza01f7d92015-11-18 17:47:561016 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051017 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:421018
1019 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:351020 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:421021 new TestDownloadManagerDelegate(browser()->profile()));
peterccb33e82017-05-02 19:00:441022 DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091023 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421024
1025 // Run a dangerous download, but the user doesn't make a decision.
1026 // This .swf normally would be categorized as DANGEROUS_FILE, but
1027 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
tfarina5e7b57232015-10-17 23:37:401028 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(
1029 "downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421030 content::DownloadTestObserverInterrupted observer(
1031 content::BrowserContext::GetDownloadManager(browser()->profile()),
1032 1,
1033 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1034 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191035 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421036 ui_test_utils::BROWSER_TEST_NONE);
1037 observer.WaitForFinished();
1038
1039 // Check that the download manager has the expected state.
1040 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1041 browser()->profile())->InProgressCount());
1042 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1043 browser()->profile())->NonMaliciousInProgressCount());
1044
1045 // Close the browser with no user action.
Scott Violet1f106b582017-07-12 15:49:581046 RepeatedNotificationObserver close_observer(
[email protected]422a7d12013-10-21 12:10:421047 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1048 TestBrowserCloseManager::AttemptClose(
1049 TestBrowserCloseManager::NO_USER_CHOICE);
1050 close_observer.Wait();
1051 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371052 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421053}
1054
[email protected]edfca702013-08-16 08:58:141055// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:051056IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1057 TestWithDownloads) {
svaldeza01f7d92015-11-18 17:47:561058 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051059 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141060 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1061 content::TestNavigationObserver navigation_observer(
1062 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1063 TestBrowserCloseManager::AttemptClose(
1064 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1065 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1066 navigation_observer.Wait();
1067 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1068 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1069
Scott Violet1f106b582017-07-12 15:49:581070 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141071 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1072
1073 TestBrowserCloseManager::AttemptClose(
1074 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1075 close_observer.Wait();
1076 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371077 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381078 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441079 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381080 else
peterccb33e82017-05-02 19:00:441081 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141082}
1083
sammccd347712015-03-13 22:02:321084// Test shutdown with a download in progress in an off-the-record profile.
1085IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1086 TestWithOffTheRecordDownloads) {
svaldeza01f7d92015-11-18 17:47:561087 ASSERT_TRUE(embedded_test_server()->Start());
sammccd347712015-03-13 22:02:321088 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1089 SetDownloadPathForProfile(otr_profile);
1090 Browser* otr_browser = CreateBrowser(otr_profile);
1091 {
Scott Violet1f106b582017-07-12 15:49:581092 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321093 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1094 browser()->window()->Close();
1095 close_observer.Wait();
1096 }
1097 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1098 content::TestNavigationObserver navigation_observer(
1099 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1100 TestBrowserCloseManager::AttemptClose(
1101 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1102 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1103 navigation_observer.Wait();
1104 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1105 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1106
Scott Violet1f106b582017-07-12 15:49:581107 RepeatedNotificationObserver close_observer(
sammccd347712015-03-13 22:02:321108 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1109
1110 TestBrowserCloseManager::AttemptClose(
1111 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1112 close_observer.Wait();
1113 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371114 EXPECT_TRUE(BrowserList::GetInstance()->empty());
peterccb33e82017-05-02 19:00:441115 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
sammccd347712015-03-13 22:02:321116}
1117
Asanka Herathd1341dc2017-04-19 18:28:391118// Test shutdown with a download in progress in a regular profile an inconito
1119// browser is opened and closed. While there are active downloads, closing the
1120// incognito window shouldn't block on the active downloads which belong to the
1121// parent profile.
1122IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1123 TestWithOffTheRecordWindowAndRegularDownload) {
1124 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1125 SetDownloadPathForProfile(otr_profile);
1126 Browser* otr_browser = CreateBrowser(otr_profile);
1127 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1128
1129 content::TestNavigationObserver navigation_observer(
1130 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1131 ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank"));
1132 navigation_observer.Wait();
1133
1134 int num_downloads_blocking = 0;
1135 ASSERT_EQ(
1136 Browser::DOWNLOAD_CLOSE_OK,
1137 otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1138 ASSERT_EQ(0, num_downloads_blocking);
1139
1140 {
Scott Violet1f106b582017-07-12 15:49:581141 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391142 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1143 otr_browser->window()->Close();
1144 close_observer.Wait();
1145 }
1146
1147 ASSERT_EQ(
1148 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
1149 browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking));
1150 ASSERT_EQ(1, num_downloads_blocking);
1151
1152 {
Scott Violet1f106b582017-07-12 15:49:581153 RepeatedNotificationObserver close_observer(
Asanka Herathd1341dc2017-04-19 18:28:391154 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1155 TestBrowserCloseManager::AttemptClose(
1156 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1157 close_observer.Wait();
1158 }
1159
1160 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
1161 EXPECT_TRUE(BrowserList::GetInstance()->empty());
1162 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441163 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391164 else
peterccb33e82017-05-02 19:00:441165 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
Asanka Herathd1341dc2017-04-19 18:28:391166}
1167
[email protected]edfca702013-08-16 08:58:141168// Test shutdown with a download in progress from one profile, where the only
1169// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:051170IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:141171 TestWithDownloadsFromDifferentProfiles) {
1172 ProfileManager* profile_manager = g_browser_process->profile_manager();
jam3f2d3932017-04-26 20:28:511173 Profile* other_profile = nullptr;
1174 {
1175 base::FilePath path =
1176 profile_manager->user_data_dir().AppendASCII("test_profile");
1177 base::ThreadRestrictions::ScopedAllowIO allow_io;
1178 if (!base::PathExists(path))
1179 ASSERT_TRUE(base::CreateDirectory(path));
1180 other_profile =
1181 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1182 }
[email protected]edfca702013-08-16 08:58:141183 profile_manager->RegisterTestingProfile(other_profile, true, false);
1184 Browser* other_profile_browser = CreateBrowser(other_profile);
1185
svaldeza01f7d92015-11-18 17:47:561186 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051187 SetDownloadPathForProfile(browser()->profile());
1188 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:141189 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1190 {
Scott Violet1f106b582017-07-12 15:49:581191 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141192 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1193 browser()->window()->Close();
1194 close_observer.Wait();
1195 }
1196
1197 // When the shutdown is cancelled, the downloads page should be opened in a
1198 // browser for that profile. Because there are no browsers for that profile, a
1199 // new browser should be opened.
1200 ui_test_utils::BrowserAddedObserver new_browser_observer;
1201 TestBrowserCloseManager::AttemptClose(
1202 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1203 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1204 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1205 EXPECT_EQ(
1206 GURL(chrome::kChromeUIDownloadsURL),
1207 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1208 EXPECT_EQ(GURL("about:blank"),
1209 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1210 ->GetURL());
1211
Scott Violet1f106b582017-07-12 15:49:581212 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141213 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1214 TestBrowserCloseManager::AttemptClose(
1215 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1216 close_observer.Wait();
1217 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371218 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381219 if (browser_defaults::kBrowserAliveWithNoWindows)
peterccb33e82017-05-02 19:00:441220 EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]5fec4702013-11-20 04:37:381221 else
peterccb33e82017-05-02 19:00:441222 EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141223}
1224
Reza.Zakerinasab9d686a0d2017-07-26 15:22:561225// Fails on ChromeOS only. crbug.com/749098
1226#if defined(OS_CHROMEOS)
1227#define MAYBE_TestBeforeUnloadAndDownloads DISABLED_TestBeforeUnloadAndDownloads
1228#else
1229#define MAYBE_TestBeforeUnloadAndDownloads TestBeforeUnloadAndDownloads
1230#endif
[email protected]edfca702013-08-16 08:58:141231// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]0ed53872014-05-10 05:51:051232IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
Reza.Zakerinasab9d686a0d2017-07-26 15:22:561233 MAYBE_TestBeforeUnloadAndDownloads) {
svaldeza01f7d92015-11-18 17:47:561234 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051235 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141236 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1237 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1238 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
avi336125f72017-05-06 22:25:401239 PrepareForDialog(browser());
[email protected]edfca702013-08-16 08:58:141240
1241 content::WindowedNotificationObserver cancel_observer(
1242 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1243 content::NotificationService::AllSources());
1244 TestBrowserCloseManager::AttemptClose(
1245 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321246 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141247 cancel_observer.Wait();
1248 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1249
Scott Violet1f106b582017-07-12 15:49:581250 RepeatedNotificationObserver close_observer(
[email protected]edfca702013-08-16 08:58:141251 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1252 TestBrowserCloseManager::AttemptClose(
1253 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321254 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141255 close_observer.Wait();
1256 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371257 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141258}
1259
jackhouae8e6e52015-05-29 06:36:461260#endif // defined(OS_MACOSX)
1261
[email protected]0ed53872014-05-10 05:51:051262INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
1263 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561264 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201265
1266class BrowserCloseManagerWithBackgroundModeBrowserTest
1267 : public BrowserCloseManagerBrowserTest {
1268 public:
1269 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1270
Daniel Chenga542fca2014-10-21 09:51:291271 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201272 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1273 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351274 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201275 }
1276
1277 bool IsBackgroundModeSuspended() {
1278 return static_cast<FakeBackgroundModeManager*>(
1279 g_browser_process->background_mode_manager())
1280 ->IsBackgroundModeSuspended();
1281 }
1282
1283 private:
1284 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1285};
1286
1287// Check that background mode is suspended when closing all browsers unless we
1288// are quitting and that background mode is resumed when a new browser window is
1289// opened.
1290IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1291 CloseAllBrowsersWithBackgroundMode) {
1292 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351293 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201294 Profile* profile = browser()->profile();
1295 {
Scott Violet1f106b582017-07-12 15:49:581296 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201297 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381298 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1299 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201300 chrome::CloseAllBrowsers();
1301 close_observer.Wait();
1302 }
1303 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371304 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201305 EXPECT_TRUE(IsBackgroundModeSuspended());
1306
1307 // Background mode should be resumed when a new browser window is opened.
1308 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441309 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201310 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381311 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201312 EXPECT_FALSE(IsBackgroundModeSuspended());
Scott Violet1f106b582017-07-12 15:49:581313 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201314 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1315
1316 // Background mode should not be suspended when quitting.
1317 chrome::CloseAllBrowsersAndQuit();
1318 close_observer.Wait();
1319 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371320 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201321 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201322}
1323
1324// Check that closing the last browser window individually does not affect
1325// background mode.
1326IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161327 DISABLED_CloseSingleBrowserWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581328 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201329 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1330 EXPECT_FALSE(IsBackgroundModeSuspended());
1331 browser()->window()->Close();
1332 close_observer.Wait();
1333 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371334 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201335 EXPECT_FALSE(IsBackgroundModeSuspended());
1336}
1337
1338// Check that closing all browsers with no browser windows open suspends
1339// background mode but does not cause Chrome to quit.
1340IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031341 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
Scott Violet1f106b582017-07-12 15:49:581342 RepeatedNotificationObserver close_observer(
[email protected]0c95faf42013-10-28 06:27:201343 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1344 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381345 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1346 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201347 browser()->window()->Close();
1348 close_observer.Wait();
1349 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371350 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201351 EXPECT_FALSE(IsBackgroundModeSuspended());
1352
1353 chrome::CloseAllBrowsers();
1354 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371355 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201356 EXPECT_TRUE(IsBackgroundModeSuspended());
1357}
1358
1359INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1360 BrowserCloseManagerWithBackgroundModeBrowserTest,
1361 testing::Bool());