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