render_view_test.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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 "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/public/renderer/history_item_serialization.h" 18#include "content/renderer/history_controller.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/mock_render_process.h" 24#include "third_party/WebKit/public/platform/WebScreenInfo.h" 25#include "third_party/WebKit/public/platform/WebURLRequest.h" 26#include "third_party/WebKit/public/web/WebHistoryItem.h" 27#include "third_party/WebKit/public/web/WebInputEvent.h" 28#include "third_party/WebKit/public/web/WebKit.h" 29#include "third_party/WebKit/public/web/WebLocalFrame.h" 30#include "third_party/WebKit/public/web/WebScriptSource.h" 31#include "third_party/WebKit/public/web/WebView.h" 32#include "ui/base/resource/resource_bundle.h" 33#include "v8/include/v8.h" 34 35#if defined(OS_MACOSX) 36#include "base/mac/scoped_nsautorelease_pool.h" 37#endif 38 39using blink::WebInputEvent; 40using blink::WebLocalFrame; 41using blink::WebMouseEvent; 42using blink::WebScriptSource; 43using blink::WebString; 44using blink::WebURLRequest; 45 46namespace { 47const int32 kOpenerId = -2; 48const int32 kRouteId = 5; 49const int32 kMainFrameRouteId = 6; 50const int32 kNewWindowRouteId = 7; 51const int32 kNewFrameRouteId = 10; 52const int32 kSurfaceId = 42; 53 54} // namespace 55 56namespace content { 57 58class RendererWebKitPlatformSupportImplNoSandboxImpl 59 : public RendererWebKitPlatformSupportImpl { 60 public: 61 virtual blink::WebSandboxSupport* sandboxSupport() { 62 return NULL; 63 } 64}; 65 66RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox:: 67 RendererWebKitPlatformSupportImplNoSandbox() { 68 webkit_platform_support_.reset( 69 new RendererWebKitPlatformSupportImplNoSandboxImpl()); 70} 71 72RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox:: 73 ~RendererWebKitPlatformSupportImplNoSandbox() { 74} 75 76blink::Platform* 77 RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::Get() { 78 return webkit_platform_support_.get(); 79} 80 81RenderViewTest::RenderViewTest() 82 : view_(NULL) { 83} 84 85RenderViewTest::~RenderViewTest() { 86} 87 88void RenderViewTest::ProcessPendingMessages() { 89 msg_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); 90 msg_loop_.Run(); 91} 92 93WebLocalFrame* RenderViewTest::GetMainFrame() { 94 return view_->GetWebView()->mainFrame()->toWebLocalFrame(); 95} 96 97void RenderViewTest::ExecuteJavaScript(const char* js) { 98 GetMainFrame()->executeScript(WebScriptSource(WebString::fromUTF8(js))); 99} 100 101bool RenderViewTest::ExecuteJavaScriptAndReturnIntValue( 102 const base::string16& script, 103 int* int_result) { 104 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); 105 v8::Handle<v8::Value> result = 106 GetMainFrame()->executeScriptAndReturnValue(WebScriptSource(script)); 107 if (result.IsEmpty() || !result->IsInt32()) 108 return false; 109 110 if (int_result) 111 *int_result = result->Int32Value(); 112 113 return true; 114} 115 116void RenderViewTest::LoadHTML(const char* html) { 117 std::string url_str = "data:text/html;charset=utf-8,"; 118 url_str.append(html); 119 GURL url(url_str); 120 121 GetMainFrame()->loadRequest(WebURLRequest(url)); 122 123 // The load actually happens asynchronously, so we pump messages to process 124 // the pending continuation. 125 ProcessPendingMessages(); 126} 127 128void RenderViewTest::GoBack(const blink::WebHistoryItem& item) { 129 GoToOffset(-1, item); 130} 131 132void RenderViewTest::GoForward(const blink::WebHistoryItem& item) { 133 GoToOffset(1, item); 134} 135 136void RenderViewTest::GoBackToPrevious() { 137 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 138 GoBack(impl->history_controller()->GetPreviousItemForExport()); 139} 140 141void RenderViewTest::SetUp() { 142 content_client_.reset(CreateContentClient()); 143 content_browser_client_.reset(CreateContentBrowserClient()); 144 content_renderer_client_.reset(CreateContentRendererClient()); 145 SetContentClient(content_client_.get()); 146 SetBrowserClientForTesting(content_browser_client_.get()); 147 SetRendererClientForTesting(content_renderer_client_.get()); 148 149 // Subclasses can set render_thread_ with their own implementation before 150 // calling RenderViewTest::SetUp(). 151 if (!render_thread_) 152 render_thread_.reset(new MockRenderThread()); 153 render_thread_->set_routing_id(kRouteId); 154 render_thread_->set_surface_id(kSurfaceId); 155 render_thread_->set_new_window_routing_id(kNewWindowRouteId); 156 render_thread_->set_new_frame_routing_id(kNewFrameRouteId); 157 158#if defined(OS_MACOSX) 159 autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool()); 160#endif 161 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); 162 params_.reset(new MainFunctionParams(*command_line_)); 163 platform_.reset(new RendererMainPlatformDelegate(*params_)); 164 platform_->PlatformInitialize(); 165 166 // Setting flags and really doing anything with WebKit is fairly fragile and 167 // hacky, but this is the world we live in... 168 std::string flags("--expose-gc"); 169 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); 170 blink::initialize(webkit_platform_support_.Get()); 171 172 // Ensure that we register any necessary schemes when initializing WebKit, 173 // since we are using a MockRenderThread. 174 RenderThreadImpl::RegisterSchemes(); 175 176 // This check is needed because when run under content_browsertests, 177 // ResourceBundle isn't initialized (since we have to use a diferent test 178 // suite implementation than for content_unittests). For browser_tests, this 179 // is already initialized. 180 if (!ResourceBundle::HasSharedInstance()) 181 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); 182 183 mock_process_.reset(new MockRenderProcess); 184 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 false, // hidden 199 false, // never_visible 200 1, // next_page_id 201 blink::WebScreenInfo(), 202 AccessibilityModeOff); 203 view->AddRef(); 204 view_ = view; 205} 206 207void 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(); 213 214 // Run the loop so the release task from the renderwidget executes. 215 ProcessPendingMessages(); 216 217 for (int i = 0; i < kGCIterations; i++) 218 GetMainFrame()->collectGarbage(); 219 220 render_thread_->SendCloseMessage(); 221 view_ = NULL; 222 mock_process_.reset(); 223 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(); 228 229#if defined(OS_MACOSX) 230 // Needs to run before blink::shutdown(). 231 autorelease_pool_.reset(NULL); 232#endif 233 234 blink::shutdown(); 235 236 platform_->PlatformUninitialize(); 237 platform_.reset(); 238 params_.reset(); 239 command_line_.reset(); 240} 241 242void RenderViewTest::SendNativeKeyEvent( 243 const NativeWebKeyboardEvent& key_event) { 244 SendWebKeyboardEvent(key_event); 245} 246 247void 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)); 252} 253 254void 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)); 259} 260 261const 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 coordinates;" 271 " };" 272 " var elem = document.getElementById('$1');" 273 " if (!elem)" 274 " return null;" 275 " var bounds = GetCoordinates(elem);" 276 " bounds[2] = elem.offsetWidth;" 277 " bounds[3] = elem.offsetHeight;" 278 " return bounds;" 279 "})();"; 280gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) { 281 std::vector<std::string> params; 282 params.push_back(element_id); 283 std::string script = 284 ReplaceStringPlaceholders(kGetCoordinatesScript, params, NULL); 285 286 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 287 v8::HandleScope handle_scope(isolate); 288 v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue( 289 WebScriptSource(WebString::fromUTF8(script))); 290 if (value.IsEmpty() || !value->IsArray()) 291 return gfx::Rect(); 292 293 v8::Handle<v8::Array> array = value.As<v8::Array>(); 294 if (array->Length() != 4) 295 return gfx::Rect(); 296 std::vector<int> coords; 297 for (int i = 0; i < 4; ++i) { 298 v8::Handle<v8::Number> index = v8::Number::New(isolate, i); 299 v8::Local<v8::Value> value = array->Get(index); 300 if (value.IsEmpty() || !value->IsInt32()) 301 return gfx::Rect(); 302 coords.push_back(value->Int32Value()); 303 } 304 return gfx::Rect(coords[0], coords[1], coords[2], coords[3]); 305} 306 307bool RenderViewTest::SimulateElementClick(const std::string& element_id) { 308 gfx::Rect bounds = GetElementBounds(element_id); 309 if (bounds.IsEmpty()) 310 return false; 311 WebMouseEvent mouse_event; 312 mouse_event.type = WebInputEvent::MouseDown; 313 mouse_event.button = WebMouseEvent::ButtonLeft; 314 mouse_event.x = bounds.CenterPoint().x(); 315 mouse_event.y = bounds.CenterPoint().y(); 316 mouse_event.clickCount = 1; 317 scoped_ptr<IPC::Message> input_message( 318 new InputMsg_HandleInputEvent(0, &mouse_event, ui::LatencyInfo(), false)); 319 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 320 impl->OnMessageReceived(*input_message); 321 return true; 322} 323 324void RenderViewTest::SetFocused(const blink::WebNode& node) { 325 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 326 impl->focusedNodeChanged(node); 327} 328 329void RenderViewTest::ClearHistory() { 330 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 331 impl->page_id_ = -1; 332 impl->history_list_offset_ = -1; 333 impl->history_list_length_ = 0; 334 impl->history_page_ids_.clear(); 335} 336 337void RenderViewTest::Reload(const GURL& url) { 338 FrameMsg_Navigate_Params params; 339 params.url = url; 340 params.navigation_type = FrameMsg_Navigate_Type::RELOAD; 341 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 342 impl->main_render_frame()->OnNavigate(params); 343} 344 345uint32 RenderViewTest::GetNavigationIPCType() { 346 return FrameHostMsg_DidCommitProvisionalLoad::ID; 347} 348 349void RenderViewTest::Resize(gfx::Size new_size, 350 gfx::Rect resizer_rect, 351 bool is_fullscreen) { 352 ViewMsg_Resize_Params params; 353 params.screen_info = blink::WebScreenInfo(); 354 params.new_size = new_size; 355 params.physical_backing_size = new_size; 356 params.overdraw_bottom_height = 0.f; 357 params.resizer_rect = resizer_rect; 358 params.is_fullscreen = is_fullscreen; 359 scoped_ptr<IPC::Message> resize_message(new ViewMsg_Resize(0, params)); 360 OnMessageReceived(*resize_message); 361} 362 363bool RenderViewTest::OnMessageReceived(const IPC::Message& msg) { 364 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 365 return impl->OnMessageReceived(msg); 366} 367 368void RenderViewTest::DidNavigateWithinPage(blink::WebLocalFrame* frame, 369 bool is_new_navigation) { 370 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 371 blink::WebHistoryItem item; 372 item.initialize(); 373 impl->main_render_frame()->didNavigateWithinPage( 374 frame, 375 item, 376 is_new_navigation ? blink::WebStandardCommit 377 : blink::WebHistoryInertCommit); 378} 379 380void RenderViewTest::SendContentStateImmediately() { 381 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 382 impl->set_send_content_state_immediately(true); 383} 384 385blink::WebWidget* RenderViewTest::GetWebWidget() { 386 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 387 return impl->webwidget(); 388} 389 390 391ContentClient* RenderViewTest::CreateContentClient() { 392 return new ContentClient; 393} 394 395ContentBrowserClient* RenderViewTest::CreateContentBrowserClient() { 396 return new ContentBrowserClient; 397} 398 399ContentRendererClient* RenderViewTest::CreateContentRendererClient() { 400 return new ContentRendererClient; 401} 402 403void RenderViewTest::GoToOffset(int offset, 404 const blink::WebHistoryItem& history_item) { 405 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 406 407 int history_list_length = impl->historyBackListCount() + 408 impl->historyForwardListCount() + 1; 409 int pending_offset = offset + impl->history_list_offset(); 410 411 FrameMsg_Navigate_Params navigate_params; 412 navigate_params.navigation_type = FrameMsg_Navigate_Type::NORMAL; 413 navigate_params.transition = PAGE_TRANSITION_FORWARD_BACK; 414 navigate_params.current_history_list_length = history_list_length; 415 navigate_params.current_history_list_offset = impl->history_list_offset(); 416 navigate_params.pending_history_list_offset = pending_offset; 417 navigate_params.page_id = impl->GetPageId() + offset; 418 navigate_params.page_state = HistoryItemToPageState(history_item); 419 navigate_params.request_time = base::Time::Now(); 420 421 FrameMsg_Navigate navigate_message(impl->main_render_frame()->GetRoutingID(), 422 navigate_params); 423 impl->main_render_frame()->OnMessageReceived(navigate_message); 424 425 // The load actually happens asynchronously, so we pump messages to process 426 // the pending continuation. 427 ProcessPendingMessages(); 428} 429 430} // namespace content 431