OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | |
6 #define CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/message_loop.h" | |
10 #include "build/build_config.h" | |
11 #include "chrome/browser/renderer_host/mock_render_process_host.h" | |
12 #include "chrome/browser/renderer_host/render_widget_host_view.h" | |
13 #include "chrome/browser/renderer_host/render_view_host.h" | |
14 #include "chrome/browser/renderer_host/render_view_host_factory.h" | |
15 #include "chrome/browser/renderer_host/site_instance.h" | |
16 #include "chrome/browser/tab_contents/test_web_contents.h" | |
17 #include "chrome/test/testing_profile.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 #if defined(OS_WIN) | |
21 #include "chrome/browser/tab_contents/navigation_controller.h" | |
22 #elif defined(OS_POSIX) | |
23 #include "chrome/common/temp_scaffolding_stubs.h" | |
24 #endif | |
25 | |
26 class TestTabContents; | |
27 | |
28 // This file provides a testing framework for mocking out the RenderProcessHost | |
29 // layer. It allows you to test RenderViewHost, TabContents, | |
30 // NavigationController, and other layers above that without running an actual | |
31 // renderer process. | |
32 // | |
33 // To use, derive your test base class from RenderViewHostTestHarness. | |
34 | |
35 // TestRenderViewHostView ------------------------------------------------------ | |
36 | |
37 // Subclass the RenderViewHost's view so that we can call Show(), etc., | |
38 // without having side-effects. | |
39 class TestRenderWidgetHostView : public RenderWidgetHostView { | |
40 public: | |
41 explicit TestRenderWidgetHostView(RenderWidgetHost* rwh); | |
42 | |
43 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view, | |
44 const gfx::Rect& pos) {} | |
45 virtual RenderWidgetHost* GetRenderWidgetHost() const { return NULL; } | |
46 virtual void DidBecomeSelected() {} | |
47 virtual void WasHidden() {} | |
48 virtual void SetSize(const gfx::Size& size) {} | |
49 virtual gfx::NativeView GetNativeView() { return NULL; } | |
50 virtual void MovePluginWindows( | |
51 const std::vector<WebPluginGeometry>& plugin_window_moves) {} | |
52 #if defined(OS_WIN) | |
53 virtual void ForwardMouseEventToRenderer(UINT message, | |
54 WPARAM wparam, | |
55 LPARAM lparam) {} | |
56 #endif | |
57 virtual void Focus() {} | |
58 virtual void Blur() {} | |
59 virtual bool HasFocus() { return true; } | |
60 virtual void AdvanceFocus(bool reverse) {} | |
61 virtual void Show() { is_showing_ = true; } | |
62 virtual void Hide() { is_showing_ = false; } | |
63 virtual gfx::Rect GetViewBounds() const { return gfx::Rect(); } | |
64 virtual void SetIsLoading(bool is_loading) {} | |
65 virtual void UpdateCursor(const WebCursor& cursor) {} | |
66 virtual void UpdateCursorIfOverSelf() {} | |
67 virtual void IMEUpdateStatus(int control, const gfx::Rect& caret_rect) {} | |
68 virtual void DidPaintRect(const gfx::Rect& rect) {} | |
69 virtual void DidScrollRect(const gfx::Rect& rect, int dx, int dy) {} | |
70 virtual void RenderViewGone() { delete this; } | |
71 virtual void Destroy() {} | |
72 virtual void PrepareToDestroy() {} | |
73 virtual void SetTooltipText(const std::wstring& tooltip_text) {} | |
74 virtual BackingStore* AllocBackingStore(const gfx::Size& size); | |
75 #if defined(OS_MACOSX) | |
76 virtual void ShowPopupWithItems(gfx::Rect bounds, | |
77 int item_height, | |
78 int selected_item, | |
79 const std::vector<WebMenuItem>& items) {} | |
80 #endif | |
81 | |
82 bool is_showing() const { return is_showing_; } | |
83 | |
84 private: | |
85 RenderWidgetHost* rwh_; | |
86 bool is_showing_; | |
87 }; | |
88 | |
89 // TestRenderViewHost ---------------------------------------------------------- | |
90 | |
91 // TODO(brettw) this should use a TestTabContents which should be generalized | |
92 // from the TabContents test. We will probably also need that class' version of | |
93 // CreateRenderViewForRenderManager when more complicate tests start using this. | |
94 class TestRenderViewHost : public RenderViewHost { | |
95 public: | |
96 TestRenderViewHost(SiteInstance* instance, | |
97 RenderViewHostDelegate* delegate, | |
98 int routing_id, | |
99 base::WaitableEvent* modal_dialog_event); | |
100 virtual ~TestRenderViewHost(); | |
101 | |
102 // Testing functions --------------------------------------------------------- | |
103 | |
104 // Calls the RenderViewHosts' private OnMessageReceived function with the | |
105 // given message. | |
106 void TestOnMessageReceived(const IPC::Message& msg); | |
107 | |
108 // Calls OnMsgNavigate on the RenderViewHost with the given information, | |
109 // setting the rest of the parameters in the message to the "typical" values. | |
110 // This is a helper function for simulating the most common types of loads. | |
111 void SendNavigate(int page_id, const GURL& url); | |
112 | |
113 // If set, *delete_counter is incremented when this object destructs. | |
114 void set_delete_counter(int* delete_counter) { | |
115 delete_counter_ = delete_counter; | |
116 } | |
117 | |
118 // Sets whether the RenderView currently exists or not. This controls the | |
119 // return value from IsRenderViewLive, which the rest of the system uses to | |
120 // check whether the RenderView has crashed or not. | |
121 void set_render_view_created(bool created) { | |
122 render_view_created_ = created; | |
123 } | |
124 | |
125 // RenderViewHost overrides -------------------------------------------------- | |
126 | |
127 virtual bool CreateRenderView(); | |
128 virtual bool IsRenderViewLive() const; | |
129 | |
130 private: | |
131 FRIEND_TEST(RenderViewHostTest, FilterNavigate); | |
132 | |
133 // Tracks if the caller thinks if it created the RenderView. This is so we can | |
134 // respond to IsRenderViewLive appropriately. | |
135 bool render_view_created_; | |
136 | |
137 // See set_delete_counter() above. May be NULL. | |
138 int* delete_counter_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); | |
141 }; | |
142 | |
143 // TestRenderViewHostFactory --------------------------------------------------- | |
144 | |
145 // Manages creation of the RenderViewHosts using our special subclass. This | |
146 // automatically registers itself when it goes in scope, and unregisters itself | |
147 // when it goes out of scope. Since you can't have more than one factory | |
148 // registered at a time, you can only have one of these objects at a time. | |
149 class TestRenderViewHostFactory : public RenderViewHostFactory { | |
150 public: | |
151 TestRenderViewHostFactory(RenderProcessHostFactory* rph_factory) | |
152 : render_process_host_factory_(rph_factory) { | |
153 RenderViewHostFactory::RegisterFactory(this); | |
154 } | |
155 virtual ~TestRenderViewHostFactory() { | |
156 RenderViewHostFactory::UnregisterFactory(); | |
157 } | |
158 | |
159 virtual RenderViewHost* CreateRenderViewHost( | |
160 SiteInstance* instance, | |
161 RenderViewHostDelegate* delegate, | |
162 int routing_id, | |
163 base::WaitableEvent* modal_dialog_event) { | |
164 // See declaration of render_process_host_factory_ below. | |
165 instance->set_render_process_host_factory(render_process_host_factory_); | |
166 return new TestRenderViewHost(instance, delegate, routing_id, | |
167 modal_dialog_event); | |
168 } | |
169 | |
170 private: | |
171 // This is a bit of a hack. With the current design of the site instances / | |
172 // browsing instances, it's difficult to pass a RenderProcessHostFactory | |
173 // around properly. | |
174 // | |
175 // Instead, we set it right before we create a new RenderViewHost, which | |
176 // happens before the RenderProcessHost is created. This way, the instance | |
177 // has the correct factory and creates our special RenderProcessHosts. | |
178 RenderProcessHostFactory* render_process_host_factory_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); | |
181 }; | |
182 | |
183 // RenderViewHostTestHarness --------------------------------------------------- | |
184 | |
185 class RenderViewHostTestHarness : public testing::Test { | |
186 public: | |
187 RenderViewHostTestHarness() | |
188 : rph_factory_(), | |
189 rvh_factory_(&rph_factory_), | |
190 contents_(NULL) {} | |
191 virtual ~RenderViewHostTestHarness() {} | |
192 | |
193 NavigationController& controller() { | |
194 return contents_->controller(); | |
195 } | |
196 | |
197 TestTabContents* contents() { | |
198 return contents_.get(); | |
199 } | |
200 | |
201 TestRenderViewHost* rvh() { | |
202 return static_cast<TestRenderViewHost*>(contents_->render_view_host()); | |
203 } | |
204 | |
205 TestRenderViewHost* pending_rvh() { | |
206 return static_cast<TestRenderViewHost*>( | |
207 contents_->render_manager()->pending_render_view_host()); | |
208 } | |
209 | |
210 TestRenderViewHost* active_rvh() { | |
211 return pending_rvh() ? pending_rvh() : rvh(); | |
212 } | |
213 | |
214 TestingProfile* profile() { | |
215 return profile_.get(); | |
216 } | |
217 | |
218 MockRenderProcessHost* process() { | |
219 return static_cast<MockRenderProcessHost*>(rvh()->process()); | |
220 } | |
221 | |
222 // Frees the current tab contents for tests that want to test destruction. | |
223 void DeleteContents() { | |
224 contents_.reset(); | |
225 } | |
226 | |
227 // Creates a pending navigation to the given oURL with the default parameters | |
228 // and the commits the load with a page ID one larger than any seen. This | |
229 // emulates what happens on a new navigation. | |
230 void NavigateAndCommit(const GURL& url); | |
231 | |
232 protected: | |
233 // testing::Test | |
234 virtual void SetUp(); | |
235 virtual void TearDown(); | |
236 | |
237 // This profile will be created in SetUp if it has not already been created. | |
238 // This allows tests to override the profile if they so choose in their own | |
239 // SetUp function before calling the base class's (us) SetUp(). | |
240 scoped_ptr<TestingProfile> profile_; | |
241 | |
242 MessageLoopForUI message_loop_; | |
243 | |
244 MockRenderProcessHostFactory rph_factory_; | |
245 TestRenderViewHostFactory rvh_factory_; | |
246 | |
247 scoped_ptr<TestTabContents> contents_; | |
248 | |
249 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); | |
250 }; | |
251 | |
252 #endif // CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | |
OLD | NEW |