Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / public / test / render_view_test.cc
blobf083d808c00082c8a07b32f9e5cefb6748fae632
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 #include "content/public/test/render_view_test.h"
7 #include "base/run_loop.h"
8 #include "content/common/dom_storage/dom_storage_types.h"
9 #include "content/common/frame_messages.h"
10 #include "content/common/input_messages.h"
11 #include "content/common/view_messages.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/browser/native_web_keyboard_event.h"
14 #include "content/public/common/content_client.h"
15 #include "content/public/common/renderer_preferences.h"
16 #include "content/public/renderer/content_renderer_client.h"
17 #include "content/renderer/history_controller.h"
18 #include "content/renderer/history_serialization.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "content/renderer/render_view_impl.h"
21 #include "content/renderer/renderer_main_platform_delegate.h"
22 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
23 #include "content/test/frame_load_waiter.h"
24 #include "content/test/mock_render_process.h"
25 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
26 #include "third_party/WebKit/public/platform/WebURLRequest.h"
27 #include "third_party/WebKit/public/web/WebHistoryItem.h"
28 #include "third_party/WebKit/public/web/WebInputEvent.h"
29 #include "third_party/WebKit/public/web/WebKit.h"
30 #include "third_party/WebKit/public/web/WebLocalFrame.h"
31 #include "third_party/WebKit/public/web/WebScriptSource.h"
32 #include "third_party/WebKit/public/web/WebView.h"
33 #include "ui/base/resource/resource_bundle.h"
34 #include "v8/include/v8.h"
36 #if defined(OS_MACOSX)
37 #include "base/mac/scoped_nsautorelease_pool.h"
38 #endif
40 using blink::WebInputEvent;
41 using blink::WebLocalFrame;
42 using blink::WebMouseEvent;
43 using blink::WebScriptSource;
44 using blink::WebString;
45 using blink::WebURLRequest;
47 namespace {
48 const int32 kOpenerId = -2;
49 const int32 kRouteId = 5;
50 const int32 kMainFrameRouteId = 6;
51 const int32 kNewWindowRouteId = 7;
52 const int32 kNewFrameRouteId = 10;
53 const int32 kSurfaceId = 42;
55 } // namespace
57 namespace content {
59 class RendererWebKitPlatformSupportImplNoSandboxImpl
60 : public RendererWebKitPlatformSupportImpl {
61 public:
62 virtual blink::WebSandboxSupport* sandboxSupport() {
63 return NULL;
67 RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::
68 RendererWebKitPlatformSupportImplNoSandbox() {
69 webkit_platform_support_.reset(
70 new RendererWebKitPlatformSupportImplNoSandboxImpl());
73 RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::
74 ~RendererWebKitPlatformSupportImplNoSandbox() {
77 blink::Platform*
78 RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::Get() {
79 return webkit_platform_support_.get();
82 RenderViewTest::RenderViewTest()
83 : view_(NULL) {
86 RenderViewTest::~RenderViewTest() {
89 void RenderViewTest::ProcessPendingMessages() {
90 msg_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
91 msg_loop_.Run();
94 WebLocalFrame* RenderViewTest::GetMainFrame() {
95 return view_->GetWebView()->mainFrame()->toWebLocalFrame();
98 void RenderViewTest::ExecuteJavaScript(const char* js) {
99 GetMainFrame()->executeScript(WebScriptSource(WebString::fromUTF8(js)));
102 bool RenderViewTest::ExecuteJavaScriptAndReturnIntValue(
103 const base::string16& script,
104 int* int_result) {
105 v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
106 v8::Handle<v8::Value> result =
107 GetMainFrame()->executeScriptAndReturnValue(WebScriptSource(script));
108 if (result.IsEmpty() || !result->IsInt32())
109 return false;
111 if (int_result)
112 *int_result = result->Int32Value();
114 return true;
117 void RenderViewTest::LoadHTML(const char* html) {
118 std::string url_str = "data:text/html;charset=utf-8,";
119 url_str.append(html);
120 GURL url(url_str);
121 GetMainFrame()->loadRequest(WebURLRequest(url));
122 // The load actually happens asynchronously, so we pump messages to process
123 // the pending continuation.
124 FrameLoadWaiter(view_->GetMainRenderFrame()).Wait();
127 PageState RenderViewTest::GetCurrentPageState() {
128 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
129 return HistoryEntryToPageState(impl->history_controller()->GetCurrentEntry());
132 void RenderViewTest::GoBack(const PageState& state) {
133 GoToOffset(-1, state);
136 void RenderViewTest::GoForward(const PageState& state) {
137 GoToOffset(1, state);
140 void RenderViewTest::SetUp() {
141 content_client_.reset(CreateContentClient());
142 content_browser_client_.reset(CreateContentBrowserClient());
143 content_renderer_client_.reset(CreateContentRendererClient());
144 SetContentClient(content_client_.get());
145 SetBrowserClientForTesting(content_browser_client_.get());
146 SetRendererClientForTesting(content_renderer_client_.get());
148 // Subclasses can set render_thread_ with their own implementation before
149 // calling RenderViewTest::SetUp().
150 if (!render_thread_)
151 render_thread_.reset(new MockRenderThread());
152 render_thread_->set_routing_id(kRouteId);
153 render_thread_->set_surface_id(kSurfaceId);
154 render_thread_->set_new_window_routing_id(kNewWindowRouteId);
155 render_thread_->set_new_frame_routing_id(kNewFrameRouteId);
157 #if defined(OS_MACOSX)
158 autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool());
159 #endif
160 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM));
161 params_.reset(new MainFunctionParams(*command_line_));
162 platform_.reset(new RendererMainPlatformDelegate(*params_));
163 platform_->PlatformInitialize();
165 // Setting flags and really doing anything with WebKit is fairly fragile and
166 // hacky, but this is the world we live in...
167 std::string flags("--expose-gc");
168 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
169 blink::initialize(webkit_platform_support_.Get());
171 // Ensure that we register any necessary schemes when initializing WebKit,
172 // since we are using a MockRenderThread.
173 RenderThreadImpl::RegisterSchemes();
175 // This check is needed because when run under content_browsertests,
176 // ResourceBundle isn't initialized (since we have to use a diferent test
177 // suite implementation than for content_unittests). For browser_tests, this
178 // is already initialized.
179 if (!ui::ResourceBundle::HasSharedInstance())
180 ui::ResourceBundle::InitSharedInstanceWithLocale(
181 "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
183 mock_process_.reset(new MockRenderProcess);
185 // This needs to pass the mock render thread to the view.
186 RenderViewImpl* view =
187 RenderViewImpl::Create(kOpenerId,
188 false, // window_was_created_with_opener
189 RendererPreferences(),
190 WebPreferences(),
191 kRouteId,
192 kMainFrameRouteId,
193 kSurfaceId,
194 kInvalidSessionStorageNamespaceId,
195 base::string16(),
196 false, // is_renderer_created
197 false, // swapped_out
198 MSG_ROUTING_NONE, // proxy_routing_id
199 false, // hidden
200 false, // never_visible
201 1, // next_page_id
202 blink::WebScreenInfo());
203 view->AddRef();
204 view_ = view;
207 void RenderViewTest::TearDown() {
208 // Try very hard to collect garbage before shutting down.
209 // "5" was chosen following http://crbug.com/46571#c9
210 const int kGCIterations = 5;
211 for (int i = 0; i < kGCIterations; i++)
212 GetMainFrame()->collectGarbage();
214 // Run the loop so the release task from the renderwidget executes.
215 ProcessPendingMessages();
217 for (int i = 0; i < kGCIterations; i++)
218 GetMainFrame()->collectGarbage();
220 render_thread_->SendCloseMessage();
221 view_ = NULL;
222 mock_process_.reset();
224 // After telling the view to close and resetting mock_process_ we may get
225 // some new tasks which need to be processed before shutting down WebKit
226 // (http://crbug.com/21508).
227 base::RunLoop().RunUntilIdle();
229 #if defined(OS_MACOSX)
230 // Needs to run before blink::shutdown().
231 autorelease_pool_.reset(NULL);
232 #endif
234 blink::shutdown();
236 platform_->PlatformUninitialize();
237 platform_.reset();
238 params_.reset();
239 command_line_.reset();
242 void RenderViewTest::SendNativeKeyEvent(
243 const NativeWebKeyboardEvent& key_event) {
244 SendWebKeyboardEvent(key_event);
247 void RenderViewTest::SendWebKeyboardEvent(
248 const blink::WebKeyboardEvent& key_event) {
249 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
250 impl->OnMessageReceived(
251 InputMsg_HandleInputEvent(0, &key_event, ui::LatencyInfo(), false));
254 void RenderViewTest::SendWebMouseEvent(
255 const blink::WebMouseEvent& mouse_event) {
256 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
257 impl->OnMessageReceived(
258 InputMsg_HandleInputEvent(0, &mouse_event, ui::LatencyInfo(), false));
261 const char* const kGetCoordinatesScript =
262 "(function() {"
263 " function GetCoordinates(elem) {"
264 " if (!elem)"
265 " return [ 0, 0];"
266 " var coordinates = [ elem.offsetLeft, elem.offsetTop];"
267 " var parent_coordinates = GetCoordinates(elem.offsetParent);"
268 " coordinates[0] += parent_coordinates[0];"
269 " coordinates[1] += parent_coordinates[1];"
270 " return [ Math.round(coordinates[0]),"
271 " Math.round(coordinates[1])];"
272 " };"
273 " var elem = document.getElementById('$1');"
274 " if (!elem)"
275 " return null;"
276 " var bounds = GetCoordinates(elem);"
277 " bounds[2] = Math.round(elem.offsetWidth);"
278 " bounds[3] = Math.round(elem.offsetHeight);"
279 " return bounds;"
280 "})();";
281 gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) {
282 std::vector<std::string> params;
283 params.push_back(element_id);
284 std::string script =
285 ReplaceStringPlaceholders(kGetCoordinatesScript, params, NULL);
287 v8::Isolate* isolate = v8::Isolate::GetCurrent();
288 v8::HandleScope handle_scope(isolate);
289 v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue(
290 WebScriptSource(WebString::fromUTF8(script)));
291 if (value.IsEmpty() || !value->IsArray())
292 return gfx::Rect();
294 v8::Handle<v8::Array> array = value.As<v8::Array>();
295 if (array->Length() != 4)
296 return gfx::Rect();
297 std::vector<int> coords;
298 for (int i = 0; i < 4; ++i) {
299 v8::Handle<v8::Number> index = v8::Number::New(isolate, i);
300 v8::Local<v8::Value> value = array->Get(index);
301 if (value.IsEmpty() || !value->IsInt32())
302 return gfx::Rect();
303 coords.push_back(value->Int32Value());
305 return gfx::Rect(coords[0], coords[1], coords[2], coords[3]);
308 bool RenderViewTest::SimulateElementClick(const std::string& element_id) {
309 gfx::Rect bounds = GetElementBounds(element_id);
310 if (bounds.IsEmpty())
311 return false;
312 WebMouseEvent mouse_event;
313 mouse_event.type = WebInputEvent::MouseDown;
314 mouse_event.button = WebMouseEvent::ButtonLeft;
315 mouse_event.x = bounds.CenterPoint().x();
316 mouse_event.y = bounds.CenterPoint().y();
317 mouse_event.clickCount = 1;
318 scoped_ptr<IPC::Message> input_message(
319 new InputMsg_HandleInputEvent(0, &mouse_event, ui::LatencyInfo(), false));
320 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
321 impl->OnMessageReceived(*input_message);
322 return true;
325 void RenderViewTest::SetFocused(const blink::WebNode& node) {
326 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
327 impl->focusedNodeChanged(node);
330 void RenderViewTest::ClearHistory() {
331 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
332 impl->page_id_ = -1;
333 impl->history_list_offset_ = -1;
334 impl->history_list_length_ = 0;
335 impl->history_page_ids_.clear();
338 void RenderViewTest::Reload(const GURL& url) {
339 FrameMsg_Navigate_Params params;
340 params.url = url;
341 params.navigation_type = FrameMsg_Navigate_Type::RELOAD;
342 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
343 impl->main_render_frame()->OnNavigate(params);
344 FrameLoadWaiter(impl->main_render_frame()).Wait();
347 uint32 RenderViewTest::GetNavigationIPCType() {
348 return FrameHostMsg_DidCommitProvisionalLoad::ID;
351 void RenderViewTest::Resize(gfx::Size new_size,
352 gfx::Rect resizer_rect,
353 bool is_fullscreen) {
354 ViewMsg_Resize_Params params;
355 params.screen_info = blink::WebScreenInfo();
356 params.new_size = new_size;
357 params.physical_backing_size = new_size;
358 params.top_controls_layout_height = 0.f;
359 params.resizer_rect = resizer_rect;
360 params.is_fullscreen = is_fullscreen;
361 scoped_ptr<IPC::Message> resize_message(new ViewMsg_Resize(0, params));
362 OnMessageReceived(*resize_message);
365 bool RenderViewTest::OnMessageReceived(const IPC::Message& msg) {
366 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
367 return impl->OnMessageReceived(msg);
370 void RenderViewTest::DidNavigateWithinPage(blink::WebLocalFrame* frame,
371 bool is_new_navigation) {
372 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
373 blink::WebHistoryItem item;
374 item.initialize();
375 impl->main_render_frame()->didNavigateWithinPage(
376 frame,
377 item,
378 is_new_navigation ? blink::WebStandardCommit
379 : blink::WebHistoryInertCommit);
382 void RenderViewTest::SendContentStateImmediately() {
383 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
384 impl->set_send_content_state_immediately(true);
387 blink::WebWidget* RenderViewTest::GetWebWidget() {
388 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
389 return impl->webwidget();
393 ContentClient* RenderViewTest::CreateContentClient() {
394 return new ContentClient;
397 ContentBrowserClient* RenderViewTest::CreateContentBrowserClient() {
398 return new ContentBrowserClient;
401 ContentRendererClient* RenderViewTest::CreateContentRendererClient() {
402 return new ContentRendererClient;
405 void RenderViewTest::GoToOffset(int offset, const PageState& state) {
406 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
408 int history_list_length = impl->historyBackListCount() +
409 impl->historyForwardListCount() + 1;
410 int pending_offset = offset + impl->history_list_offset();
412 FrameMsg_Navigate_Params navigate_params;
413 navigate_params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
414 navigate_params.transition = PAGE_TRANSITION_FORWARD_BACK;
415 navigate_params.current_history_list_length = history_list_length;
416 navigate_params.current_history_list_offset = impl->history_list_offset();
417 navigate_params.pending_history_list_offset = pending_offset;
418 navigate_params.page_id = impl->page_id_ + offset;
419 navigate_params.page_state = state;
420 navigate_params.request_time = base::Time::Now();
422 FrameMsg_Navigate navigate_message(impl->main_render_frame()->GetRoutingID(),
423 navigate_params);
424 impl->main_render_frame()->OnMessageReceived(navigate_message);
426 // The load actually happens asynchronously, so we pump messages to process
427 // the pending continuation.
428 FrameLoadWaiter(view_->GetMainRenderFrame()).Wait();
431 } // namespace content