blob: 63bbc8654d0139a4b20b1e6c75ad46da77a869f2 [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"
13#include "build/build_config.h"
[email protected]0c95faf42013-10-28 06:27:2014#include "chrome/browser/background/background_mode_manager.h"
[email protected]edfca702013-08-16 08:58:1415#include "chrome/browser/browser_process.h"
[email protected]2e9d79f2013-08-16 05:45:5616#include "chrome/browser/browser_shutdown.h"
17#include "chrome/browser/chrome_notification_types.h"
[email protected]5fec4702013-11-20 04:37:3818#include "chrome/browser/defaults.h"
[email protected]422a7d12013-10-21 12:10:4219#include "chrome/browser/download/chrome_download_manager_delegate.h"
[email protected]0ed53872014-05-10 05:51:0520#include "chrome/browser/download/download_prefs.h"
[email protected]422a7d12013-10-21 12:10:4221#include "chrome/browser/download/download_service.h"
22#include "chrome/browser/download/download_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5623#include "chrome/browser/lifetime/application_lifetime.h"
dgn02377782016-03-12 00:58:3824#include "chrome/browser/lifetime/keep_alive_types.h"
25#include "chrome/browser/lifetime/scoped_keep_alive.h"
[email protected]edfca702013-08-16 08:58:1426#include "chrome/browser/net/url_request_mock_util.h"
[email protected]2e9d79f2013-08-16 05:45:5627#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]edfca702013-08-16 08:58:1428#include "chrome/browser/profiles/profile.h"
29#include "chrome/browser/profiles/profile_manager.h"
mkolom65a0b3c2017-03-02 06:11:4030#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]2e9d79f2013-08-16 05:45:5631#include "chrome/browser/ui/browser.h"
[email protected]0c95faf42013-10-28 06:27:2032#include "chrome/browser/ui/browser_commands.h"
scottmg8abbff832016-01-28 22:57:3733#include "chrome/browser/ui/browser_list.h"
[email protected]edfca702013-08-16 08:58:1434#include "chrome/browser/ui/browser_window.h"
[email protected]2e9d79f2013-08-16 05:45:5635#include "chrome/browser/ui/tabs/tab_strip_model.h"
36#include "chrome/common/chrome_switches.h"
37#include "chrome/common/url_constants.h"
38#include "chrome/test/base/in_process_browser_test.h"
39#include "chrome/test/base/ui_test_utils.h"
oshimaf65398422014-11-18 23:30:4240#include "components/app_modal/javascript_app_modal_dialog.h"
41#include "components/app_modal/native_app_modal_dialog.h"
mkolom65a0b3c2017-03-02 06:11:4042#include "components/sessions/core/tab_restore_service.h"
43#include "components/sessions/core/tab_restore_service_observer.h"
[email protected]edfca702013-08-16 08:58:1444#include "content/public/browser/browser_context.h"
45#include "content/public/browser/download_item.h"
46#include "content/public/browser/download_manager.h"
[email protected]2e9d79f2013-08-16 05:45:5647#include "content/public/browser/notification_service.h"
avi64b8b65b2017-03-29 18:50:3448#include "content/public/browser/render_frame_host.h"
metaflow812bb8542016-05-24 21:10:4649#include "content/public/browser/render_view_host.h"
50#include "content/public/browser/render_widget_host.h"
[email protected]2e9d79f2013-08-16 05:45:5651#include "content/public/browser/web_contents.h"
mkolom65a0b3c2017-03-02 06:11:4052#include "content/public/test/browser_test_utils.h"
[email protected]edfca702013-08-16 08:58:1453#include "content/public/test/download_test_observer.h"
[email protected]2e9d79f2013-08-16 05:45:5654#include "content/public/test/test_navigation_observer.h"
55#include "net/test/embedded_test_server/embedded_test_server.h"
xunjieli0332c192014-09-10 23:23:3156#include "net/test/url_request/url_request_mock_http_job.h"
jam8e45cd72015-01-20 16:33:4457#include "net/test/url_request/url_request_slow_download_job.h"
[email protected]2e9d79f2013-08-16 05:45:5658
[email protected]f9357a442014-05-15 18:44:0759#if defined(OS_CHROMEOS)
60#include "chromeos/chromeos_switches.h"
61#endif
62
[email protected]2e9d79f2013-08-16 05:45:5663namespace {
64
oshima0929be2a2014-11-19 22:21:0365app_modal::NativeAppModalDialog* GetNextDialog() {
66 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
oshima82f72482014-10-24 14:14:3267 EXPECT_TRUE(dialog->IsJavaScriptModalDialog());
oshima0929be2a2014-11-19 22:21:0368 app_modal::JavaScriptAppModalDialog* js_dialog =
69 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
oshima82f72482014-10-24 14:14:3270 CHECK(js_dialog->native_dialog());
71 return js_dialog->native_dialog();
72}
[email protected]2e9d79f2013-08-16 05:45:5673
metaflow812bb8542016-05-24 21:10:4674// Note: call |DisableHangMonitor| on the relevant WebContents or Browser before
75// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3276void AcceptClose() {
77 GetNextDialog()->AcceptAppModalDialog();
78}
[email protected]2e9d79f2013-08-16 05:45:5679
metaflow812bb8542016-05-24 21:10:4680// Note: call |DisableHangMonitor| on the relevant WebContents or Browser before
81// trying to close it, to avoid flakiness. https://crbug.com/519646
oshima82f72482014-10-24 14:14:3282void CancelClose() {
83 GetNextDialog()->CancelAppModalDialog();
84}
[email protected]2e9d79f2013-08-16 05:45:5685
86class RepeatedNotificationObserver : public content::NotificationObserver {
87 public:
88 explicit RepeatedNotificationObserver(int type, int count)
89 : num_outstanding_(count), running_(false) {
90 registrar_.Add(this, type, content::NotificationService::AllSources());
91 }
92
Daniel Chenga542fca2014-10-21 09:51:2993 void Observe(int type,
94 const content::NotificationSource& source,
95 const content::NotificationDetails& details) override {
[email protected]2e9d79f2013-08-16 05:45:5696 ASSERT_GT(num_outstanding_, 0);
97 if (!--num_outstanding_ && running_) {
98 content::BrowserThread::PostTask(
99 content::BrowserThread::UI, FROM_HERE, run_loop_.QuitClosure());
100 }
101 }
102
103 void Wait() {
104 if (num_outstanding_ <= 0)
105 return;
106
107 running_ = true;
108 run_loop_.Run();
109 running_ = false;
110 }
111
112 private:
113 int num_outstanding_;
114 content::NotificationRegistrar registrar_;
115 bool running_;
116 base::RunLoop run_loop_;
117
118 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver);
119};
120
mkolom65a0b3c2017-03-02 06:11:40121class TabRestoreServiceChangesObserver
122 : public sessions::TabRestoreServiceObserver {
123 public:
124 explicit TabRestoreServiceChangesObserver(Profile* profile)
125 : service_(TabRestoreServiceFactory::GetForProfile(profile)) {
126 if (service_)
127 service_->AddObserver(this);
128 }
129
130 ~TabRestoreServiceChangesObserver() override {
131 if (service_)
132 service_->RemoveObserver(this);
133 }
134
135 size_t changes_count() const { return changes_count_; }
136
137 private:
138 // sessions::TabRestoreServiceObserver:
139 void TabRestoreServiceChanged(sessions::TabRestoreService*) override {
140 changes_count_++;
141 }
142
143 // sessions::TabRestoreServiceObserver:
144 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override {
145 service_ = nullptr;
146 }
147
148 sessions::TabRestoreService* service_ = nullptr;
149 size_t changes_count_ = 0;
150
151 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver);
152};
153
[email protected]edfca702013-08-16 08:58:14154class TestBrowserCloseManager : public BrowserCloseManager {
155 public:
156 enum UserChoice {
157 USER_CHOICE_USER_CANCELS_CLOSE,
158 USER_CHOICE_USER_ALLOWS_CLOSE,
[email protected]422a7d12013-10-21 12:10:42159 NO_USER_CHOICE
[email protected]edfca702013-08-16 08:58:14160 };
161
162 static void AttemptClose(UserChoice user_choice) {
163 scoped_refptr<BrowserCloseManager> browser_close_manager =
164 new TestBrowserCloseManager(user_choice);
[email protected]0c95faf42013-10-28 06:27:20165 browser_shutdown::SetTryingToQuit(true);
[email protected]edfca702013-08-16 08:58:14166 browser_close_manager->StartClosingBrowsers();
167 }
168
169 protected:
Daniel Chenga542fca2014-10-21 09:51:29170 ~TestBrowserCloseManager() override {}
[email protected]edfca702013-08-16 08:58:14171
Daniel Chenga542fca2014-10-21 09:51:29172 void ConfirmCloseWithPendingDownloads(
[email protected]edfca702013-08-16 08:58:14173 int download_count,
mostynb2b52d1db2014-10-07 02:47:17174 const base::Callback<void(bool)>& callback) override {
[email protected]422a7d12013-10-21 12:10:42175 EXPECT_NE(NO_USER_CHOICE, user_choice_);
[email protected]edfca702013-08-16 08:58:14176 switch (user_choice_) {
[email protected]422a7d12013-10-21 12:10:42177 case NO_USER_CHOICE:
[email protected]edfca702013-08-16 08:58:14178 case USER_CHOICE_USER_CANCELS_CLOSE: {
179 callback.Run(false);
180 break;
181 }
182 case USER_CHOICE_USER_ALLOWS_CLOSE: {
183 callback.Run(true);
184 break;
185 }
186 }
187 }
188
189 private:
190 explicit TestBrowserCloseManager(UserChoice user_choice)
191 : user_choice_(user_choice) {}
192
193 UserChoice user_choice_;
194
195 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager);
196};
197
[email protected]422a7d12013-10-21 12:10:42198class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
199 public:
200 explicit TestDownloadManagerDelegate(Profile* profile)
201 : ChromeDownloadManagerDelegate(profile) {
[email protected]861b4d0a2013-12-13 20:10:41202 GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1);
[email protected]422a7d12013-10-21 12:10:42203 }
Daniel Chenga542fca2014-10-21 09:51:29204 ~TestDownloadManagerDelegate() override {}
[email protected]422a7d12013-10-21 12:10:42205
Daniel Chenga542fca2014-10-21 09:51:29206 bool DetermineDownloadTarget(
[email protected]422a7d12013-10-21 12:10:42207 content::DownloadItem* item,
mostynb2b52d1db2014-10-07 02:47:17208 const content::DownloadTargetCallback& callback) override {
[email protected]422a7d12013-10-21 12:10:42209 content::DownloadTargetCallback dangerous_callback =
[email protected]861b4d0a2013-12-13 20:10:41210 base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback);
[email protected]422a7d12013-10-21 12:10:42211 return ChromeDownloadManagerDelegate::DetermineDownloadTarget(
212 item, dangerous_callback);
213 }
214
Asanka Herath1ba0e9f2017-04-03 18:48:53215 static void SetDangerous(const content::DownloadTargetCallback& callback,
216 const base::FilePath& target_path,
217 content::DownloadItem::TargetDisposition disp,
218 content::DownloadDangerType danger_type,
219 const base::FilePath& intermediate_path,
220 content::DownloadInterruptReason reason) {
221 callback.Run(target_path, disp, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
222 intermediate_path, reason);
[email protected]422a7d12013-10-21 12:10:42223 }
[email protected]422a7d12013-10-21 12:10:42224};
225
[email protected]0c95faf42013-10-28 06:27:20226class FakeBackgroundModeManager : public BackgroundModeManager {
227 public:
228 FakeBackgroundModeManager()
lwchkg9c183942016-03-13 06:29:54229 : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(),
230 &g_browser_process->profile_manager()
231 ->GetProfileAttributesStorage()),
[email protected]0c95faf42013-10-28 06:27:20232 suspended_(false) {}
233
Daniel Chenga542fca2014-10-21 09:51:29234 void SuspendBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20235 BackgroundModeManager::SuspendBackgroundMode();
236 suspended_ = true;
237 }
238
Daniel Chenga542fca2014-10-21 09:51:29239 void ResumeBackgroundMode() override {
[email protected]0c95faf42013-10-28 06:27:20240 BackgroundModeManager::ResumeBackgroundMode();
241 suspended_ = false;
242 }
243
244 bool IsBackgroundModeSuspended() {
245 return suspended_;
246 }
247
248 private:
249 bool suspended_;
250
251 DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager);
252};
253
[email protected]2e9d79f2013-08-16 05:45:56254} // namespace
255
256class BrowserCloseManagerBrowserTest
257 : public InProcessBrowserTest,
258 public testing::WithParamInterface<bool> {
259 protected:
Daniel Chenga542fca2014-10-21 09:51:29260 void SetUpOnMainThread() override {
[email protected]2e9d79f2013-08-16 05:45:56261 InProcessBrowserTest::SetUpOnMainThread();
262 SessionStartupPref::SetStartupPref(
263 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
264 browsers_.push_back(browser());
[email protected]edfca702013-08-16 08:58:14265 content::BrowserThread::PostTask(
266 content::BrowserThread::IO,
267 FROM_HERE,
268 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
[email protected]2e9d79f2013-08-16 05:45:56269 }
270
avi556c05022014-12-22 23:31:43271 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]2e9d79f2013-08-16 05:45:56272 if (GetParam())
273 command_line->AppendSwitch(switches::kEnableFastUnload);
[email protected]f9357a442014-05-15 18:44:07274#if defined(OS_CHROMEOS)
275 command_line->AppendSwitch(
276 chromeos::switches::kIgnoreUserProfileMappingForTests);
277#endif
[email protected]2e9d79f2013-08-16 05:45:56278 }
279
[email protected]edfca702013-08-16 08:58:14280 void CreateStalledDownload(Browser* browser) {
281 content::DownloadTestObserverInProgress observer(
282 content::BrowserContext::GetDownloadManager(browser->profile()), 1);
283 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19284 browser, GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl),
285 WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]edfca702013-08-16 08:58:14286 ui_test_utils::BROWSER_TEST_NONE);
287 observer.WaitForFinished();
288 EXPECT_EQ(
289 1UL,
290 observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS));
291 }
292
avi64b8b65b2017-03-29 18:50:34293 // Makes sure that the beforeunload hang monitor will not trigger. That must
294 // be called before close action when using |AcceptClose| or |CancelClose|, to
295 // ensure the timeout does not prevent the dialog from appearing.
296 // https://crbug.com/519646
metaflow812bb8542016-05-24 21:10:46297 void DisableHangMonitor(content::WebContents* web_contents) {
avi64b8b65b2017-03-29 18:50:34298 web_contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
metaflow812bb8542016-05-24 21:10:46299 }
300
301 void DisableHangMonitor(Browser* browser) {
302 for (int i = 0; i < browser->tab_strip_model()->count(); i++)
303 DisableHangMonitor(browser->tab_strip_model()->GetWebContentsAt(i));
304 }
305
[email protected]2e9d79f2013-08-16 05:45:56306 std::vector<Browser*> browsers_;
[email protected]2e9d79f2013-08-16 05:45:56307};
308
309IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) {
svaldeza01f7d92015-11-18 17:47:56310 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56311 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
312 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46313 DisableHangMonitor(browser());
314
[email protected]2e9d79f2013-08-16 05:45:56315 RepeatedNotificationObserver cancel_observer(
316 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20317 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32318 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56319 cancel_observer.Wait();
320 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
321 EXPECT_EQ(1, browser()->tab_strip_model()->count());
322
323 RepeatedNotificationObserver close_observer(
324 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20325 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32326 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56327 close_observer.Wait();
328 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37329 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56330}
331
332IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
333 TestShutdownMoreThanOnce) {
svaldeza01f7d92015-11-18 17:47:56334 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56335 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
336 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46337 DisableHangMonitor(browser());
338
[email protected]2e9d79f2013-08-16 05:45:56339 RepeatedNotificationObserver cancel_observer(
340 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20341 chrome::CloseAllBrowsersAndQuit();
342 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32343 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56344 cancel_observer.Wait();
345 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
346 EXPECT_EQ(1, browser()->tab_strip_model()->count());
347
348 RepeatedNotificationObserver close_observer(
349 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20350 chrome::CloseAllBrowsersAndQuit();
351 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32352 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56353 close_observer.Wait();
354 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37355 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56356}
357
wafflesb63234052015-08-07 02:44:02358IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46359 PRE_TestSessionRestore) {
svaldeza01f7d92015-11-18 17:47:56360 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56361 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
362 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
363 AddBlankTabAndShow(browser());
364 ASSERT_NO_FATAL_FAILURE(
365 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)));
metaflow812bb8542016-05-24 21:10:46366 DisableHangMonitor(browser());
367
[email protected]2e9d79f2013-08-16 05:45:56368 RepeatedNotificationObserver cancel_observer(
369 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20370 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32371 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56372 cancel_observer.Wait();
373 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
374
375 browser()->tab_strip_model()
376 ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE);
377 content::TestNavigationObserver navigation_observer(
378 browser()->tab_strip_model()->GetActiveWebContents(), 1);
nick3b04f322016-08-31 19:29:19379 ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition(
380 browser(), GURL(chrome::kChromeUIVersionURL),
381 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE));
oshima82f72482014-10-24 14:14:32382 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56383 navigation_observer.Wait();
384
385 RepeatedNotificationObserver close_observer(
386 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20387 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56388 close_observer.Wait();
389 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37390 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56391}
392
393// Test that the tab closed after the aborted shutdown attempt is not re-opened
394// when restoring the session.
wafflesb63234052015-08-07 02:44:02395IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46396 TestSessionRestore) {
[email protected]2e9d79f2013-08-16 05:45:56397 // The testing framework launches Chrome with about:blank as args.
398 EXPECT_EQ(2, browser()->tab_strip_model()->count());
399 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL),
400 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
401 EXPECT_EQ(GURL("about:blank"),
402 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
403}
404
405// Test that browser windows are only closed if all browsers are ready to close
406// and that all beforeunload dialogs are shown again after a cancel.
[email protected]40cd13a2013-10-24 13:42:01407IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) {
svaldeza01f7d92015-11-18 17:47:56408 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56409 browsers_.push_back(CreateBrowser(browser()->profile()));
410 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
411 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
412 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
413 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46414 DisableHangMonitor(browsers_[0]);
415 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56416
417 // Cancel shutdown on the first beforeunload event.
418 {
419 RepeatedNotificationObserver cancel_observer(
420 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20421 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32422 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56423 cancel_observer.Wait();
424 }
425 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
426 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
427 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
428
429 // Cancel shutdown on the second beforeunload event.
430 {
431 RepeatedNotificationObserver cancel_observer(
432 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20433 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32434 ASSERT_NO_FATAL_FAILURE(AcceptClose());
435 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56436 cancel_observer.Wait();
437 }
438 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
439 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
440 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
441
442 // Allow shutdown for both beforeunload events.
443 RepeatedNotificationObserver close_observer(
444 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20445 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32446 ASSERT_NO_FATAL_FAILURE(AcceptClose());
447 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56448 close_observer.Wait();
449 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37450 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56451}
452
453// Test that tabs in the same window with a beforeunload event that hangs are
454// treated the same as the user accepting the close, but do not close the tab
455// early.
[email protected]2e9d79f2013-08-16 05:45:56456IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46457 TestHangInBeforeUnloadMultipleTabs) {
svaldeza01f7d92015-11-18 17:47:56458 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56459 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
460 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
461 AddBlankTabAndShow(browsers_[0]);
462 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
463 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
464 AddBlankTabAndShow(browsers_[0]);
465 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
466 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46467 // Disable the hang monitor in the tab that is not expected to hang, so that
468 // the dialog is guaranteed to show.
469 DisableHangMonitor(browsers_[0]->tab_strip_model()->GetWebContentsAt(1));
[email protected]2e9d79f2013-08-16 05:45:56470
471 RepeatedNotificationObserver cancel_observer(
472 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20473 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32474 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56475 cancel_observer.Wait();
476 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
477 // All tabs should still be open.
478 EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count());
479
480 RepeatedNotificationObserver close_observer(
481 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
[email protected]0c95faf42013-10-28 06:27:20482 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32483 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56484 close_observer.Wait();
485 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37486 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56487}
488
489// Test that tabs in different windows with a beforeunload event that hangs are
490// treated the same as the user accepting the close, but do not close the tab
491// early.
492IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
493 TestHangInBeforeUnloadMultipleWindows) {
svaldeza01f7d92015-11-18 17:47:56494 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56495 browsers_.push_back(CreateBrowser(browser()->profile()));
496 browsers_.push_back(CreateBrowser(browser()->profile()));
497 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
498 browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html")));
499 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
500 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
501 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
502 browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html")));
metaflow812bb8542016-05-24 21:10:46503 // Disable the hang monitor in the tab that is not expected to hang, so that
504 // the dialog is guaranteed to show.
505 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56506
507 RepeatedNotificationObserver cancel_observer(
508 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20509 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32510 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56511 cancel_observer.Wait();
512 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
513 // All windows should still be open.
514 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
515 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
516 EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count());
517
518 RepeatedNotificationObserver close_observer(
519 chrome::NOTIFICATION_BROWSER_CLOSED, 3);
[email protected]0c95faf42013-10-28 06:27:20520 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32521 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56522 close_observer.Wait();
523 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37524 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56525}
526
metaflow73fc9552016-05-23 18:26:48527// Test that tabs that are slow to respond are not closed prematurely.
528// Regression for crbug.com/365052 caused some of tabs to be closed even if
529// user chose to cancel browser close.
metaflow73fc9552016-05-23 18:26:48530IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46531 TestUnloadMultipleSlowTabs) {
metaflow73fc9552016-05-23 18:26:48532 ASSERT_TRUE(embedded_test_server()->Start());
533 const int kTabCount = 5;
534 const int kResposiveTabIndex = 2;
535 // Create tab strip with all tabs except one responding after
536 // RenderViewHostImpl::kUnloadTimeoutMS.
537 // Minimum configuration is two slow tabs and then responsive tab.
538 // But we also want to check how slow tabs behave in tail.
539 for (int i = 0; i < kTabCount; i++) {
540 if (i)
541 AddBlankTabAndShow(browsers_[0]);
542 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
543 browsers_[0],
544 embedded_test_server()->GetURL((i == kResposiveTabIndex)
545 ? "/beforeunload.html"
546 : "/beforeunload_slow.html")));
547 }
metaflow812bb8542016-05-24 21:10:46548 // Disable the hang monitor in the tab that is not expected to hang, so that
549 // the dialog is guaranteed to show.
550 DisableHangMonitor(
551 browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex));
552
metaflow73fc9552016-05-23 18:26:48553 RepeatedNotificationObserver cancel_observer(
554 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
555 chrome::CloseAllBrowsersAndQuit();
556 ASSERT_NO_FATAL_FAILURE(CancelClose());
557 cancel_observer.Wait();
558 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
559
560 // All tabs should still be open.
561 EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count());
562 RepeatedNotificationObserver close_observer(
563 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
564
565 // Quit, this time accepting close confirmation dialog.
566 chrome::CloseAllBrowsersAndQuit();
567 ASSERT_NO_FATAL_FAILURE(AcceptClose());
568 close_observer.Wait();
569 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
570 EXPECT_TRUE(BrowserList::GetInstance()->empty());
571}
572
573// Test that tabs in different windows with a slow beforeunload event response
574// are treated the same as the user accepting the close, but do not close the
575// tab early.
576// Regression for crbug.com/365052 caused CHECK in tabstrip.
metaflow73fc9552016-05-23 18:26:48577IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46578 TestBeforeUnloadMultipleSlowWindows) {
metaflow73fc9552016-05-23 18:26:48579 ASSERT_TRUE(embedded_test_server()->Start());
580 const int kBrowserCount = 5;
581 const int kResposiveBrowserIndex = 2;
582 // Create multiple browsers with all tabs except one responding after
583 // RenderViewHostImpl::kUnloadTimeoutMS .
584 // Minimum configuration is just one browser with slow tab and then
585 // browser with responsive tab.
586 // But we also want to check how slow tabs behave in tail and make test
587 // more robust.
588 for (int i = 0; i < kBrowserCount; i++) {
589 if (i)
590 browsers_.push_back(CreateBrowser(browser()->profile()));
591 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
592 browsers_[i],
593 embedded_test_server()->GetURL((i == kResposiveBrowserIndex)
594 ? "/beforeunload.html"
595 : "/beforeunload_slow.html")));
596 }
metaflow812bb8542016-05-24 21:10:46597 // Disable the hang monitor in the tab that is not expected to hang, so that
598 // the dialog is guaranteed to show.
599 DisableHangMonitor(browsers_[kResposiveBrowserIndex]);
metaflow73fc9552016-05-23 18:26:48600
601 RepeatedNotificationObserver cancel_observer(
602 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1);
603 chrome::CloseAllBrowsersAndQuit();
604 ASSERT_NO_FATAL_FAILURE(CancelClose());
605 cancel_observer.Wait();
606 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
607
608 // All windows should still be open.
609 for (int i = 0; i < kBrowserCount; i++)
610 EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count());
611
612 // Quit, this time accepting close confirmation dialog.
613 RepeatedNotificationObserver close_observer(
614 chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount);
615 chrome::CloseAllBrowsersAndQuit();
616 ASSERT_NO_FATAL_FAILURE(AcceptClose());
617 close_observer.Wait();
618 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
619 EXPECT_TRUE(BrowserList::GetInstance()->empty());
620}
621
[email protected]2e9d79f2013-08-16 05:45:56622// Test that a window created during shutdown is closed.
623IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
624 TestAddWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56625 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56626 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
627 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46628 DisableHangMonitor(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56629
630 RepeatedNotificationObserver close_observer(
631 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20632 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56633 browsers_.push_back(CreateBrowser(browser()->profile()));
oshima82f72482014-10-24 14:14:32634 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56635 close_observer.Wait();
636 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37637 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56638}
639
640// Test that a window created during shutdown with a beforeunload handler can
641// cancel the shutdown.
642IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
[email protected]40cd13a2013-10-24 13:42:01643 TestAddWindowWithBeforeUnloadDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56644 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56645 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
646 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46647 DisableHangMonitor(browsers_[0]);
[email protected]2e9d79f2013-08-16 05:45:56648
649 RepeatedNotificationObserver cancel_observer(
650 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20651 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56652 browsers_.push_back(CreateBrowser(browser()->profile()));
653 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
654 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46655 DisableHangMonitor(browsers_[1]);
oshima82f72482014-10-24 14:14:32656 ASSERT_NO_FATAL_FAILURE(AcceptClose());
657 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56658 cancel_observer.Wait();
659 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
660 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
661 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
662
663 // Allow shutdown for both beforeunload dialogs.
664 RepeatedNotificationObserver close_observer(
665 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20666 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32667 ASSERT_NO_FATAL_FAILURE(AcceptClose());
668 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56669 close_observer.Wait();
670 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37671 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56672}
673
674// Test that tabs added during shutdown are closed.
[email protected]2e9d79f2013-08-16 05:45:56675IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46676 TestAddTabDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56677 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56678 browsers_.push_back(CreateBrowser(browser()->profile()));
679 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
680 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
681 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
682 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46683 DisableHangMonitor(browsers_[0]);
684 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56685
686 RepeatedNotificationObserver close_observer(
687 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20688 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32689 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56690 AddBlankTabAndShow(browsers_[0]);
691 AddBlankTabAndShow(browsers_[1]);
oshima82f72482014-10-24 14:14:32692 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56693 close_observer.Wait();
694 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37695 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56696}
697
698// Test that tabs created during shutdown with beforeunload handlers can cancel
699// the shutdown.
metaflow812bb8542016-05-24 21:10:46700
[email protected]2e9d79f2013-08-16 05:45:56701IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46702 TestAddTabWithBeforeUnloadDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56703 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56704 browsers_.push_back(CreateBrowser(browser()->profile()));
705 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
706 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
707 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
708 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46709 DisableHangMonitor(browsers_[0]);
710 DisableHangMonitor(browsers_[1]);
711
[email protected]2e9d79f2013-08-16 05:45:56712 RepeatedNotificationObserver cancel_observer(
713 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20714 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32715 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56716 AddBlankTabAndShow(browsers_[0]);
717 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
718 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
719 AddBlankTabAndShow(browsers_[1]);
720 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
721 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46722 DisableHangMonitor(browsers_[0]);
723 DisableHangMonitor(browsers_[1]);
oshima82f72482014-10-24 14:14:32724 ASSERT_NO_FATAL_FAILURE(AcceptClose());
725 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56726 cancel_observer.Wait();
727 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
728 EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count());
729 EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count());
730
731 RepeatedNotificationObserver close_observer(
732 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20733 chrome::CloseAllBrowsersAndQuit();
oshima82f72482014-10-24 14:14:32734 ASSERT_NO_FATAL_FAILURE(AcceptClose());
735 ASSERT_NO_FATAL_FAILURE(AcceptClose());
736 ASSERT_NO_FATAL_FAILURE(AcceptClose());
737 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56738
739 close_observer.Wait();
740 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37741 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56742}
743
[email protected]2e9d79f2013-08-16 05:45:56744IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi48d2fdd2017-03-30 03:50:17745 AddBeforeUnloadDuringClosing) {
mkolom65a0b3c2017-03-02 06:11:40746 // TODO(crbug.com/250305): Currently FastUnloadController is broken.
747 // And it is difficult to fix this issue without fixing that one.
748 if (GetParam())
749 return;
750
751 ASSERT_TRUE(embedded_test_server()->Start());
752 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
753 browser(), embedded_test_server()->GetURL("/title1.html")));
754
755 // Open second window.
756 ui_test_utils::NavigateToURLWithDisposition(
757 browser(), embedded_test_server()->GetURL("/beforeunload.html"),
758 WindowOpenDisposition::NEW_WINDOW,
759 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
760 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
761 auto* browser2 = BrowserList::GetInstance()->get(0) != browser()
762 ? BrowserList::GetInstance()->get(0)
763 : BrowserList::GetInstance()->get(1);
764 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0));
765
766 // Let's work with second window only.
767 // This page has beforeunload handler already.
768 EXPECT_TRUE(browser2->tab_strip_model()
769 ->GetWebContentsAt(0)
770 ->NeedToFireBeforeUnload());
771 // This page doesn't have beforeunload handler. Yet.
772 ui_test_utils::NavigateToURLWithDisposition(
773 browser2, embedded_test_server()->GetURL("/title2.html"),
774 WindowOpenDisposition::NEW_FOREGROUND_TAB,
775 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
776 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1));
777 EXPECT_FALSE(browser2->tab_strip_model()
778 ->GetWebContentsAt(1)
779 ->NeedToFireBeforeUnload());
780 EXPECT_EQ(2, browser2->tab_strip_model()->count());
781
782 DisableHangMonitor(browser2);
783
784 // The test.
785
786 TabRestoreServiceChangesObserver restore_observer(browser2->profile());
787 content::WindowedNotificationObserver observer(
788 chrome::NOTIFICATION_BROWSER_CLOSED,
789 content::NotificationService::AllSources());
790 chrome::CloseWindow(browser2);
791 // Just to be sure CloseWindow doesn't have asynchronous tasks
792 // that could have an impact.
793 content::RunAllPendingInMessageLoop();
794
795 // Closing browser shouldn't happen because of beforeunload handler.
796 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
797 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it
798 // yet.
799 ASSERT_TRUE(content::ExecuteScript(
800 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(),
801 "window.addEventListener('beforeunload', "
802 "function(event) { event.returnValue = 'Foo'; });"));
803 EXPECT_TRUE(browser2->tab_strip_model()
804 ->GetWebContentsAt(1)
805 ->NeedToFireBeforeUnload());
806 // Accept closing the first tab.
807 ASSERT_NO_FATAL_FAILURE(AcceptClose());
808 // Just to be sure accepting a dialog doesn't have asynchronous tasks
809 // that could have an impact.
810 content::RunAllPendingInMessageLoop();
811 // It shouldn't close the whole window/browser.
812 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
813 EXPECT_EQ(2, browser2->tab_strip_model()->count());
814 // Accept closing the second tab.
815 ASSERT_NO_FATAL_FAILURE(AcceptClose());
816 observer.Wait();
817 // Now the second window/browser should be closed.
818 EXPECT_EQ(1u, BrowserList::GetInstance()->size());
819 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0));
820 EXPECT_EQ(1u, restore_observer.changes_count());
821
822 // Restore the closed browser.
823 content::WindowedNotificationObserver open_window_observer(
824 chrome::NOTIFICATION_BROWSER_OPENED,
825 content::NotificationService::AllSources());
826 chrome::OpenWindowWithRestoredTabs(browser()->profile());
827 open_window_observer.Wait();
828 EXPECT_EQ(2u, BrowserList::GetInstance()->size());
829 browser2 = BrowserList::GetInstance()->get(0) != browser()
830 ? BrowserList::GetInstance()->get(0)
831 : BrowserList::GetInstance()->get(1);
832
833 // Check the restored browser contents.
834 EXPECT_EQ(2, browser2->tab_strip_model()->count());
835 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"),
836 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL());
837 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"),
838 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL());
839}
840
mkolom65a0b3c2017-03-02 06:11:40841IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
avi88040912017-03-30 03:48:42842 TestCloseTabDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56843 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56844 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
845 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46846 DisableHangMonitor(browsers_[0]);
847
[email protected]2e9d79f2013-08-16 05:45:56848 RepeatedNotificationObserver cancel_observer(
849 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20850 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56851
852 browsers_.push_back(CreateBrowser(browser()->profile()));
853 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
854 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46855 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56856 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32857 ASSERT_NO_FATAL_FAILURE(CancelClose());
858 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56859 cancel_observer.Wait();
860 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
861 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
862 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
863
864 RepeatedNotificationObserver close_observer(
865 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20866 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56867 browsers_[1]->tab_strip_model()->CloseAllTabs();
oshima82f72482014-10-24 14:14:32868 ASSERT_NO_FATAL_FAILURE(AcceptClose());
869 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56870
871 close_observer.Wait();
872 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37873 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56874}
875
[email protected]2e9d79f2013-08-16 05:45:56876IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
metaflow812bb8542016-05-24 21:10:46877 TestOpenAndCloseWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56878 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56879 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
880 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46881 DisableHangMonitor(browsers_[0]);
882
[email protected]2e9d79f2013-08-16 05:45:56883 RepeatedNotificationObserver cancel_observer(
884 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2);
[email protected]0c95faf42013-10-28 06:27:20885 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56886
887 browsers_.push_back(CreateBrowser(browser()->profile()));
888 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
889 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46890 DisableHangMonitor(browsers_[1]);
[email protected]2e9d79f2013-08-16 05:45:56891 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32892 ASSERT_NO_FATAL_FAILURE(CancelClose());
893 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56894 cancel_observer.Wait();
895 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
896 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
897 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
898
899 RepeatedNotificationObserver close_observer(
900 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20901 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56902 ASSERT_FALSE(browsers_[1]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32903 ASSERT_NO_FATAL_FAILURE(AcceptClose());
904 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56905
906 close_observer.Wait();
907 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37908 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56909}
910
911IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest,
912 TestCloseWindowDuringShutdown) {
svaldeza01f7d92015-11-18 17:47:56913 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]2e9d79f2013-08-16 05:45:56914 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
915 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html")));
916 browsers_.push_back(CreateBrowser(browser()->profile()));
917 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
918 browsers_[1], embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:46919 DisableHangMonitor(browsers_[0]);
920 DisableHangMonitor(browsers_[1]);
921
[email protected]2e9d79f2013-08-16 05:45:56922 RepeatedNotificationObserver cancel_observer(
923 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1);
[email protected]0c95faf42013-10-28 06:27:20924 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56925
926 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32927 ASSERT_NO_FATAL_FAILURE(CancelClose());
[email protected]2e9d79f2013-08-16 05:45:56928 cancel_observer.Wait();
929 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
930 EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count());
931 EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count());
932
933 RepeatedNotificationObserver close_observer(
934 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
[email protected]0c95faf42013-10-28 06:27:20935 chrome::CloseAllBrowsersAndQuit();
[email protected]2e9d79f2013-08-16 05:45:56936 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow());
oshima82f72482014-10-24 14:14:32937 ASSERT_NO_FATAL_FAILURE(AcceptClose());
938 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]2e9d79f2013-08-16 05:45:56939
940 close_observer.Wait();
941 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37942 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]2e9d79f2013-08-16 05:45:56943}
944
[email protected]0ed53872014-05-10 05:51:05945INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest,
946 BrowserCloseManagerBrowserTest,
947 testing::Bool());
948
949class BrowserCloseManagerWithDownloadsBrowserTest :
950 public BrowserCloseManagerBrowserTest {
951 public:
952 BrowserCloseManagerWithDownloadsBrowserTest() {}
953 virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {}
954
Daniel Chenga542fca2014-10-21 09:51:29955 void SetUpOnMainThread() override {
[email protected]0ed53872014-05-10 05:51:05956 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
957 ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir());
958 }
959
[email protected]0ed53872014-05-10 05:51:05960 void SetDownloadPathForProfile(Profile* profile) {
961 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile);
962 download_prefs->SetDownloadPath(download_path());
963 }
964
965 const base::FilePath& download_path() const {
vabr0c237ae2016-09-14 09:24:28966 return scoped_download_directory_.GetPath();
[email protected]0ed53872014-05-10 05:51:05967 }
968
969 private:
970 base::ScopedTempDir scoped_download_directory_;
971};
972
jackhouae8e6e52015-05-29 06:36:46973// Mac has its own in-progress download prompt in app_controller_mac.mm, so
974// BrowserCloseManager should simply close all browsers. If there are no
975// browsers, it should not crash.
976#if defined(OS_MACOSX)
977IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
978 TestWithDownloads) {
svaldeza01f7d92015-11-18 17:47:56979 ASSERT_TRUE(embedded_test_server()->Start());
jackhouae8e6e52015-05-29 06:36:46980 SetDownloadPathForProfile(browser()->profile());
981 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
982
983 RepeatedNotificationObserver close_observer(
984 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
985
986 TestBrowserCloseManager::AttemptClose(
987 TestBrowserCloseManager::NO_USER_CHOICE);
988 close_observer.Wait();
989 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:37990 EXPECT_TRUE(BrowserList::GetInstance()->empty());
jackhouae8e6e52015-05-29 06:36:46991 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
992
993 // Attempting to close again should not crash.
994 TestBrowserCloseManager::AttemptClose(
995 TestBrowserCloseManager::NO_USER_CHOICE);
996}
997#else // defined(OS_MACOSX)
998
[email protected]422a7d12013-10-21 12:10:42999// Test shutdown with a DANGEROUS_URL download undecided.
[email protected]0ed53872014-05-10 05:51:051000IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]422a7d12013-10-21 12:10:421001 TestWithDangerousUrlDownload) {
svaldeza01f7d92015-11-18 17:47:561002 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051003 SetDownloadPathForProfile(browser()->profile());
[email protected]422a7d12013-10-21 12:10:421004
1005 // Set up the fake delegate that forces the download to be malicious.
dcheng4af48582016-04-19 00:29:351006 std::unique_ptr<TestDownloadManagerDelegate> test_delegate(
[email protected]422a7d12013-10-21 12:10:421007 new TestDownloadManagerDelegate(browser()->profile()));
dcheng383ba8a2014-10-16 23:55:191008 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
dchenge73d8520c2015-12-27 01:19:091009 ->SetDownloadManagerDelegateForTesting(std::move(test_delegate));
[email protected]422a7d12013-10-21 12:10:421010
1011 // Run a dangerous download, but the user doesn't make a decision.
1012 // This .swf normally would be categorized as DANGEROUS_FILE, but
1013 // TestDownloadManagerDelegate turns it into DANGEROUS_URL.
tfarina5e7b57232015-10-17 23:37:401014 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(
1015 "downloads/dangerous/dangerous.swf"));
[email protected]422a7d12013-10-21 12:10:421016 content::DownloadTestObserverInterrupted observer(
1017 content::BrowserContext::GetDownloadManager(browser()->profile()),
1018 1,
1019 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT);
1020 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:191021 browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB,
[email protected]422a7d12013-10-21 12:10:421022 ui_test_utils::BROWSER_TEST_NONE);
1023 observer.WaitForFinished();
1024
1025 // Check that the download manager has the expected state.
1026 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager(
1027 browser()->profile())->InProgressCount());
1028 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager(
1029 browser()->profile())->NonMaliciousInProgressCount());
1030
1031 // Close the browser with no user action.
1032 RepeatedNotificationObserver close_observer(
1033 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1034 TestBrowserCloseManager::AttemptClose(
1035 TestBrowserCloseManager::NO_USER_CHOICE);
1036 close_observer.Wait();
1037 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371038 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]422a7d12013-10-21 12:10:421039}
1040
[email protected]edfca702013-08-16 08:58:141041// Test shutdown with a download in progress.
[email protected]0ed53872014-05-10 05:51:051042IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1043 TestWithDownloads) {
svaldeza01f7d92015-11-18 17:47:561044 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051045 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141046 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1047 content::TestNavigationObserver navigation_observer(
1048 browser()->tab_strip_model()->GetActiveWebContents(), 1);
1049 TestBrowserCloseManager::AttemptClose(
1050 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1051 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1052 navigation_observer.Wait();
1053 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1054 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1055
1056 RepeatedNotificationObserver close_observer(
1057 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1058
1059 TestBrowserCloseManager::AttemptClose(
1060 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1061 close_observer.Wait();
1062 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371063 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381064 if (browser_defaults::kBrowserAliveWithNoWindows)
1065 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
1066 else
1067 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141068}
1069
sammccd347712015-03-13 22:02:321070// Test shutdown with a download in progress in an off-the-record profile.
1071IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
1072 TestWithOffTheRecordDownloads) {
svaldeza01f7d92015-11-18 17:47:561073 ASSERT_TRUE(embedded_test_server()->Start());
sammccd347712015-03-13 22:02:321074 Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile();
1075 SetDownloadPathForProfile(otr_profile);
1076 Browser* otr_browser = CreateBrowser(otr_profile);
1077 {
1078 RepeatedNotificationObserver close_observer(
1079 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1080 browser()->window()->Close();
1081 close_observer.Wait();
1082 }
1083 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser));
1084 content::TestNavigationObserver navigation_observer(
1085 otr_browser->tab_strip_model()->GetActiveWebContents(), 1);
1086 TestBrowserCloseManager::AttemptClose(
1087 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1088 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1089 navigation_observer.Wait();
1090 EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1091 otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1092
1093 RepeatedNotificationObserver close_observer(
1094 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1095
1096 TestBrowserCloseManager::AttemptClose(
1097 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1098 close_observer.Wait();
1099 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371100 EXPECT_TRUE(BrowserList::GetInstance()->empty());
sammccd347712015-03-13 22:02:321101 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
1102}
1103
[email protected]edfca702013-08-16 08:58:141104// Test shutdown with a download in progress from one profile, where the only
1105// open windows are for another profile.
[email protected]0ed53872014-05-10 05:51:051106IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]edfca702013-08-16 08:58:141107 TestWithDownloadsFromDifferentProfiles) {
1108 ProfileManager* profile_manager = g_browser_process->profile_manager();
1109 base::FilePath path =
1110 profile_manager->user_data_dir().AppendASCII("test_profile");
1111 if (!base::PathExists(path))
[email protected]426d1c92013-12-03 20:08:541112 ASSERT_TRUE(base::CreateDirectory(path));
[email protected]edfca702013-08-16 08:58:141113 Profile* other_profile =
1114 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
1115 profile_manager->RegisterTestingProfile(other_profile, true, false);
1116 Browser* other_profile_browser = CreateBrowser(other_profile);
1117
svaldeza01f7d92015-11-18 17:47:561118 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051119 SetDownloadPathForProfile(browser()->profile());
1120 SetDownloadPathForProfile(other_profile);
[email protected]edfca702013-08-16 08:58:141121 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1122 {
1123 RepeatedNotificationObserver close_observer(
1124 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1125 browser()->window()->Close();
1126 close_observer.Wait();
1127 }
1128
1129 // When the shutdown is cancelled, the downloads page should be opened in a
1130 // browser for that profile. Because there are no browsers for that profile, a
1131 // new browser should be opened.
1132 ui_test_utils::BrowserAddedObserver new_browser_observer;
1133 TestBrowserCloseManager::AttemptClose(
1134 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
1135 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1136 Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser();
1137 EXPECT_EQ(
1138 GURL(chrome::kChromeUIDownloadsURL),
1139 opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1140 EXPECT_EQ(GURL("about:blank"),
1141 other_profile_browser->tab_strip_model()->GetActiveWebContents()
1142 ->GetURL());
1143
1144 RepeatedNotificationObserver close_observer(
1145 chrome::NOTIFICATION_BROWSER_CLOSED, 2);
1146 TestBrowserCloseManager::AttemptClose(
1147 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
1148 close_observer.Wait();
1149 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371150 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]5fec4702013-11-20 04:37:381151 if (browser_defaults::kBrowserAliveWithNoWindows)
1152 EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles());
1153 else
1154 EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles());
[email protected]edfca702013-08-16 08:58:141155}
1156
1157// Test shutdown with downloads in progress and beforeunload handlers.
[email protected]0ed53872014-05-10 05:51:051158IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest,
metaflow812bb8542016-05-24 21:10:461159 TestBeforeUnloadAndDownloads) {
svaldeza01f7d92015-11-18 17:47:561160 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]0ed53872014-05-10 05:51:051161 SetDownloadPathForProfile(browser()->profile());
[email protected]edfca702013-08-16 08:58:141162 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
1163 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
1164 browser(), embedded_test_server()->GetURL("/beforeunload.html")));
metaflow812bb8542016-05-24 21:10:461165 DisableHangMonitor(browser());
[email protected]edfca702013-08-16 08:58:141166
1167 content::WindowedNotificationObserver cancel_observer(
1168 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
1169 content::NotificationService::AllSources());
1170 TestBrowserCloseManager::AttemptClose(
1171 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE);
oshima82f72482014-10-24 14:14:321172 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141173 cancel_observer.Wait();
1174 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
1175
1176 RepeatedNotificationObserver close_observer(
1177 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1178 TestBrowserCloseManager::AttemptClose(
1179 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE);
oshima82f72482014-10-24 14:14:321180 ASSERT_NO_FATAL_FAILURE(AcceptClose());
[email protected]edfca702013-08-16 08:58:141181 close_observer.Wait();
1182 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371183 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]edfca702013-08-16 08:58:141184}
1185
jackhouae8e6e52015-05-29 06:36:461186#endif // defined(OS_MACOSX)
1187
[email protected]0ed53872014-05-10 05:51:051188INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest,
1189 BrowserCloseManagerWithDownloadsBrowserTest,
[email protected]2e9d79f2013-08-16 05:45:561190 testing::Bool());
[email protected]0c95faf42013-10-28 06:27:201191
1192class BrowserCloseManagerWithBackgroundModeBrowserTest
1193 : public BrowserCloseManagerBrowserTest {
1194 public:
1195 BrowserCloseManagerWithBackgroundModeBrowserTest() {}
1196
Daniel Chenga542fca2014-10-21 09:51:291197 void SetUpOnMainThread() override {
[email protected]0c95faf42013-10-28 06:27:201198 BrowserCloseManagerBrowserTest::SetUpOnMainThread();
1199 g_browser_process->set_background_mode_manager_for_test(
dcheng4af48582016-04-19 00:29:351200 std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
[email protected]0c95faf42013-10-28 06:27:201201 }
1202
1203 bool IsBackgroundModeSuspended() {
1204 return static_cast<FakeBackgroundModeManager*>(
1205 g_browser_process->background_mode_manager())
1206 ->IsBackgroundModeSuspended();
1207 }
1208
1209 private:
1210 DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest);
1211};
1212
1213// Check that background mode is suspended when closing all browsers unless we
1214// are quitting and that background mode is resumed when a new browser window is
1215// opened.
1216IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1217 CloseAllBrowsersWithBackgroundMode) {
1218 EXPECT_FALSE(IsBackgroundModeSuspended());
dcheng4af48582016-04-19 00:29:351219 std::unique_ptr<ScopedKeepAlive> tmp_keep_alive;
[email protected]0c95faf42013-10-28 06:27:201220 Profile* profile = browser()->profile();
1221 {
1222 RepeatedNotificationObserver close_observer(
1223 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
dgn02377782016-03-12 00:58:381224 tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW,
1225 KeepAliveRestartOption::DISABLED));
[email protected]0c95faf42013-10-28 06:27:201226 chrome::CloseAllBrowsers();
1227 close_observer.Wait();
1228 }
1229 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371230 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201231 EXPECT_TRUE(IsBackgroundModeSuspended());
1232
1233 // Background mode should be resumed when a new browser window is opened.
1234 ui_test_utils::BrowserAddedObserver new_browser_observer;
scottmg851949002016-02-09 20:09:441235 chrome::NewEmptyWindow(profile);
[email protected]0c95faf42013-10-28 06:27:201236 new_browser_observer.WaitForSingleNewBrowser();
dgn02377782016-03-12 00:58:381237 tmp_keep_alive.reset();
[email protected]0c95faf42013-10-28 06:27:201238 EXPECT_FALSE(IsBackgroundModeSuspended());
1239 RepeatedNotificationObserver close_observer(
1240 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1241
1242 // Background mode should not be suspended when quitting.
1243 chrome::CloseAllBrowsersAndQuit();
1244 close_observer.Wait();
1245 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371246 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201247 EXPECT_FALSE(IsBackgroundModeSuspended());
[email protected]0c95faf42013-10-28 06:27:201248}
1249
1250// Check that closing the last browser window individually does not affect
1251// background mode.
1252IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich981e50f2016-03-03 23:39:161253 DISABLED_CloseSingleBrowserWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201254 RepeatedNotificationObserver close_observer(
1255 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1256 EXPECT_FALSE(IsBackgroundModeSuspended());
1257 browser()->window()->Close();
1258 close_observer.Wait();
1259 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371260 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201261 EXPECT_FALSE(IsBackgroundModeSuspended());
1262}
1263
1264// Check that closing all browsers with no browser windows open suspends
1265// background mode but does not cause Chrome to quit.
1266IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
dimich8148afc22016-03-04 01:57:031267 DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) {
[email protected]0c95faf42013-10-28 06:27:201268 RepeatedNotificationObserver close_observer(
1269 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
1270 EXPECT_FALSE(IsBackgroundModeSuspended());
dgn02377782016-03-12 00:58:381271 ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW,
1272 KeepAliveRestartOption::DISABLED);
[email protected]0c95faf42013-10-28 06:27:201273 browser()->window()->Close();
1274 close_observer.Wait();
1275 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371276 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201277 EXPECT_FALSE(IsBackgroundModeSuspended());
1278
1279 chrome::CloseAllBrowsers();
1280 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
scottmg8abbff832016-01-28 22:57:371281 EXPECT_TRUE(BrowserList::GetInstance()->empty());
[email protected]0c95faf42013-10-28 06:27:201282 EXPECT_TRUE(IsBackgroundModeSuspended());
1283}
1284
1285INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest,
1286 BrowserCloseManagerWithBackgroundModeBrowserTest,
1287 testing::Bool());