Commit cb2509b7 authored by cbentzel@chromium.org's avatar cbentzel@chromium.org

Add PrerenderUnload browser test to ensure that unload handlers are run on the...

Add PrerenderUnload browser test to ensure that unload handlers are run on the navigated-away from page.

This test fails if the bug fix for 85350 is not in, and succeeds with it in.

BUG=85350,82856
TEST=browser_tests --gtest_filter=*PrerenderUnload

Review URL: http://codereview.chromium.org/7046053

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88654 0039d316-1c4b-4281-b951-d872f2087c98
parent c807cb19
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/favicon/favicon_tab_helper.h" #include "chrome/browser/favicon/favicon_tab_helper.h"
...@@ -317,7 +318,8 @@ class PrerenderBrowserTest : public InProcessBrowserTest { ...@@ -317,7 +318,8 @@ class PrerenderBrowserTest : public InProcessBrowserTest {
: safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), : safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
prerender_contents_factory_(NULL), prerender_contents_factory_(NULL),
use_https_src_server_(false), use_https_src_server_(false),
call_javascript_(true) { call_javascript_(true),
loader_path_("files/prerender/prerender_loader.html") {
EnableDOMAutomation(); EnableDOMAutomation();
} }
...@@ -468,6 +470,10 @@ class PrerenderBrowserTest : public InProcessBrowserTest { ...@@ -468,6 +470,10 @@ class PrerenderBrowserTest : public InProcessBrowserTest {
prerender_manager()->FindEntry(dest_url_)); prerender_manager()->FindEntry(dest_url_));
} }
void set_loader_path(const std::string& path) {
loader_path_ = path;
}
private: private:
void PrerenderTestURLImpl( void PrerenderTestURLImpl(
const GURL& url, const GURL& url,
...@@ -480,7 +486,7 @@ class PrerenderBrowserTest : public InProcessBrowserTest { ...@@ -480,7 +486,7 @@ class PrerenderBrowserTest : public InProcessBrowserTest {
make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec())); make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec()));
std::string replacement_path; std::string replacement_path;
ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
"files/prerender/prerender_loader.html", loader_path_,
replacement_text, replacement_text,
&replacement_path)); &replacement_path));
...@@ -571,6 +577,7 @@ class PrerenderBrowserTest : public InProcessBrowserTest { ...@@ -571,6 +577,7 @@ class PrerenderBrowserTest : public InProcessBrowserTest {
GURL dest_url_; GURL dest_url_;
bool use_https_src_server_; bool use_https_src_server_;
bool call_javascript_; bool call_javascript_;
std::string loader_path_;
}; };
// Checks that a page is correctly prerendered in the case of a // Checks that a page is correctly prerendered in the case of a
...@@ -1360,4 +1367,15 @@ IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderFavicon) { ...@@ -1360,4 +1367,15 @@ IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderFavicon) {
->FaviconIsValid()); ->FaviconIsValid());
} }
// Checks that when a prerendered page is swapped in to a referring page, the
// unload handlers on the referring page are executed.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) {
set_loader_path("files/prerender/prerender_loader_with_unload.html");
PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
ui_test_utils::TitleWatcher title_watcher(browser()->GetSelectedTabContents(),
ASCIIToUTF16("Unloaded"));
NavigateToDestURL();
EXPECT_TRUE(title_watcher.Wait());
}
} // namespace prerender } // namespace prerender
<html>
<head>
<title>Preloader</title>
<script>
function UnloadHandler() {
document.title = 'Unloaded';
}
addEventListener('unload', UnloadHandler, false);
</script>
</head>
<body>
<script>
document.write('<link rel="prerender" href="REPLACE_WITH_PRERENDER_URL"/>');
</script>
<a href="REPLACE_WITH_PRERENDER_URL">Link To Click</a>
</body>
</html>
...@@ -889,6 +889,45 @@ void WindowedNotificationObserver::Observe(NotificationType type, ...@@ -889,6 +889,45 @@ void WindowedNotificationObserver::Observe(NotificationType type,
} }
} }
TitleWatcher::TitleWatcher(TabContents* tab_contents,
const string16& expected_title)
: expected_tab_(tab_contents),
expected_title_(expected_title),
title_observed_(false),
quit_loop_on_observation_(false) {
EXPECT_TRUE(tab_contents != NULL);
notification_registrar_.Add(this,
NotificationType::TAB_CONTENTS_TITLE_UPDATED,
Source<TabContents>(tab_contents));
}
TitleWatcher::~TitleWatcher() {
}
bool TitleWatcher::Wait() {
if (title_observed_)
return true;
quit_loop_on_observation_ = true;
ui_test_utils::RunMessageLoop();
return title_observed_;
}
void TitleWatcher::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type != NotificationType::TAB_CONTENTS_TITLE_UPDATED)
return;
TabContents* source_contents = Source<TabContents>(source).ptr();
ASSERT_EQ(expected_tab_, source_contents);
if (source_contents->GetTitle() != expected_title_)
return;
title_observed_ = true;
if (quit_loop_on_observation_)
MessageLoopForUI::current()->Quit();
}
DOMMessageQueue::DOMMessageQueue() { DOMMessageQueue::DOMMessageQueue() {
registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE, registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
NotificationService::AllSources()); NotificationService::AllSources());
......
...@@ -471,6 +471,34 @@ class WindowedNotificationObserverWithDetails ...@@ -471,6 +471,34 @@ class WindowedNotificationObserverWithDetails
DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails); DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails);
}; };
// Watches title changes on a tab, blocking until an expected title is set.
class TitleWatcher : public NotificationObserver {
public:
// |tab_contents| must be non-NULL and needs to stay alive for the
// entire lifetime of |this|. |expected_title| is the title that |this|
// will wait for.
TitleWatcher(TabContents* tab_contents, const string16& expected_title);
~TitleWatcher();
// Waits until the title for the tab is set to the |expected_title|
// passed into the constructor.
bool Wait() WARN_UNUSED_RESULT;
private:
// NotificationObserver
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) OVERRIDE;
TabContents* expected_tab_;
string16 expected_title_;
NotificationRegistrar notification_registrar_;
bool title_observed_;
bool quit_loop_on_observation_;
DISALLOW_COPY_AND_ASSIGN(TitleWatcher);
};
// See SendKeyPressAndWait. This function additionally performs a check on the // See SendKeyPressAndWait. This function additionally performs a check on the
// NotificationDetails using the provided Details<U>. // NotificationDetails using the provided Details<U>.
template <class U> template <class U>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment