• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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/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/browser/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"
22 
23 namespace content {
24 
25 class RenderViewHostTest : public ContentBrowserTest {
26  public:
RenderViewHostTest()27   RenderViewHostTest() {}
28 };
29 
30 class RenderViewHostTestWebContentsObserver : public WebContentsObserver {
31  public:
RenderViewHostTestWebContentsObserver(WebContents * web_contents)32   explicit RenderViewHostTestWebContentsObserver(WebContents* web_contents)
33       : WebContentsObserver(web_contents),
34         navigation_count_(0) {}
~RenderViewHostTestWebContentsObserver()35   virtual ~RenderViewHostTestWebContentsObserver() {}
36 
DidNavigateMainFrame(const LoadCommittedDetails & details,const FrameNavigateParams & params)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;
42     ++navigation_count_;
43   }
44 
observed_socket_address() const45   const net::HostPortPair& observed_socket_address() const {
46     return observed_socket_address_;
47   }
48 
base_url() const49   GURL base_url() const {
50     return base_url_;
51   }
52 
navigation_count() const53   int navigation_count() const { return navigation_count_; }
54 
55  private:
56   net::HostPortPair observed_socket_address_;
57   GURL base_url_;
58   int navigation_count_;
59 
60   DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestWebContentsObserver);
61 };
62 
IN_PROC_BROWSER_TEST_F(RenderViewHostTest,FrameNavigateSocketAddress)63 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, FrameNavigateSocketAddress) {
64   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
65   RenderViewHostTestWebContentsObserver observer(shell()->web_contents());
66 
67   GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
68   NavigateToURL(shell(), test_url);
69 
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());
74 }
75 
IN_PROC_BROWSER_TEST_F(RenderViewHostTest,BaseURLParam)76 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, BaseURLParam) {
77   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
78   RenderViewHostTestWebContentsObserver observer(shell()->web_contents());
79 
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());
85 
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());
93 }
94 
95 // This test ensures a RenderFrameHost object is created for the top level frame
96 // in each RenderViewHost.
IN_PROC_BROWSER_TEST_F(RenderViewHostTest,BasicRenderFrameHost)97 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, BasicRenderFrameHost) {
98   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
99 
100   GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
101   NavigateToURL(shell(), test_url);
102 
103   RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
104       shell()->web_contents()->GetRenderViewHost());
105   EXPECT_TRUE(rvh->main_render_frame_host());
106 
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());
112 
113   EXPECT_TRUE(new_rvh->main_render_frame_host());
114   EXPECT_NE(rvh->main_render_frame_host()->routing_id(),
115             new_rvh->main_render_frame_host()->routing_id());
116 }
117 
118 }  // namespace content
119