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