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