Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / content / public / test / test_renderer_host.h
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 #ifndef CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
6 #define CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
7
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/page_transition_types.h"
15
16 #if defined(USE_AURA)
17 #include "ui/aura/test/aura_test_helper.h"
18 #endif
19
20 namespace aura {
21 namespace test {
22 class AuraTestHelper;
23 }
24 }
25
26 namespace ui {
27 class ScopedOleInitializer;
28 }
29
30 namespace content {
31
32 class BrowserContext;
33 class MockRenderProcessHost;
34 class MockRenderProcessHostFactory;
35 class NavigationController;
36 class RenderProcessHostFactory;
37 class RenderViewHostDelegate;
38 class TestRenderFrameHostFactory;
39 class TestRenderViewHostFactory;
40 class WebContents;
41
42 // An interface and utility for driving tests of RenderFrameHost.
43 class RenderFrameHostTester {
44  public:
45   // Retrieves the RenderFrameHostTester that drives the specified
46   // RenderFrameHost. The RenderFrameHost must have been created while
47   // RenderFrameHost testing was enabled; use a
48   // RenderViewHostTestEnabler instance (see below) to do this.
49   static RenderFrameHostTester* For(RenderFrameHost* host);
50
51   // If the given NavigationController has a pending main frame, returns it,
52   // otherwise NULL. This is an alternative to
53   // WebContentsTester::GetPendingMainFrame() when your WebContents was not
54   // created via a TestWebContents.
55   static RenderFrameHost* GetPendingForController(
56       NavigationController* controller);
57
58   // This removes the need to expose
59   // RenderFrameHostImpl::is_swapped_out() outside of content.
60   //
61   // This is safe to call on any RenderFrameHost, not just ones
62   // constructed while a RenderViewHostTestEnabler is in play.
63   static bool IsRenderFrameHostSwappedOut(RenderFrameHost* rfh);
64
65   virtual ~RenderFrameHostTester() {}
66
67   // Gives tests access to RenderFrameHostImpl::OnCreateChild. The returned
68   // RenderFrameHost is owned by the parent RenderFrameHost.
69   virtual RenderFrameHost* AppendChild(const std::string& frame_name) = 0;
70
71   // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
72   // information. Sets the rest of the parameters in the message to the
73   // "typical" values. This is a helper function for simulating the most common
74   // types of loads.
75   virtual void SendNavigate(int page_id, const GURL& url) = 0;
76   virtual void SendFailedNavigate(int page_id, const GURL& url) = 0;
77
78   // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
79   // information, including a custom PageTransition.  Sets the rest of the
80   // parameters in the message to the "typical" values. This is a helper
81   // function for simulating the most common types of loads.
82   virtual void SendNavigateWithTransition(int page_id,
83                                           const GURL& url,
84                                           ui::PageTransition transition) = 0;
85
86   // If set, future loads will have |mime_type| set as the mime type.
87   // If not set, the mime type will default to "text/html".
88   virtual void SetContentsMimeType(const std::string& mime_type) = 0;
89
90   // Calls OnBeforeUnloadACK on this RenderFrameHost with the given parameter.
91   virtual void SendBeforeUnloadACK(bool proceed) = 0;
92
93   // Simulates the SwapOut_ACK that fires if you commit a cross-site
94   // navigation without making any network requests.
95   virtual void SimulateSwapOutACK() = 0;
96 };
97
98 // An interface and utility for driving tests of RenderViewHost.
99 class RenderViewHostTester {
100  public:
101   // Retrieves the RenderViewHostTester that drives the specified
102   // RenderViewHost.  The RenderViewHost must have been created while
103   // RenderViewHost testing was enabled; use a
104   // RenderViewHostTestEnabler instance (see below) to do this.
105   static RenderViewHostTester* For(RenderViewHost* host);
106
107   // Calls the RenderViewHosts' private OnMessageReceived function with the
108   // given message.
109   static bool TestOnMessageReceived(RenderViewHost* rvh,
110                                     const IPC::Message& msg);
111
112   // Returns whether the underlying web-page has any touch-event handlers.
113   static bool HasTouchEventHandler(RenderViewHost* rvh);
114
115   virtual ~RenderViewHostTester() {}
116
117   // Gives tests access to RenderViewHostImpl::CreateRenderView.
118   virtual bool CreateRenderView(const base::string16& frame_name,
119                                 int opener_route_id,
120                                 int proxy_routing_id,
121                                 int32 max_page_id,
122                                 bool created_with_opener) = 0;
123
124   // Makes the WasHidden/WasShown calls to the RenderWidget that
125   // tell it it has been hidden or restored from having been hidden.
126   virtual void SimulateWasHidden() = 0;
127   virtual void SimulateWasShown() = 0;
128 };
129
130 // You can instantiate only one class like this at a time.  During its
131 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
132 // RenderViewHostTester and RenderFrameHostTester respectively.
133 class RenderViewHostTestEnabler {
134  public:
135   RenderViewHostTestEnabler();
136   ~RenderViewHostTestEnabler();
137
138  private:
139   DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler);
140   friend class RenderViewHostTestHarness;
141
142   scoped_ptr<MockRenderProcessHostFactory> rph_factory_;
143   scoped_ptr<TestRenderViewHostFactory> rvh_factory_;
144   scoped_ptr<TestRenderFrameHostFactory> rfh_factory_;
145 };
146
147 // RenderViewHostTestHarness ---------------------------------------------------
148 class RenderViewHostTestHarness : public testing::Test {
149  public:
150   RenderViewHostTestHarness();
151   ~RenderViewHostTestHarness() override;
152
153   NavigationController& controller();
154
155   // The contents under test.
156   WebContents* web_contents();
157
158   // RVH/RFH getters are shorthand for oft-used bits of web_contents().
159
160   // rvh() is equivalent to either of:
161   //   web_contents()->GetMainFrame()->GetRenderViewHost()
162   //   web_contents()->GetRenderViewHost()
163   RenderViewHost* rvh();
164
165   // pending_rvh() is equivalent to:
166   //   WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
167   RenderViewHost* pending_rvh();
168
169   // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
170   RenderViewHost* active_rvh();
171
172   // main_rfh() is equivalent to web_contents()->GetMainFrame()
173   RenderFrameHost* main_rfh();
174
175   // pending_main_rfh() is equivalent to:
176   //   WebContentsTester::For(web_contents())->GetPendingMainFrame()
177   RenderFrameHost* pending_main_rfh();
178
179   BrowserContext* browser_context();
180   MockRenderProcessHost* process();
181
182   // Frees the current WebContents for tests that want to test destruction.
183   void DeleteContents();
184
185   // Sets the current WebContents for tests that want to alter it. Takes
186   // ownership of the WebContents passed.
187   void SetContents(WebContents* contents);
188
189   // Creates a new test-enabled WebContents. Ownership passes to the
190   // caller.
191   WebContents* CreateTestWebContents();
192
193   // Cover for |contents()->NavigateAndCommit(url)|. See
194   // WebContentsTester::NavigateAndCommit for details.
195   void NavigateAndCommit(const GURL& url);
196
197   // Simulates a reload of the current page.
198   void Reload();
199   void FailedReload();
200
201  protected:
202   // testing::Test
203   void SetUp() override;
204   void TearDown() override;
205
206   // Derived classes should override this method to use a custom BrowserContext.
207   // It is invoked by SetUp after threads were started.
208   // RenderViewHostTestHarness will take ownership of the returned
209   // BrowserContext.
210   virtual BrowserContext* CreateBrowserContext();
211
212   // Configures which TestBrowserThreads inside |thread_bundle| are backed by
213   // real threads. Must be called before SetUp().
214   void SetThreadBundleOptions(int options) {
215     DCHECK(thread_bundle_.get() == NULL);
216     thread_bundle_options_ = options;
217   }
218
219   TestBrowserThreadBundle* thread_bundle() { return thread_bundle_.get(); }
220
221 #if defined(USE_AURA)
222   aura::Window* root_window() { return aura_test_helper_->root_window(); }
223 #endif
224
225   // Replaces the RPH being used.
226   void SetRenderProcessHostFactory(RenderProcessHostFactory* factory);
227
228  private:
229   scoped_ptr<BrowserContext> browser_context_;
230
231   scoped_ptr<WebContents> contents_;
232 #if defined(OS_WIN)
233   scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
234 #endif
235 #if defined(USE_AURA)
236   scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
237 #endif
238   RenderViewHostTestEnabler rvh_test_enabler_;
239
240   int thread_bundle_options_;
241   scoped_ptr<TestBrowserThreadBundle> thread_bundle_;
242
243   DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness);
244 };
245
246 }  // namespace content
247
248 #endif  // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_