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