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