[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" |
Gabriel Charette | 078e366 | 2017-08-28 22:59:04 | [diff] [blame] | 13 | #include "base/run_loop.h" |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 14 | #include "base/threading/thread_restrictions.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 16 | #include "chrome/browser/background/background_mode_manager.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 17 | #include "chrome/browser/browser_process.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 18 | #include "chrome/browser/browser_shutdown.h" |
| 19 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 20 | #include "chrome/browser/defaults.h" |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 21 | #include "chrome/browser/download/chrome_download_manager_delegate.h" |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 22 | #include "chrome/browser/download/download_core_service.h" |
| 23 | #include "chrome/browser/download/download_core_service_factory.h" |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 24 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 25 | #include "chrome/browser/lifetime/application_lifetime.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" |
Denis Kuznetsov | 32099f3 | 2017-12-15 14:04:57 | [diff] [blame] | 37 | #include "chrome/common/features.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 38 | #include "chrome/common/url_constants.h" |
| 39 | #include "chrome/test/base/in_process_browser_test.h" |
| 40 | #include "chrome/test/base/ui_test_utils.h" |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 41 | #include "components/app_modal/javascript_app_modal_dialog.h" |
| 42 | #include "components/app_modal/native_app_modal_dialog.h" |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame^] | 43 | #include "components/download/public/common/download_item.h" |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 44 | #include "components/keep_alive_registry/keep_alive_types.h" |
| 45 | #include "components/keep_alive_registry/scoped_keep_alive.h" |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 46 | #include "components/sessions/core/tab_restore_service.h" |
| 47 | #include "components/sessions/core/tab_restore_service_observer.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 48 | #include "content/public/browser/browser_context.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 49 | #include "content/public/browser/download_manager.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 50 | #include "content/public/browser/notification_service.h" |
avi | 64b8b65b | 2017-03-29 18:50:34 | [diff] [blame] | 51 | #include "content/public/browser/render_frame_host.h" |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 52 | #include "content/public/browser/render_view_host.h" |
| 53 | #include "content/public/browser/render_widget_host.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 54 | #include "content/public/browser/web_contents.h" |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 55 | #include "content/public/test/browser_test_utils.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 56 | #include "content/public/test/download_test_observer.h" |
Colin Blundell | fb2cfb2b | 2018-01-18 18:50:09 | [diff] [blame] | 57 | #include "content/public/test/slow_download_http_response.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 58 | #include "content/public/test/test_navigation_observer.h" |
| 59 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 60 | |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 61 | #if defined(OS_CHROMEOS) |
| 62 | #include "chromeos/chromeos_switches.h" |
| 63 | #endif |
| 64 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 65 | namespace { |
| 66 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 67 | app_modal::NativeAppModalDialog* GetNextDialog() { |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 68 | app_modal::JavaScriptAppModalDialog* dialog = |
| 69 | ui_test_utils::WaitForAppModalDialog(); |
| 70 | CHECK(dialog->native_dialog()); |
| 71 | return dialog->native_dialog(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 72 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 73 | |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 74 | // Note: call |PrepareForDialog| on the relevant WebContents or Browser before |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 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 | |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 80 | // Note: call |PrepareForDialog| on the relevant WebContents or Browser before |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 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 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 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 | |
| 93 | void Observe(int type, |
| 94 | const content::NotificationSource& source, |
| 95 | const content::NotificationDetails& details) override { |
| 96 | ASSERT_GT(num_outstanding_, 0); |
| 97 | if (!--num_outstanding_ && running_) { |
| 98 | content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 99 | 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) { |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame^] | 202 | GetDownloadIdReceiverCallback().Run(download::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( |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame^] | 207 | download::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, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame^] | 217 | download::DownloadItem::TargetDisposition disp, |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 218 | download::DownloadDangerType danger_type, |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 219 | const base::FilePath& intermediate_path, |
Min Qin | eb78b7a | 2018-02-03 00:43:16 | [diff] [blame] | 220 | download::DownloadInterruptReason reason) { |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 221 | callback.Run(target_path, disp, |
| 222 | download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 223 | intermediate_path, reason); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 224 | } |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 225 | }; |
| 226 | |
Denis Kuznetsov | 32099f3 | 2017-12-15 14:04:57 | [diff] [blame] | 227 | #if BUILDFLAG(ENABLE_BACKGROUND_MODE) |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 228 | class FakeBackgroundModeManager : public BackgroundModeManager { |
| 229 | public: |
| 230 | FakeBackgroundModeManager() |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 231 | : BackgroundModeManager(*base::CommandLine::ForCurrentProcess(), |
| 232 | &g_browser_process->profile_manager() |
| 233 | ->GetProfileAttributesStorage()), |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 234 | suspended_(false) {} |
| 235 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 236 | void SuspendBackgroundMode() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 237 | BackgroundModeManager::SuspendBackgroundMode(); |
| 238 | suspended_ = true; |
| 239 | } |
| 240 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 241 | void ResumeBackgroundMode() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 242 | BackgroundModeManager::ResumeBackgroundMode(); |
| 243 | suspended_ = false; |
| 244 | } |
| 245 | |
| 246 | bool IsBackgroundModeSuspended() { |
| 247 | return suspended_; |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | bool suspended_; |
| 252 | |
| 253 | DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager); |
| 254 | }; |
Denis Kuznetsov | 32099f3 | 2017-12-15 14:04:57 | [diff] [blame] | 255 | #endif // BUILDFLAG(ENABLE_BACKGROUND_MODE) |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 256 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 257 | } // namespace |
| 258 | |
| 259 | class BrowserCloseManagerBrowserTest |
| 260 | : public InProcessBrowserTest, |
| 261 | public testing::WithParamInterface<bool> { |
| 262 | protected: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 263 | void SetUpOnMainThread() override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 264 | SessionStartupPref::SetStartupPref( |
| 265 | browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); |
| 266 | browsers_.push_back(browser()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 267 | content::BrowserThread::PostTask( |
tzik | 3f7781d | 2017-04-20 17:09:33 | [diff] [blame] | 268 | content::BrowserThread::IO, FROM_HERE, |
| 269 | base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
Colin Blundell | fb2cfb2b | 2018-01-18 18:50:09 | [diff] [blame] | 270 | embedded_test_server()->RegisterRequestHandler(base::BindRepeating( |
| 271 | &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest)); |
| 272 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 273 | } |
| 274 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 275 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 276 | if (GetParam()) |
| 277 | command_line->AppendSwitch(switches::kEnableFastUnload); |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 278 | #if defined(OS_CHROMEOS) |
| 279 | command_line->AppendSwitch( |
| 280 | chromeos::switches::kIgnoreUserProfileMappingForTests); |
| 281 | #endif |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 284 | void CreateStalledDownload(Browser* browser) { |
Colin Blundell | fb2cfb2b | 2018-01-18 18:50:09 | [diff] [blame] | 285 | ASSERT_TRUE(embedded_test_server()->Started()); |
| 286 | |
| 287 | GURL slow_download_url = embedded_test_server()->GetURL( |
| 288 | content::SlowDownloadHttpResponse::kKnownSizeUrl); |
| 289 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 290 | content::DownloadTestObserverInProgress observer( |
| 291 | content::BrowserContext::GetDownloadManager(browser->profile()), 1); |
| 292 | ui_test_utils::NavigateToURLWithDisposition( |
Colin Blundell | fb2cfb2b | 2018-01-18 18:50:09 | [diff] [blame] | 293 | browser, slow_download_url, WindowOpenDisposition::NEW_BACKGROUND_TAB, |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 294 | ui_test_utils::BROWSER_TEST_NONE); |
| 295 | observer.WaitForFinished(); |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame^] | 296 | EXPECT_EQ(1UL, observer.NumDownloadsSeenInState( |
| 297 | download::DownloadItem::IN_PROGRESS)); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 298 | } |
| 299 | |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 300 | void PrepareForDialog(content::WebContents* web_contents) { |
| 301 | content::PrepContentsForBeforeUnloadTest(web_contents); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 302 | } |
| 303 | |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 304 | void PrepareForDialog(Browser* browser) { |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 305 | for (int i = 0; i < browser->tab_strip_model()->count(); i++) |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 306 | PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i)); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 309 | std::vector<Browser*> browsers_; |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 313 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 314 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 315 | PrepareForDialog(browser()); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 316 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 317 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 318 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 319 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 320 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 321 | cancel_observer.Wait(); |
| 322 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 323 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 324 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 325 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 326 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 327 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 328 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 329 | close_observer.Wait(); |
| 330 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 331 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 335 | TestShutdownMoreThanOnce) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 336 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 337 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 338 | PrepareForDialog(browser()); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 339 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 340 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 341 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 342 | chrome::CloseAllBrowsersAndQuit(); |
| 343 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 344 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 345 | cancel_observer.Wait(); |
| 346 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 347 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 348 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 349 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 350 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 351 | chrome::CloseAllBrowsersAndQuit(); |
| 352 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 353 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 354 | close_observer.Wait(); |
| 355 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 356 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 357 | } |
| 358 | |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 359 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 360 | PRE_TestSessionRestore) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 361 | 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))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 366 | PrepareForDialog(browser()); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 367 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 368 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 369 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 370 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 371 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 372 | 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); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 379 | ASSERT_NO_FATAL_FAILURE(NavigateToURLWithDisposition( |
| 380 | browser(), GURL(chrome::kChromeUIVersionURL), |
| 381 | WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE)); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 382 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 383 | navigation_observer.Wait(); |
| 384 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 385 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 386 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 387 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 388 | close_observer.Wait(); |
| 389 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 390 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Test that the tab closed after the aborted shutdown attempt is not re-opened |
| 394 | // when restoring the session. |
benwells | 70cc9322 | 2017-06-29 05:42:36 | [diff] [blame] | 395 | // Flaky on Windows trybots, see https://crbug.com/737860. |
| 396 | #if defined(OS_WIN) |
| 397 | #define MAYBE_TestSessionRestore DISABLED_TestSessionRestore |
| 398 | #else |
| 399 | #define MAYBE_TestSessionRestore TestSessionRestore |
| 400 | #endif |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 401 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
benwells | 70cc9322 | 2017-06-29 05:42:36 | [diff] [blame] | 402 | MAYBE_TestSessionRestore) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 403 | // The testing framework launches Chrome with about:blank as args. |
| 404 | EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 405 | EXPECT_EQ(GURL(chrome::kChromeUIVersionURL), |
| 406 | browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL()); |
| 407 | EXPECT_EQ(GURL("about:blank"), |
| 408 | browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL()); |
| 409 | } |
| 410 | |
| 411 | // Test that browser windows are only closed if all browsers are ready to close |
| 412 | // and that all beforeunload dialogs are shown again after a cancel. |
[email protected] | 40cd13a | 2013-10-24 13:42:01 | [diff] [blame] | 413 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 414 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 415 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 416 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 417 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 418 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 419 | PrepareForDialog(browsers_[0]); |
| 420 | PrepareForDialog(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 421 | |
| 422 | // Cancel shutdown on the first beforeunload event. |
| 423 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 424 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 425 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 426 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 427 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 428 | cancel_observer.Wait(); |
| 429 | } |
| 430 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 431 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 432 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 433 | |
| 434 | // Cancel shutdown on the second beforeunload event. |
| 435 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 436 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 437 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 438 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 439 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 440 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 441 | cancel_observer.Wait(); |
| 442 | } |
| 443 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 444 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 445 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 446 | |
| 447 | // Allow shutdown for both beforeunload events. |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 448 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 449 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 450 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 451 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 452 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 453 | close_observer.Wait(); |
| 454 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 455 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Test that tabs in the same window with a beforeunload event that hangs are |
| 459 | // treated the same as the user accepting the close, but do not close the tab |
| 460 | // early. |
| 461 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 462 | TestHangInBeforeUnloadMultipleTabs) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 463 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 464 | browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
| 465 | AddBlankTabAndShow(browsers_[0]); |
| 466 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 467 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 468 | AddBlankTabAndShow(browsers_[0]); |
| 469 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 470 | browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 471 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 472 | // the dialog is guaranteed to show. |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 473 | PrepareForDialog(browsers_[0]->tab_strip_model()->GetWebContentsAt(1)); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 474 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 475 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 476 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 477 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 478 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 479 | cancel_observer.Wait(); |
| 480 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 481 | // All tabs should still be open. |
| 482 | EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count()); |
| 483 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 484 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 485 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 486 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 487 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 488 | close_observer.Wait(); |
| 489 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 490 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Test that tabs in different windows with a beforeunload event that hangs are |
| 494 | // treated the same as the user accepting the close, but do not close the tab |
| 495 | // early. |
| 496 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 497 | TestHangInBeforeUnloadMultipleWindows) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 498 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 499 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 500 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 501 | browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
| 502 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 503 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 504 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 505 | browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 506 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 507 | // the dialog is guaranteed to show. |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 508 | PrepareForDialog(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 509 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 510 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 511 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 512 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 513 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 514 | cancel_observer.Wait(); |
| 515 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 516 | // All windows should still be open. |
| 517 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 518 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 519 | EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count()); |
| 520 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 521 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 522 | chrome::NOTIFICATION_BROWSER_CLOSED, 3); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 523 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 524 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 525 | close_observer.Wait(); |
| 526 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 527 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 528 | } |
| 529 | |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 530 | // Test that tabs that are slow to respond are not closed prematurely. |
| 531 | // Regression for crbug.com/365052 caused some of tabs to be closed even if |
| 532 | // user chose to cancel browser close. |
Kevin McNee | 98ba00f | 2018-02-09 19:19:36 | [diff] [blame] | 533 | // Flaky on ChromeOS ASan. https://crbug.com/805457 |
| 534 | #if defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER) |
| 535 | #define MAYBE_TestUnloadMultipleSlowTabs DISABLED_TestUnloadMultipleSlowTabs |
| 536 | #else |
| 537 | #define MAYBE_TestUnloadMultipleSlowTabs TestUnloadMultipleSlowTabs |
| 538 | #endif |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 539 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
Kevin McNee | 98ba00f | 2018-02-09 19:19:36 | [diff] [blame] | 540 | MAYBE_TestUnloadMultipleSlowTabs) { |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 541 | const int kTabCount = 5; |
| 542 | const int kResposiveTabIndex = 2; |
| 543 | // Create tab strip with all tabs except one responding after |
| 544 | // RenderViewHostImpl::kUnloadTimeoutMS. |
| 545 | // Minimum configuration is two slow tabs and then responsive tab. |
| 546 | // But we also want to check how slow tabs behave in tail. |
| 547 | for (int i = 0; i < kTabCount; i++) { |
| 548 | if (i) |
| 549 | AddBlankTabAndShow(browsers_[0]); |
| 550 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 551 | browsers_[0], |
| 552 | embedded_test_server()->GetURL((i == kResposiveTabIndex) |
| 553 | ? "/beforeunload.html" |
| 554 | : "/beforeunload_slow.html"))); |
| 555 | } |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 556 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 557 | // the dialog is guaranteed to show. |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 558 | PrepareForDialog( |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 559 | browsers_[0]->tab_strip_model()->GetWebContentsAt(kResposiveTabIndex)); |
| 560 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 561 | RepeatedNotificationObserver cancel_observer( |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 562 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
| 563 | chrome::CloseAllBrowsersAndQuit(); |
| 564 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 565 | cancel_observer.Wait(); |
| 566 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 567 | |
| 568 | // All tabs should still be open. |
| 569 | EXPECT_EQ(kTabCount, browsers_[0]->tab_strip_model()->count()); |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 570 | RepeatedNotificationObserver close_observer( |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 571 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 572 | |
| 573 | // Quit, this time accepting close confirmation dialog. |
| 574 | chrome::CloseAllBrowsersAndQuit(); |
| 575 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 576 | close_observer.Wait(); |
| 577 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 578 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
| 579 | } |
| 580 | |
| 581 | // Test that tabs in different windows with a slow beforeunload event response |
| 582 | // are treated the same as the user accepting the close, but do not close the |
| 583 | // tab early. |
| 584 | // Regression for crbug.com/365052 caused CHECK in tabstrip. |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 585 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 586 | TestBeforeUnloadMultipleSlowWindows) { |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 587 | const int kBrowserCount = 5; |
| 588 | const int kResposiveBrowserIndex = 2; |
| 589 | // Create multiple browsers with all tabs except one responding after |
| 590 | // RenderViewHostImpl::kUnloadTimeoutMS . |
| 591 | // Minimum configuration is just one browser with slow tab and then |
| 592 | // browser with responsive tab. |
| 593 | // But we also want to check how slow tabs behave in tail and make test |
| 594 | // more robust. |
| 595 | for (int i = 0; i < kBrowserCount; i++) { |
| 596 | if (i) |
| 597 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 598 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 599 | browsers_[i], |
| 600 | embedded_test_server()->GetURL((i == kResposiveBrowserIndex) |
| 601 | ? "/beforeunload.html" |
| 602 | : "/beforeunload_slow.html"))); |
| 603 | } |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 604 | // Disable the hang monitor in the tab that is not expected to hang, so that |
| 605 | // the dialog is guaranteed to show. |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 606 | PrepareForDialog(browsers_[kResposiveBrowserIndex]); |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 607 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 608 | RepeatedNotificationObserver cancel_observer( |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 609 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, kResposiveBrowserIndex + 1); |
| 610 | chrome::CloseAllBrowsersAndQuit(); |
| 611 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 612 | cancel_observer.Wait(); |
| 613 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 614 | |
| 615 | // All windows should still be open. |
| 616 | for (int i = 0; i < kBrowserCount; i++) |
| 617 | EXPECT_EQ(1, browsers_[i]->tab_strip_model()->count()); |
| 618 | |
| 619 | // Quit, this time accepting close confirmation dialog. |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 620 | RepeatedNotificationObserver close_observer( |
metaflow | 73fc955 | 2016-05-23 18:26:48 | [diff] [blame] | 621 | chrome::NOTIFICATION_BROWSER_CLOSED, kBrowserCount); |
| 622 | chrome::CloseAllBrowsersAndQuit(); |
| 623 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 624 | close_observer.Wait(); |
| 625 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 626 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
| 627 | } |
| 628 | |
Jan Krcal | 9a23f22 | 2017-08-01 14:13:07 | [diff] [blame] | 629 | // Flaky on Windows 7 (dbg) trybot, see https://crbug.com/751081. |
| 630 | #if defined(OS_WIN) && !defined(NDEBUG) |
| 631 | #define MAYBE_TestAddWindowDuringShutdown DISABLED_TestAddWindowDuringShutdown |
| 632 | #else |
| 633 | #define MAYBE_TestAddWindowDuringShutdown TestAddWindowDuringShutdown |
| 634 | #endif |
| 635 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 636 | // Test that a window created during shutdown is closed. |
| 637 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
Jan Krcal | 9a23f22 | 2017-08-01 14:13:07 | [diff] [blame] | 638 | MAYBE_TestAddWindowDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 639 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 640 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 641 | PrepareForDialog(browsers_[0]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 642 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 643 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 644 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 645 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 646 | browsers_.push_back(CreateBrowser(browser()->profile())); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 647 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 648 | close_observer.Wait(); |
| 649 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 650 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | // Test that a window created during shutdown with a beforeunload handler can |
| 654 | // cancel the shutdown. |
| 655 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
[email protected] | 40cd13a | 2013-10-24 13:42:01 | [diff] [blame] | 656 | TestAddWindowWithBeforeUnloadDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 657 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 658 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 659 | PrepareForDialog(browsers_[0]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 660 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 661 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 662 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 663 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 664 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 665 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 666 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 667 | PrepareForDialog(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 668 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 669 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 670 | cancel_observer.Wait(); |
| 671 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 672 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 673 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 674 | |
| 675 | // Allow shutdown for both beforeunload dialogs. |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 676 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 677 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 678 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 679 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 680 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 681 | close_observer.Wait(); |
| 682 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 683 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // Test that tabs added during shutdown are closed. |
| 687 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 688 | TestAddTabDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 689 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 690 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 691 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 692 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 693 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 694 | PrepareForDialog(browsers_[0]); |
| 695 | PrepareForDialog(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 696 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 697 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 698 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 699 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 700 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 701 | AddBlankTabAndShow(browsers_[0]); |
| 702 | AddBlankTabAndShow(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 703 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 704 | close_observer.Wait(); |
| 705 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 706 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // Test that tabs created during shutdown with beforeunload handlers can cancel |
| 710 | // the shutdown. |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 711 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 712 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 713 | TestAddTabWithBeforeUnloadDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 714 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 715 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 716 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 717 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 718 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 719 | PrepareForDialog(browsers_[0]); |
| 720 | PrepareForDialog(browsers_[1]); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 721 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 722 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 723 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 724 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 725 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 726 | AddBlankTabAndShow(browsers_[0]); |
| 727 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 728 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 729 | AddBlankTabAndShow(browsers_[1]); |
| 730 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 731 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 732 | PrepareForDialog(browsers_[0]); |
| 733 | PrepareForDialog(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 734 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 735 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 736 | cancel_observer.Wait(); |
| 737 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 738 | EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count()); |
| 739 | EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count()); |
| 740 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 741 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 742 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 743 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 744 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 745 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 746 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 747 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 748 | |
| 749 | close_observer.Wait(); |
| 750 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 751 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 752 | } |
| 753 | |
rogerm | f18929c | 2017-04-19 19:59:33 | [diff] [blame] | 754 | // TODO(crbug/713201): |
| 755 | // BrowserCloseManagerBrowserTest.AddBeforeUnloadDuringClosing flaky on Mac. |
| 756 | #if defined(OS_MACOSX) |
| 757 | #define MAYBE_AddBeforeUnloadDuringClosing DISABLED_AddBeforeUnloadDuringClosing |
| 758 | #else |
| 759 | #define MAYBE_AddBeforeUnloadDuringClosing AddBeforeUnloadDuringClosing |
| 760 | #endif |
| 761 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 762 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
rogerm | f18929c | 2017-04-19 19:59:33 | [diff] [blame] | 763 | MAYBE_AddBeforeUnloadDuringClosing) { |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 764 | // TODO(crbug.com/250305): Currently FastUnloadController is broken. |
| 765 | // And it is difficult to fix this issue without fixing that one. |
| 766 | if (GetParam()) |
| 767 | return; |
| 768 | |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 769 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 770 | browser(), embedded_test_server()->GetURL("/title1.html"))); |
| 771 | |
| 772 | // Open second window. |
| 773 | ui_test_utils::NavigateToURLWithDisposition( |
| 774 | browser(), embedded_test_server()->GetURL("/beforeunload.html"), |
| 775 | WindowOpenDisposition::NEW_WINDOW, |
| 776 | ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| 777 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 778 | auto* browser2 = BrowserList::GetInstance()->get(0) != browser() |
| 779 | ? BrowserList::GetInstance()->get(0) |
| 780 | : BrowserList::GetInstance()->get(1); |
| 781 | content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0)); |
| 782 | |
| 783 | // Let's work with second window only. |
| 784 | // This page has beforeunload handler already. |
| 785 | EXPECT_TRUE(browser2->tab_strip_model() |
| 786 | ->GetWebContentsAt(0) |
| 787 | ->NeedToFireBeforeUnload()); |
| 788 | // This page doesn't have beforeunload handler. Yet. |
| 789 | ui_test_utils::NavigateToURLWithDisposition( |
| 790 | browser2, embedded_test_server()->GetURL("/title2.html"), |
| 791 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 792 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 793 | content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1)); |
| 794 | EXPECT_FALSE(browser2->tab_strip_model() |
| 795 | ->GetWebContentsAt(1) |
| 796 | ->NeedToFireBeforeUnload()); |
| 797 | EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 798 | |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 799 | PrepareForDialog(browser2); |
mkolom | 65a0b3c | 2017-03-02 06:11:40 | [diff] [blame] | 800 | |
| 801 | // The test. |
| 802 | |
| 803 | TabRestoreServiceChangesObserver restore_observer(browser2->profile()); |
| 804 | content::WindowedNotificationObserver observer( |
| 805 | chrome::NOTIFICATION_BROWSER_CLOSED, |
| 806 | content::NotificationService::AllSources()); |
| 807 | chrome::CloseWindow(browser2); |
| 808 | // Just to be sure CloseWindow doesn't have asynchronous tasks |
| 809 | // that could have an impact. |
| 810 | content::RunAllPendingInMessageLoop(); |
| 811 | |
| 812 | // Closing browser shouldn't happen because of beforeunload handler. |
| 813 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 814 | // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it |
| 815 | // yet. |
| 816 | ASSERT_TRUE(content::ExecuteScript( |
| 817 | browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(), |
| 818 | "window.addEventListener('beforeunload', " |
| 819 | "function(event) { event.returnValue = 'Foo'; });")); |
| 820 | EXPECT_TRUE(browser2->tab_strip_model() |
| 821 | ->GetWebContentsAt(1) |
| 822 | ->NeedToFireBeforeUnload()); |
| 823 | // Accept closing the first tab. |
| 824 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 825 | // Just to be sure accepting a dialog doesn't have asynchronous tasks |
| 826 | // that could have an impact. |
| 827 | content::RunAllPendingInMessageLoop(); |
| 828 | // It shouldn't close the whole window/browser. |
| 829 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 830 | EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 831 | // Accept closing the second tab. |
| 832 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 833 | observer.Wait(); |
| 834 | // Now the second window/browser should be closed. |
| 835 | EXPECT_EQ(1u, BrowserList::GetInstance()->size()); |
| 836 | EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0)); |
| 837 | EXPECT_EQ(1u, restore_observer.changes_count()); |
| 838 | |
| 839 | // Restore the closed browser. |
| 840 | content::WindowedNotificationObserver open_window_observer( |
| 841 | chrome::NOTIFICATION_BROWSER_OPENED, |
| 842 | content::NotificationService::AllSources()); |
| 843 | chrome::OpenWindowWithRestoredTabs(browser()->profile()); |
| 844 | open_window_observer.Wait(); |
| 845 | EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 846 | browser2 = BrowserList::GetInstance()->get(0) != browser() |
| 847 | ? BrowserList::GetInstance()->get(0) |
| 848 | : BrowserList::GetInstance()->get(1); |
| 849 | |
| 850 | // Check the restored browser contents. |
| 851 | EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 852 | EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"), |
| 853 | browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL()); |
| 854 | EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"), |
| 855 | browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL()); |
| 856 | } |
| 857 | |
| 858 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
avi | 8804091 | 2017-03-30 03:48:42 | [diff] [blame] | 859 | TestCloseTabDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 860 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 861 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 862 | PrepareForDialog(browsers_[0]); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 863 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 864 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 865 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 866 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 867 | |
| 868 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 869 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 870 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 871 | PrepareForDialog(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 872 | browsers_[1]->tab_strip_model()->CloseAllTabs(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 873 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 874 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 875 | cancel_observer.Wait(); |
| 876 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 877 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 878 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 879 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 880 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 881 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 882 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 883 | browsers_[1]->tab_strip_model()->CloseAllTabs(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 884 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 885 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 886 | |
| 887 | close_observer.Wait(); |
| 888 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 889 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 893 | TestOpenAndCloseWindowDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 894 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 895 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 896 | PrepareForDialog(browsers_[0]); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 897 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 898 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 899 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 900 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 901 | |
| 902 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 903 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 904 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 905 | PrepareForDialog(browsers_[1]); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 906 | ASSERT_FALSE(browsers_[1]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 907 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 908 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 909 | cancel_observer.Wait(); |
| 910 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 911 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 912 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 913 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 914 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 915 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 916 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 917 | ASSERT_FALSE(browsers_[1]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 918 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 919 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 920 | |
| 921 | close_observer.Wait(); |
| 922 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 923 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 927 | TestCloseWindowDuringShutdown) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 928 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 929 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 930 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 931 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 932 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 933 | PrepareForDialog(browsers_[0]); |
| 934 | PrepareForDialog(browsers_[1]); |
metaflow | 812bb854 | 2016-05-24 21:10:46 | [diff] [blame] | 935 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 936 | RepeatedNotificationObserver cancel_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 937 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 938 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 939 | |
| 940 | ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 941 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 942 | cancel_observer.Wait(); |
| 943 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 944 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 945 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 946 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 947 | RepeatedNotificationObserver close_observer( |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 948 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 949 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 950 | ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 951 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 952 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 953 | |
| 954 | close_observer.Wait(); |
| 955 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 956 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 957 | } |
| 958 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 959 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, |
| 960 | BrowserCloseManagerBrowserTest, |
| 961 | testing::Bool()); |
| 962 | |
| 963 | class BrowserCloseManagerWithDownloadsBrowserTest : |
| 964 | public BrowserCloseManagerBrowserTest { |
| 965 | public: |
| 966 | BrowserCloseManagerWithDownloadsBrowserTest() {} |
| 967 | virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {} |
| 968 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 969 | void SetUpOnMainThread() override { |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 970 | BrowserCloseManagerBrowserTest::SetUpOnMainThread(); |
| 971 | ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir()); |
| 972 | } |
| 973 | |
| 974 | void SetDownloadPathForProfile(Profile* profile) { |
| 975 | DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile); |
| 976 | download_prefs->SetDownloadPath(download_path()); |
| 977 | } |
| 978 | |
| 979 | const base::FilePath& download_path() const { |
vabr | 0c237ae | 2016-09-14 09:24:28 | [diff] [blame] | 980 | return scoped_download_directory_.GetPath(); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | private: |
| 984 | base::ScopedTempDir scoped_download_directory_; |
| 985 | }; |
| 986 | |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 987 | // Mac has its own in-progress download prompt in app_controller_mac.mm, so |
| 988 | // BrowserCloseManager should simply close all browsers. If there are no |
| 989 | // browsers, it should not crash. |
| 990 | #if defined(OS_MACOSX) |
| 991 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 992 | TestWithDownloads) { |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 993 | SetDownloadPathForProfile(browser()->profile()); |
| 994 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 995 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 996 | RepeatedNotificationObserver close_observer( |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 997 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 998 | |
| 999 | TestBrowserCloseManager::AttemptClose( |
| 1000 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 1001 | close_observer.Wait(); |
| 1002 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1003 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1004 | EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 1005 | |
| 1006 | // Attempting to close again should not crash. |
| 1007 | TestBrowserCloseManager::AttemptClose( |
| 1008 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 1009 | } |
| 1010 | #else // defined(OS_MACOSX) |
| 1011 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1012 | // Test shutdown with a DANGEROUS_URL download undecided. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1013 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1014 | TestWithDangerousUrlDownload) { |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1015 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1016 | |
| 1017 | // Set up the fake delegate that forces the download to be malicious. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 1018 | std::unique_ptr<TestDownloadManagerDelegate> test_delegate( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1019 | new TestDownloadManagerDelegate(browser()->profile())); |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1020 | DownloadCoreServiceFactory::GetForBrowserContext(browser()->profile()) |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 1021 | ->SetDownloadManagerDelegateForTesting(std::move(test_delegate)); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1022 | |
| 1023 | // Run a dangerous download, but the user doesn't make a decision. |
| 1024 | // This .swf normally would be categorized as DANGEROUS_FILE, but |
| 1025 | // TestDownloadManagerDelegate turns it into DANGEROUS_URL. |
Jun Cai | 3ec06a12 | 2017-11-07 21:01:43 | [diff] [blame] | 1026 | GURL download_url( |
| 1027 | embedded_test_server()->GetURL("/downloads/dangerous/dangerous.swf")); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1028 | content::DownloadTestObserverInterrupted observer( |
| 1029 | content::BrowserContext::GetDownloadManager(browser()->profile()), |
| 1030 | 1, |
| 1031 | content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT); |
| 1032 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 1033 | browser(), GURL(download_url), WindowOpenDisposition::NEW_BACKGROUND_TAB, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1034 | ui_test_utils::BROWSER_TEST_NONE); |
| 1035 | observer.WaitForFinished(); |
| 1036 | |
| 1037 | // Check that the download manager has the expected state. |
| 1038 | EXPECT_EQ(1, content::BrowserContext::GetDownloadManager( |
| 1039 | browser()->profile())->InProgressCount()); |
| 1040 | EXPECT_EQ(0, content::BrowserContext::GetDownloadManager( |
| 1041 | browser()->profile())->NonMaliciousInProgressCount()); |
| 1042 | |
| 1043 | // Close the browser with no user action. |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1044 | RepeatedNotificationObserver close_observer( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1045 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1046 | TestBrowserCloseManager::AttemptClose( |
| 1047 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 1048 | close_observer.Wait(); |
| 1049 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1050 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 1051 | } |
| 1052 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1053 | // Test shutdown with a download in progress. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1054 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1055 | TestWithDownloads) { |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1056 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1057 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1058 | content::TestNavigationObserver navigation_observer( |
| 1059 | browser()->tab_strip_model()->GetActiveWebContents(), 1); |
| 1060 | TestBrowserCloseManager::AttemptClose( |
| 1061 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 1062 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1063 | navigation_observer.Wait(); |
| 1064 | EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL), |
| 1065 | browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 1066 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1067 | RepeatedNotificationObserver close_observer( |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1068 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1069 | |
| 1070 | TestBrowserCloseManager::AttemptClose( |
| 1071 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1072 | close_observer.Wait(); |
| 1073 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1074 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 1075 | if (browser_defaults::kBrowserAliveWithNoWindows) |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1076 | EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 1077 | else |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1078 | EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1079 | } |
| 1080 | |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1081 | // Test shutdown with a download in progress in an off-the-record profile. |
| 1082 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1083 | TestWithOffTheRecordDownloads) { |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1084 | Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile(); |
| 1085 | SetDownloadPathForProfile(otr_profile); |
| 1086 | Browser* otr_browser = CreateBrowser(otr_profile); |
| 1087 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1088 | RepeatedNotificationObserver close_observer( |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1089 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1090 | browser()->window()->Close(); |
| 1091 | close_observer.Wait(); |
| 1092 | } |
| 1093 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser)); |
| 1094 | content::TestNavigationObserver navigation_observer( |
| 1095 | otr_browser->tab_strip_model()->GetActiveWebContents(), 1); |
| 1096 | TestBrowserCloseManager::AttemptClose( |
| 1097 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 1098 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1099 | navigation_observer.Wait(); |
| 1100 | EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL), |
| 1101 | otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 1102 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1103 | RepeatedNotificationObserver close_observer( |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1104 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1105 | |
| 1106 | TestBrowserCloseManager::AttemptClose( |
| 1107 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1108 | close_observer.Wait(); |
| 1109 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1110 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1111 | EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 1112 | } |
| 1113 | |
Asanka Herath | d1341dc | 2017-04-19 18:28:39 | [diff] [blame] | 1114 | // Test shutdown with a download in progress in a regular profile an inconito |
| 1115 | // browser is opened and closed. While there are active downloads, closing the |
| 1116 | // incognito window shouldn't block on the active downloads which belong to the |
| 1117 | // parent profile. |
| 1118 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1119 | TestWithOffTheRecordWindowAndRegularDownload) { |
| 1120 | Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile(); |
| 1121 | SetDownloadPathForProfile(otr_profile); |
| 1122 | Browser* otr_browser = CreateBrowser(otr_profile); |
| 1123 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1124 | |
| 1125 | content::TestNavigationObserver navigation_observer( |
| 1126 | otr_browser->tab_strip_model()->GetActiveWebContents(), 1); |
| 1127 | ui_test_utils::NavigateToURL(otr_browser, GURL("about:blank")); |
| 1128 | navigation_observer.Wait(); |
| 1129 | |
| 1130 | int num_downloads_blocking = 0; |
| 1131 | ASSERT_EQ( |
| 1132 | Browser::DOWNLOAD_CLOSE_OK, |
| 1133 | otr_browser->OkToCloseWithInProgressDownloads(&num_downloads_blocking)); |
| 1134 | ASSERT_EQ(0, num_downloads_blocking); |
| 1135 | |
| 1136 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1137 | RepeatedNotificationObserver close_observer( |
Asanka Herath | d1341dc | 2017-04-19 18:28:39 | [diff] [blame] | 1138 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1139 | otr_browser->window()->Close(); |
| 1140 | close_observer.Wait(); |
| 1141 | } |
| 1142 | |
| 1143 | ASSERT_EQ( |
| 1144 | Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN, |
| 1145 | browser()->OkToCloseWithInProgressDownloads(&num_downloads_blocking)); |
| 1146 | ASSERT_EQ(1, num_downloads_blocking); |
| 1147 | |
| 1148 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1149 | RepeatedNotificationObserver close_observer( |
Asanka Herath | d1341dc | 2017-04-19 18:28:39 | [diff] [blame] | 1150 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
| 1151 | TestBrowserCloseManager::AttemptClose( |
| 1152 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1153 | close_observer.Wait(); |
| 1154 | } |
| 1155 | |
| 1156 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 1157 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
| 1158 | if (browser_defaults::kBrowserAliveWithNoWindows) |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1159 | EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
Asanka Herath | d1341dc | 2017-04-19 18:28:39 | [diff] [blame] | 1160 | else |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1161 | EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
Asanka Herath | d1341dc | 2017-04-19 18:28:39 | [diff] [blame] | 1162 | } |
| 1163 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1164 | // Test shutdown with a download in progress from one profile, where the only |
| 1165 | // open windows are for another profile. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1166 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1167 | TestWithDownloadsFromDifferentProfiles) { |
| 1168 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 1169 | Profile* other_profile = nullptr; |
| 1170 | { |
| 1171 | base::FilePath path = |
| 1172 | profile_manager->user_data_dir().AppendASCII("test_profile"); |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 1173 | base::ScopedAllowBlockingForTesting allow_blocking; |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 1174 | if (!base::PathExists(path)) |
| 1175 | ASSERT_TRUE(base::CreateDirectory(path)); |
| 1176 | other_profile = |
| 1177 | Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); |
| 1178 | } |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1179 | profile_manager->RegisterTestingProfile(other_profile, true, false); |
| 1180 | Browser* other_profile_browser = CreateBrowser(other_profile); |
| 1181 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1182 | SetDownloadPathForProfile(browser()->profile()); |
| 1183 | SetDownloadPathForProfile(other_profile); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1184 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1185 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1186 | RepeatedNotificationObserver close_observer( |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1187 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1188 | browser()->window()->Close(); |
| 1189 | close_observer.Wait(); |
| 1190 | } |
| 1191 | |
| 1192 | // When the shutdown is cancelled, the downloads page should be opened in a |
| 1193 | // browser for that profile. Because there are no browsers for that profile, a |
| 1194 | // new browser should be opened. |
| 1195 | ui_test_utils::BrowserAddedObserver new_browser_observer; |
| 1196 | TestBrowserCloseManager::AttemptClose( |
| 1197 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 1198 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1199 | Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser(); |
| 1200 | EXPECT_EQ( |
| 1201 | GURL(chrome::kChromeUIDownloadsURL), |
| 1202 | opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 1203 | EXPECT_EQ(GURL("about:blank"), |
| 1204 | other_profile_browser->tab_strip_model()->GetActiveWebContents() |
| 1205 | ->GetURL()); |
| 1206 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1207 | RepeatedNotificationObserver close_observer( |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1208 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
| 1209 | TestBrowserCloseManager::AttemptClose( |
| 1210 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 1211 | close_observer.Wait(); |
| 1212 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1213 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 1214 | if (browser_defaults::kBrowserAliveWithNoWindows) |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1215 | EXPECT_EQ(1, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 1216 | else |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 1217 | EXPECT_EQ(0, DownloadCoreService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1218 | } |
| 1219 | |
Vasilii Sukhanov | 4a65b86 | 2017-08-04 10:01:37 | [diff] [blame] | 1220 | // Fails on ChromeOS and Linux, times out on Win. crbug.com/749098 |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1221 | // Test shutdown with downloads in progress and beforeunload handlers. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1222 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
Vasilii Sukhanov | 4a65b86 | 2017-08-04 10:01:37 | [diff] [blame] | 1223 | DISABLED_TestBeforeUnloadAndDownloads) { |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1224 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1225 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 1226 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 1227 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
avi | 336125f7 | 2017-05-06 22:25:40 | [diff] [blame] | 1228 | PrepareForDialog(browser()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1229 | |
| 1230 | content::WindowedNotificationObserver cancel_observer( |
| 1231 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 1232 | content::NotificationService::AllSources()); |
| 1233 | TestBrowserCloseManager::AttemptClose( |
| 1234 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 1235 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1236 | cancel_observer.Wait(); |
| 1237 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1238 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1239 | RepeatedNotificationObserver close_observer( |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1240 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1241 | TestBrowserCloseManager::AttemptClose( |
| 1242 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 1243 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1244 | close_observer.Wait(); |
| 1245 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1246 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 1247 | } |
| 1248 | |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 1249 | #endif // defined(OS_MACOSX) |
| 1250 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 1251 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 1252 | BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 1253 | testing::Bool()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1254 | |
Denis Kuznetsov | 32099f3 | 2017-12-15 14:04:57 | [diff] [blame] | 1255 | #if BUILDFLAG(ENABLE_BACKGROUND_MODE) |
| 1256 | |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1257 | class BrowserCloseManagerWithBackgroundModeBrowserTest |
| 1258 | : public BrowserCloseManagerBrowserTest { |
| 1259 | public: |
| 1260 | BrowserCloseManagerWithBackgroundModeBrowserTest() {} |
| 1261 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 1262 | void SetUpOnMainThread() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1263 | BrowserCloseManagerBrowserTest::SetUpOnMainThread(); |
| 1264 | g_browser_process->set_background_mode_manager_for_test( |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 1265 | std::unique_ptr<BackgroundModeManager>(new FakeBackgroundModeManager)); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | bool IsBackgroundModeSuspended() { |
| 1269 | return static_cast<FakeBackgroundModeManager*>( |
| 1270 | g_browser_process->background_mode_manager()) |
| 1271 | ->IsBackgroundModeSuspended(); |
| 1272 | } |
| 1273 | |
| 1274 | private: |
| 1275 | DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest); |
| 1276 | }; |
| 1277 | |
| 1278 | // Check that background mode is suspended when closing all browsers unless we |
| 1279 | // are quitting and that background mode is resumed when a new browser window is |
| 1280 | // opened. |
| 1281 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1282 | CloseAllBrowsersWithBackgroundMode) { |
| 1283 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 1284 | std::unique_ptr<ScopedKeepAlive> tmp_keep_alive; |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1285 | Profile* profile = browser()->profile(); |
| 1286 | { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1287 | RepeatedNotificationObserver close_observer( |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1288 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 1289 | tmp_keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::PANEL_VIEW, |
| 1290 | KeepAliveRestartOption::DISABLED)); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1291 | chrome::CloseAllBrowsers(); |
| 1292 | close_observer.Wait(); |
| 1293 | } |
| 1294 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1295 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1296 | EXPECT_TRUE(IsBackgroundModeSuspended()); |
| 1297 | |
| 1298 | // Background mode should be resumed when a new browser window is opened. |
| 1299 | ui_test_utils::BrowserAddedObserver new_browser_observer; |
scottmg | 85194900 | 2016-02-09 20:09:44 | [diff] [blame] | 1300 | chrome::NewEmptyWindow(profile); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1301 | new_browser_observer.WaitForSingleNewBrowser(); |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 1302 | tmp_keep_alive.reset(); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1303 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1304 | RepeatedNotificationObserver close_observer( |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1305 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1306 | |
| 1307 | // Background mode should not be suspended when quitting. |
| 1308 | chrome::CloseAllBrowsersAndQuit(); |
| 1309 | close_observer.Wait(); |
| 1310 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1311 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1312 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | // Check that closing the last browser window individually does not affect |
| 1316 | // background mode. |
| 1317 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
dimich | 981e50f | 2016-03-03 23:39:16 | [diff] [blame] | 1318 | DISABLED_CloseSingleBrowserWithBackgroundMode) { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1319 | RepeatedNotificationObserver close_observer( |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1320 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1321 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1322 | browser()->window()->Close(); |
| 1323 | close_observer.Wait(); |
| 1324 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1325 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1326 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1327 | } |
| 1328 | |
| 1329 | // Check that closing all browsers with no browser windows open suspends |
| 1330 | // background mode but does not cause Chrome to quit. |
| 1331 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
dimich | 8148afc2 | 2016-03-04 01:57:03 | [diff] [blame] | 1332 | DISABLED_CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1333 | RepeatedNotificationObserver close_observer( |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1334 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1335 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 1336 | ScopedKeepAlive tmp_keep_alive(KeepAliveOrigin::PANEL_VIEW, |
| 1337 | KeepAliveRestartOption::DISABLED); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1338 | browser()->window()->Close(); |
| 1339 | close_observer.Wait(); |
| 1340 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1341 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1342 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1343 | |
| 1344 | chrome::CloseAllBrowsers(); |
| 1345 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1346 | EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1347 | EXPECT_TRUE(IsBackgroundModeSuspended()); |
| 1348 | } |
| 1349 | |
| 1350 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1351 | BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1352 | testing::Bool()); |
Denis Kuznetsov | 32099f3 | 2017-12-15 14:04:57 | [diff] [blame] | 1353 | #endif // BUILDFLAG(ENABLE_BACKGROUND_MODE) |