Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / render_view_test.h
blobd512c547bced141a42e460340a1cf1e946f6edbf
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_
8 #include <string>
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;
24 namespace blink {
25 class WebWidget;
28 namespace gfx {
29 class Rect;
32 namespace content {
33 class ContentBrowserClient;
34 class ContentClient;
35 class ContentRendererClient;
36 class FakeCompositorDependencies;
37 class MockRenderProcess;
38 class PageState;
39 class RendererMainPlatformDelegate;
40 class RendererBlinkPlatformImplNoSandboxImpl;
41 class RendererScheduler;
42 class RenderView;
44 class RenderViewTest : public testing::Test {
45 public:
46 // A special BlinkPlatformImpl class for getting rid off the dependency to the
47 // sandbox, which is not available in RenderViewTest.
48 class RendererBlinkPlatformImplNoSandbox {
49 public:
50 RendererBlinkPlatformImplNoSandbox();
51 ~RendererBlinkPlatformImplNoSandbox();
52 blink::Platform* Get();
54 private:
55 scoped_ptr<RendererScheduler> renderer_scheduler_;
56 scoped_ptr<RendererBlinkPlatformImplNoSandboxImpl> blink_platform_impl_;
59 RenderViewTest();
60 ~RenderViewTest() override;
62 protected:
63 // Spins the message loop to process all messages that are currently pending.
64 void ProcessPendingMessages();
66 // Returns a pointer to the main frame.
67 blink::WebLocalFrame* GetMainFrame();
69 // Executes the given JavaScript in the context of the main frame. The input
70 // is a NULL-terminated UTF-8 string.
71 void ExecuteJavaScript(const char* js);
73 // Executes the given JavaScript and sets the int value it evaluates to in
74 // |result|.
75 // Returns true if the JavaScript was evaluated correctly to an int value,
76 // false otherwise.
77 bool ExecuteJavaScriptAndReturnIntValue(const base::string16& script,
78 int* result);
80 // Loads the given HTML into the main frame as a data: URL and blocks until
81 // the navigation is committed.
82 void LoadHTML(const char* html);
84 // Returns the current PageState.
85 PageState GetCurrentPageState();
87 // Navigates the main frame back or forward in session history and commits.
88 // The caller must capture a PageState for the target page.
89 void GoBack(const PageState& state);
90 void GoForward(const PageState& state);
92 // Sends one native key event over IPC.
93 void SendNativeKeyEvent(const NativeWebKeyboardEvent& key_event);
95 // Send a raw keyboard event to the renderer.
96 void SendWebKeyboardEvent(const blink::WebKeyboardEvent& key_event);
98 // Send a raw mouse event to the renderer.
99 void SendWebMouseEvent(const blink::WebMouseEvent& key_event);
101 // Returns the bounds (coordinates and size) of the element with id
102 // |element_id|. Returns an empty rect if such an element was not found.
103 gfx::Rect GetElementBounds(const std::string& element_id);
105 // Sends a left mouse click in the middle of the element with id |element_id|.
106 // Returns true if the event was sent, false otherwise (typically because
107 // the element was not found).
108 bool SimulateElementClick(const std::string& element_id);
110 // Sends a left mouse click at the |point|.
111 void SimulatePointClick(const gfx::Point& point);
113 // Sends a tap at the |rect|.
114 void SimulateRectTap(const gfx::Rect& rect);
116 // Simulates |node| being focused.
117 void SetFocused(const blink::WebNode& node);
119 // Simulates a navigation with a type of reload to the given url.
120 void Reload(const GURL& url);
122 // Returns the IPC message ID of the navigation message.
123 uint32 GetNavigationIPCType();
125 // Resize the view.
126 void Resize(gfx::Size new_size,
127 gfx::Rect resizer_rect,
128 bool is_fullscreen);
130 // These are all methods from RenderViewImpl that we expose to testing code.
131 bool OnMessageReceived(const IPC::Message& msg);
132 void DidNavigateWithinPage(blink::WebLocalFrame* frame,
133 bool is_new_navigation);
134 void SendContentStateImmediately();
135 blink::WebWidget* GetWebWidget();
137 // Allows a subclass to override the various content client implementations.
138 virtual ContentClient* CreateContentClient();
139 virtual ContentBrowserClient* CreateContentBrowserClient();
140 virtual ContentRendererClient* CreateContentRendererClient();
142 // Allows a subclass to customize the initial size of the RenderView.
143 virtual scoped_ptr<ViewMsg_Resize_Params> InitialSizeParams();
145 // testing::Test
146 void SetUp() override;
148 void TearDown() override;
150 base::MessageLoop msg_loop_;
151 scoped_ptr<FakeCompositorDependencies> compositor_deps_;
152 scoped_ptr<MockRenderProcess> mock_process_;
153 // We use a naked pointer because we don't want to expose RenderViewImpl in
154 // the embedder's namespace.
155 RenderView* view_;
156 RendererBlinkPlatformImplNoSandbox blink_platform_impl_;
157 scoped_ptr<ContentClient> content_client_;
158 scoped_ptr<ContentBrowserClient> content_browser_client_;
159 scoped_ptr<ContentRendererClient> content_renderer_client_;
160 scoped_ptr<MockRenderThread> render_thread_;
162 // Used to setup the process so renderers can run.
163 scoped_ptr<RendererMainPlatformDelegate> platform_;
164 scoped_ptr<MainFunctionParams> params_;
165 scoped_ptr<base::CommandLine> command_line_;
167 #if defined(OS_MACOSX)
168 scoped_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool_;
169 #endif
171 private:
172 void GoToOffset(int offset, const PageState& state);
175 } // namespace content
177 #endif // CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_