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