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