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