[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 | |
| 5 | #include <vector> |
| 6 | |
| 7 | #include "base/command_line.h" |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame^] | 9 | #include "base/macros.h" |
| 10 | #include "build/build_config.h" |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 11 | #include "chrome/browser/background/background_mode_manager.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 12 | #include "chrome/browser/browser_process.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 13 | #include "chrome/browser/browser_shutdown.h" |
| 14 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 15 | #include "chrome/browser/defaults.h" |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 16 | #include "chrome/browser/download/chrome_download_manager_delegate.h" |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 17 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 18 | #include "chrome/browser/download/download_service.h" |
| 19 | #include "chrome/browser/download/download_service_factory.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 20 | #include "chrome/browser/lifetime/application_lifetime.h" |
| 21 | #include "chrome/browser/lifetime/browser_close_manager.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 22 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 23 | #include "chrome/browser/prefs/session_startup_pref.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 24 | #include "chrome/browser/profiles/profile.h" |
| 25 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 26 | #include "chrome/browser/ui/browser.h" |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 27 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 28 | #include "chrome/browser/ui/browser_iterator.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 29 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 30 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 31 | #include "chrome/common/chrome_switches.h" |
| 32 | #include "chrome/common/url_constants.h" |
| 33 | #include "chrome/test/base/in_process_browser_test.h" |
| 34 | #include "chrome/test/base/ui_test_utils.h" |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 35 | #include "components/app_modal/javascript_app_modal_dialog.h" |
| 36 | #include "components/app_modal/native_app_modal_dialog.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 37 | #include "content/public/browser/browser_context.h" |
| 38 | #include "content/public/browser/download_item.h" |
| 39 | #include "content/public/browser/download_manager.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 40 | #include "content/public/browser/notification_service.h" |
| 41 | #include "content/public/browser/web_contents.h" |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 42 | #include "content/public/test/download_test_observer.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 43 | #include "content/public/test/test_navigation_observer.h" |
| 44 | #include "net/test/embedded_test_server/embedded_test_server.h" |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 45 | #include "net/test/url_request/url_request_mock_http_job.h" |
jam | 8e45cd7 | 2015-01-20 16:33:44 | [diff] [blame] | 46 | #include "net/test/url_request/url_request_slow_download_job.h" |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 47 | |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 48 | #if defined(OS_CHROMEOS) |
| 49 | #include "chromeos/chromeos_switches.h" |
| 50 | #endif |
| 51 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 52 | namespace { |
| 53 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 54 | app_modal::NativeAppModalDialog* GetNextDialog() { |
| 55 | app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 56 | EXPECT_TRUE(dialog->IsJavaScriptModalDialog()); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 57 | app_modal::JavaScriptAppModalDialog* js_dialog = |
| 58 | static_cast<app_modal::JavaScriptAppModalDialog*>(dialog); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 59 | CHECK(js_dialog->native_dialog()); |
| 60 | return js_dialog->native_dialog(); |
| 61 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 62 | |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 63 | void AcceptClose() { |
| 64 | GetNextDialog()->AcceptAppModalDialog(); |
| 65 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 66 | |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 67 | void CancelClose() { |
| 68 | GetNextDialog()->CancelAppModalDialog(); |
| 69 | } |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 70 | |
| 71 | class RepeatedNotificationObserver : public content::NotificationObserver { |
| 72 | public: |
| 73 | explicit RepeatedNotificationObserver(int type, int count) |
| 74 | : num_outstanding_(count), running_(false) { |
| 75 | registrar_.Add(this, type, content::NotificationService::AllSources()); |
| 76 | } |
| 77 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 78 | void Observe(int type, |
| 79 | const content::NotificationSource& source, |
| 80 | const content::NotificationDetails& details) override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 81 | ASSERT_GT(num_outstanding_, 0); |
| 82 | if (!--num_outstanding_ && running_) { |
| 83 | content::BrowserThread::PostTask( |
| 84 | content::BrowserThread::UI, FROM_HERE, run_loop_.QuitClosure()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void Wait() { |
| 89 | if (num_outstanding_ <= 0) |
| 90 | return; |
| 91 | |
| 92 | running_ = true; |
| 93 | run_loop_.Run(); |
| 94 | running_ = false; |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | int num_outstanding_; |
| 99 | content::NotificationRegistrar registrar_; |
| 100 | bool running_; |
| 101 | base::RunLoop run_loop_; |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver); |
| 104 | }; |
| 105 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 106 | class TestBrowserCloseManager : public BrowserCloseManager { |
| 107 | public: |
| 108 | enum UserChoice { |
| 109 | USER_CHOICE_USER_CANCELS_CLOSE, |
| 110 | USER_CHOICE_USER_ALLOWS_CLOSE, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 111 | NO_USER_CHOICE |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static void AttemptClose(UserChoice user_choice) { |
| 115 | scoped_refptr<BrowserCloseManager> browser_close_manager = |
| 116 | new TestBrowserCloseManager(user_choice); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 117 | browser_shutdown::SetTryingToQuit(true); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 118 | browser_close_manager->StartClosingBrowsers(); |
| 119 | } |
| 120 | |
| 121 | protected: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 122 | ~TestBrowserCloseManager() override {} |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 123 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 124 | void ConfirmCloseWithPendingDownloads( |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 125 | int download_count, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 126 | const base::Callback<void(bool)>& callback) override { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 127 | EXPECT_NE(NO_USER_CHOICE, user_choice_); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 128 | switch (user_choice_) { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 129 | case NO_USER_CHOICE: |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 130 | case USER_CHOICE_USER_CANCELS_CLOSE: { |
| 131 | callback.Run(false); |
| 132 | break; |
| 133 | } |
| 134 | case USER_CHOICE_USER_ALLOWS_CLOSE: { |
| 135 | callback.Run(true); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | explicit TestBrowserCloseManager(UserChoice user_choice) |
| 143 | : user_choice_(user_choice) {} |
| 144 | |
| 145 | UserChoice user_choice_; |
| 146 | |
| 147 | DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager); |
| 148 | }; |
| 149 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 150 | class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 151 | public: |
| 152 | explicit TestDownloadManagerDelegate(Profile* profile) |
| 153 | : ChromeDownloadManagerDelegate(profile) { |
[email protected] | 861b4d0a | 2013-12-13 20:10:41 | [diff] [blame] | 154 | GetDownloadIdReceiverCallback().Run(content::DownloadItem::kInvalidId + 1); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 155 | } |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 156 | ~TestDownloadManagerDelegate() override {} |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 157 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 158 | bool DetermineDownloadTarget( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 159 | content::DownloadItem* item, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 160 | const content::DownloadTargetCallback& callback) override { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 161 | content::DownloadTargetCallback dangerous_callback = |
[email protected] | 861b4d0a | 2013-12-13 20:10:41 | [diff] [blame] | 162 | base::Bind(&TestDownloadManagerDelegate::SetDangerous, callback); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 163 | return ChromeDownloadManagerDelegate::DetermineDownloadTarget( |
| 164 | item, dangerous_callback); |
| 165 | } |
| 166 | |
[email protected] | 861b4d0a | 2013-12-13 20:10:41 | [diff] [blame] | 167 | static void SetDangerous( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 168 | const content::DownloadTargetCallback& callback, |
| 169 | const base::FilePath& target_path, |
| 170 | content::DownloadItem::TargetDisposition disp, |
| 171 | content::DownloadDangerType danger_type, |
| 172 | const base::FilePath& intermediate_path) { |
| 173 | callback.Run(target_path, |
| 174 | disp, |
| 175 | content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 176 | intermediate_path); |
| 177 | } |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 178 | }; |
| 179 | |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 180 | class FakeBackgroundModeManager : public BackgroundModeManager { |
| 181 | public: |
| 182 | FakeBackgroundModeManager() |
| 183 | : BackgroundModeManager( |
gab | 9447481 | 2015-01-08 20:04:36 | [diff] [blame] | 184 | *base::CommandLine::ForCurrentProcess(), |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 185 | &g_browser_process->profile_manager()->GetProfileInfoCache()), |
| 186 | suspended_(false) {} |
| 187 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 188 | void SuspendBackgroundMode() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 189 | BackgroundModeManager::SuspendBackgroundMode(); |
| 190 | suspended_ = true; |
| 191 | } |
| 192 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 193 | void ResumeBackgroundMode() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 194 | BackgroundModeManager::ResumeBackgroundMode(); |
| 195 | suspended_ = false; |
| 196 | } |
| 197 | |
| 198 | bool IsBackgroundModeSuspended() { |
| 199 | return suspended_; |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | bool suspended_; |
| 204 | |
| 205 | DISALLOW_COPY_AND_ASSIGN(FakeBackgroundModeManager); |
| 206 | }; |
| 207 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 208 | } // namespace |
| 209 | |
| 210 | class BrowserCloseManagerBrowserTest |
| 211 | : public InProcessBrowserTest, |
| 212 | public testing::WithParamInterface<bool> { |
| 213 | protected: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 214 | void SetUpOnMainThread() override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 215 | InProcessBrowserTest::SetUpOnMainThread(); |
| 216 | SessionStartupPref::SetStartupPref( |
| 217 | browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); |
| 218 | browsers_.push_back(browser()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 219 | content::BrowserThread::PostTask( |
| 220 | content::BrowserThread::IO, |
| 221 | FROM_HERE, |
| 222 | base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 223 | } |
| 224 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 225 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 226 | if (GetParam()) |
| 227 | command_line->AppendSwitch(switches::kEnableFastUnload); |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 228 | #if defined(OS_CHROMEOS) |
| 229 | command_line->AppendSwitch( |
| 230 | chromeos::switches::kIgnoreUserProfileMappingForTests); |
| 231 | #endif |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 234 | void CreateStalledDownload(Browser* browser) { |
| 235 | content::DownloadTestObserverInProgress observer( |
| 236 | content::BrowserContext::GetDownloadManager(browser->profile()), 1); |
| 237 | ui_test_utils::NavigateToURLWithDisposition( |
| 238 | browser, |
jam | 8e45cd7 | 2015-01-20 16:33:44 | [diff] [blame] | 239 | GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl), |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 240 | NEW_BACKGROUND_TAB, |
| 241 | ui_test_utils::BROWSER_TEST_NONE); |
| 242 | observer.WaitForFinished(); |
| 243 | EXPECT_EQ( |
| 244 | 1UL, |
| 245 | observer.NumDownloadsSeenInState(content::DownloadItem::IN_PROGRESS)); |
| 246 | } |
| 247 | |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 248 | std::vector<Browser*> browsers_; |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestSingleTabShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 252 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 253 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 254 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
| 255 | RepeatedNotificationObserver cancel_observer( |
| 256 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 257 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 258 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 259 | cancel_observer.Wait(); |
| 260 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 261 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 262 | |
| 263 | RepeatedNotificationObserver close_observer( |
| 264 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 265 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 266 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 267 | close_observer.Wait(); |
| 268 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 269 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 270 | } |
| 271 | |
| 272 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 273 | TestShutdownMoreThanOnce) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 274 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 275 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 276 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
| 277 | RepeatedNotificationObserver cancel_observer( |
| 278 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 279 | chrome::CloseAllBrowsersAndQuit(); |
| 280 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 281 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 282 | cancel_observer.Wait(); |
| 283 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 284 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 285 | |
| 286 | RepeatedNotificationObserver close_observer( |
| 287 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 288 | chrome::CloseAllBrowsersAndQuit(); |
| 289 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 290 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 291 | close_observer.Wait(); |
| 292 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 293 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 294 | } |
| 295 | |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 296 | // Test is flaky on Mac. http://crbug.com/517687 |
| 297 | #if defined(OS_MACOSX) |
| 298 | #define MAYBE_PRE_TestSessionRestore DISABLED_PRE_TestSessionRestore |
| 299 | #else |
| 300 | #define MAYBE_PRE_TestSessionRestore DISABLED_PRE_TestSessionRestore |
| 301 | #endif |
| 302 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 303 | MAYBE_PRE_TestSessionRestore) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 304 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 305 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 306 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
| 307 | AddBlankTabAndShow(browser()); |
| 308 | ASSERT_NO_FATAL_FAILURE( |
| 309 | ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL))); |
| 310 | RepeatedNotificationObserver cancel_observer( |
| 311 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 312 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 313 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 314 | cancel_observer.Wait(); |
| 315 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 316 | |
| 317 | browser()->tab_strip_model() |
| 318 | ->CloseWebContentsAt(1, TabStripModel::CLOSE_USER_GESTURE); |
| 319 | content::TestNavigationObserver navigation_observer( |
| 320 | browser()->tab_strip_model()->GetActiveWebContents(), 1); |
| 321 | ASSERT_NO_FATAL_FAILURE( |
| 322 | NavigateToURLWithDisposition(browser(), |
| 323 | GURL(chrome::kChromeUIVersionURL), |
| 324 | CURRENT_TAB, |
| 325 | ui_test_utils::BROWSER_TEST_NONE)); |
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 | navigation_observer.Wait(); |
| 328 | |
| 329 | RepeatedNotificationObserver close_observer( |
| 330 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 331 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 332 | close_observer.Wait(); |
| 333 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 334 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 335 | } |
| 336 | |
| 337 | // Test that the tab closed after the aborted shutdown attempt is not re-opened |
| 338 | // when restoring the session. |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 339 | // Test is flaky on Mac. http://crbug.com/517687 |
| 340 | #if defined(OS_MACOSX) |
| 341 | #define MAYBE_TestSessionRestore DISABLED_TestSessionRestore |
| 342 | #else |
| 343 | #define MAYBE_TestSessionRestore DISABLED_TestSessionRestore |
| 344 | #endif |
| 345 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 346 | MAYBE_TestSessionRestore) { |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 347 | // The testing framework launches Chrome with about:blank as args. |
| 348 | EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 349 | EXPECT_EQ(GURL(chrome::kChromeUIVersionURL), |
| 350 | browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL()); |
| 351 | EXPECT_EQ(GURL("about:blank"), |
| 352 | browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL()); |
| 353 | } |
| 354 | |
| 355 | // Test that browser windows are only closed if all browsers are ready to close |
| 356 | // and that all beforeunload dialogs are shown again after a cancel. |
[email protected] | 40cd13a | 2013-10-24 13:42:01 | [diff] [blame] | 357 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestMultipleWindows) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 358 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 359 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 360 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 361 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 362 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 363 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 364 | |
| 365 | // Cancel shutdown on the first beforeunload event. |
| 366 | { |
| 367 | RepeatedNotificationObserver cancel_observer( |
| 368 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 369 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 370 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 371 | cancel_observer.Wait(); |
| 372 | } |
| 373 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 374 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 375 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 376 | |
| 377 | // Cancel shutdown on the second beforeunload event. |
| 378 | { |
| 379 | RepeatedNotificationObserver cancel_observer( |
| 380 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 381 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 382 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 383 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 384 | cancel_observer.Wait(); |
| 385 | } |
| 386 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 387 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 388 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 389 | |
| 390 | // Allow shutdown for both beforeunload events. |
| 391 | RepeatedNotificationObserver close_observer( |
| 392 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 393 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 394 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 395 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 396 | close_observer.Wait(); |
| 397 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 398 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 399 | } |
| 400 | |
| 401 | // Test that tabs in the same window with a beforeunload event that hangs are |
| 402 | // treated the same as the user accepting the close, but do not close the tab |
| 403 | // early. |
[email protected] | 52656e0 | 2013-08-20 15:50:13 | [diff] [blame] | 404 | // Test is flaky on windows, disabled. See http://crbug.com/276366 |
waffles | b6323405 | 2015-08-07 02:44:02 | [diff] [blame] | 405 | // Test is flaky on Mac. See http://crbug.com/517687. |
| 406 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 52656e0 | 2013-08-20 15:50:13 | [diff] [blame] | 407 | #define MAYBE_TestHangInBeforeUnloadMultipleTabs \ |
| 408 | DISABLED_TestHangInBeforeUnloadMultipleTabs |
| 409 | #else |
| 410 | #define MAYBE_TestHangInBeforeUnloadMultipleTabs \ |
| 411 | TestHangInBeforeUnloadMultipleTabs |
| 412 | #endif |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 413 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
[email protected] | 52656e0 | 2013-08-20 15:50:13 | [diff] [blame] | 414 | MAYBE_TestHangInBeforeUnloadMultipleTabs) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 415 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 416 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 417 | browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
| 418 | AddBlankTabAndShow(browsers_[0]); |
| 419 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 420 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 421 | AddBlankTabAndShow(browsers_[0]); |
| 422 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 423 | browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
| 424 | |
| 425 | RepeatedNotificationObserver cancel_observer( |
| 426 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 427 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 428 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 429 | cancel_observer.Wait(); |
| 430 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 431 | // All tabs should still be open. |
| 432 | EXPECT_EQ(3, browsers_[0]->tab_strip_model()->count()); |
| 433 | |
| 434 | RepeatedNotificationObserver close_observer( |
| 435 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 436 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 437 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 438 | close_observer.Wait(); |
| 439 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 440 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 441 | } |
| 442 | |
| 443 | // Test that tabs in different windows with a beforeunload event that hangs are |
| 444 | // treated the same as the user accepting the close, but do not close the tab |
| 445 | // early. |
| 446 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 447 | TestHangInBeforeUnloadMultipleWindows) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 448 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 449 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 450 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 451 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 452 | browsers_[0], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
| 453 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 454 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 455 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 456 | browsers_[2], embedded_test_server()->GetURL("/beforeunload_hang.html"))); |
| 457 | |
| 458 | RepeatedNotificationObserver cancel_observer( |
| 459 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 460 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 461 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 462 | cancel_observer.Wait(); |
| 463 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 464 | // All windows should still be open. |
| 465 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 466 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 467 | EXPECT_EQ(1, browsers_[2]->tab_strip_model()->count()); |
| 468 | |
| 469 | RepeatedNotificationObserver close_observer( |
| 470 | chrome::NOTIFICATION_BROWSER_CLOSED, 3); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 471 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 472 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 473 | close_observer.Wait(); |
| 474 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 475 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 476 | } |
| 477 | |
| 478 | // Test that a window created during shutdown is closed. |
| 479 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 480 | TestAddWindowDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 481 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 482 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 483 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 484 | |
| 485 | RepeatedNotificationObserver close_observer( |
| 486 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 487 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 488 | browsers_.push_back(CreateBrowser(browser()->profile())); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 489 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 490 | close_observer.Wait(); |
| 491 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 492 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 493 | } |
| 494 | |
| 495 | // Test that a window created during shutdown with a beforeunload handler can |
| 496 | // cancel the shutdown. |
| 497 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
[email protected] | 40cd13a | 2013-10-24 13:42:01 | [diff] [blame] | 498 | TestAddWindowWithBeforeUnloadDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 499 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 500 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 501 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 502 | |
| 503 | RepeatedNotificationObserver cancel_observer( |
| 504 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 505 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 506 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 507 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 508 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 509 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 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 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 514 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 515 | |
| 516 | // Allow shutdown for both beforeunload dialogs. |
| 517 | RepeatedNotificationObserver close_observer( |
| 518 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[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()); |
| 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()); |
| 524 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 525 | } |
| 526 | |
| 527 | // Test that tabs added during shutdown are closed. |
Dana Jansens | ee190094 | 2015-08-12 17:33:24 | [diff] [blame] | 528 | // Disabled for being flaky tests: crbug.com/519646 |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 529 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
Dana Jansens | ee190094 | 2015-08-12 17:33:24 | [diff] [blame] | 530 | DISABLED_TestAddTabDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 531 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 532 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 533 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 534 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 535 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 536 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 537 | |
| 538 | RepeatedNotificationObserver close_observer( |
| 539 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 540 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 541 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 542 | AddBlankTabAndShow(browsers_[0]); |
| 543 | AddBlankTabAndShow(browsers_[1]); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 544 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 545 | close_observer.Wait(); |
| 546 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 547 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 548 | } |
| 549 | |
| 550 | // Test that tabs created during shutdown with beforeunload handlers can cancel |
| 551 | // the shutdown. |
Dana Jansens | 4d4f4667 | 2015-08-11 20:16:14 | [diff] [blame] | 552 | // Disabled for being flaky tests: crbug.com/519646 |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 553 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
Dana Jansens | 4d4f4667 | 2015-08-11 20:16:14 | [diff] [blame] | 554 | DISABLED_TestAddTabWithBeforeUnloadDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 555 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 556 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 557 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 558 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 559 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 560 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 561 | RepeatedNotificationObserver cancel_observer( |
| 562 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 563 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 564 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 565 | AddBlankTabAndShow(browsers_[0]); |
| 566 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 567 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 568 | AddBlankTabAndShow(browsers_[1]); |
| 569 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 570 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 571 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 572 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 573 | cancel_observer.Wait(); |
| 574 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 575 | EXPECT_EQ(2, browsers_[0]->tab_strip_model()->count()); |
| 576 | EXPECT_EQ(2, browsers_[1]->tab_strip_model()->count()); |
| 577 | |
| 578 | RepeatedNotificationObserver close_observer( |
| 579 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 580 | chrome::CloseAllBrowsersAndQuit(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 581 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 582 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 583 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 584 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 585 | |
| 586 | close_observer.Wait(); |
| 587 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 588 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 589 | } |
| 590 | |
| 591 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 592 | TestCloseTabDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 593 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 594 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 595 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 596 | RepeatedNotificationObserver cancel_observer( |
| 597 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 598 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 599 | |
| 600 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 601 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 602 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 603 | browsers_[1]->tab_strip_model()->CloseAllTabs(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 604 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 605 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 606 | cancel_observer.Wait(); |
| 607 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 608 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 609 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 610 | |
| 611 | RepeatedNotificationObserver close_observer( |
| 612 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 613 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 614 | browsers_[1]->tab_strip_model()->CloseAllTabs(); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 615 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 616 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 617 | |
| 618 | close_observer.Wait(); |
| 619 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 620 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 621 | } |
| 622 | |
avi | 848c4dc | 2015-06-11 18:59:40 | [diff] [blame] | 623 | // Test is flaky on Windows and Mac. See http://crbug.com/276366. |
| 624 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 8376e17 | 2013-10-03 08:42:49 | [diff] [blame] | 625 | #define MAYBE_TestOpenAndCloseWindowDuringShutdown \ |
| 626 | DISABLED_TestOpenAndCloseWindowDuringShutdown |
| 627 | #else |
| 628 | #define MAYBE_TestOpenAndCloseWindowDuringShutdown \ |
| 629 | TestOpenAndCloseWindowDuringShutdown |
| 630 | #endif |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 631 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
[email protected] | 8376e17 | 2013-10-03 08:42:49 | [diff] [blame] | 632 | MAYBE_TestOpenAndCloseWindowDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 633 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 634 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 635 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 636 | RepeatedNotificationObserver cancel_observer( |
| 637 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 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 | |
| 640 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 641 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 642 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 643 | ASSERT_FALSE(browsers_[1]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 644 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
| 645 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 646 | cancel_observer.Wait(); |
| 647 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 648 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 649 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 650 | |
| 651 | RepeatedNotificationObserver close_observer( |
| 652 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 653 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 654 | ASSERT_FALSE(browsers_[1]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 655 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 656 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 657 | |
| 658 | close_observer.Wait(); |
| 659 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 660 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 661 | } |
| 662 | |
| 663 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 664 | TestCloseWindowDuringShutdown) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 665 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 666 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 667 | browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 668 | browsers_.push_back(CreateBrowser(browser()->profile())); |
| 669 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 670 | browsers_[1], embedded_test_server()->GetURL("/beforeunload.html"))); |
| 671 | RepeatedNotificationObserver cancel_observer( |
| 672 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 673 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 674 | |
| 675 | ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 676 | ASSERT_NO_FATAL_FAILURE(CancelClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 677 | cancel_observer.Wait(); |
| 678 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 679 | EXPECT_EQ(1, browsers_[0]->tab_strip_model()->count()); |
| 680 | EXPECT_EQ(1, browsers_[1]->tab_strip_model()->count()); |
| 681 | |
| 682 | RepeatedNotificationObserver close_observer( |
| 683 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 684 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 685 | ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 686 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 687 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 688 | |
| 689 | close_observer.Wait(); |
| 690 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 691 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 692 | } |
| 693 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 694 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, |
| 695 | BrowserCloseManagerBrowserTest, |
| 696 | testing::Bool()); |
| 697 | |
| 698 | class BrowserCloseManagerWithDownloadsBrowserTest : |
| 699 | public BrowserCloseManagerBrowserTest { |
| 700 | public: |
| 701 | BrowserCloseManagerWithDownloadsBrowserTest() {} |
| 702 | virtual ~BrowserCloseManagerWithDownloadsBrowserTest() {} |
| 703 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 704 | void SetUpOnMainThread() override { |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 705 | BrowserCloseManagerBrowserTest::SetUpOnMainThread(); |
| 706 | ASSERT_TRUE(scoped_download_directory_.CreateUniqueTempDir()); |
| 707 | } |
| 708 | |
benwells | f3c6fac | 2015-09-01 23:38:52 | [diff] [blame] | 709 | // Disable new downloads UI as it is very very slow. https://crbug.com/526577 |
| 710 | // TODO(dbeam): remove this once the downloads UI is not slow. |
| 711 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 712 | BrowserCloseManagerBrowserTest::SetUpCommandLine(command_line); |
| 713 | command_line->AppendSwitch(switches::kDisableMaterialDesignDownloads); |
| 714 | } |
| 715 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 716 | void SetDownloadPathForProfile(Profile* profile) { |
| 717 | DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(profile); |
| 718 | download_prefs->SetDownloadPath(download_path()); |
| 719 | } |
| 720 | |
| 721 | const base::FilePath& download_path() const { |
| 722 | return scoped_download_directory_.path(); |
| 723 | } |
| 724 | |
| 725 | private: |
| 726 | base::ScopedTempDir scoped_download_directory_; |
| 727 | }; |
| 728 | |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 729 | // Mac has its own in-progress download prompt in app_controller_mac.mm, so |
| 730 | // BrowserCloseManager should simply close all browsers. If there are no |
| 731 | // browsers, it should not crash. |
| 732 | #if defined(OS_MACOSX) |
| 733 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 734 | TestWithDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 735 | ASSERT_TRUE(embedded_test_server()->Start()); |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 736 | SetDownloadPathForProfile(browser()->profile()); |
| 737 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 738 | |
| 739 | RepeatedNotificationObserver close_observer( |
| 740 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 741 | |
| 742 | TestBrowserCloseManager::AttemptClose( |
| 743 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 744 | close_observer.Wait(); |
| 745 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 746 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 747 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 748 | |
| 749 | // Attempting to close again should not crash. |
| 750 | TestBrowserCloseManager::AttemptClose( |
| 751 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 752 | } |
| 753 | #else // defined(OS_MACOSX) |
| 754 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 755 | // Test shutdown with a DANGEROUS_URL download undecided. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 756 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 757 | TestWithDangerousUrlDownload) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 758 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 759 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 760 | |
| 761 | // Set up the fake delegate that forces the download to be malicious. |
[email protected] | 861b4d0a | 2013-12-13 20:10:41 | [diff] [blame] | 762 | scoped_ptr<TestDownloadManagerDelegate> test_delegate( |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 763 | new TestDownloadManagerDelegate(browser()->profile())); |
dcheng | 383ba8a | 2014-10-16 23:55:19 | [diff] [blame] | 764 | DownloadServiceFactory::GetForBrowserContext(browser()->profile()) |
| 765 | ->SetDownloadManagerDelegateForTesting(test_delegate.Pass()); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 766 | |
| 767 | // Run a dangerous download, but the user doesn't make a decision. |
| 768 | // This .swf normally would be categorized as DANGEROUS_FILE, but |
| 769 | // TestDownloadManagerDelegate turns it into DANGEROUS_URL. |
tfarina | 5e7b5723 | 2015-10-17 23:37:40 | [diff] [blame] | 770 | GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl( |
| 771 | "downloads/dangerous/dangerous.swf")); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 772 | content::DownloadTestObserverInterrupted observer( |
| 773 | content::BrowserContext::GetDownloadManager(browser()->profile()), |
| 774 | 1, |
| 775 | content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT); |
| 776 | ui_test_utils::NavigateToURLWithDisposition( |
| 777 | browser(), |
| 778 | GURL(download_url), |
| 779 | NEW_BACKGROUND_TAB, |
| 780 | ui_test_utils::BROWSER_TEST_NONE); |
| 781 | observer.WaitForFinished(); |
| 782 | |
| 783 | // Check that the download manager has the expected state. |
| 784 | EXPECT_EQ(1, content::BrowserContext::GetDownloadManager( |
| 785 | browser()->profile())->InProgressCount()); |
| 786 | EXPECT_EQ(0, content::BrowserContext::GetDownloadManager( |
| 787 | browser()->profile())->NonMaliciousInProgressCount()); |
| 788 | |
| 789 | // Close the browser with no user action. |
| 790 | RepeatedNotificationObserver close_observer( |
| 791 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 792 | TestBrowserCloseManager::AttemptClose( |
| 793 | TestBrowserCloseManager::NO_USER_CHOICE); |
| 794 | close_observer.Wait(); |
| 795 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 796 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 797 | } |
| 798 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 799 | // Test shutdown with a download in progress. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 800 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 801 | TestWithDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 802 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 803 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 804 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 805 | content::TestNavigationObserver navigation_observer( |
| 806 | browser()->tab_strip_model()->GetActiveWebContents(), 1); |
| 807 | TestBrowserCloseManager::AttemptClose( |
| 808 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 809 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 810 | navigation_observer.Wait(); |
| 811 | EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL), |
| 812 | browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 813 | |
| 814 | RepeatedNotificationObserver close_observer( |
| 815 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 816 | |
| 817 | TestBrowserCloseManager::AttemptClose( |
| 818 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 819 | close_observer.Wait(); |
| 820 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 821 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 822 | if (browser_defaults::kBrowserAliveWithNoWindows) |
| 823 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 824 | else |
| 825 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 826 | } |
| 827 | |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 828 | // Test shutdown with a download in progress in an off-the-record profile. |
| 829 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 830 | TestWithOffTheRecordDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 831 | ASSERT_TRUE(embedded_test_server()->Start()); |
sammc | cd34771 | 2015-03-13 22:02:32 | [diff] [blame] | 832 | Profile* otr_profile = browser()->profile()->GetOffTheRecordProfile(); |
| 833 | SetDownloadPathForProfile(otr_profile); |
| 834 | Browser* otr_browser = CreateBrowser(otr_profile); |
| 835 | { |
| 836 | RepeatedNotificationObserver close_observer( |
| 837 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 838 | browser()->window()->Close(); |
| 839 | close_observer.Wait(); |
| 840 | } |
| 841 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(otr_browser)); |
| 842 | content::TestNavigationObserver navigation_observer( |
| 843 | otr_browser->tab_strip_model()->GetActiveWebContents(), 1); |
| 844 | TestBrowserCloseManager::AttemptClose( |
| 845 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 846 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 847 | navigation_observer.Wait(); |
| 848 | EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL), |
| 849 | otr_browser->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 850 | |
| 851 | RepeatedNotificationObserver close_observer( |
| 852 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 853 | |
| 854 | TestBrowserCloseManager::AttemptClose( |
| 855 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 856 | close_observer.Wait(); |
| 857 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 858 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 859 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 860 | } |
| 861 | |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 862 | // Test shutdown with a download in progress from one profile, where the only |
| 863 | // open windows are for another profile. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 864 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 865 | TestWithDownloadsFromDifferentProfiles) { |
| 866 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 867 | base::FilePath path = |
| 868 | profile_manager->user_data_dir().AppendASCII("test_profile"); |
| 869 | if (!base::PathExists(path)) |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 870 | ASSERT_TRUE(base::CreateDirectory(path)); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 871 | Profile* other_profile = |
| 872 | Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); |
| 873 | profile_manager->RegisterTestingProfile(other_profile, true, false); |
| 874 | Browser* other_profile_browser = CreateBrowser(other_profile); |
| 875 | |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 876 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 877 | SetDownloadPathForProfile(browser()->profile()); |
| 878 | SetDownloadPathForProfile(other_profile); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 879 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 880 | { |
| 881 | RepeatedNotificationObserver close_observer( |
| 882 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 883 | browser()->window()->Close(); |
| 884 | close_observer.Wait(); |
| 885 | } |
| 886 | |
| 887 | // When the shutdown is cancelled, the downloads page should be opened in a |
| 888 | // browser for that profile. Because there are no browsers for that profile, a |
| 889 | // new browser should be opened. |
| 890 | ui_test_utils::BrowserAddedObserver new_browser_observer; |
| 891 | TestBrowserCloseManager::AttemptClose( |
| 892 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
| 893 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 894 | Browser* opened_browser = new_browser_observer.WaitForSingleNewBrowser(); |
| 895 | EXPECT_EQ( |
| 896 | GURL(chrome::kChromeUIDownloadsURL), |
| 897 | opened_browser->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 898 | EXPECT_EQ(GURL("about:blank"), |
| 899 | other_profile_browser->tab_strip_model()->GetActiveWebContents() |
| 900 | ->GetURL()); |
| 901 | |
| 902 | RepeatedNotificationObserver close_observer( |
| 903 | chrome::NOTIFICATION_BROWSER_CLOSED, 2); |
| 904 | TestBrowserCloseManager::AttemptClose( |
| 905 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
| 906 | close_observer.Wait(); |
| 907 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 908 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
[email protected] | 5fec470 | 2013-11-20 04:37:38 | [diff] [blame] | 909 | if (browser_defaults::kBrowserAliveWithNoWindows) |
| 910 | EXPECT_EQ(1, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
| 911 | else |
| 912 | EXPECT_EQ(0, DownloadService::NonMaliciousDownloadCountAllProfiles()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | // Test shutdown with downloads in progress and beforeunload handlers. |
[email protected] | 4c23057a | 2013-11-06 18:29:52 | [diff] [blame] | 916 | // Disabled, see http://crbug.com/315754. |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 917 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 4c23057a | 2013-11-06 18:29:52 | [diff] [blame] | 918 | DISABLED_TestBeforeUnloadAndDownloads) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 919 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 920 | SetDownloadPathForProfile(browser()->profile()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 921 | ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
| 922 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 923 | browser(), embedded_test_server()->GetURL("/beforeunload.html"))); |
| 924 | |
| 925 | content::WindowedNotificationObserver cancel_observer( |
| 926 | chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 927 | content::NotificationService::AllSources()); |
| 928 | TestBrowserCloseManager::AttemptClose( |
| 929 | TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 930 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 931 | cancel_observer.Wait(); |
| 932 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 933 | |
| 934 | RepeatedNotificationObserver close_observer( |
| 935 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 936 | TestBrowserCloseManager::AttemptClose( |
| 937 | TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
oshima | 82f7248 | 2014-10-24 14:14:32 | [diff] [blame] | 938 | ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
[email protected] | edfca70 | 2013-08-16 08:58:14 | [diff] [blame] | 939 | close_observer.Wait(); |
| 940 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 941 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 942 | } |
| 943 | |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 944 | #endif // defined(OS_MACOSX) |
| 945 | |
[email protected] | 0ed5387 | 2014-05-10 05:51:05 | [diff] [blame] | 946 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithDownloadsBrowserTest, |
| 947 | BrowserCloseManagerWithDownloadsBrowserTest, |
[email protected] | 2e9d79f | 2013-08-16 05:45:56 | [diff] [blame] | 948 | testing::Bool()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 949 | |
| 950 | class BrowserCloseManagerWithBackgroundModeBrowserTest |
| 951 | : public BrowserCloseManagerBrowserTest { |
| 952 | public: |
| 953 | BrowserCloseManagerWithBackgroundModeBrowserTest() {} |
| 954 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 955 | void SetUpOnMainThread() override { |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 956 | BrowserCloseManagerBrowserTest::SetUpOnMainThread(); |
| 957 | g_browser_process->set_background_mode_manager_for_test( |
| 958 | scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager)); |
| 959 | } |
| 960 | |
| 961 | bool IsBackgroundModeSuspended() { |
| 962 | return static_cast<FakeBackgroundModeManager*>( |
| 963 | g_browser_process->background_mode_manager()) |
| 964 | ->IsBackgroundModeSuspended(); |
| 965 | } |
| 966 | |
| 967 | private: |
| 968 | DISALLOW_COPY_AND_ASSIGN(BrowserCloseManagerWithBackgroundModeBrowserTest); |
| 969 | }; |
| 970 | |
| 971 | // Check that background mode is suspended when closing all browsers unless we |
| 972 | // are quitting and that background mode is resumed when a new browser window is |
| 973 | // opened. |
| 974 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 975 | CloseAllBrowsersWithBackgroundMode) { |
| 976 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 977 | Profile* profile = browser()->profile(); |
| 978 | { |
| 979 | RepeatedNotificationObserver close_observer( |
| 980 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
[email protected] | 9a718b2 | 2014-02-25 15:17:27 | [diff] [blame] | 981 | chrome::IncrementKeepAliveCount(); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 982 | chrome::CloseAllBrowsers(); |
| 983 | close_observer.Wait(); |
| 984 | } |
| 985 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 986 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 987 | EXPECT_TRUE(IsBackgroundModeSuspended()); |
| 988 | |
| 989 | // Background mode should be resumed when a new browser window is opened. |
| 990 | ui_test_utils::BrowserAddedObserver new_browser_observer; |
| 991 | chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 992 | new_browser_observer.WaitForSingleNewBrowser(); |
[email protected] | 9a718b2 | 2014-02-25 15:17:27 | [diff] [blame] | 993 | chrome::DecrementKeepAliveCount(); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 994 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 995 | RepeatedNotificationObserver close_observer( |
| 996 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 997 | |
| 998 | // Background mode should not be suspended when quitting. |
| 999 | chrome::CloseAllBrowsersAndQuit(); |
| 1000 | close_observer.Wait(); |
| 1001 | EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 1002 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 1003 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1004 | |
| 1005 | } |
| 1006 | |
| 1007 | // Check that closing the last browser window individually does not affect |
| 1008 | // background mode. |
| 1009 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1010 | CloseSingleBrowserWithBackgroundMode) { |
| 1011 | RepeatedNotificationObserver close_observer( |
| 1012 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1013 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1014 | browser()->window()->Close(); |
| 1015 | close_observer.Wait(); |
| 1016 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1017 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 1018 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1019 | } |
| 1020 | |
| 1021 | // Check that closing all browsers with no browser windows open suspends |
| 1022 | // background mode but does not cause Chrome to quit. |
| 1023 | IN_PROC_BROWSER_TEST_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1024 | CloseAllBrowsersWithNoOpenBrowsersWithBackgroundMode) { |
| 1025 | RepeatedNotificationObserver close_observer( |
| 1026 | chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 1027 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
[email protected] | 9a718b2 | 2014-02-25 15:17:27 | [diff] [blame] | 1028 | chrome::IncrementKeepAliveCount(); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 1029 | browser()->window()->Close(); |
| 1030 | close_observer.Wait(); |
| 1031 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1032 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 1033 | EXPECT_FALSE(IsBackgroundModeSuspended()); |
| 1034 | |
| 1035 | chrome::CloseAllBrowsers(); |
| 1036 | EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
| 1037 | EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 1038 | EXPECT_TRUE(IsBackgroundModeSuspended()); |
| 1039 | } |
| 1040 | |
| 1041 | INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1042 | BrowserCloseManagerWithBackgroundModeBrowserTest, |
| 1043 | testing::Bool()); |