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