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