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