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