Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / public / test / render_view_test.cc
index a32cc89..43b1e16 100644 (file)
@@ -6,41 +6,50 @@
 
 #include "base/run_loop.h"
 #include "content/common/dom_storage/dom_storage_types.h"
+#include "content/common/frame_messages.h"
 #include "content/common/input_messages.h"
 #include "content/common/view_messages.h"
+#include "content/public/browser/content_browser_client.h"
 #include "content/public/browser/native_web_keyboard_event.h"
+#include "content/public/common/content_client.h"
 #include "content/public/common/renderer_preferences.h"
-#include "content/public/renderer/history_item_serialization.h"
+#include "content/public/renderer/content_renderer_client.h"
+#include "content/renderer/history_controller.h"
+#include "content/renderer/history_serialization.h"
 #include "content/renderer/render_thread_impl.h"
 #include "content/renderer/render_view_impl.h"
 #include "content/renderer/renderer_main_platform_delegate.h"
 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
+#include "content/test/frame_load_waiter.h"
 #include "content/test/mock_render_process.h"
+#include "third_party/WebKit/public/platform/WebScreenInfo.h"
 #include "third_party/WebKit/public/platform/WebURLRequest.h"
-#include "third_party/WebKit/public/web/WebFrame.h"
 #include "third_party/WebKit/public/web/WebHistoryItem.h"
 #include "third_party/WebKit/public/web/WebInputEvent.h"
 #include "third_party/WebKit/public/web/WebKit.h"
-#include "third_party/WebKit/public/web/WebScreenInfo.h"
-#include "third_party/WebKit/public/web/WebScriptController.h"
+#include "third_party/WebKit/public/web/WebLocalFrame.h"
 #include "third_party/WebKit/public/web/WebScriptSource.h"
 #include "third_party/WebKit/public/web/WebView.h"
 #include "ui/base/resource/resource_bundle.h"
-#include "webkit/glue/webkit_glue.h"
+#include "v8/include/v8.h"
 
-using WebKit::WebFrame;
-using WebKit::WebInputEvent;
-using WebKit::WebMouseEvent;
-using WebKit::WebScriptController;
-using WebKit::WebScriptSource;
-using WebKit::WebString;
-using WebKit::WebURLRequest;
+#if defined(OS_MACOSX)
+#include "base/mac/scoped_nsautorelease_pool.h"
+#endif
+
+using blink::WebInputEvent;
+using blink::WebLocalFrame;
+using blink::WebMouseEvent;
+using blink::WebScriptSource;
+using blink::WebString;
+using blink::WebURLRequest;
 
 namespace {
 const int32 kOpenerId = -2;
 const int32 kRouteId = 5;
 const int32 kMainFrameRouteId = 6;
 const int32 kNewWindowRouteId = 7;
+const int32 kNewFrameRouteId = 10;
 const int32 kSurfaceId = 42;
 
 }  // namespace
