diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 18:04:43 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 18:04:43 +0000 |
commit | 060439127775e61484a771d76b49f50c3f476faa (patch) | |
tree | 39cc00b0b6827ac812e42c48fb2346e129426fd6 /content/test | |
parent | 058ead952840337a224b61c949f18270dcb31a72 (diff) | |
download | chromium_src-060439127775e61484a771d76b49f50c3f476faa.zip chromium_src-060439127775e61484a771d76b49f50c3f476faa.tar.gz chromium_src-060439127775e61484a771d76b49f50c3f476faa.tar.bz2 |
Move RenderViewFakeResourcesTest to content\test. This is because we don't want any Chrome code from including RenderThreadImpl.
BUG=98716
Review URL: http://codereview.chromium.org/8183007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104518 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/test')
-rw-r--r-- | content/test/render_view_fake_resources_test.cc | 202 | ||||
-rw-r--r-- | content/test/render_view_fake_resources_test.h | 144 |
2 files changed, 346 insertions, 0 deletions
diff --git a/content/test/render_view_fake_resources_test.cc b/content/test/render_view_fake_resources_test.cc new file mode 100644 index 0000000..38aadfb --- /dev/null +++ b/content/test/render_view_fake_resources_test.cc @@ -0,0 +1,202 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/test/render_view_fake_resources_test.h" + +#include <string.h> + +#include "base/process.h" +#include "base/shared_memory.h" +#include "base/time.h" +#include "content/common/dom_storage_common.h" +#include "content/common/resource_messages.h" +#include "content/common/resource_response.h" +#include "content/common/view_messages.h" +#include "content/renderer/mock_render_process.h" +#include "content/renderer/render_thread_impl.h" +#include "content/renderer/render_view.h" +#include "googleurl/src/gurl.h" +#include "net/base/upload_data.h" +#include "net/http/http_response_headers.h" +#include "net/url_request/url_request_status.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" +#include "webkit/glue/glue_serialize.h" +#include "webkit/glue/webkit_glue.h" + +#if defined(OS_MACOSX) +#include "third_party/WebKit/Source/WebKit/mac/WebCoreSupport/WebSystemInterface.h" +#endif + +const int32 RenderViewFakeResourcesTest::kViewId = 5; + +RenderViewFakeResourcesTest::RenderViewFakeResourcesTest() {} +RenderViewFakeResourcesTest::~RenderViewFakeResourcesTest() {} + +bool RenderViewFakeResourcesTest::OnMessageReceived( + const IPC::Message& message) { + IPC_BEGIN_MESSAGE_MAP(RenderViewFakeResourcesTest, message) + IPC_MESSAGE_HANDLER(ViewHostMsg_RenderViewReady, OnRenderViewReady) + IPC_MESSAGE_HANDLER(ViewHostMsg_DidStopLoading, OnDidStopLoading) + IPC_MESSAGE_HANDLER(ResourceHostMsg_RequestResource, OnRequestResource) + IPC_END_MESSAGE_MAP() + return true; +} + +bool RenderViewFakeResourcesTest::Visit(RenderView* render_view) { + view_ = render_view; + return false; +} + +void RenderViewFakeResourcesTest::SetUp() { + // Set up the renderer. This code is largely adapted from + // render_view_test.cc and renderer_main.cc. Note that we use a + // MockRenderProcess (because we don't need to use IPC for painting), + // but we use a real RenderThread so that we can use the ResourceDispatcher + // to fetch network resources. These are then served canned content + // in OnRequestResource(). + content::GetContentClient()->set_renderer(&content_renderer_client_); + static const char kThreadName[] = "RenderViewFakeResourcesTest"; + channel_.reset(new IPC::Channel(kThreadName, + IPC::Channel::MODE_SERVER, this)); + ASSERT_TRUE(channel_->Connect()); + + webkit_glue::SetJavaScriptFlags("--expose-gc"); + mock_process_.reset(new MockRenderProcess); + render_thread_ = new RenderThreadImpl(kThreadName); + mock_process_->set_main_thread(render_thread_); +#if defined(OS_MACOSX) + InitWebCoreSystemInterface(); +#endif + + // Tell the renderer to create a view, then wait until it's ready. + // We can't call View::Create() directly here or else we won't get + // RenderProcess's lazy initialization of WebKit. + view_ = NULL; + ViewMsg_New_Params params; + params.parent_window = 0; + params.view_id = kViewId; + params.session_storage_namespace_id = kInvalidSessionStorageNamespaceId; + ASSERT_TRUE(channel_->Send(new ViewMsg_New(params))); + message_loop_.Run(); +} + +void RenderViewFakeResourcesTest::TearDown() { + // Try very hard to collect garbage before shutting down. + GetMainFrame()->collectGarbage(); + GetMainFrame()->collectGarbage(); + + ASSERT_TRUE(channel_->Send(new ViewMsg_Close(kViewId))); + do { + message_loop_.RunAllPending(); + view_ = NULL; + RenderView::ForEach(this); + } while (view_); + + mock_process_.reset(); +} + +WebKit::WebFrame* RenderViewFakeResourcesTest::GetMainFrame() { + return view_->webview()->mainFrame(); +} + +void RenderViewFakeResourcesTest::LoadURL(const std::string& url) { + GURL g_url(url); + GetMainFrame()->loadRequest(WebKit::WebURLRequest(g_url)); + message_loop_.Run(); +} + +void RenderViewFakeResourcesTest::LoadURLWithPost(const std::string& url) { + GURL g_url(url); + WebKit::WebURLRequest request(g_url); + request.setHTTPMethod(WebKit::WebString::fromUTF8("POST")); + GetMainFrame()->loadRequest(request); + message_loop_.Run(); +} + +void RenderViewFakeResourcesTest::GoBack() { + GoToOffset(-1, GetMainFrame()->previousHistoryItem()); +} + +void RenderViewFakeResourcesTest::GoForward( + const WebKit::WebHistoryItem& history_item) { + GoToOffset(1, history_item); +} + +void RenderViewFakeResourcesTest::OnDidStopLoading() { + message_loop_.Quit(); +} + +void RenderViewFakeResourcesTest::OnRequestResource( + const IPC::Message& message, + int request_id, + const ResourceHostMsg_Request& request_data) { + std::string headers, body; + std::map<std::string, std::string>::const_iterator it = + responses_.find(request_data.url.spec()); + if (it == responses_.end()) { + headers = "HTTP/1.1 404 Not Found\0Content-Type:text/html\0\0"; + body = "content not found"; + } else { + headers = "HTTP/1.1 200 OK\0Content-Type:text/html\0\0"; + body = it->second; + } + + ResourceResponseHead response_head; + response_head.headers = new net::HttpResponseHeaders(headers); + response_head.mime_type = "text/html"; + ASSERT_TRUE(channel_->Send(new ResourceMsg_ReceivedResponse( + message.routing_id(), request_id, response_head))); + + base::SharedMemory shared_memory; + ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(body.size())); + memcpy(shared_memory.memory(), body.data(), body.size()); + + base::SharedMemoryHandle handle; + ASSERT_TRUE(shared_memory.GiveToProcess(base::Process::Current().handle(), + &handle)); + ASSERT_TRUE(channel_->Send(new ResourceMsg_DataReceived( + message.routing_id(), + request_id, + handle, + body.size(), + body.size()))); + + ASSERT_TRUE(channel_->Send(new ResourceMsg_RequestComplete( + message.routing_id(), + request_id, + net::URLRequestStatus(), + std::string(), + base::Time()))); +} + +void RenderViewFakeResourcesTest::OnRenderViewReady() { + // Grab a pointer to the new view using RenderViewVisitor. + ASSERT_TRUE(!view_); + RenderView::ForEach(this); + ASSERT_TRUE(view_); + message_loop_.Quit(); +} + +void RenderViewFakeResourcesTest::GoToOffset( + int offset, + const WebKit::WebHistoryItem& history_item) { + ViewMsg_Navigate_Params params; + params.page_id = view_->page_id() + offset; + params.pending_history_list_offset = + view_->history_list_offset() + offset; + params.current_history_list_offset = view_->history_list_offset(); + params.current_history_list_length = (view_->historyBackListCount() + + view_->historyForwardListCount() + 1); + params.url = GURL(history_item.urlString()); + params.transition = PageTransition::FORWARD_BACK; + params.state = webkit_glue::HistoryItemToString(history_item); + params.navigation_type = ViewMsg_Navigate_Type::NORMAL; + params.request_time = base::Time::Now(); + channel_->Send(new ViewMsg_Navigate(view_->routing_id(), params)); + message_loop_.Run(); +} diff --git a/content/test/render_view_fake_resources_test.h b/content/test/render_view_fake_resources_test.h new file mode 100644 index 0000000..d42e8a8 --- /dev/null +++ b/content/test/render_view_fake_resources_test.h @@ -0,0 +1,144 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// RenderViewFakeResourcesTest can be used as a base class for tests that need +// to simulate loading network resources (such as http: urls) into a +// RenderView. It does this by handling the relevant IPC messages that the +// renderer would normally send to the browser, and responding with static +// content from an internal map. A request for a url that is not in the map +// will return a 404. Currently, content is always returned as text/html, and +// there is no support for handling POST data. +// +// RenderViewFakeResourcesTest sets up a MessageLoop (message_loop_) that +// can be used by the subclass to post or process tasks. +// +// Note that since WebKit cannot be safely shut down and re-initialized, +// any test that uses this base class should run as part of browser_tests +// so that the test is launched in its own process. +// +// Typical usage: +// +// class MyTest : public RenderVieFakeResourcesTest { +// protected: +// virtual void SetUp() { +// RenderViewFakeResourcesTest::SetUp(); +// <insert test-specific setup> +// } +// +// virtual void TearDown() { +// <insert test-specific teardown> +// RenderViewFakeResourcesTest::TearDown(); +// } +// ... +// }; +// +// TEST_F(MyTest, TestFoo) { +// responses_["http://host.com/"] = "<html><body>some content</body></html>"; +// LoadURL("http://host.com/"); +// ... +// } + +#ifndef CONTENT_TEST_RENDER_VIEW_FAKE_RESOURCES_TEST_H_ +#define CONTENT_TEST_RENDER_VIEW_FAKE_RESOURCES_TEST_H_ + +#include <map> +#include <string> + +#include "base/memory/scoped_ptr.h" +#include "base/message_loop.h" +#include "content/public/renderer/render_view_visitor.h" +#include "content/renderer/mock_content_renderer_client.h" +#include "ipc/ipc_channel.h" +#include "testing/gtest/include/gtest/gtest.h" + +class MockRenderProcess; +class RendererMainPlatformDelegate; +class RenderThreadImpl; +class RenderView; +struct ResourceHostMsg_Request; + +namespace WebKit { +class WebFrame; +class WebHistoryItem; +} + +class RenderViewFakeResourcesTest : public ::testing::Test, + public IPC::Channel::Listener, + public content::RenderViewVisitor { + public: + // IPC::Channel::Listener implementation. + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + + // RenderViewVisitor implementation. + virtual bool Visit(RenderView* render_view) OVERRIDE; + + protected: + RenderViewFakeResourcesTest(); + virtual ~RenderViewFakeResourcesTest(); + + // Call the base class SetUp and TearDown methods as part of your + // test fixture's SetUp / TearDown. + virtual void SetUp() OVERRIDE; + virtual void TearDown() OVERRIDE; + + // Loads |url| into the RenderView, waiting for the load to finish. + // Before loading the url, add any content that you want to return + // to responses_. + void LoadURL(const std::string& url); + + // Same as LoadURL, but sends a POST request. Note that POST data is + // not supported. + void LoadURLWithPost(const std::string& url); + + // Navigates the main frame back in session history. + void GoBack(); + + // Navigates the main frame forward in session history. Note that for + // forward navigations, the caller needs to capture the WebHistoryItem + // for the page to go forward to (before going back) and pass it to + // this method. The WebHistoryItem is available from the WebFrame. + void GoForward(const WebKit::WebHistoryItem& history_item); + + // Returns the main WebFrame for our RenderView. + WebKit::WebFrame* GetMainFrame(); + + // IPC message handlers below + + // Notification that page load has finished. Exit the message loop + // so that the test can continue. + void OnDidStopLoading(); + + // Notification that the renderer wants to load a resource. + // If the requested url is in responses_, we send the renderer a 200 + // and the supplied content, otherwise we send it a 404 error. + void OnRequestResource(const IPC::Message& message, + int request_id, + const ResourceHostMsg_Request& request_data); + + // Notification that the render view we've created is ready to use. + void OnRenderViewReady(); + + static const int32 kViewId; // arbitrary id for our testing view + + MessageLoopForIO message_loop_; + content::MockContentRendererClient content_renderer_client_; + // channel that the renderer uses to talk to the browser. + // For this test, we will handle the browser end of the channel. + scoped_ptr<IPC::Channel> channel_; + RenderThreadImpl* render_thread_; // owned by mock_process_ + scoped_ptr<MockRenderProcess> mock_process_; + RenderView* view_; // not owned, deletes itself on close + + // Map of url -> response body for network requests from the renderer. + // Any urls not in this map are served a 404 error. + std::map<std::string, std::string> responses_; + + private: + // A helper for GoBack and GoForward. + void GoToOffset(int offset, const WebKit::WebHistoryItem& history_item); + + DISALLOW_COPY_AND_ASSIGN(RenderViewFakeResourcesTest); +}; + +#endif // CONTENT_TEST_RENDER_VIEW_FAKE_RESOURCES_TEST_H_ |