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