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"
7 #include "base/time/time.h"
8 #include "base/values.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h"
10 #include "content/browser/renderer_host/render_view_host_impl.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/common/view_messages.h"
13 #include "content/public/browser/notification_types.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "content/public/common/content_paths.h"
16 #include "content/public/common/frame_navigate_params.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "content/public/test/content_browser_test.h"
19 #include "content/public/test/content_browser_test_utils.h"
20 #include "content/shell/browser/shell.h"
21 #include "net/base/filename_util.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
27 class RenderViewHostTest
: public ContentBrowserTest
{
29 RenderViewHostTest() {}
32 class RenderViewHostTestWebContentsObserver
: public WebContentsObserver
{
34 explicit RenderViewHostTestWebContentsObserver(WebContents
* web_contents
)
35 : WebContentsObserver(web_contents
),
36 navigation_count_(0) {}
37 virtual ~RenderViewHostTestWebContentsObserver() {}
39 virtual void DidNavigateMainFrame(
40 const LoadCommittedDetails
& details
,
41 const FrameNavigateParams
& params
) OVERRIDE
{
42 observed_socket_address_
= params
.socket_address
;
43 base_url_
= params
.base_url
;
47 const net::HostPortPair
& observed_socket_address() const {
48 return observed_socket_address_
;
51 GURL
base_url() const {
55 int navigation_count() const { return navigation_count_
; }
58 net::HostPortPair observed_socket_address_
;
60 int navigation_count_
;
62 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestWebContentsObserver
);
65 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, FrameNavigateSocketAddress
) {
66 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
67 RenderViewHostTestWebContentsObserver
observer(shell()->web_contents());
69 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
70 NavigateToURL(shell(), test_url
);
72 EXPECT_EQ(net::HostPortPair::FromURL(
73 embedded_test_server()->base_url()).ToString(),
74 observer
.observed_socket_address().ToString());
75 EXPECT_EQ(1, observer
.navigation_count());
78 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, BaseURLParam
) {
79 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
80 RenderViewHostTestWebContentsObserver
observer(shell()->web_contents());
82 // Base URL is not set if it is the same as the URL.
83 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
84 NavigateToURL(shell(), test_url
);
85 EXPECT_TRUE(observer
.base_url().is_empty());
86 EXPECT_EQ(1, observer
.navigation_count());
88 // But should be set to the original page when reading MHTML.
89 base::FilePath content_test_data_dir
;
90 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &content_test_data_dir
));
91 test_url
= net::FilePathToFileURL(
92 content_test_data_dir
.AppendASCII("google.mht"));
93 NavigateToURL(shell(), test_url
);
94 EXPECT_EQ("http://www.google.com/", observer
.base_url().spec());
97 // This test ensures a RenderFrameHost object is created for the top level frame
98 // in each RenderViewHost.
99 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, BasicRenderFrameHost
) {
100 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
102 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
103 NavigateToURL(shell(), test_url
);
105 FrameTreeNode
* old_root
= static_cast<WebContentsImpl
*>(
106 shell()->web_contents())->GetFrameTree()->root();
107 EXPECT_TRUE(old_root
->current_frame_host());
109 ShellAddedObserver new_shell_observer
;
110 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), "window.open();"));
111 Shell
* new_shell
= new_shell_observer
.GetShell();
112 FrameTreeNode
* new_root
= static_cast<WebContentsImpl
*>(
113 new_shell
->web_contents())->GetFrameTree()->root();
115 EXPECT_TRUE(new_root
->current_frame_host());
116 EXPECT_NE(old_root
->current_frame_host()->routing_id(),
117 new_root
->current_frame_host()->routing_id());
120 } // namespace content