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 ~RenderViewHostTestWebContentsObserver() override
{}
39 void DidNavigateMainFrame(const LoadCommittedDetails
& details
,
40 const FrameNavigateParams
& params
) override
{
41 observed_socket_address_
= params
.socket_address
;
42 base_url_
= params
.base_url
;
46 const net::HostPortPair
& observed_socket_address() const {
47 return observed_socket_address_
;
50 GURL
base_url() const {
54 int navigation_count() const { return navigation_count_
; }
57 net::HostPortPair observed_socket_address_
;
59 int navigation_count_
;
61 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestWebContentsObserver
);
64 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, FrameNavigateSocketAddress
) {
65 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
66 RenderViewHostTestWebContentsObserver
observer(shell()->web_contents());
68 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
69 NavigateToURL(shell(), test_url
);
71 EXPECT_EQ(net::HostPortPair::FromURL(
72 embedded_test_server()->base_url()).ToString(),
73 observer
.observed_socket_address().ToString());
74 EXPECT_EQ(1, observer
.navigation_count());
77 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, BaseURLParam
) {
78 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
79 RenderViewHostTestWebContentsObserver
observer(shell()->web_contents());
81 // Base URL is not set if it is the same as the URL.
82 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
83 NavigateToURL(shell(), test_url
);
84 EXPECT_TRUE(observer
.base_url().is_empty());
85 EXPECT_EQ(1, observer
.navigation_count());
87 // But should be set to the original page when reading MHTML.
88 base::FilePath content_test_data_dir
;
89 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &content_test_data_dir
));
90 test_url
= net::FilePathToFileURL(
91 content_test_data_dir
.AppendASCII("google.mht"));
92 NavigateToURL(shell(), test_url
);
93 EXPECT_EQ("http://www.google.com/", observer
.base_url().spec());
96 // This test ensures a RenderFrameHost object is created for the top level frame
97 // in each RenderViewHost.
98 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, BasicRenderFrameHost
) {
99 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
101 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
102 NavigateToURL(shell(), test_url
);
104 FrameTreeNode
* old_root
= static_cast<WebContentsImpl
*>(
105 shell()->web_contents())->GetFrameTree()->root();
106 EXPECT_TRUE(old_root
->current_frame_host());
108 ShellAddedObserver new_shell_observer
;
109 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), "window.open();"));
110 Shell
* new_shell
= new_shell_observer
.GetShell();
111 FrameTreeNode
* new_root
= static_cast<WebContentsImpl
*>(
112 new_shell
->web_contents())->GetFrameTree()->root();
114 EXPECT_TRUE(new_root
->current_frame_host());
115 EXPECT_NE(old_root
->current_frame_host()->routing_id(),
116 new_root
->current_frame_host()->routing_id());
119 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, IsFocusedElementEditable
) {
120 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
122 GURL test_url
= embedded_test_server()->GetURL("/touch_selection.html");
123 NavigateToURL(shell(), test_url
);
125 RenderViewHost
* rvh
= shell()->web_contents()->GetRenderViewHost();
126 EXPECT_FALSE(rvh
->IsFocusedElementEditable());
127 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), "focus_textfield();"));
128 EXPECT_TRUE(rvh
->IsFocusedElementEditable());
131 IN_PROC_BROWSER_TEST_F(RenderViewHostTest
, ReleaseSessionOnCloseACK
) {
132 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
133 GURL test_url
= embedded_test_server()->GetURL(
134 "/access-session-storage.html");
135 NavigateToURL(shell(), test_url
);
137 // Make a new Shell, a seperate tab with it's own session namespace and
138 // have it start loading a url but still be in progress.
139 ShellAddedObserver new_shell_observer
;
140 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), "window.open();"));
141 Shell
* new_shell
= new_shell_observer
.GetShell();
142 new_shell
->LoadURL(test_url
);
143 RenderViewHost
* rvh
= new_shell
->web_contents()->GetRenderViewHost();
144 SiteInstance
* site_instance
= rvh
->GetSiteInstance();
145 scoped_refptr
<SessionStorageNamespace
> session_namespace
=
146 rvh
->GetDelegate()->GetSessionStorageNamespace(site_instance
);
147 EXPECT_FALSE(session_namespace
->HasOneRef());
149 // Close it, or rather start the close operation. The session namespace
150 // should remain until RPH gets an ACK from the renderer about having
153 EXPECT_FALSE(session_namespace
->HasOneRef());
155 // Do something that causes ipc queues to flush and tasks in
156 // flight to complete such that we should have received the ACK.
157 NavigateToURL(shell(), test_url
);
159 // Verify we have the only remaining reference to the namespace.
160 EXPECT_TRUE(session_namespace
->HasOneRef());
163 } // namespace content