15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <signal.h>
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/url_request_mock_util.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_commands.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_list.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/ui_test_utils.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/browser_test_utils.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/test/net/url_request_mock_http_job.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_test_util.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
30a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#if defined(OS_WIN)
31a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// For version specific disabled tests below (http://crbug.com/267597).
32a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/win/windows_version.h"
33a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#endif
34a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::TimeDelta;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string NOLISTENERS_HTML =
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>nolisteners</title></head><body></body></html>";
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string UNLOAD_HTML =
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>unload</title></head><body>"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onunload=function(e){}</script></body></html>";
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string BEFORE_UNLOAD_HTML =
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>beforeunload</title></head><body>"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onbeforeunload=function(e){"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "setTimeout('document.title=\"cancelled\"', 0);return 'foo'}</script>"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "</body></html>";
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string INNER_FRAME_WITH_FOCUS_HTML =
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>innerframewithfocus</title></head><body>"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<iframe src=\"data:text/html,<html><head><script>window.onload="
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "function(){document.getElementById('box').focus()}</script>"
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<body><input id='box'></input></body></html>\"></iframe>"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "</body></html>";
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string TWO_SECOND_BEFORE_UNLOAD_HTML =
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>twosecondbeforeunload</title></head><body>"
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onbeforeunload=function(e){"
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "var start = new Date().getTime();"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "while(new Date().getTime() - start < 2000){}"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "return 'foo';"
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}</script></body></html>";
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string INFINITE_UNLOAD_HTML =
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>infiniteunload</title></head><body>"
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onunload=function(e){while(true){}}</script>"
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "</body></html>";
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string INFINITE_BEFORE_UNLOAD_HTML =
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>infinitebeforeunload</title></head><body>"
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onbeforeunload=function(e){while(true){}}</script>"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "</body></html>";
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string INFINITE_UNLOAD_ALERT_HTML =
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>infiniteunloadalert</title></head><body>"
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onunload=function(e){"
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "while(true){}"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "alert('foo');"
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}</script></body></html>";
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string INFINITE_BEFORE_UNLOAD_ALERT_HTML =
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>infinitebeforeunloadalert</title></head><body>"
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onbeforeunload=function(e){"
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "while(true){}"
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "alert('foo');"
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}</script></body></html>";
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string TWO_SECOND_UNLOAD_ALERT_HTML =
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>twosecondunloadalert</title></head><body>"
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onunload=function(e){"
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "var start = new Date().getTime();"
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "while(new Date().getTime() - start < 2000){}"
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "alert('foo');"
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}</script></body></html>";
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string TWO_SECOND_BEFORE_UNLOAD_ALERT_HTML =
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>twosecondbeforeunloadalert</title></head><body>"
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<script>window.onbeforeunload=function(e){"
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "var start = new Date().getTime();"
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "while(new Date().getTime() - start < 2000){}"
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "alert('foo');"
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}</script></body></html>";
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string CLOSE_TAB_WHEN_OTHER_TAB_HAS_LISTENER =
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>only_one_unload</title></head>"
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<body onclick=\"window.open('data:text/html,"
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "<html><head><title>popup</title></head></body>')\" "
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "onbeforeunload='return;'>"
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "</body></html>";
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UnloadTest : public InProcessBrowserTest {
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const testing::TestInfo* const test_info =
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        testing::UnitTest::GetInstance()->current_test_info();
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (strcmp(test_info->name(),
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "BrowserCloseTabWhenOtherTabHasListener") == 0) {
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendSwitch(switches::kDisablePopupBlocking);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (strcmp(test_info->name(), "BrowserTerminateBeforeUnload") == 0) {
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DisableSIGTERMHandling();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserThread::PostTask(
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        BrowserThread::IO, FROM_HERE,
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CheckTitle(const char* expected_title) {
136a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 expected = ASCIIToUTF16(expected_title);
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(expected,
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NavigateToDataURL(const std::string& html_content,
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const char* expected_title) {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ui_test_utils::NavigateToURL(browser(),
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 GURL("data:text/html," + html_content));
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CheckTitle(expected_title);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NavigateToNolistenersFileTwice() {
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL url(content::URLRequestMockHTTPJob::GetMockUrl(
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::FilePath(FILE_PATH_LITERAL("title2.html"))));
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ui_test_utils::NavigateToURL(browser(), url);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CheckTitle("Title Of Awesomeness");
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ui_test_utils::NavigateToURL(browser(), url);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CheckTitle("Title Of Awesomeness");
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Navigates to a URL asynchronously, then again synchronously. The first
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // load is purposely async to test the case where the user loads another
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // page without waiting for the first load to complete.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NavigateToNolistenersFileTwiceAsync() {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL url(content::URLRequestMockHTTPJob::GetMockUrl(
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::FilePath(FILE_PATH_LITERAL("title2.html"))));
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ui_test_utils::NavigateToURLWithDisposition(browser(), url, CURRENT_TAB, 0);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ui_test_utils::NavigateToURL(browser(), url);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CheckTitle("Title Of Awesomeness");
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void LoadUrlAndQuitBrowser(const std::string& html_content,
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const char* expected_title) {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NavigateToDataURL(html_content, expected_title);
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WindowedNotificationObserver window_observer(
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        chrome::NOTIFICATION_BROWSER_CLOSED,
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    chrome::CloseWindow(browser());
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    window_observer.Wait();
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |accept| is true, simulates user clicking OK, otherwise simulates
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // clicking Cancel.
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ClickModalDialogButton(bool accept) {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    JavaScriptAppModalDialog* js_dialog =
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        static_cast<JavaScriptAppModalDialog*>(dialog);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (accept)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      js_dialog->native_dialog()->AcceptAppModalDialog();
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      js_dialog->native_dialog()->CancelAppModalDialog();
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Navigate to a page with an infinite unload handler.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Then two async crosssite requests to ensure
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we don't get confused and think we're closing the tab.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This test is flaky on the valgrind UI bots. http://crbug.com/39057
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, CrossSiteInfiniteUnloadAsync) {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(INFINITE_UNLOAD_HTML, "infiniteunload");
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must navigate to a non-data URL to trigger cross-site codepath.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToNolistenersFileTwiceAsync();
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Navigate to a page with an infinite unload handler.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Then two sync crosssite requests to ensure
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we correctly nav to each one.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, CrossSiteInfiniteUnloadSync) {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(INFINITE_UNLOAD_HTML, "infiniteunload");
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must navigate to a non-data URL to trigger cross-site codepath.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToNolistenersFileTwice();
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Navigate to a page with an infinite beforeunload handler.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Then two two async crosssite requests to ensure
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we don't get confused and think we're closing the tab.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This test is flaky on the valgrind UI bots. http://crbug.com/39057 and
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://crbug.com/86469
2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, CrossSiteInfiniteBeforeUnloadAsync) {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(INFINITE_BEFORE_UNLOAD_HTML, "infinitebeforeunload");
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must navigate to a non-data URL to trigger cross-site codepath.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToNolistenersFileTwiceAsync();
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Navigate to a page with an infinite beforeunload handler.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Then two two sync crosssite requests to ensure
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we correctly nav to each one.
2387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// If this flakes, see bug http://crbug.com/86469.
2397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, CrossSiteInfiniteBeforeUnloadSync) {
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(INFINITE_BEFORE_UNLOAD_HTML, "infinitebeforeunload");
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must navigate to a non-data URL to trigger cross-site codepath.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToNolistenersFileTwice();
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser on a page with no unload listeners registered.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseNoUnloadListeners) {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(NOLISTENERS_HTML, "nolisteners");
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser on a page with an unload listener registered.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test marked as flaky in http://crbug.com/51698
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, DISABLED_BrowserCloseUnload) {
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(UNLOAD_HTML, "unload");
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser with a beforeunload handler and clicking
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OK in the beforeunload confirm dialog.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadOK) {
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload");
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        chrome::NOTIFICATION_BROWSER_CLOSED,
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::CloseWindow(browser());
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClickModalDialogButton(true);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  window_observer.Wait();
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser with a beforeunload handler and clicking
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CANCEL in the beforeunload confirm dialog.
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If this test flakes, reopen http://crbug.com/123110
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadCancel) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload");
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::CloseWindow(browser());
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We wait for the title to change after cancelling the popup to ensure that
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in-flight IPCs from the renderer reach the browser. Otherwise the browser
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // won't put up the beforeunload dialog because it's waiting for an ack from
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the renderer.
284a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("cancelled");
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TitleWatcher title_watcher(
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClickModalDialogButton(false);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        chrome::NOTIFICATION_BROWSER_CLOSED,
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::CloseWindow(browser());
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClickModalDialogButton(true);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  window_observer.Wait();
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests terminating the browser with a beforeunload handler.
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Currently only ChromeOS shuts down gracefully.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserTerminateBeforeUnload) {
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload");
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kill(base::GetCurrentProcessHandle(), SIGTERM), 0);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser and clicking OK in the beforeunload confirm dialog
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// if an inner frame has the focus.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If this flakes, use http://crbug.com/32615 and http://crbug.com/45675
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseWithInnerFocusedFrame) {
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(INNER_FRAME_WITH_FOCUS_HTML, "innerframewithfocus");
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        chrome::NOTIFICATION_BROWSER_CLOSED,
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::CloseWindow(browser());
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClickModalDialogButton(true);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  window_observer.Wait();
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser with a beforeunload handler that takes
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// two seconds to run.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseTwoSecondBeforeUnload) {
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(TWO_SECOND_BEFORE_UNLOAD_HTML,
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        "twosecondbeforeunload");
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser on a page with an unload listener registered where
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the unload handler has an infinite loop.
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseInfiniteUnload) {
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(INFINITE_UNLOAD_HTML, "infiniteunload");
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser with a beforeunload handler that hangs.
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If this flakes, use http://crbug.com/78803 and http://crbug.com/86469
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, DISABLED_BrowserCloseInfiniteBeforeUnload) {
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(INFINITE_BEFORE_UNLOAD_HTML, "infinitebeforeunload");
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser on a page with an unload listener registered where
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the unload handler has an infinite loop followed by an alert.
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If this flakes, use http://crbug.com/86469
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseInfiniteUnloadAlert) {
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(INFINITE_UNLOAD_ALERT_HTML, "infiniteunloadalert");
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser with a beforeunload handler that hangs then
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pops up an alert.
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If this flakes, use http://crbug.com/78803 and http://crbug.com/86469.
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest,
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       DISABLED_BrowserCloseInfiniteBeforeUnloadAlert) {
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(INFINITE_BEFORE_UNLOAD_ALERT_HTML,
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        "infinitebeforeunloadalert");
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser on a page with an unload listener registered where
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the unload handler has an 2 second long loop followed by an alert.
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseTwoSecondUnloadAlert) {
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(TWO_SECOND_UNLOAD_ALERT_HTML, "twosecondunloadalert");
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests closing the browser with a beforeunload handler that takes
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// two seconds to run then pops up an alert.
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseTwoSecondBeforeUnloadAlert) {
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LoadUrlAndQuitBrowser(TWO_SECOND_BEFORE_UNLOAD_ALERT_HTML,
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        "twosecondbeforeunloadalert");
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests that if there's a renderer process with two tabs, one of which has an
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// unload handler, and the other doesn't, the tab that doesn't have an unload
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// handler can be closed.
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If this flakes, see http://crbug.com/45162, http://crbug.com/45281 and
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://crbug.com/86769.
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseTabWhenOtherTabHasListener) {
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToDataURL(CLOSE_TAB_WHEN_OTHER_TAB_HAS_LISTENER, "only_one_unload");
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Simulate a click to force user_gesture to true; if we don't, the resulting
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // popup will be constrained, which isn't what we want to test.
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::WindowedNotificationObserver observer(
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        chrome::NOTIFICATION_TAB_ADDED,
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::WindowedNotificationObserver load_stop_observer(
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::NOTIFICATION_LOAD_STOP,
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::NotificationService::AllSources());
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::SimulateMouseClick(
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents(), 0,
404f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      blink::WebMouseEvent::ButtonLeft);
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer.Wait();
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  load_stop_observer.Wait();
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckTitle("popup");
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  content::WebContentsDestroyedWatcher destroyed_watcher(
4108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents());
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::CloseTab(browser());
4128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  destroyed_watcher.Wait();
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckTitle("only_one_unload");
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
417868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FastUnloadTest : public UnloadTest {
418868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
419eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
420eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UnloadTest::SetUpCommandLine(command_line);
421eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    command_line->AppendSwitch(switches::kEnableFastUnload);
422eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
423eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
424868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
425868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_TRUE(test_server()->Start());
426868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
427868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
428868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
429868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    test_server()->Stop();
430868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
431868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
432868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GURL GetUrl(const std::string& name) {
433868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return GURL(test_server()->GetURL(
434868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        "files/fast_tab_close/" + name + ".html"));
435868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
436868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
437868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void NavigateToPage(const char* name) {
438868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ui_test_utils::NavigateToURL(browser(), GetUrl(name));
439868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    CheckTitle(name);
440868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
441868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
442868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void NavigateToPageInNewTab(const char* name) {
443868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ui_test_utils::NavigateToURLWithDisposition(
444868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        browser(), GetUrl(name), NEW_FOREGROUND_TAB,
445868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
446868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    CheckTitle(name);
447868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
448868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
449868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string GetCookies(const char* name) {
450868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::WebContents* contents =
451868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        browser()->tab_strip_model()->GetActiveWebContents();
452868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return content::GetCookies(contents->GetBrowserContext(), GetUrl(name));
453868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
454868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
455868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FastTabCloseTabStripModelObserver : public TabStripModelObserver {
457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
458868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FastTabCloseTabStripModelObserver(TabStripModel* model,
459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    base::RunLoop* run_loop)
460868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      : model_(model),
461868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        run_loop_(run_loop) {
462868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    model_->AddObserver(this);
463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~FastTabCloseTabStripModelObserver() {
466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    model_->RemoveObserver(this);
467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TabStripModelObserver:
470868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void TabDetachedAt(content::WebContents* contents,
471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                             int index) OVERRIDE {
472868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    run_loop_->Quit();
473868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
474868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
475868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
476868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TabStripModel* const model_;
477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop* const run_loop_;
478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
479868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
480868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
481868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that fast-tab-close works when closing a tab with an unload handler
482868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (http://crbug.com/142458).
4833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Flaky on Windows bots (http://crbug.com/267597).
484a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#if defined(OS_WIN)
4853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#define MAYBE_UnloadHidden \
4863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    DISABLED_UnloadHidden
4873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#else
4883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#define MAYBE_UnloadHidden \
4893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    UnloadHidden
490a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#endif
4913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest, MAYBE_UnloadHidden) {
492868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPage("no_listeners");
493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPageInNewTab("unload_sets_cookie");
494868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("", GetCookies("no_listeners"));
495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  content::WebContentsDestroyedWatcher destroyed_watcher(
4978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents());
4988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
499868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  {
500868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::RunLoop run_loop;
501868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FastTabCloseTabStripModelObserver observer(
502868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        browser()->tab_strip_model(), &run_loop);
503868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    chrome::CloseTab(browser());
504868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    run_loop.Run();
505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
506868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
507868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check that the browser only has the original tab.
508868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CheckTitle("no_listeners");
509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1, browser()->tab_strip_model()->count());
510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Wait for the actual destruction.
5128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  destroyed_watcher.Wait();
513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Verify that the destruction had the desired effect.
515868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("unloaded=ohyeah", GetCookies("no_listeners"));
516868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
517868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
518868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that fast-tab-close does not break a solo tab.
519868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest, PRE_ClosingLastTabFinishesUnload) {
520868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The unload handler sleeps before setting the cookie to catch cases when
521868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // unload handlers are not allowed to run to completion. (For example,
522868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // using the detached handler for the tab and then closing the browser.)
523868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPage("unload_sleep_before_cookie");
524868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1, browser()->tab_strip_model()->count());
525868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("", GetCookies("unload_sleep_before_cookie"));
526868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
527868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
528868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      chrome::NOTIFICATION_BROWSER_CLOSED,
529868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::AllSources());
530868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  chrome::CloseTab(browser());
531868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  window_observer.Wait();
532868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
5334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Fails on Mac, Linux, Win7 (http://crbug.com/301173).
5354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest, DISABLED_ClosingLastTabFinishesUnload) {
536a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#if defined(OS_WIN)
537a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Flaky on Win7+ bots (http://crbug.com/267597).
538a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (base::win::GetVersion() >= base::win::VERSION_WIN7)
539a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
540a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#endif
541868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check for cookie set in unload handler of PRE_ test.
542868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPage("no_listeners");
543868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("unloaded=ohyeah", GetCookies("no_listeners"));
544868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
545868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
546868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that fast-tab-close does not break window close.
547868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest, PRE_WindowCloseFinishesUnload) {
548868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPage("no_listeners");
549868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
550868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The unload handler sleeps before setting the cookie to catch cases when
551868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // unload handlers are not allowed to run to completion. Without the sleep,
552868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the cookie can get set even if the browser does not wait for
553868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the unload handler to finish.
554868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPageInNewTab("unload_sleep_before_cookie");
555868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(2, browser()->tab_strip_model()->count());
556868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("", GetCookies("no_listeners"));
557868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
558868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
559868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      chrome::NOTIFICATION_BROWSER_CLOSED,
560868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::AllSources());
561868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  chrome::CloseWindow(browser());
562868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  window_observer.Wait();
563868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
564424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
5654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Flaky on Windows bots (http://crbug.com/279267) and fails on Mac / Linux bots
5664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// (http://crbug.com/301173).
5674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest, DISABLED_WindowCloseFinishesUnload) {
568868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check for cookie set in unload during PRE_ test.
569868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPage("no_listeners");
570868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("unloaded=ohyeah", GetCookies("no_listeners"));
571868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
572868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
573868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that a tab crash during unload does not break window close.
574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Hits assertion on Linux and Mac:
576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//     [FATAL:profile_destroyer.cc(46)] Check failed:
577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//         hosts.empty() ||
578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//         profile->IsOffTheRecord() ||
579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//         content::RenderProcessHost::run_renderer_in_process().
580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//     More details: The renderer process host matches the closed, crashed tab.
581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//     The |UnloadController| receives |NOTIFICATION_WEB_CONTENTS_DISCONNECTED|
582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//     and proceeds with the close.
583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest, DISABLED_WindowCloseAfterUnloadCrash) {
584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPage("no_listeners");
585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToPageInNewTab("unload_sets_cookie");
586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WebContents* unload_contents =
587868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents();
588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("", GetCookies("no_listeners"));
589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
590868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  {
591868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::RunLoop run_loop;
592868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FastTabCloseTabStripModelObserver observer(
593868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        browser()->tab_strip_model(), &run_loop);
594868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    chrome::CloseTab(browser());
595868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    run_loop.Run();
596868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
597868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
598868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check that the browser only has the original tab.
599868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CheckTitle("no_listeners");
600868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1, browser()->tab_strip_model()->count());
601868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
602868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CrashTab(unload_contents);
603868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
604868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check that the browser only has the original tab.
605868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CheckTitle("no_listeners");
606868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1, browser()->tab_strip_model()->count());
607868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
608868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
609868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      chrome::NOTIFICATION_BROWSER_CLOSED,
610868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::AllSources());
611868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  chrome::CloseWindow(browser());
612868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  window_observer.Wait();
613868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
615868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Times out on Windows and Linux.
616868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_WIN) || defined(OS_LINUX)
617868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define MAYBE_WindowCloseAfterBeforeUnloadCrash \
618868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DISABLED_WindowCloseAfterBeforeUnloadCrash
619868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#else
620868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define MAYBE_WindowCloseAfterBeforeUnloadCrash \
621868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    WindowCloseAfterBeforeUnloadCrash
622868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif
623868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(FastUnloadTest,
624868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       MAYBE_WindowCloseAfterBeforeUnloadCrash) {
625868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Tests makes no sense in single-process mode since the renderer is hung.
626868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
627868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
628868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
629868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload");
630868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WebContents* beforeunload_contents =
631868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents();
632868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
633868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::WindowedNotificationObserver window_observer(
634868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        chrome::NOTIFICATION_BROWSER_CLOSED,
635868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::NotificationService::AllSources());
636868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  chrome::CloseWindow(browser());
637868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CrashTab(beforeunload_contents);
638868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  window_observer.Wait();
639868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
640868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(ojan): Add tests for unload/beforeunload that have multiple tabs
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and multiple windows.
643