@@ -50,7 +59,7 @@ namespace content {
 class RendererWebKitPlatformSupportImplNoSandboxImpl
     : public RendererWebKitPlatformSupportImpl {
  public:
-  virtual WebKit::WebSandboxSupport* sandboxSupport() {
+  virtual blink::WebSandboxSupport* sandboxSupport() {
     return NULL;
   }
 };
@@ -65,7 +74,7 @@ RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::
     ~RendererWebKitPlatformSupportImplNoSandbox() {
 }
 
-WebKit::Platform*
+blink::Platform*
     RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox::Get() {
   return webkit_platform_support_.get();
 }
@@ -82,8 +91,8 @@ void RenderViewTest::ProcessPendingMessages() {
   msg_loop_.Run();
 }
 
-WebFrame* RenderViewTest::GetMainFrame() {
-  return view_->GetWebView()->mainFrame();
+WebLocalFrame* RenderViewTest::GetMainFrame() {
+  return view_->GetWebView()->mainFrame()->toWebLocalFrame();
 }
 
 void RenderViewTest::ExecuteJavaScript(const char* js) {
@@ -91,7 +100,7 @@ void RenderViewTest::ExecuteJavaScript(const char* js) {
 }
 
 bool RenderViewTest::ExecuteJavaScriptAndReturnIntValue(
-    const string16& script,
+    const base::string16& script,
     int* int_result) {
   v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
   v8::Handle<v8::Value> result =
@@ -109,29 +118,33 @@ void RenderViewTest::LoadHTML(const char* html) {
   std::string url_str = "data:text/html;charset=utf-8,";
   url_str.append(html);
   GURL url(url_str);
-
   GetMainFrame()->loadRequest(WebURLRequest(url));
-
   // The load actually happens asynchronously, so we pump messages to process
   // the pending continuation.
-  ProcessPendingMessages();
+  FrameLoadWaiter(view_->GetMainRenderFrame()).Wait();
 }
 
-void RenderViewTest::GoBack(const WebKit::WebHistoryItem& item) {
-  GoToOffset(-1, item);
+void RenderViewTest::GoBack(const PageState& state) {
+  GoToOffset(-1, state);
 }
 
-void RenderViewTest::GoForward(const WebKit::WebHistoryItem& item) {
-  GoToOffset(1, item);
+void RenderViewTest::GoForward(const PageState& state) {
+  GoToOffset(1, state);
+}
+
+void RenderViewTest::GoBackToPrevious() {
+  RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
+  GoBack(HistoryEntryToPageState(
+      impl->history_controller()->GetPreviousEntry()));
 }
 
 void RenderViewTest::SetUp() {
-  // Subclasses can set the ContentClient's renderer before calling
-  // RenderViewTest::SetUp().
-  ContentRendererClient* old_client =
-      SetRendererClientForTesting(&content_renderer_client_);
-  if (old_client)
-    SetRendererClientForTesting(old_client);
+  content_client_.reset(CreateContentClient());
+  content_browser_client_.reset(CreateContentBrowserClient());
+  content_renderer_client_.reset(CreateContentRendererClient());
+  SetContentClient(content_client_.get());
+  SetBrowserClientForTesting(content_browser_client_.get());
+  SetRendererClientForTesting(content_renderer_client_.get());
 
   // Subclasses can set render_thread_ with their own implementation before
   // calling RenderViewTest::SetUp().
@@ -140,7 +153,11 @@ void RenderViewTest::SetUp() {
   render_thread_->set_routing_id(kRouteId);
   render_thread_->set_surface_id(kSurfaceId);
   render_thread_->set_new_window_routing_id(kNewWindowRouteId);
+  render_thread_->set_new_frame_routing_id(kNewFrameRouteId);
 
+#if defined(OS_MACOSX)
+  autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool());
+#endif
   command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM));
   params_.reset(new MainFunctionParams(*command_line_));
   platform_.reset(new RendererMainPlatformDelegate(*params_));
@@ -148,8 +165,9 @@ void RenderViewTest::SetUp() {
 
   // Setting flags and really doing anything with WebKit is fairly fragile and
   // hacky, but this is the world we live in...
-  webkit_glue::SetJavaScriptFlags(" --expose-gc");
-  WebKit::initialize(webkit_platform_support_.Get());
+  std::string flags("--expose-gc");
+  v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
+  blink::initialize(webkit_platform_support_.Get());
 
   // Ensure that we register any necessary schemes when initializing WebKit,
   // since we are using a MockRenderThread.
@@ -165,22 +183,23 @@ void RenderViewTest::SetUp() {
   mock_process_.reset(new MockRenderProcess);
 
   // This needs to pass the mock render thread to the view.
-  RenderViewImpl* view = RenderViewImpl::Create(
-      kOpenerId,
-      RendererPreferences(),
-      WebPreferences(),
-      kRouteId,
-      kMainFrameRouteId,
-      kSurfaceId,
-      kInvalidSessionStorageNamespaceId,
-      string16(),
-      false, // is_renderer_created
-      false, // swapped_out
-      false, // hidden
-      1, // next_page_id
-      WebKit::WebScreenInfo(),
-      AccessibilityModeOff,
-      true);
+  RenderViewImpl* view =
+      RenderViewImpl::Create(kOpenerId,
+                             false,  // window_was_created_with_opener
+                             RendererPreferences(),
+                             WebPreferences(),
+                             kRouteId,
+                             kMainFrameRouteId,
+                             kSurfaceId,
+                             kInvalidSessionStorageNamespaceId,
+                             base::string16(),
+                             false,  // is_renderer_created
+                             false,  // swapped_out
+                             false,  // hidden
+                             false,  // never_visible
+                             1,      // next_page_id
+                             blink::WebScreenInfo(),
+                             AccessibilityModeOff);
   view->AddRef();
   view_ = view;
 }
@@ -207,7 +226,12 @@ void RenderViewTest::TearDown() {
   // (http://crbug.com/21508).
   base::RunLoop().RunUntilIdle();
 
-  WebKit::shutdown();
+#if defined(OS_MACOSX)
+  // Needs to run before blink::shutdown().
+  autorelease_pool_.reset(NULL);
+#endif
+
+  blink::shutdown();
 
   platform_->PlatformUninitialize();
   platform_.reset();
@@ -221,14 +245,14 @@ void RenderViewTest::SendNativeKeyEvent(
 }
 
 void RenderViewTest::SendWebKeyboardEvent(
-    const WebKit::WebKeyboardEvent& key_event) {
+    const blink::WebKeyboardEvent& key_event) {
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
   impl->OnMessageReceived(
       InputMsg_HandleInputEvent(0, &key_event, ui::LatencyInfo(), false));
 }
 
 void RenderViewTest::SendWebMouseEvent(
-    const WebKit::WebMouseEvent& mouse_event) {
+    const blink::WebMouseEvent& mouse_event) {
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
   impl->OnMessageReceived(
       InputMsg_HandleInputEvent(0, &mouse_event, ui::LatencyInfo(), false));
@@ -243,14 +267,15 @@ const char* const kGetCoordinatesScript =
     "    var parent_coordinates = GetCoordinates(elem.offsetParent);"
     "    coordinates[0] += parent_coordinates[0];"
     "    coordinates[1] += parent_coordinates[1];"
-    "    return coordinates;"
+    "    return [ Math.round(coordinates[0]),"
+    "             Math.round(coordinates[1])];"
     "  };"
     "  var elem = document.getElementById('$1');"
     "  if (!elem)"
     "    return null;"
     "  var bounds = GetCoordinates(elem);"
-    "  bounds[2] = elem.offsetWidth;"
-    "  bounds[3] = elem.offsetHeight;"
+    "  bounds[2] = Math.round(elem.offsetWidth);"
+    "  bounds[3] = Math.round(elem.offsetHeight);"
     "  return bounds;"
     "})();";
 gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) {
@@ -259,7 +284,8 @@ gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) {
   std::string script =
       ReplaceStringPlaceholders(kGetCoordinatesScript, params, NULL);
 
-  v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  v8::HandleScope handle_scope(isolate);
   v8::Handle<v8::Value>  value = GetMainFrame()->executeScriptAndReturnValue(
       WebScriptSource(WebString::fromUTF8(script)));
   if (value.IsEmpty() || !value->IsArray())
@@ -270,7 +296,7 @@ gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) {
     return gfx::Rect();
   std::vector<int> coords;
   for (int i = 0; i < 4; ++i) {
-    v8::Handle<v8::Number> index = v8::Number::New(i);
+    v8::Handle<v8::Number> index = v8::Number::New(isolate, i);
     v8::Local<v8::Value> value = array->Get(index);
     if (value.IsEmpty() || !value->IsInt32())
       return gfx::Rect();
@@ -296,7 +322,7 @@ bool RenderViewTest::SimulateElementClick(const std::string& element_id) {
   return true;
 }
 
-void RenderViewTest::SetFocused(const WebKit::WebNode& node) {
+void RenderViewTest::SetFocused(const blink::WebNode& node) {
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
   impl->focusedNodeChanged(node);
 }
@@ -310,22 +336,23 @@ void RenderViewTest::ClearHistory() {
 }
 
 void RenderViewTest::Reload(const GURL& url) {
-  ViewMsg_Navigate_Params params;
+  FrameMsg_Navigate_Params params;
   params.url = url;
-  params.navigation_type = ViewMsg_Navigate_Type::RELOAD;
+  params.navigation_type = FrameMsg_Navigate_Type::RELOAD;
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
-  impl->OnNavigate(params);
+  impl->main_render_frame()->OnNavigate(params);
+  FrameLoadWaiter(impl->main_render_frame()).Wait();
 }
 
 uint32 RenderViewTest::GetNavigationIPCType() {
-  return ViewHostMsg_FrameNavigate::ID;
+  return FrameHostMsg_DidCommitProvisionalLoad::ID;
 }
 
 void RenderViewTest::Resize(gfx::Size new_size,
                             gfx::Rect resizer_rect,
                             bool is_fullscreen) {
   ViewMsg_Resize_Params params;
-  params.screen_info = WebKit::WebScreenInfo();
+  params.screen_info = blink::WebScreenInfo();
   params.new_size = new_size;
   params.physical_backing_size = new_size;
   params.overdraw_bottom_height = 0.f;
@@ -340,10 +367,16 @@ bool RenderViewTest::OnMessageReceived(const IPC::Message& msg) {
   return impl->OnMessageReceived(msg);
 }
 
-void RenderViewTest::DidNavigateWithinPage(WebKit::WebFrame* frame,
+void RenderViewTest::DidNavigateWithinPage(blink::WebLocalFrame* frame,
                                            bool is_new_navigation) {
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
-  impl->didNavigateWithinPage(frame, is_new_navigation);
+  blink::WebHistoryItem item;
+  item.initialize();
+  impl->main_render_frame()->didNavigateWithinPage(
+      frame,
+      item,
+      is_new_navigation ? blink::WebStandardCommit
+                        : blink::WebHistoryInertCommit);
 }
 
 void RenderViewTest::SendContentStateImmediately() {
@@ -351,35 +384,48 @@ void RenderViewTest::SendContentStateImmediately() {
   impl->set_send_content_state_immediately(true);
 }
 
-WebKit::WebWidget* RenderViewTest::GetWebWidget() {
+blink::WebWidget* RenderViewTest::GetWebWidget() {
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
   return impl->webwidget();
 }
 
-void RenderViewTest::GoToOffset(int offset,
-                                const WebKit::WebHistoryItem& history_item) {
+
+ContentClient* RenderViewTest::CreateContentClient() {
+  return new ContentClient;
+}
+
+ContentBrowserClient* RenderViewTest::CreateContentBrowserClient() {
+  return new ContentBrowserClient;
+}
+
+ContentRendererClient* RenderViewTest::CreateContentRendererClient() {
+  return new ContentRendererClient;
+}
+
+void RenderViewTest::GoToOffset(int offset, const PageState& state) {
   RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
 
   int history_list_length = impl->historyBackListCount() +
                             impl->historyForwardListCount() + 1;
   int pending_offset = offset + impl->history_list_offset();
 
-  ViewMsg_Navigate_Params navigate_params;
-  navigate_params.navigation_type = ViewMsg_Navigate_Type::NORMAL;
+  FrameMsg_Navigate_Params navigate_params;
+  navigate_params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
   navigate_params.transition = PAGE_TRANSITION_FORWARD_BACK;
   navigate_params.current_history_list_length = history_list_length;
   navigate_params.current_history_list_offset = impl->history_list_offset();
   navigate_params.pending_history_list_offset = pending_offset;
   navigate_params.page_id = impl->GetPageId() + offset;
-  navigate_params.page_state = HistoryItemToPageState(history_item);
+  navigate_params.page_state = state;
   navigate_params.request_time = base::Time::Now();
 
-  ViewMsg_Navigate navigate_message(impl->GetRoutingID(), navigate_params);
-  OnMessageReceived(navigate_message);
+  FrameMsg_Navigate navigate_message(impl->main_render_frame()->GetRoutingID(),
+                                     navigate_params);
+  impl->main_render_frame()->OnMessageReceived(navigate_message);
 
   // The load actually happens asynchronously, so we pump messages to process
   // the pending continuation.
-  ProcessPendingMessages();
+  FrameLoadWaiter(view_->GetMainRenderFrame()).Wait();
 }
 
 }  // namespace content