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