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 #ifndef CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
6 #define CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string16.h"
14 #include "content/public/browser/native_web_keyboard_event.h"
15 #include "content/public/common/main_function_params.h"
16 #include "content/public/common/page_state.h"
17 #include "content/public/test/mock_render_thread.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/public/platform/Platform.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
22 struct ViewMsg_Resize_Params
;
25 class WebInputElement
;
34 class RendererScheduler
;
38 class ContentBrowserClient
;
40 class ContentRendererClient
;
41 class FakeCompositorDependencies
;
42 class MockRenderProcess
;
44 class RendererMainPlatformDelegate
;
45 class RendererBlinkPlatformImplNoSandboxImpl
;
48 class RenderViewTest
: public testing::Test
{
50 // A special BlinkPlatformImpl class for getting rid off the dependency to the
51 // sandbox, which is not available in RenderViewTest.
52 class RendererBlinkPlatformImplNoSandbox
{
54 RendererBlinkPlatformImplNoSandbox();
55 ~RendererBlinkPlatformImplNoSandbox();
56 blink::Platform
* Get() const;
57 scheduler::RendererScheduler
* Scheduler() const;
60 scoped_ptr
<scheduler::RendererScheduler
> renderer_scheduler_
;
61 scoped_ptr
<RendererBlinkPlatformImplNoSandboxImpl
> blink_platform_impl_
;
65 ~RenderViewTest() override
;
68 // Spins the message loop to process all messages that are currently pending.
69 void ProcessPendingMessages();
71 // Returns a pointer to the main frame.
72 blink::WebLocalFrame
* GetMainFrame();
74 // Executes the given JavaScript in the context of the main frame. The input
75 // is a NULL-terminated UTF-8 string.
76 void ExecuteJavaScript(const char* js
);
78 // Executes the given JavaScript and sets the int value it evaluates to in
80 // Returns true if the JavaScript was evaluated correctly to an int value,
82 bool ExecuteJavaScriptAndReturnIntValue(const base::string16
& script
,
85 // Loads the given HTML into the main frame as a data: URL and blocks until
86 // the navigation is committed.
87 void LoadHTML(const char* html
);
89 // Returns the current PageState.
90 PageState
GetCurrentPageState();
92 // Navigates the main frame back or forward in session history and commits.
93 // The caller must capture a PageState for the target page.
94 void GoBack(const PageState
& state
);
95 void GoForward(const PageState
& state
);
97 // Sends one native key event over IPC.
98 void SendNativeKeyEvent(const NativeWebKeyboardEvent
& key_event
);
100 // Send a raw keyboard event to the renderer.
101 void SendWebKeyboardEvent(const blink::WebKeyboardEvent
& key_event
);
103 // Send a raw mouse event to the renderer.
104 void SendWebMouseEvent(const blink::WebMouseEvent
& mouse_event
);
106 // Returns the bounds (coordinates and size) of the element with id
107 // |element_id|. Returns an empty rect if such an element was not found.
108 gfx::Rect
GetElementBounds(const std::string
& element_id
);
110 // Sends a left mouse click in the middle of the element with id |element_id|.
111 // Returns true if the event was sent, false otherwise (typically because
112 // the element was not found).
113 bool SimulateElementClick(const std::string
& element_id
);
115 // Sends a left mouse click at the |point|.
116 void SimulatePointClick(const gfx::Point
& point
);
118 // Sends a tap at the |rect|.
119 void SimulateRectTap(const gfx::Rect
& rect
);
121 // Simulates |node| being focused.
122 void SetFocused(const blink::WebNode
& node
);
124 // Simulates a navigation with a type of reload to the given url.
125 void Reload(const GURL
& url
);
127 // Returns the IPC message ID of the navigation message.
128 uint32
GetNavigationIPCType();
131 void Resize(gfx::Size new_size
,
132 gfx::Rect resizer_rect
,
135 // Simulates typing the |ascii_character| into this render view. Also accepts
136 // ui::VKEY_BACK for backspace. Will flush the message loop if
137 // |flush_message_loop| is true.
138 void SimulateUserTypingASCIICharacter(char ascii_character
,
139 bool flush_message_loop
);
141 // Simulates user focusing |input|, erasing all text, and typing the
142 // |new_value| instead. Will process input events for autofill. This is a user
144 void SimulateUserInputChangeForElement(blink::WebInputElement
* input
,
145 const std::string
& new_value
);
147 // These are all methods from RenderViewImpl that we expose to testing code.
148 bool OnMessageReceived(const IPC::Message
& msg
);
149 void DidNavigateWithinPage(blink::WebLocalFrame
* frame
,
150 bool is_new_navigation
);
151 void SendContentStateImmediately();
152 blink::WebWidget
* GetWebWidget();
154 // Allows a subclass to override the various content client implementations.
155 virtual ContentClient
* CreateContentClient();
156 virtual ContentBrowserClient
* CreateContentBrowserClient();
157 virtual ContentRendererClient
* CreateContentRendererClient();
159 // Allows a subclass to customize the initial size of the RenderView.
160 virtual scoped_ptr
<ViewMsg_Resize_Params
> InitialSizeParams();
163 void SetUp() override
;
165 void TearDown() override
;
167 base::MessageLoop msg_loop_
;
168 scoped_ptr
<FakeCompositorDependencies
> compositor_deps_
;
169 scoped_ptr
<MockRenderProcess
> mock_process_
;
170 // We use a naked pointer because we don't want to expose RenderViewImpl in
171 // the embedder's namespace.
173 RendererBlinkPlatformImplNoSandbox blink_platform_impl_
;
174 scoped_ptr
<ContentClient
> content_client_
;
175 scoped_ptr
<ContentBrowserClient
> content_browser_client_
;
176 scoped_ptr
<ContentRendererClient
> content_renderer_client_
;
177 scoped_ptr
<MockRenderThread
> render_thread_
;
179 // Used to setup the process so renderers can run.
180 scoped_ptr
<RendererMainPlatformDelegate
> platform_
;
181 scoped_ptr
<MainFunctionParams
> params_
;
182 scoped_ptr
<base::CommandLine
> command_line_
;
184 #if defined(OS_MACOSX)
185 scoped_ptr
<base::mac::ScopedNSAutoreleasePool
> autorelease_pool_
;
189 void GoToOffset(int offset
, const PageState
& state
);
192 } // namespace content
194 #endif // CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_