1 // Copyright 2014 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.
5 #include "base/basictypes.h"
6 #include "base/command_line.h"
7 #include "base/strings/stringprintf.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/public/test/test_navigation_observer.h"
15 #include "content/shell/browser/shell.h"
16 #include "net/dns/mock_host_resolver.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
22 class BrowserSideNavigationBrowserTest
: public ContentBrowserTest
{
24 BrowserSideNavigationBrowserTest() {}
27 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
28 command_line
->AppendSwitch(switches::kEnableBrowserSideNavigation
);
31 void SetUpOnMainThread() override
{
32 host_resolver()->AddRule("*", "127.0.0.1");
33 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
37 // Ensure that browser initiated basic navigations work with browser side
39 IN_PROC_BROWSER_TEST_F(BrowserSideNavigationBrowserTest
,
40 BrowserInitiatedNavigations
) {
41 // Perform a navigation with no live renderer.
43 TestNavigationObserver
observer(shell()->web_contents());
44 GURL
url(embedded_test_server()->GetURL("/title1.html"));
45 NavigateToURL(shell(), url
);
46 EXPECT_EQ(url
, observer
.last_navigation_url());
47 EXPECT_TRUE(observer
.last_navigation_succeeded());
50 RenderFrameHost
* initial_rfh
=
51 static_cast<WebContentsImpl
*>(shell()->web_contents())
52 ->GetFrameTree()->root()->current_frame_host();
54 // Perform a same site navigation.
56 TestNavigationObserver
observer(shell()->web_contents());
57 GURL
url(embedded_test_server()->GetURL("/title2.html"));
58 NavigateToURL(shell(), url
);
59 EXPECT_EQ(url
, observer
.last_navigation_url());
60 EXPECT_TRUE(observer
.last_navigation_succeeded());
63 // The RenderFrameHost should not have changed.
64 EXPECT_EQ(initial_rfh
, static_cast<WebContentsImpl
*>(shell()->web_contents())
65 ->GetFrameTree()->root()->current_frame_host());
67 // Perform a cross-site navigation.
69 TestNavigationObserver
observer(shell()->web_contents());
70 GURL url
= embedded_test_server()->GetURL("foo.com", "/title3.html");
71 NavigateToURL(shell(), url
);
72 EXPECT_EQ(url
, observer
.last_navigation_url());
73 EXPECT_TRUE(observer
.last_navigation_succeeded());
76 // The RenderFrameHost should have changed.
77 EXPECT_NE(initial_rfh
, static_cast<WebContentsImpl
*>(shell()->web_contents())
78 ->GetFrameTree()->root()->current_frame_host());
81 // Ensure that renderer initiated same-site navigations work with browser side
83 IN_PROC_BROWSER_TEST_F(BrowserSideNavigationBrowserTest
,
84 RendererInitiatedSameSiteNavigation
) {
85 // Perform a navigation with no live renderer.
87 TestNavigationObserver
observer(shell()->web_contents());
88 GURL
url(embedded_test_server()->GetURL("/simple_links.html"));
89 NavigateToURL(shell(), url
);
90 EXPECT_EQ(url
, observer
.last_navigation_url());
91 EXPECT_TRUE(observer
.last_navigation_succeeded());
94 RenderFrameHost
* initial_rfh
=
95 static_cast<WebContentsImpl
*>(shell()->web_contents())
96 ->GetFrameTree()->root()->current_frame_host();
98 // Simulate clicking on a same-site link.
100 TestNavigationObserver
observer(shell()->web_contents());
101 GURL
url(embedded_test_server()->GetURL("/title2.html"));
102 bool success
= false;
103 EXPECT_TRUE(ExecuteScriptAndExtractBool(
104 shell()->web_contents(),
105 "window.domAutomationController.send(clickSameSiteLink());", &success
));
106 EXPECT_TRUE(success
);
107 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
108 EXPECT_EQ(url
, observer
.last_navigation_url());
109 EXPECT_TRUE(observer
.last_navigation_succeeded());
112 // The RenderFrameHost should not have changed.
113 EXPECT_EQ(initial_rfh
, static_cast<WebContentsImpl
*>(shell()->web_contents())
114 ->GetFrameTree()->root()->current_frame_host());
117 // Ensure that renderer initiated cross-site navigations work with browser side
119 IN_PROC_BROWSER_TEST_F(BrowserSideNavigationBrowserTest
,
120 RendererInitiatedCrossSiteNavigation
) {
121 // Perform a navigation with no live renderer.
123 TestNavigationObserver
observer(shell()->web_contents());
124 GURL
url(embedded_test_server()->GetURL("/simple_links.html"));
125 NavigateToURL(shell(), url
);
126 EXPECT_EQ(url
, observer
.last_navigation_url());
127 EXPECT_TRUE(observer
.last_navigation_succeeded());
130 RenderFrameHost
* initial_rfh
=
131 static_cast<WebContentsImpl
*>(shell()->web_contents())
132 ->GetFrameTree()->root()->current_frame_host();
134 // Simulate clicking on a cross-site link.
136 TestNavigationObserver
observer(shell()->web_contents());
137 const char kReplacePortNumber
[] =
138 "window.domAutomationController.send(setPortNumber(%d));";
139 uint16 port_number
= embedded_test_server()->port();
140 GURL url
= embedded_test_server()->GetURL("foo.com", "/title2.html");
141 bool success
= false;
142 EXPECT_TRUE(ExecuteScriptAndExtractBool(
143 shell()->web_contents(),
144 base::StringPrintf(kReplacePortNumber
, port_number
),
147 EXPECT_TRUE(ExecuteScriptAndExtractBool(
148 shell()->web_contents(),
149 "window.domAutomationController.send(clickCrossSiteLink());",
151 EXPECT_TRUE(success
);
152 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
153 EXPECT_EQ(url
, observer
.last_navigation_url());
154 EXPECT_TRUE(observer
.last_navigation_succeeded());
157 // The RenderFrameHost should not have changed.
158 EXPECT_EQ(initial_rfh
, static_cast<WebContentsImpl
*>(shell()->web_contents())
159 ->GetFrameTree()->root()->current_frame_host());
162 } // namespace content