render_view_test.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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#include "content/public/test/render_view_test.h"
6
7#include "content/common/view_messages.h"
8#include "content/public/browser/native_web_keyboard_event.h"
9#include "content/public/common/renderer_preferences.h"
10#include "content/renderer/render_thread_impl.h"
11#include "content/renderer/render_view_impl.h"
12#include "content/renderer/renderer_main_platform_delegate.h"
13#include "content/renderer/renderer_webkitplatformsupport_impl.h"
14#include "content/test/mock_render_process.h"
15#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
16#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
17#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
18#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
19#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
20#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
21#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h"
22#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRequest.h"
23#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
24#include "ui/base/resource/resource_bundle.h"
25#include "webkit/dom_storage/dom_storage_types.h"
26#include "webkit/glue/glue_serialize.h"
27#include "webkit/glue/webkit_glue.h"
28
29using WebKit::WebFrame;
30using WebKit::WebInputEvent;
31using WebKit::WebMouseEvent;
32using WebKit::WebScriptController;
33using WebKit::WebScriptSource;
34using WebKit::WebString;
35using WebKit::WebURLRequest;
36
37namespace {
38const int32 kOpenerId = -2;
39const int32 kRouteId = 5;
40const int32 kNewWindowRouteId = 6;
41const int32 kSurfaceId = 42;
42
43}  // namespace
44
45namespace content {
46
47class RendererWebKitPlatformSupportImplNoSandboxImpl :
48    public RendererWebKitPlatformSupportImpl {
49 public:
50  virtual WebKit::WebSandboxSupport* sandboxSupport() {
51    return NULL;
52  }
53};
54
55RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::
56    RendererWebKitPlatformSupportImplNoSandbox() {
57  webkit_platform_support_.reset(
58      new RendererWebKitPlatformSupportImplNoSandboxImpl());
59}
60
61RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::
62    ~RendererWebKitPlatformSupportImplNoSandbox() {
63}
64
65WebKit::WebKitPlatformSupport*
66    RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::Get() {
67  return webkit_platform_support_.get();
68}
69
70RenderViewTest::RenderViewTest()
71    : view_(NULL) {
72}
73
74RenderViewTest::~RenderViewTest() {
75}
76
77void RenderViewTest::ProcessPendingMessages() {
78  msg_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure());
79  msg_loop_.Run();
80}
81
82WebFrame* RenderViewTest::GetMainFrame() {
83  return view_->GetWebView()->mainFrame();
84}
85
86void RenderViewTest::ExecuteJavaScript(const char* js) {
87  GetMainFrame()->executeScript(WebScriptSource(WebString::fromUTF8(js)));
88}
89
90bool RenderViewTest::ExecuteJavaScriptAndReturnIntValue(
91    const string16& script,
92    int* int_result) {
93  v8::Handle<v8::Value> result =
94      GetMainFrame()->executeScriptAndReturnValue(WebScriptSource(script));
95  if (result.IsEmpty() || !result->IsInt32())
96    return false;
97
98  if (int_result)
99    *int_result = result->Int32Value();
100
101  return true;
102}
103
104void RenderViewTest::LoadHTML(const char* html) {
105  std::string url_str = "data:text/html;charset=utf-8,";
106  url_str.append(html);
107  GURL url(url_str);
108
109  GetMainFrame()->loadRequest(WebURLRequest(url));
110
111  // The load actually happens asynchronously, so we pump messages to process
112  // the pending continuation.
113  ProcessPendingMessages();
114}
115
116void RenderViewTest::GoBack(const WebKit::WebHistoryItem& item) {
117  GoToOffset(-1, item);
118}
119
120void RenderViewTest::GoForward(const WebKit::WebHistoryItem& item) {
121  GoToOffset(1, item);
122}
123
124void RenderViewTest::SetUp() {
125  // Subclasses can set the ContentClient's renderer before calling
126  // RenderViewTest::SetUp().
127  if (!GetContentClient()->renderer())
128    GetContentClient()->set_renderer_for_testing(&content_renderer_client_);
129
130  // Subclasses can set render_thread_ with their own implementation before
131  // calling RenderViewTest::SetUp().
132  if (!render_thread_.get())
133    render_thread_.reset(new MockRenderThread());
134  render_thread_->set_routing_id(kRouteId);
135  render_thread_->set_surface_id(kSurfaceId);
136  render_thread_->set_new_window_routing_id(kNewWindowRouteId);
137
138  command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM));
139  params_.reset(new MainFunctionParams(*command_line_));
140  platform_.reset(new RendererMainPlatformDelegate(*params_));
141  platform_->PlatformInitialize();
142
143  // Setting flags and really doing anything with WebKit is fairly fragile and
144  // hacky, but this is the world we live in...
145  webkit_glue::SetJavaScriptFlags(" --expose-gc");
146  WebKit::initialize(webkit_platform_support_.Get());
147
148  // Ensure that we register any necessary schemes when initializing WebKit,
149  // since we are using a MockRenderThread.
150  RenderThreadImpl::RegisterSchemes();
151
152  // This check is needed because when run under content_browsertests,
153  // ResourceBundle isn't initialized (since we have to use a diferent test
154  // suite implementation than for content_unittests). For browser_tests, this
155  // is already initialized.
156  if (!ResourceBundle::HasSharedInstance())
157    ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
158
159  mock_process_.reset(new MockRenderProcess);
160
161  // This needs to pass the mock render thread to the view.
162  RenderViewImpl* view = RenderViewImpl::Create(
163      kOpenerId,
164      RendererPreferences(),
165      webkit_glue::WebPreferences(),
166      new SharedRenderViewCounter(0),
167      kRouteId,
168      kSurfaceId,
169      dom_storage::kInvalidSessionStorageNamespaceId,
170      string16(),
171      false,
172      false,
173      1,
174      WebKit::WebScreenInfo(),
175      AccessibilityModeOff);
176  view->AddRef();
177  view_ = view;
178}
179
180void RenderViewTest::TearDown() {
181  // Try very hard to collect garbage before shutting down.
182  GetMainFrame()->collectGarbage();
183  GetMainFrame()->collectGarbage();
184
185  // Run the loop so the release task from the renderwidget executes.
186  ProcessPendingMessages();
187
188  render_thread_->SendCloseMessage();
189  view_ = NULL;
190  mock_process_.reset();
191
192  // After telling the view to close and resetting mock_process_ we may get
193  // some new tasks which need to be processed before shutting down WebKit
194  // (http://crbug.com/21508).
195  msg_loop_.RunUntilIdle();
196
197  WebKit::shutdown();
198
199  platform_->PlatformUninitialize();
200  platform_.reset();
201  params_.reset();
202  command_line_.reset();
203}
204
205void RenderViewTest::SendNativeKeyEvent(
206    const NativeWebKeyboardEvent& key_event) {
207  SendWebKeyboardEvent(key_event);
208}
209
210void RenderViewTest::SendWebKeyboardEvent(
211    const WebKit::WebKeyboardEvent& key_event) {
212  scoped_ptr<IPC::Message> input_message(new ViewMsg_HandleInputEvent(0));
213  input_message->WriteData(reinterpret_cast<const char*>(&key_event),
214                           sizeof(WebKit::WebKeyboardEvent));
215  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
216  impl->OnMessageReceived(*input_message);
217}
218
219void RenderViewTest::SendWebMouseEvent(
220    const WebKit::WebMouseEvent& mouse_event) {
221  scoped_ptr<IPC::Message> input_message(new ViewMsg_HandleInputEvent(0));
222  input_message->WriteData(reinterpret_cast<const char*>(&mouse_event),
223                           sizeof(WebKit::WebMouseEvent));
224  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
225  impl->OnMessageReceived(*input_message);
226}
227
228const char* const kGetCoordinatesScript =
229    "(function() {"
230    "  function GetCoordinates(elem) {"
231    "    if (!elem)"
232    "      return [ 0, 0];"
233    "    var coordinates = [ elem.offsetLeft, elem.offsetTop];"
234    "    var parent_coordinates = GetCoordinates(elem.offsetParent);"
235    "    coordinates[0] += parent_coordinates[0];"
236    "    coordinates[1] += parent_coordinates[1];"
237    "    return coordinates;"
238    "  };"
239    "  var elem = document.getElementById('$1');"
240    "  if (!elem)"
241    "    return null;"
242    "  var bounds = GetCoordinates(elem);"
243    "  bounds[2] = elem.offsetWidth;"
244    "  bounds[3] = elem.offsetHeight;"
245    "  return bounds;"
246    "})();";
247gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) {
248  std::vector<std::string> params;
249  params.push_back(element_id);
250  std::string script =
251      ReplaceStringPlaceholders(kGetCoordinatesScript, params, NULL);
252
253  v8::HandleScope handle_scope;
254  v8::Handle<v8::Value>  value = GetMainFrame()->executeScriptAndReturnValue(
255      WebScriptSource(WebString::fromUTF8(script)));
256  if (value.IsEmpty() || !value->IsArray())
257    return gfx::Rect();
258
259  v8::Handle<v8::Array> array = value.As<v8::Array>();
260  if (array->Length() != 4)
261    return gfx::Rect();
262  std::vector<int> coords;
263  for (int i = 0; i < 4; ++i) {
264    v8::Handle<v8::Number> index = v8::Number::New(i);
265    v8::Local<v8::Value> value = array->Get(index);
266    if (value.IsEmpty() || !value->IsInt32())
267      return gfx::Rect();
268    coords.push_back(value->Int32Value());
269  }
270  return gfx::Rect(coords[0], coords[1], coords[2], coords[3]);
271}
272
273bool RenderViewTest::SimulateElementClick(const std::string& element_id) {
274  gfx::Rect bounds = GetElementBounds(element_id);
275  if (bounds.IsEmpty())
276    return false;
277  WebMouseEvent mouse_event;
278  mouse_event.type = WebInputEvent::MouseDown;
279  mouse_event.button = WebMouseEvent::ButtonLeft;
280  mouse_event.x = bounds.CenterPoint().x();
281  mouse_event.y = bounds.CenterPoint().y();
282  mouse_event.clickCount = 1;
283  ViewMsg_HandleInputEvent input_event(0);
284  scoped_ptr<IPC::Message> input_message(new ViewMsg_HandleInputEvent(0));
285  input_message->WriteData(reinterpret_cast<const char*>(&mouse_event),
286                           sizeof(WebMouseEvent));
287  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
288  impl->OnMessageReceived(*input_message);
289  return true;
290}
291
292void RenderViewTest::SetFocused(const WebKit::WebNode& node) {
293  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
294  impl->focusedNodeChanged(node);
295}
296
297void RenderViewTest::ClearHistory() {
298  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
299  impl->page_id_ = -1;
300  impl->history_list_offset_ = -1;
301  impl->history_list_length_ = 0;
302  impl->history_page_ids_.clear();
303}
304
305void RenderViewTest::Reload(const GURL& url) {
306  ViewMsg_Navigate_Params params;
307  params.url = url;
308  params.navigation_type = ViewMsg_Navigate_Type::RELOAD;
309  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
310  impl->OnNavigate(params);
311}
312
313uint32 RenderViewTest::GetNavigationIPCType() {
314  return ViewHostMsg_FrameNavigate::ID;
315}
316
317void RenderViewTest::Resize(gfx::Size new_size,
318                            gfx::Rect resizer_rect,
319                            bool is_fullscreen) {
320  scoped_ptr<IPC::Message> resize_message(new ViewMsg_Resize(
321      0, new_size, resizer_rect, is_fullscreen));
322  OnMessageReceived(*resize_message);
323}
324
325bool RenderViewTest::OnMessageReceived(const IPC::Message& msg) {
326  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
327  return impl->OnMessageReceived(msg);
328}
329
330void RenderViewTest::DidNavigateWithinPage(WebKit::WebFrame* frame,
331                                           bool is_new_navigation) {
332  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
333  impl->didNavigateWithinPage(frame, is_new_navigation);
334}
335
336void RenderViewTest::SendContentStateImmediately() {
337  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
338  impl->set_send_content_state_immediately(true);
339}
340
341WebKit::WebWidget* RenderViewTest::GetWebWidget() {
342  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
343  return impl->webwidget();
344}
345
346void RenderViewTest::GoToOffset(int offset,
347                                const WebKit::WebHistoryItem& history_item) {
348  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
349
350  int history_list_length = impl->historyBackListCount() +
351                            impl->historyForwardListCount() + 1;
352  int pending_offset = offset + impl->history_list_offset();
353
354  ViewMsg_Navigate_Params navigate_params;
355  navigate_params.navigation_type = ViewMsg_Navigate_Type::NORMAL;
356  navigate_params.transition = PAGE_TRANSITION_FORWARD_BACK;
357  navigate_params.current_history_list_length = history_list_length;
358  navigate_params.current_history_list_offset = impl->history_list_offset();
359  navigate_params.pending_history_list_offset = pending_offset;
360  navigate_params.page_id = impl->GetPageId() + offset;
361  navigate_params.state = webkit_glue::HistoryItemToString(history_item);
362  navigate_params.request_time = base::Time::Now();
363
364  ViewMsg_Navigate navigate_message(impl->GetRoutingID(), navigate_params);
365  OnMessageReceived(navigate_message);
366
367  // The load actually happens asynchronously, so we pump messages to process
368  // the pending continuation.
369  ProcessPendingMessages();
370}
371
372}  // namespace content
373