Commit f1ccb4dd authored by clamy's avatar clamy Committed by Commit bot

Revert "Revert of PlzNavigate: Add a browser test for basic navigations"

This reverts the revert of https://chromiumcodereview.appspot.com/715203004/ by
fixing the build error.

BUG=376014, 376006, 376091

Review URL: https://codereview.chromium.org/868313004

Cr-Commit-Position: refs/heads/master@{#313538}
parent d194dd13
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"
namespace content {
class BrowserSideNavigationBrowserTest : public ContentBrowserTest {
public:
BrowserSideNavigationBrowserTest() {}
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kEnableBrowserSideNavigation);
}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
}
};
// Ensure that browser initiated basic navigations work with browser side
// navigation.
IN_PROC_BROWSER_TEST_F(BrowserSideNavigationBrowserTest,
BrowserInitiatedNavigations) {
// Perform a navigation with no live renderer.
{
TestNavigationObserver observer(shell()->web_contents());
GURL url(embedded_test_server()->GetURL("/title1.html"));
NavigateToURL(shell(), url);
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
RenderFrameHost* initial_rfh =
static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host();
// Perform a same site navigation.
{
TestNavigationObserver observer(shell()->web_contents());
GURL url(embedded_test_server()->GetURL("/title2.html"));
NavigateToURL(shell(), url);
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
// The RenderFrameHost should not have changed.
EXPECT_EQ(initial_rfh, static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host());
// Perform a cross-site navigation.
{
TestNavigationObserver observer(shell()->web_contents());
GURL url = embedded_test_server()->GetURL("foo.com", "/title3.html");
NavigateToURL(shell(), url);
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
// The RenderFrameHost should have changed.
EXPECT_NE(initial_rfh, static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host());
}
// Ensure that renderer initiated same-site navigations work with browser side
// navigation.
IN_PROC_BROWSER_TEST_F(BrowserSideNavigationBrowserTest,
RendererInitiatedSameSiteNavigation) {
// Perform a navigation with no live renderer.
{
TestNavigationObserver observer(shell()->web_contents());
GURL url(embedded_test_server()->GetURL("/simple_links.html"));
NavigateToURL(shell(), url);
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
RenderFrameHost* initial_rfh =
static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host();
// Simulate clicking on a same-site link.
{
TestNavigationObserver observer(shell()->web_contents());
GURL url(embedded_test_server()->GetURL("/title2.html"));
bool success = false;
EXPECT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(),
"window.domAutomationController.send(clickSameSiteLink());", &success));
EXPECT_TRUE(success);
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
// The RenderFrameHost should not have changed.
EXPECT_EQ(initial_rfh, static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host());
}
// Ensure that renderer initiated cross-site navigations work with browser side
// navigation.
IN_PROC_BROWSER_TEST_F(BrowserSideNavigationBrowserTest,
RendererInitiatedCrossSiteNavigation) {
// Perform a navigation with no live renderer.
{
TestNavigationObserver observer(shell()->web_contents());
GURL url(embedded_test_server()->GetURL("/simple_links.html"));
NavigateToURL(shell(), url);
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
RenderFrameHost* initial_rfh =
static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host();
// Simulate clicking on a cross-site link.
{
TestNavigationObserver observer(shell()->web_contents());
const char kReplacePortNumber[] =
"window.domAutomationController.send(setPortNumber(%d));";
uint16 port_number = embedded_test_server()->port();
GURL url = embedded_test_server()->GetURL("foo.com", "/title2.html");
bool success = false;
EXPECT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(),
base::StringPrintf(kReplacePortNumber, port_number),
&success));
success = false;
EXPECT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(),
"window.domAutomationController.send(clickCrossSiteLink());",
&success));
EXPECT_TRUE(success);
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_EQ(url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
}
// The RenderFrameHost should have changed.
EXPECT_NE(initial_rfh, static_cast<WebContentsImpl*>(shell()->web_contents())
->GetFrameTree()->root()->current_frame_host());
}
} // namespace content
...@@ -192,6 +192,7 @@ ...@@ -192,6 +192,7 @@
'browser/battery_status/battery_monitor_integration_browsertest.cc', 'browser/battery_status/battery_monitor_integration_browsertest.cc',
'browser/compositor/image_transport_factory_browsertest.cc', 'browser/compositor/image_transport_factory_browsertest.cc',
'browser/bookmarklet_browsertest.cc', 'browser/bookmarklet_browsertest.cc',
'browser/browser_side_navigation_browsertest.cc',
'browser/child_process_launcher_browsertest.cc', 'browser/child_process_launcher_browsertest.cc',
'browser/child_process_security_policy_browsertest.cc', 'browser/child_process_security_policy_browsertest.cc',
'browser/cross_site_transfer_browsertest.cc', 'browser/cross_site_transfer_browsertest.cc',
......
...@@ -46,6 +46,31 @@ class TestNavigationObserver::TestWebContentsObserver ...@@ -46,6 +46,31 @@ class TestNavigationObserver::TestWebContentsObserver
parent_->OnDidStopLoading(web_contents()); parent_->OnDidStopLoading(web_contents());
} }
void DidStartProvisionalLoadForFrame(RenderFrameHost* render_frame_host,
const GURL& validated_url,
bool is_error_page,
bool is_iframe_srcdoc) override {
parent_->OnDidStartProvisionalLoadForFrame(
render_frame_host, validated_url, is_error_page, is_iframe_srcdoc);
}
void DidFailProvisionalLoad(
RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description) override {
parent_->OnDidFailProvisionalLoad(render_frame_host, validated_url,
error_code, error_description);
}
void DidCommitProvisionalLoadForFrame(
RenderFrameHost* render_frame_host,
const GURL& url,
ui::PageTransition transition_type) override {
parent_->OnDidCommitProvisionalLoadForFrame(
render_frame_host, url, transition_type);
}
TestNavigationObserver* parent_; TestNavigationObserver* parent_;
DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver); DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver);
...@@ -146,4 +171,29 @@ void TestNavigationObserver::OnDidStopLoading(WebContents* web_contents) { ...@@ -146,4 +171,29 @@ void TestNavigationObserver::OnDidStopLoading(WebContents* web_contents) {
} }
} }
void TestNavigationObserver::OnDidStartProvisionalLoadForFrame(
RenderFrameHost* render_frame_host,
const GURL& validated_url,
bool is_error_page,
bool is_iframe_srcdoc) {
last_navigation_succeeded_ = false;
}
void TestNavigationObserver::OnDidFailProvisionalLoad(
RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description) {
last_navigation_url_ = validated_url;
last_navigation_succeeded_ = false;
}
void TestNavigationObserver::OnDidCommitProvisionalLoadForFrame(
RenderFrameHost* render_frame_host,
const GURL& url,
ui::PageTransition transition_type) {
last_navigation_url_ = url;
last_navigation_succeeded_ = true;
}
} // namespace content } // namespace content
...@@ -11,8 +11,11 @@ ...@@ -11,8 +11,11 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"
namespace content { namespace content {
class RenderFrameHost;
class WebContents; class WebContents;
struct LoadCommittedDetails; struct LoadCommittedDetails;
...@@ -37,6 +40,10 @@ class TestNavigationObserver { ...@@ -37,6 +40,10 @@ class TestNavigationObserver {
void StartWatchingNewWebContents(); void StartWatchingNewWebContents();
void StopWatchingNewWebContents(); void StopWatchingNewWebContents();
const GURL& last_navigation_url() const { return last_navigation_url_; }
int last_navigation_succeeded() const { return last_navigation_succeeded_; }
protected: protected:
// Register this TestNavigationObserver as an observer of the |web_contents|. // Register this TestNavigationObserver as an observer of the |web_contents|.
void RegisterAsObserver(WebContents* web_contents); void RegisterAsObserver(WebContents* web_contents);
...@@ -55,6 +62,17 @@ class TestNavigationObserver { ...@@ -55,6 +62,17 @@ class TestNavigationObserver {
void OnDidAttachInterstitialPage(WebContents* web_contents); void OnDidAttachInterstitialPage(WebContents* web_contents);
void OnDidStartLoading(WebContents* web_contents); void OnDidStartLoading(WebContents* web_contents);
void OnDidStopLoading(WebContents* web_contents); void OnDidStopLoading(WebContents* web_contents);
void OnDidStartProvisionalLoadForFrame(RenderFrameHost* render_frame_host,
const GURL& validated_url,
bool is_error_page,
bool is_iframe_srcdoc);
void OnDidFailProvisionalLoad(RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description);
void OnDidCommitProvisionalLoadForFrame(RenderFrameHost* render_frame_host,
const GURL& url,
ui::PageTransition transition_type);
// If true the navigation has started. // If true the navigation has started.
bool navigation_started_; bool navigation_started_;
...@@ -65,6 +83,12 @@ class TestNavigationObserver { ...@@ -65,6 +83,12 @@ class TestNavigationObserver {
// The number of navigations to wait for. // The number of navigations to wait for.
int number_of_navigations_; int number_of_navigations_;
// The url of the navigation that last committed.
GURL last_navigation_url_;
// True if the last navigation succeeded.
bool last_navigation_succeeded_;
// The MessageLoopRunner used to spin the message loop. // The MessageLoopRunner used to spin the message loop.
scoped_refptr<MessageLoopRunner> message_loop_runner_; scoped_refptr<MessageLoopRunner> message_loop_runner_;
......
...@@ -4393,6 +4393,7 @@ void RenderFrameImpl::BeginNavigation(blink::WebURLRequest* request) { ...@@ -4393,6 +4393,7 @@ void RenderFrameImpl::BeginNavigation(blink::WebURLRequest* request) {
// TODO(clamy): Same-document navigations should not be sent back to the // TODO(clamy): Same-document navigations should not be sent back to the
// browser. // browser.
// TODO(clamy): Data urls should not be sent back to the browser either. // TODO(clamy): Data urls should not be sent back to the browser either.
Send(new FrameHostMsg_DidStartLoading(routing_id_, true));
Send(new FrameHostMsg_BeginNavigation(routing_id_, Send(new FrameHostMsg_BeginNavigation(routing_id_,
MakeBeginNavigationParams(request), MakeBeginNavigationParams(request),
MakeCommonNavigationParams(request))); MakeCommonNavigationParams(request)));
......
<html>
<head><title>Simple links</title>
<script>
function simulateClick(target) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false,
false, false, 0, null);
return target.dispatchEvent(evt);
}
function setPortNumber(portNumber) {
var link = document.getElementById("cross_site_link");
link.setAttribute("href", "http://foo.com:" + portNumber + "/title2.html");
return true;
}
function clickSameSiteLink() {
return simulateClick(document.getElementById("same_site_link"));
}
function clickCrossSiteLink() {
return simulateClick(document.getElementById("cross_site_link"));
}
</script>
</head>
<a href="title2.html" id="same_site_link">same-site</a><br>
<a href="http://foo.com/title2.html" id="cross_site_link">cross-site</a><br>
</html>
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