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/renderer/content_renderer_client.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"
32 class MockRenderProcess;
33 class RendererMainPlatformDelegate;
34 class RendererWebKitPlatformSupportImplNoSandboxImpl;
36 class RenderViewTest : public testing::Test {
38 // A special WebKitPlatformSupportImpl class for getting rid off the
39 // dependency to the sandbox, which is not available in RenderViewTest.
40 class RendererWebKitPlatformSupportImplNoSandbox {
42 RendererWebKitPlatformSupportImplNoSandbox();
43 ~RendererWebKitPlatformSupportImplNoSandbox();
44 WebKit::Platform* Get();
47 scoped_ptr<RendererWebKitPlatformSupportImplNoSandboxImpl>
48 webkit_platform_support_;
52 virtual ~RenderViewTest();
55 // Spins the message loop to process all messages that are currently pending.
56 void ProcessPendingMessages();
58 // Returns a pointer to the main frame.
59 WebKit::WebFrame* GetMainFrame();
61 // Executes the given JavaScript in the context of the main frame. The input
62 // is a NULL-terminated UTF-8 string.
63 void ExecuteJavaScript(const char* js);
65 // Executes the given JavaScript and sets the int value it evaluates to in
67 // Returns true if the JavaScript was evaluated correctly to an int value,
69 bool ExecuteJavaScriptAndReturnIntValue(const string16& script, int* result);
71 // Loads the given HTML into the main frame as a data: URL and blocks until
72 // the navigation is committed.
73 void LoadHTML(const char* html);
75 // Navigates the main frame back or forward in session history and commits.
76 // The caller must capture a WebHistoryItem for the target page. This is
77 // available from the WebFrame.
78 void GoBack(const WebKit::WebHistoryItem& item);
79 void GoForward(const WebKit::WebHistoryItem& item);
81 // Sends one native key event over IPC.
82 void SendNativeKeyEvent(const NativeWebKeyboardEvent& key_event);
84 // Send a raw keyboard event to the renderer.
85 void SendWebKeyboardEvent(const WebKit::WebKeyboardEvent& key_event);
87 // Send a raw mouse event to the renderer.
88 void SendWebMouseEvent(const WebKit::WebMouseEvent& key_event);
90 // Returns the bounds (coordinates and size) of the element with id
91 // |element_id|. Returns an empty rect if such an element was not found.
92 gfx::Rect GetElementBounds(const std::string& element_id);
94 // Sends a left mouse click in the middle of the element with id |element_id|.
95 // Returns true if the event was sent, false otherwise (typically because
96 // the element was not found).
97 bool SimulateElementClick(const std::string& element_id);
99 // Simulates |node| being focused.
100 void SetFocused(const WebKit::WebNode& node);
102 // Clears anything associated with the browsing history.
105 // Simulates a navigation with a type of reload to the given url.
106 void Reload(const GURL& url);
108 // Returns the IPC message ID of the navigation message.
109 uint32 GetNavigationIPCType();
112 void Resize(gfx::Size new_size,
113 gfx::Rect resizer_rect,
116 // These are all methods from RenderViewImpl that we expose to testing code.
117 bool OnMessageReceived(const IPC::Message& msg);
118 void DidNavigateWithinPage(WebKit::WebFrame* frame, bool is_new_navigation);
119 void SendContentStateImmediately();
120 WebKit::WebWidget* GetWebWidget();
123 virtual void SetUp() OVERRIDE;
125 virtual void TearDown() OVERRIDE;
127 base::MessageLoop msg_loop_;
128 scoped_ptr<MockRenderProcess> mock_process_;
129 // We use a naked pointer because we don't want to expose RenderViewImpl in
130 // the embedder's namespace.
132 RendererWebKitPlatformSupportImplNoSandbox webkit_platform_support_;
133 ContentRendererClient content_renderer_client_;
134 scoped_ptr<MockRenderThread> render_thread_;
136 // Used to setup the process so renderers can run.
137 scoped_ptr<RendererMainPlatformDelegate> platform_;
138 scoped_ptr<MainFunctionParams> params_;
139 scoped_ptr<CommandLine> command_line_;
142 void GoToOffset(int offset, const WebKit::WebHistoryItem& history_item);
145 } // namespace content
147 #endif // CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_