1 // Copyright (c) 2012 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/path_service.h"
6 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "content/browser/renderer_host/render_view_host_impl.h"
10 #include "content/browser/web_contents/web_contents_impl.h"
11 #include "content/common/view_messages.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/common/content_paths.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "content/shell/shell.h"
17 #include "content/test/content_browser_test.h"
18 #include "content/test/content_browser_test_utils.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/net_util.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
25 class RenderViewHostTest
: public ContentBrowserTest
{
27 RenderViewHostTest() {}
30 class RenderViewHostTestWebContentsObserver
: public WebContentsObserver
{
32 explicit RenderViewHostTestWebContentsObserver(WebContents
* web_contents
)
33 : WebContentsObserver(web_contents
),
34 navigation_count_(0) {}
35 virtual ~RenderViewHostTestWebContentsObserver() {}
37 virtual void DidNavigateMainFrame(
38 const LoadCommittedDetails
& details
,
39 const FrameNavigateParams
& params
) OVERRIDE
{
40 observed_socket_address_
= params
.socket_address
;
41 base_url_
= params
.base_url
;
45 const net::HostPortPair
& observed_socket_address() const {
46 return observed_socket_address_
;
49 GURL
base_url() const {
53 int navigation_count() const { return navigation_count_
; }
56 net::HostPortPair observed_socket_address_
;
58 int navigation_count_
;
60 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestWebContentsObserver
);
63 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, FrameNavigateSocketAddress
) {
64 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
65 RenderViewHostTestWebContentsObserver
observer(shell()->web_contents());
67 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
68 NavigateToURL(shell(), test_url
);
70 EXPECT_EQ(net::HostPortPair::FromURL(
71 embedded_test_server()->base_url()).ToString(),
72 observer
.observed_socket_address().ToString());
73 EXPECT_EQ(1, observer
.navigation_count());
76 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, BaseURLParam
) {
77 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
78 RenderViewHostTestWebContentsObserver
observer(shell()->web_contents());
80 // Base URL is not set if it is the same as the URL.
81 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
82 NavigateToURL(shell(), test_url
);
83 EXPECT_TRUE(observer
.base_url().is_empty());
84 EXPECT_EQ(1, observer
.navigation_count());
86 // But should be set to the original page when reading MHTML.
87 base::FilePath content_test_data_dir
;
88 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &content_test_data_dir
));
89 test_url
= net::FilePathToFileURL(
90 content_test_data_dir
.AppendASCII("google.mht"));
91 NavigateToURL(shell(), test_url
);
92 EXPECT_EQ("http://www.google.com/", observer
.base_url().spec());
95 // This test ensures a RenderFrameHost object is created for the top level frame
96 // in each RenderViewHost.
97 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, BasicRenderFrameHost
) {
98 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
100 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
101 NavigateToURL(shell(), test_url
);
103 RenderViewHostImpl
* rvh
= static_cast<RenderViewHostImpl
*>(
104 shell()->web_contents()->GetRenderViewHost());
105 EXPECT_TRUE(rvh
->main_render_frame_host_
.get());
107 ShellAddedObserver new_shell_observer
;
108 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), "window.open();"));
109 Shell
* new_shell
= new_shell_observer
.GetShell();
110 RenderViewHostImpl
* new_rvh
= static_cast<RenderViewHostImpl
*>(
111 new_shell
->web_contents()->GetRenderViewHost());
113 EXPECT_TRUE(new_rvh
->main_render_frame_host_
.get());
114 EXPECT_NE(rvh
->main_render_frame_host_
->routing_id(),
115 new_rvh
->main_render_frame_host_
->routing_id());
118 } // namespace content