[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 5 | #include "chrome/browser/lifetime/browser_close_manager.h" |
| 6 | |
| 7 | #include <utility> |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include "base/command_line.h" |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "build/build_config.h" |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 14 | #include "chrome/browser/background/background_mode_manager.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 16 | #include "chrome/browser/browser_shutdown.h" |
| 17 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 18 | #include "chrome/browser/defaults.h" |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 19 | #include "chrome/browser/download/chrome_download_manager_delegate.h" |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 20 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 21 | #include "chrome/browser/download/download_service.h" |
| 22 | #include "chrome/browser/download/download_service_factory.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 23 | #include "chrome/browser/lifetime/application_lifetime.h" |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 24 | #include "chrome/browser/lifetime/keep_alive_types.h" |
| 25 | #include "chrome/browser/lifetime/scoped_keep_alive.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 26 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 27 | #include "chrome/browser/prefs/session_startup_pref.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 28 | #include "chrome/browser/profiles/profile.h" |
| 29 | #include "chrome/browser/profiles/profile_manager.h" |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 30 | #include "chrome/browser/sessions/tab_restore_service_factory.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 31 | #include "chrome/browser/ui/browser.h" |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 32 | #include "chrome/browser/ui/browser_commands.h" |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 33 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 34 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 35 | #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" |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 40 | #include "components/app_modal/javascript_app_modal_dialog.h" |
| 41 | #include "components/app_modal/native_app_modal_dialog.h" |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 42 | #include "components/sessions/core/tab_restore_service.h" |
| 43 | #include "components/sessions/core/tab_restore_service_observer.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 44 | #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] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 47 | #include "content/public/browser/notification_service.h" |
avi | 64b8b65b | 2017-03-29 18:50:34 | [diff] [blame] | 48 | #include "content/public/browser/render_frame_host.h" |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 49 | #include "content/public/browser/render_view_host.h" |
| 50 | #include "content/public/browser/render_widget_host.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 51 | #include "content/public/browser/web_contents.h" |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 52 | #include "content/public/test/browser_test_utils.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 53 | #include "content/public/test/download_test_observer.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 54 | #include "content/public/test/test_navigation_observer.h" |
| 55 | #include "net/test/embedded_test_server/embedded_test_server.h" |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 56 | #include "net/test/url_request/url_request_mock_http_job.h" |
jam | 8e45cd7 | 2015-01-20 16:33:44 | [diff] [blame] | 57 | #include "net/test/url_request/url_request_slow_download_job.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 58 | |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 59 | #if defined(OS_CHROMEOS) |
| 60 | #include "chromeos/chromeos_switches.h" |
| 61 | #endif |
| 62 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 63 | namespace { |
| 64 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 65 | app_modal::NativeAppModalDialog* GetNextDialog() { |
| 66 | app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 67 | EXPECT_TRUE(dialog->IsJavaScriptModalDialog()); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 68 | app_modal::JavaScriptAppModalDialog* js_dialog = |
| 69 | static_cast<app_modal::JavaScriptAppModalDialog*>(dialog); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 70 | CHECK(js_dialog->native_dialog()); |
| 71 | return js_dialog->native_dialog(); |
| 72 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 73 | |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 74 | // Note: call |DisableHangMonitor| on the relevant WebContents or Browser before |
| 75 | // trying to close it, to avoid flakiness. https://crbug.com/519646 |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 76 | void AcceptClose() { |
| 77 | GetNextDialog()->AcceptAppModalDialog(); |
| 78 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 79 | |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 80 | // Note: call |DisableHangMonitor| on the relevant WebContents or Browser before |
| 81 | // trying to close it, to avoid flakiness. https://crbug.com/519646 |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 82 | void CancelClose() { |
| 83 | GetNextDialog()->CancelAppModalDialog(); |
| 84 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 85 | |
| 86 | class 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 Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 93 | void Observe(int type, |
| 94 | const content::NotificationSource& source, |
| 95 | const content::NotificationDetails& details) override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 96 | 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 | |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 121 | class 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] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 154 | class TestBrowserCloseManager : public BrowserCloseManager { |
| 155 | public: |
| 156 | enum UserChoice { |
| 157 | USER_CHOICE_USER_CANCELS_CLOSE, |
| 158 | USER_CHOICE_USER_ALLOWS_CLOSE, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 159 | NO_USER_CHOICE |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | static void AttemptClose(UserChoice user_choice) { |
| 163 | scoped_refptr<BrowserCloseManager> browser_close_manager = |
| 164 | new TestBrowserCloseManager(user_choice); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 165 | browser_shutdown::SetTryingToQuit(true); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 166 | browser_close_manager->StartClosingBrowsers(); |
| 167 | } |
| 168 | |
| 169 | protected: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 170 | ~TestBrowserCloseManager() override {} |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 171 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 172 | void ConfirmCloseWithPendingDownloads( |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 173 | int download_count, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 174 | const base::Callback<void(bool)>& callback) override { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 175 | EXPECT_NE(NO_USER_CHOICE, user_choice_); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 176 | switch (user_choice_) { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 177 | case NO_USER_CHOICE: |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 178 | 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] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 198 | class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 199 | public: |
| 200 | explicit TestDownloadManagerDelegate(Profile* profile) |
| 201 | : ChromeDownloadManagerDelegate(profile) { |
[email protected] | 861b4d0a | 2013-12-13 20:10:41 | [diff] [blame] | 202 | GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 203 | } |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 204 | ~TestDownloadManagerDelegate() override {} |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 205 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 206 | bool DetermineDownloadTarget( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 207 | content::DownloadItem* item, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 208 | const content::DownloadTargetCallback& callback) override { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 209 | content::DownloadTargetCallback dangerous_callback = |
[email protected] | 861b4d0a | 2013-12-13 20:10:41 | [diff] [blame] | 210 | base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 211 | return ChromeDownloadManagerDelegate::DetermineDownloadTarget( |
| 212 | item, dangerous_callback); |
| 213 | } |
| 214 | |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 215 | 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] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 223 | } |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 224 | }; |
| 225 | |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 226 | class FakeBackgroundModeManager : public BackgroundModeManager { |
| 227 | public: |
| 228 | FakeBackgroundModeManager() |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 229 | : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(), |
| 230 | &g_browser_process->profile_manager() |
| 231 | ->GetProfileAttributesStorage()), |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 232 | suspended_(false) {} |
| 233 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 234 | void SuspendBackgroundMode() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 235 | BackgroundModeManager::SuspendBackgroundMode(); |
| 236 | suspended_ = true; |
| 237 | } |
| 238 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 239 | void ResumeBackgroundMode() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 240 | 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] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 254 | } // namespace |
| 255 | |
| 256 | class BrowserCloseManagerBrowserTest |
| 257 | : public InProcessBrowserTest, |
| 258 | public testing::WithParamInterface<bool> { |
| 259 | protected: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 260 | void SetUpOnMainThread() override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 261 | InProcessBrowserTest::SetUpOnMainThread(); |
| 262 | SessionStartupPref::SetStartupPref( |
| 263 | browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); |
| 264 | browsers_.push_back(browser()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 265 | content::BrowserThread::PostTask( |
tzik | 3f7781d | 2017-04-20 17:09:33 | [diff] [blame] | 266 | content::BrowserThread::IO, FROM_HERE, |
| 267 | base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 268 | } |
| 269 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 270 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 271 | if (GetParam()) |
| 272 | command_line->AppendSwitch(switches::kEnableFastUnload); |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 273 | #if defined(OS_CHROMEOS) |
| 274 | command_line->AppendSwitch( |
| 275 | chromeos::switches::kIgnoreUserProfileMappingForTests); |
| 276 | #endif |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 279 | void CreateStalledDownload(Browser* browser) { |
| 280 | content::DownloadTestObserverInProgress observer( |
| 281 | content::BrowserContext::GetDownloadManager(browser->profile()), 1); |
| 282 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 283 | browser, GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl), |
| 284 | WindowOpenDisposition::NEW_BACKGROUND_TAB, |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 285 | ui_test_utils::BROWSER_TEST_NONE); |
| 286 | observer.WaitForFinished(); |
| 287 | EXPECT_EQ( |
| 288 | 1UL, |
| 289 | observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS)); |
| 290 | } |
| 291 | |
avi | 64b8b65b | 2017-03-29 18:50:34 | [diff] [blame] | 292 | // Makes sure that the beforeunload hang monitor will not trigger. That must |
| 293 | // be called before close action when using |AcceptClose| or |CancelClose|, to |
| 294 | // ensure the timeout does not prevent the dialog from appearing. |
| 295 | // https://crbug.com/519646 |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 296 | void DisableHangMonitor(content::WebContents* web_contents) { |
avi | 64b8b65b | 2017-03-29 18:50:34 | [diff] [blame] | 297 | web_contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting(); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void DisableHangMonitor(Browser* browser) { |
| 301 | for (int i = 0; i < browser->tab_strip_model()->count(); i++) |
| 302 | DisableHangMonitor(browser->tab_strip_model()->GetWebContentsAt(i)); |
| 303 | } |
| 304 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 305 | std::vector<Browser*> browsers_; |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 309 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 310 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 311 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 312 | DisableHangMonitor(browser()); |
| 313 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 314 | RepeatedNotificationObserver cancel_observer( |
| 315 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 316 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 317 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 318 | cancel_observer.Wait(); |
| 319 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 320 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 321 | |
| 322 | RepeatedNotificationObserver close_observer( |
| 323 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 324 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 325 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 326 | close_observer.Wait(); |
| 327 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 328 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 332 | TestShutdownMoreThanOnce) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 333 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 334 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 335 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 336 | DisableHangMonitor(browser()); |
| 337 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 338 | RepeatedNotificationObserver cancel_observer( |
| 339 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 340 | chrome::CloseAllBrowsersAndQuit(); |
| 341 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 342 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 343 | cancel_observer.Wait(); |
| 344 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 345 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 346 | |
| 347 | RepeatedNotificationObserver close_observer( |
| 348 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 349 | chrome::CloseAllBrowsersAndQuit(); |
| 350 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 351 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 352 | close_observer.Wait(); |
| 353 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 354 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 355 | } |
| 356 | |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 357 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 358 | PRE_TestSessionRestore) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 359 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 360 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 361 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
| 362 | AddBlankTabAndShow(browser()); |
| 363 | ASSERT_NO_FATAL_FAILURE( |
| 364 | ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 365 | DisableHangMonitor(browser()); |
| 366 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 367 | RepeatedNotificationObserver cancel_observer( |
| 368 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 369 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 370 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 371 | cancel_observer.Wait(); |
| 372 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 373 | |
| 374 | browser()->tab_strip_model() |
| 375 | ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE); |
| 376 | content::TestNavigationObserver navigation_observer( |
| 377 | browser()->tab_strip_model()->GetActiveWebContents(), 1); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 378 | ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition( |
| 379 | browser(), GURL(chrome::kChromeUIVersionURL), |
| 380 | WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE)); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 381 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 382 | navigation_observer.Wait(); |
| 383 | |
| 384 | RepeatedNotificationObserver close_observer( |
| 385 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 386 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 387 | close_observer.Wait(); |
| 388 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 389 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Test that the tab closed after the aborted shutdown attempt is not re-opened |
| 393 | // when restoring the session. |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 394 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 395 | TestSessionRestore) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 396 | // 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] | 40cd13a | 2013-10-24 13:42:01 | [diff] [blame] | 406 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 407 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 408 | 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"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 413 | DisableHangMonitor(browsers_[0]); |
| 414 | DisableHangMonitor(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 415 | |
| 416 | // Cancel shutdown on the first beforeunload event. |
| 417 | { |
| 418 | RepeatedNotificationObserver cancel_observer( |
| 419 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 420 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 421 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 422 | 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 | { |
| 430 | RepeatedNotificationObserver cancel_observer( |
| 431 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 432 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 433 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 434 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 435 | 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. |
| 442 | RepeatedNotificationObserver close_observer( |
| 443 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 444 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 445 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 446 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 447 | close_observer.Wait(); |
| 448 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 449 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 450 | } |
| 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. |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 455 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 456 | TestHangInBeforeUnloadMultipleTabs) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 457 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 458 | 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"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 466 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 467 | // the dialog is guaranteed to show. |
| 468 | DisableHangMonitor(browsers_[0]->tab_strip_model()->GetWebContentsAt(1)); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 469 | |
| 470 | RepeatedNotificationObserver cancel_observer( |
| 471 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 472 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 473 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 474 | 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 | |
| 479 | RepeatedNotificationObserver close_observer( |
| 480 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 481 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 482 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 483 | close_observer.Wait(); |
| 484 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 485 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 486 | } |
| 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. |
| 491 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 492 | TestHangInBeforeUnloadMultipleWindows) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 493 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 494 | 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"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 502 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 503 | // the dialog is guaranteed to show. |
| 504 | DisableHangMonitor(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 505 | |
| 506 | RepeatedNotificationObserver cancel_observer( |
| 507 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 508 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 509 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 510 | 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 | |
| 517 | RepeatedNotificationObserver close_observer( |
| 518 | chrome::NOTIFICATION_BROWSER_CLOSED, 3); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 519 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 520 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 521 | close_observer.Wait(); |
| 522 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 523 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 524 | } |
| 525 | |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 526 | // 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. |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 529 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 530 | TestUnloadMultipleSlowTabs) { |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 531 | 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 | } |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 547 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 548 | // the dialog is guaranteed to show. |
| 549 | DisableHangMonitor( |
| 550 | browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex)); |
| 551 | |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 552 | RepeatedNotificationObserver cancel_observer( |
| 553 | 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()); |
| 561 | RepeatedNotificationObserver close_observer( |
| 562 | 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. |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 576 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 577 | TestBeforeUnloadMultipleSlowWindows) { |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 578 | 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 | } |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 596 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 597 | // the dialog is guaranteed to show. |
| 598 | DisableHangMonitor(browsers_[kResposiveBrowserIndex]); |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 599 | |
| 600 | RepeatedNotificationObserver cancel_observer( |
| 601 | 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. |
| 612 | RepeatedNotificationObserver close_observer( |
| 613 | 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 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 621 | // Test that a window created during shutdown is closed. |
| 622 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 623 | TestAddWindowDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 624 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 625 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 626 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 627 | DisableHangMonitor(browsers_[0]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 628 | |
| 629 | RepeatedNotificationObserver close_observer( |
| 630 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 631 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 632 | browsers_.push_back(CreateBrowser(browser()->profile())); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 633 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 634 | close_observer.Wait(); |
| 635 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 636 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Test that a window created during shutdown with a beforeunload handler can |
| 640 | // cancel the shutdown. |
| 641 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
[email protected] | 40cd13a | 2013-10-24 13:42:01 | [diff] [blame] | 642 | TestAddWindowWithBeforeUnloadDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 643 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 644 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 645 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 646 | DisableHangMonitor(browsers_[0]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 647 | |
| 648 | RepeatedNotificationObserver cancel_observer( |
| 649 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 650 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 651 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 652 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 653 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 654 | DisableHangMonitor(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 655 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 656 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 657 | cancel_observer.Wait(); |
| 658 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 659 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 660 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 661 | |
| 662 | // Allow shutdown for both beforeunload dialogs. |
| 663 | RepeatedNotificationObserver close_observer( |
| 664 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 665 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 666 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 667 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 668 | close_observer.Wait(); |
| 669 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 670 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | // Test that tabs added during shutdown are closed. |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 674 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 675 | TestAddTabDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 676 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 677 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 678 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 679 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 680 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 681 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 682 | DisableHangMonitor(browsers_[0]); |
| 683 | DisableHangMonitor(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 684 | |
| 685 | RepeatedNotificationObserver close_observer( |
| 686 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 687 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 688 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 689 | AddBlankTabAndShow(browsers_[0]); |
| 690 | AddBlankTabAndShow(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 691 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 692 | close_observer.Wait(); |
| 693 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 694 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // Test that tabs created during shutdown with beforeunload handlers can cancel |
| 698 | // the shutdown. |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 699 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 700 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 701 | TestAddTabWithBeforeUnloadDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 702 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 703 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 704 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 705 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 706 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 707 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 708 | DisableHangMonitor(browsers_[0]); |
| 709 | DisableHangMonitor(browsers_[1]); |
| 710 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 711 | RepeatedNotificationObserver cancel_observer( |
| 712 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 713 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 714 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 715 | AddBlankTabAndShow(browsers_[0]); |
| 716 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 717 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 718 | AddBlankTabAndShow(browsers_[1]); |
| 719 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 720 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 721 | DisableHangMonitor(browsers_[0]); |
| 722 | DisableHangMonitor(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 723 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 724 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 725 | cancel_observer.Wait(); |
| 726 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 727 | EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count()); |
| 728 | EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count()); |
| 729 | |
| 730 | RepeatedNotificationObserver close_observer( |
| 731 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 732 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 733 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 734 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 735 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 736 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 737 | |
| 738 | close_observer.Wait(); |
| 739 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 740 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 741 | } |
| 742 | |
rogerm | f18929c | 2017-04-19 19:59:33 | [diff] [blame] | 743 | // TODO(crbug/713201): |
| 744 | // BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac. |
| 745 | #if defined(OS_MACOSX) |
| 746 | #define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing |
| 747 | #else |
| 748 | #define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing |
| 749 | #endif |
| 750 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 751 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
rogerm | f18929c | 2017-04-19 19:59:33 | [diff] [blame] | 752 | MAYBE_AddBeforeUnloadDuringClosing) { |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 753 | // TODO(crbug.com/250305): Currently FastUnloadController is broken. |
| 754 | // And it is difficult to fix this issue without fixing that one. |
| 755 | if (GetParam()) |
| 756 | return; |
| 757 | |
| 758 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 759 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 760 | browser(), embedded_test_server()->GetURL("/title1.html"))); |
| 761 | |
| 762 | // Open second window. |
| 763 | ui_test_utils::NavigateToURLWithDisposition( |
| 764 | browser(), embedded_test_server()->GetURL("/beforeunload.html"), |
| 765 | WindowOpenDisposition::NEW_WINDOW, |
| 766 | ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| 767 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 768 | auto* browser2 = BrowserList::GetInstance()->get(0) != browser() |
| 769 | ? BrowserList::GetInstance()->get(0) |
| 770 | : BrowserList::GetInstance()->get(1); |
| 771 | content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0)); |
| 772 | |
| 773 | // Let's work with second window only. |
| 774 | // This page has beforeunload handler already. |
| 775 | EXPECT_TRUE(browser2->tab_strip_model() |
| 776 | ->GetWebContentsAt(0) |
| 777 | ->NeedToFireBeforeUnload()); |
| 778 | // This page doesn't have beforeunload handler. Yet. |
| 779 | ui_test_utils::NavigateToURLWithDisposition( |
| 780 | browser2, embedded_test_server()->GetURL("/title2.html"), |
| 781 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 782 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 783 | content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1)); |
| 784 | EXPECT_FALSE(browser2->tab_strip_model() |
| 785 | ->GetWebContentsAt(1) |
| 786 | ->NeedToFireBeforeUnload()); |
| 787 | EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 788 | |
| 789 | DisableHangMonitor(browser2); |
| 790 | |
| 791 | // The test. |
| 792 | |
| 793 | TabRestoreServiceChangesObserver restore_observer(browser2->profile()); |
| 794 | content::WindowedNotificationObserver observer( |
| 795 | chrome::NOTIFICATION_BROWSER_CLOSED, |
| 796 | content::NotificationService::AllSources()); |
| 797 | chrome::CloseWindow(browser2); |
| 798 | // Just to be sure CloseWindow doesn't have asynchronous tasks |
| 799 | // that could have an impact. |
| 800 | content::RunAllPendingInMessageLoop(); |
| 801 | |
| 802 | // Closing browser shouldn't happen because of beforeunload handler. |
| 803 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 804 | // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it |
| 805 | // yet. |
| 806 | ASSERT_TRUE(content::ExecuteScript( |
| 807 | browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(), |
| 808 | "window.addEventListener('beforeunload', " |
| 809 | "function(event) { event.returnValue = 'Foo'; });")); |
| 810 | EXPECT_TRUE(browser2->tab_strip_model() |
| 811 | ->GetWebContentsAt(1) |
| 812 | ->NeedToFireBeforeUnload()); |
| 813 | // Accept closing the first tab. |
| 814 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 815 | // Just to be sure accepting a dialog doesn't have asynchronous tasks |
| 816 | // that could have an impact. |
| 817 | content::RunAllPendingInMessageLoop(); |
| 818 | // It shouldn't close the whole window/browser. |
| 819 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 820 | EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 821 | // Accept closing the second tab. |
| 822 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 823 | observer.Wait(); |
| 824 | // Now the second window/browser should be closed. |
| 825 | EXPECT_EQ(1u, BrowserList::GetInstance()->size()); |
| 826 | EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0)); |
| 827 | EXPECT_EQ(1u, restore_observer.changes_count()); |
| 828 | |
| 829 | // Restore the closed browser. |
| 830 | content::WindowedNotificationObserver open_window_observer( |
| 831 | chrome::NOTIFICATION_BROWSER_OPENED, |
| 832 | content::NotificationService::AllSources()); |
| 833 | chrome::OpenWindowWithRestoredTabs(browser()->profile()); |
| 834 | open_window_observer.Wait(); |
| 835 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 836 | browser2 = BrowserList::GetInstance()->get(0) != browser() |
| 837 | ? BrowserList::GetInstance()->get(0) |
| 838 | : BrowserList::GetInstance()->get(1); |
| 839 | |
| 840 | // Check the restored browser contents. |
| 841 | EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 842 | EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"), |
| 843 | browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL()); |
| 844 | EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"), |
| 845 | browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL()); |
| 846 | } |
| 847 | |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 848 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
avi | 8804091 | 2017-03-30 03:48:42 | [diff] [blame] | 849 | TestCloseTabDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 850 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 851 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 852 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 853 | DisableHangMonitor(browsers_[0]); |
| 854 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 855 | RepeatedNotificationObserver cancel_observer( |
| 856 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 857 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 858 | |
| 859 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 860 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 861 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 862 | DisableHangMonitor(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 863 | browsers_[1]->tab_strip_model()->CloseAllTabs(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 864 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 865 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 866 | cancel_observer.Wait(); |
| 867 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 868 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 869 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 870 | |
| 871 | RepeatedNotificationObserver close_observer( |
| 872 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 873 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 874 | browsers_[1]->tab_strip_model()->CloseAllTabs(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 875 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 876 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 877 | |
| 878 | close_observer.Wait(); |
| 879 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 880 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 881 | } |
| 882 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 883 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 884 | TestOpenAndCloseWindowDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 885 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 886 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 887 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 888 | DisableHangMonitor(browsers_[0]); |
| 889 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 890 | RepeatedNotificationObserver cancel_observer( |
| 891 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 892 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 893 | |
| 894 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 895 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 896 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 897 | DisableHangMonitor(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 898 | ASSERT_FALSE(browsers_[1]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 899 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 900 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 901 | cancel_observer.Wait(); |
| 902 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 903 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 904 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 905 | |
| 906 | RepeatedNotificationObserver close_observer( |
| 907 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 908 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 909 | ASSERT_FALSE(browsers_[1]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 910 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 911 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 912 | |
| 913 | close_observer.Wait(); |
| 914 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 915 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 919 | TestCloseWindowDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 920 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 921 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 922 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 923 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 924 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 925 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 926 | DisableHangMonitor(browsers_[0]); |
| 927 | DisableHangMonitor(browsers_[1]); |
| 928 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 929 | RepeatedNotificationObserver cancel_observer( |
| 930 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 931 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 932 | |
| 933 | ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 934 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 935 | cancel_observer.Wait(); |
| 936 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 937 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 938 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 939 | |
| 940 | RepeatedNotificationObserver close_observer( |
| 941 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 942 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 943 | ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 944 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 945 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 946 | |
| 947 | close_observer.Wait(); |
| 948 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 949 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 950 | } |
| 951 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 952 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, |
| 953 | BrowserCloseManagerBrowserTest, |
| 954 | testing::Bool()); |
| 955 | |
| 956 | class BrowserCloseManagerWithDownloadsBrowserTest : |
| 957 | public BrowserCloseManagerBrowserTest { |
| 958 | public: |
| 959 | BrowserCloseManagerWithDownloadsBrowserTest() {} |
| 960 | virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {} |
| 961 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 962 | void SetUpOnMainThread() override { |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 963 | BrowserCloseManagerBrowserTest::SetUpOnMainThread(); |
| 964 | ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir()); |
| 965 | } |
| 966 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 967 | void SetDownloadPathForProfile(Profile* profile) { |
| 968 | DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile); |
| 969 | download_prefs->SetDownloadPath(download_path()); |
| 970 | } |
| 971 | |
| 972 | const base::FilePath& download_path() const { |
vabr | 0c237ae | 2016-09-14 09:24:28 | [diff] [blame] | 973 | return scoped_download_directory_.GetPath(); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | private: |
| 977 | base::ScopedTempDir scoped_download_directory_; |
| 978 | }; |
| 979 | |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 980 | // Mac has its own in-progress download prompt in app_controller_mac.mm, so |
| 981 | // BrowserCloseManager should simply close all browsers. If there are no |
| 982 | // browsers, it should not crash. |
| 983 | #if defined(OS_MACOSX) |
| 984 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 985 | TestWithDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 986 | ASSERT_TRUE(embedded_test_server()->Start()); |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 987 | SetDownloadPathForProfile(browser()->profile()); |
| 988 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 989 | |
| 990 | RepeatedNotificationObserver close_observer( |
| 991 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 992 | |
| 993 | TestBrowserCloseManager::AttemptClose( |
| 994 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 995 | close_observer.Wait(); |
| 996 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 997 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 998 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 999 | |
| 1000 | // Attempting to close again should not crash. |
| 1001 | TestBrowserCloseManager::AttemptClose( |
| 1002 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 1003 | } |
| 1004 | #else // defined(OS_MACOSX) |
| 1005 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1006 | // Test shutdown with a DANGEROUS_URL download undecided. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1007 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1008 | TestWithDangerousUrlDownload) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 1009 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1010 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1011 | |
| 1012 | // Set up the fake delegate that forces the download to be malicious. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 1013 | std::unique_ptr<TestDownloadManagerDelegate> test_delegate( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1014 | new TestDownloadManagerDelegate(browser()->profile())); |
dcheng | 383ba8a | 2014-10-16 23:55:19 | [diff] [blame] | 1015 | DownloadServiceFactory::GetForBrowserContext(browser()->profile()) |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 1016 | ->SetDownloadManagerDelegateForTesting(std::move(test_delegate)); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1017 | |
| 1018 | // Run a dangerous download, but the user doesn't make a decision. |
| 1019 | // This .swf normally would be categorized as DANGEROUS_FILE, but |
| 1020 | // TestDownloadManagerDelegate turns it into DANGEROUS_URL. |
tfarina | 5e7b5723 | 2015-10-17 23:37:40 | [diff] [blame] | 1021 | GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl( |
| 1022 | "downloads/dangerous/dangerous.swf")); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1023 | content::DownloadTestObserverInterrupted observer( |
| 1024 | content::BrowserContext::GetDownloadManager(browser()->profile()), |
| 1025 | 1, |
| 1026 | content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT); |
| 1027 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 1028 | browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1029 | ui_test_utils::BROWSER_TEST_NONE); |
| 1030 | observer.WaitForFinished(); |
| 1031 | |
| 1032 | // Check that the download manager has the expected state. |
| 1033 | EXPECT_EQ(1, content::BrowserContext::GetDownloadManager( |
| 1034 | browser()->profile())->InProgressCount()); |
| 1035 | EXPECT_EQ(0, content::BrowserContext::GetDownloadManager( |
| 1036 | browser()->profile())->NonMaliciousInProgressCount()); |
| 1037 | |
| 1038 | // Close the browser with no user action. |
| 1039 | RepeatedNotificationObserver close_observer( |
| 1040 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1041 | TestBrowserCloseManager::AttemptClose( |
| 1042 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 1043 | close_observer.Wait(); |
| 1044 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1045 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1046 | } |
| 1047 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1048 | // Test shutdown with a download in progress. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1049 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1050 | TestWithDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 1051 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1052 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1053 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1054 | content::TestNavigationObserver navigation_observer( |
| 1055 | browser()->tab_strip_model()->GetActiveWebContents(), 1); |
| 1056 | TestBrowserCloseManager::AttemptClose( |
| 1057 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 1058 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1059 | navigation_observer.Wait(); |
| 1060 | EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL), |
| 1061 | browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 1062 | |
| 1063 | RepeatedNotificationObserver close_observer( |
| 1064 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1065 | |
| 1066 | TestBrowserCloseManager::AttemptClose( |
| 1067 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1068 | close_observer.Wait(); |
| 1069 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1070 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 1071 | if (browser_defaults::kBrowserAliveWithNoWindows) |
| 1072 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 1073 | else |
| 1074 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1075 | } |
| 1076 | |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1077 | // Test shutdown with a download in progress in an off-the-record profile. |
| 1078 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1079 | TestWithOffTheRecordDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 1080 | ASSERT_TRUE(embedded_test_server()->Start()); |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1081 | Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile(); |
| 1082 | SetDownloadPathForProfile(otr_profile); |
| 1083 | Browser* otr_browser = CreateBrowser(otr_profile); |
| 1084 | { |
| 1085 | RepeatedNotificationObserver close_observer( |
| 1086 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1087 | browser()->window()->Close(); |
| 1088 | close_observer.Wait(); |
| 1089 | } |
| 1090 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser)); |
| 1091 | content::TestNavigationObserver navigation_observer( |
| 1092 | otr_browser->tab_strip_model()->GetActiveWebContents(), 1); |
| 1093 | TestBrowserCloseManager::AttemptClose( |
| 1094 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 1095 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1096 | navigation_observer.Wait(); |
| 1097 | EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL), |
| 1098 | otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 1099 | |
| 1100 | RepeatedNotificationObserver close_observer( |
| 1101 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1102 | |
| 1103 | TestBrowserCloseManager::AttemptClose( |
| 1104 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1105 | close_observer.Wait(); |
| 1106 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1107 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1108 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 1109 | } |
| 1110 | |
Asanka Herath | d1341dc | 2017-04-19 18:28:39 | [diff] [blame^] | 1111 | // Test shutdown with a download in progress in a regular profile an inconito |
| 1112 | // browser is opened and closed. While there are active downloads, closing the |
| 1113 | // incognito window shouldn't block on the active downloads which belong to the |
| 1114 | // parent profile. |
| 1115 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1116 | TestWithOffTheRecordWindowAndRegularDownload) { |
| 1117 | Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile(); |
| 1118 | SetDownloadPathForProfile(otr_profile); |
| 1119 | Browser* otr_browser = CreateBrowser(otr_profile); |
| 1120 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1121 | |
| 1122 | content::TestNavigationObserver navigation_observer( |
| 1123 | otr_browser->tab_strip_model()->GetActiveWebContents(), 1); |
| 1124 | ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank")); |
| 1125 | navigation_observer.Wait(); |
| 1126 | |
| 1127 | int num_downloads_blocking = 0; |
| 1128 | ASSERT_EQ( |
| 1129 | Browser::DOWNLOAD_CLOSE_OK, |
| 1130 | otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking)); |
| 1131 | ASSERT_EQ(0, num_downloads_blocking); |
| 1132 | |
| 1133 | { |
| 1134 | RepeatedNotificationObserver close_observer( |
| 1135 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1136 | otr_browser->window()->Close(); |
| 1137 | close_observer.Wait(); |
| 1138 | } |
| 1139 | |
| 1140 | ASSERT_EQ( |
| 1141 | Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN, |
| 1142 | browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking)); |
| 1143 | ASSERT_EQ(1, num_downloads_blocking); |
| 1144 | |
| 1145 | { |
| 1146 | RepeatedNotificationObserver close_observer( |
| 1147 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
| 1148 | TestBrowserCloseManager::AttemptClose( |
| 1149 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1150 | close_observer.Wait(); |
| 1151 | } |
| 1152 | |
| 1153 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 1154 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
| 1155 | if (browser_defaults::kBrowserAliveWithNoWindows) |
| 1156 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 1157 | else |
| 1158 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 1159 | } |
| 1160 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1161 | // Test shutdown with a download in progress from one profile, where the only |
| 1162 | // open windows are for another profile. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1163 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1164 | TestWithDownloadsFromDifferentProfiles) { |
| 1165 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1166 | base::FilePath path = |
| 1167 | profile_manager->user_data_dir().AppendASCII("test_profile"); |
| 1168 | if (!base::PathExists(path)) |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 1169 | ASSERT_TRUE(base::CreateDirectory(path)); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1170 | Profile* other_profile = |
| 1171 | Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); |
| 1172 | profile_manager->RegisterTestingProfile(other_profile, true, false); |
| 1173 | Browser* other_profile_browser = CreateBrowser(other_profile); |
| 1174 | |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 1175 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1176 | SetDownloadPathForProfile(browser()->profile()); |
| 1177 | SetDownloadPathForProfile(other_profile); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1178 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1179 | { |
| 1180 | RepeatedNotificationObserver close_observer( |
| 1181 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1182 | browser()->window()->Close(); |
| 1183 | close_observer.Wait(); |
| 1184 | } |
| 1185 | |
| 1186 | // When the shutdown is cancelled, the downloads page should be opened in a |
| 1187 | // browser for that profile. Because there are no browsers for that profile, a |
| 1188 | // new browser should be opened. |
| 1189 | ui_test_utils::BrowserAddedObserver new_browser_observer; |
| 1190 | TestBrowserCloseManager::AttemptClose( |
| 1191 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 1192 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1193 | Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser(); |
| 1194 | EXPECT_EQ( |
| 1195 | GURL(chrome::kChromeUIDownloadsURL), |
| 1196 | opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 1197 | EXPECT_EQ(GURL("about:blank"), |
| 1198 | other_profile_browser->tab_strip_model()->GetActiveWebContents() |
| 1199 | ->GetURL()); |
| 1200 | |
| 1201 | RepeatedNotificationObserver close_observer( |
| 1202 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
| 1203 | TestBrowserCloseManager::AttemptClose( |
| 1204 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1205 | close_observer.Wait(); |
| 1206 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1207 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 1208 | if (browser_defaults::kBrowserAliveWithNoWindows) |
| 1209 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 1210 | else |
| 1211 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | // Test shutdown with downloads in progress and beforeunload handlers. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1215 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 1216 | TestBeforeUnloadAndDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 1217 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1218 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1219 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1220 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 1221 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 1222 | DisableHangMonitor(browser()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1223 | |
| 1224 | content::WindowedNotificationObserver cancel_observer( |
| 1225 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 1226 | content::NotificationService::AllSources()); |
| 1227 | TestBrowserCloseManager::AttemptClose( |
| 1228 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 1229 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1230 | cancel_observer.Wait(); |
| 1231 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1232 | |
| 1233 | RepeatedNotificationObserver close_observer( |
| 1234 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1235 | TestBrowserCloseManager::AttemptClose( |
| 1236 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 1237 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1238 | close_observer.Wait(); |
| 1239 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1240 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1241 | } |
| 1242 | |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 1243 | #endif // defined(OS_MACOSX) |
| 1244 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1245 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1246 | BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 1247 | testing::Bool()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1248 | |
| 1249 | class BrowserCloseManagerWithBackgroundModeBrowserTest |
| 1250 | : public BrowserCloseManagerBrowserTest { |
| 1251 | public: |
| 1252 | BrowserCloseManagerWithBackgroundModeBrowserTest() {} |
| 1253 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 1254 | void SetUpOnMainThread() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1255 | BrowserCloseManagerBrowserTest::SetUpOnMainThread(); |
| 1256 | g_browser_process->set_background_mode_manager_for_test( |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 1257 | std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager)); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | bool IsBackgroundModeSuspended() { |
| 1261 | return static_cast<FakeBackgroundModeManager*>( |
| 1262 | g_browser_process->background_mode_manager()) |
| 1263 | ->IsBackgroundModeSuspended(); |
| 1264 | } |
| 1265 | |
| 1266 | private: |
| 1267 | DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest); |
| 1268 | }; |
| 1269 | |
| 1270 | // Check that background mode is suspended when closing all browsers unless we |
| 1271 | // are quitting and that background mode is resumed when a new browser window is |
| 1272 | // opened. |
| 1273 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1274 | CloseAllBrowsersWithBackgroundMode) { |
| 1275 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 1276 | std::unique_ptr<ScopedKeepAlive> tmp_keep_alive; |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1277 | Profile* profile = browser()->profile(); |
| 1278 | { |
| 1279 | RepeatedNotificationObserver close_observer( |
| 1280 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 1281 | tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW, |
| 1282 | KeepAliveRestartOption::DISABLED)); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1283 | chrome::CloseAllBrowsers(); |
| 1284 | close_observer.Wait(); |
| 1285 | } |
| 1286 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1287 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1288 | EXPECT_TRUE(IsBackgroundModeSuspended()); |
| 1289 | |
| 1290 | // Background mode should be resumed when a new browser window is opened. |
| 1291 | ui_test_utils::BrowserAddedObserver new_browser_observer; |
scottmg | 85194900 | 2016-02-09 20:09:44 | [diff] [blame] | 1292 | chrome::NewEmptyWindow(profile); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1293 | new_browser_observer.WaitForSingleNewBrowser(); |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 1294 | tmp_keep_alive.reset(); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1295 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1296 | RepeatedNotificationObserver close_observer( |
| 1297 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1298 | |
| 1299 | // Background mode should not be suspended when quitting. |
| 1300 | chrome::CloseAllBrowsersAndQuit(); |
| 1301 | close_observer.Wait(); |
| 1302 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1303 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1304 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | // Check that closing the last browser window individually does not affect |
| 1308 | // background mode. |
| 1309 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
dimich | 981e50f | 2016-03-03 23:39:16 | [diff] [blame] | 1310 | DISABLED_CloseSingleBrowserWithBackgroundMode) { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1311 | RepeatedNotificationObserver close_observer( |
| 1312 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1313 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1314 | browser()->window()->Close(); |
| 1315 | close_observer.Wait(); |
| 1316 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1317 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1318 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1319 | } |
| 1320 | |
| 1321 | // Check that closing all browsers with no browser windows open suspends |
| 1322 | // background mode but does not cause Chrome to quit. |
| 1323 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
dimich | 8148afc2 | 2016-03-04 01:57:03 | [diff] [blame] | 1324 | DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1325 | RepeatedNotificationObserver close_observer( |
| 1326 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1327 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 1328 | ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW, |
| 1329 | KeepAliveRestartOption::DISABLED); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1330 | browser()->window()->Close(); |
| 1331 | close_observer.Wait(); |
| 1332 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1333 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1334 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1335 | |
| 1336 | chrome::CloseAllBrowsers(); |
| 1337 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1338 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1339 | EXPECT_TRUE(IsBackgroundModeSuspended()); |
| 1340 | } |
| 1341 | |
| 1342 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1343 | BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1344 | testing::Bool()); |