OLD | NEW |
| (Empty) |
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_fake_resources_test.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/process.h" | |
10 #include "base/shared_memory.h" | |
11 #include "base/time.h" | |
12 #include "content/common/resource_messages.h" | |
13 #include "content/common/view_messages.h" | |
14 #include "content/public/common/resource_response.h" | |
15 #include "content/renderer/render_thread_impl.h" | |
16 #include "content/renderer/render_view_impl.h" | |
17 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | |
18 #include "content/test/mock_render_process.h" | |
19 #include "googleurl/src/gurl.h" | |
20 #include "net/base/upload_data.h" | |
21 #include "net/http/http_response_headers.h" | |
22 #include "net/url_request/url_request_status.h" | |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h" | |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
28 #include "webkit/dom_storage/dom_storage_types.h" | |
29 #include "webkit/glue/glue_serialize.h" | |
30 #include "webkit/glue/webkit_glue.h" | |
31 | |
32 namespace content { | |
33 | |
34 const int32 RenderViewFakeResourcesTest::kViewId = 5; | |
35 | |
36 RenderViewFakeResourcesTest::RenderViewFakeResourcesTest() {} | |
37 RenderViewFakeResourcesTest::~RenderViewFakeResourcesTest() {} | |
38 | |
39 bool RenderViewFakeResourcesTest::OnMessageReceived( | |
40 const IPC::Message& message) { | |
41 IPC_BEGIN_MESSAGE_MAP(RenderViewFakeResourcesTest, message) | |
42 IPC_MESSAGE_HANDLER(ViewHostMsg_RenderViewReady, OnRenderViewReady) | |
43 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStopLoading, OnDidStopLoading) | |
44 IPC_MESSAGE_HANDLER(ResourceHostMsg_RequestResource, OnRequestResource) | |
45 IPC_END_MESSAGE_MAP() | |
46 return true; | |
47 } | |
48 | |
49 bool RenderViewFakeResourcesTest::Visit(RenderView* render_view) { | |
50 view_ = render_view; | |
51 return false; | |
52 } | |
53 | |
54 void RenderViewFakeResourcesTest::SetUp() { | |
55 // Set up the renderer. This code is largely adapted from | |
56 // render_view_test.cc and renderer_main.cc. Note that we use a | |
57 // MockRenderProcess (because we don't need to use IPC for painting), | |
58 // but we use a real RenderThread so that we can use the ResourceDispatcher | |
59 // to fetch network resources. These are then served canned content | |
60 // in OnRequestResource(). | |
61 GetContentClient()->set_renderer_for_testing(&content_renderer_client_); | |
62 // Generate a unique channel id so that multiple instances of the test can | |
63 // run in parallel. | |
64 std::string channel_id = IPC::Channel::GenerateVerifiedChannelID( | |
65 std::string()); | |
66 channel_.reset(new IPC::Channel(channel_id, | |
67 IPC::Channel::MODE_SERVER, this)); | |
68 ASSERT_TRUE(channel_->Connect()); | |
69 | |
70 webkit_glue::SetJavaScriptFlags("--expose-gc"); | |
71 mock_process_.reset(new MockRenderProcess); | |
72 sandbox_was_enabled_ = | |
73 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(false); | |
74 render_thread_ = new RenderThreadImpl(channel_id); | |
75 | |
76 // Tell the renderer to create a view, then wait until it's ready. | |
77 // We can't call View::Create() directly here or else we won't get | |
78 // RenderProcess's lazy initialization of WebKit. | |
79 view_ = NULL; | |
80 ViewMsg_New_Params params; | |
81 params.parent_window = 0; | |
82 params.view_id = kViewId; | |
83 params.opener_route_id = MSG_ROUTING_NONE; | |
84 params.session_storage_namespace_id = | |
85 dom_storage::kInvalidSessionStorageNamespaceId; | |
86 ASSERT_TRUE(channel_->Send(new ViewMsg_New(params))); | |
87 message_loop_.Run(); | |
88 } | |
89 | |
90 void RenderViewFakeResourcesTest::TearDown() { | |
91 // Try very hard to collect garbage before shutting down. | |
92 GetMainFrame()->collectGarbage(); | |
93 GetMainFrame()->collectGarbage(); | |
94 | |
95 ASSERT_TRUE(channel_->Send(new ViewMsg_Close(kViewId))); | |
96 do { | |
97 message_loop_.RunAllPending(); | |
98 view_ = NULL; | |
99 RenderView::ForEach(this); | |
100 } while (view_); | |
101 | |
102 mock_process_.reset(); | |
103 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting( | |
104 sandbox_was_enabled_); | |
105 } | |
106 | |
107 RenderView* RenderViewFakeResourcesTest::view() { | |
108 return view_; | |
109 } | |
110 | |
111 WebKit::WebFrame* RenderViewFakeResourcesTest::GetMainFrame() { | |
112 return view_->GetWebView()->mainFrame(); | |
113 } | |
114 | |
115 void RenderViewFakeResourcesTest::LoadURL(const std::string& url) { | |
116 GURL g_url(url); | |
117 GetMainFrame()->loadRequest(WebKit::WebURLRequest(g_url)); | |
118 message_loop_.Run(); | |
119 } | |
120 | |
121 void RenderViewFakeResourcesTest::LoadURLWithPost(const std::string& url) { | |
122 GURL g_url(url); | |
123 WebKit::WebURLRequest request(g_url); | |
124 request.setHTTPMethod(WebKit::WebString::fromUTF8("POST")); | |
125 GetMainFrame()->loadRequest(request); | |
126 message_loop_.Run(); | |
127 } | |
128 | |
129 void RenderViewFakeResourcesTest::GoBack() { | |
130 GoToOffset(-1, GetMainFrame()->previousHistoryItem()); | |
131 } | |
132 | |
133 void RenderViewFakeResourcesTest::GoForward( | |
134 const WebKit::WebHistoryItem& history_item) { | |
135 GoToOffset(1, history_item); | |
136 } | |
137 | |
138 void RenderViewFakeResourcesTest::OnDidStopLoading() { | |
139 message_loop_.Quit(); | |
140 } | |
141 | |
142 void RenderViewFakeResourcesTest::OnRequestResource( | |
143 const IPC::Message& message, | |
144 int request_id, | |
145 const ResourceHostMsg_Request& request_data) { | |
146 std::string headers, body; | |
147 std::map<std::string, std::string>::const_iterator it = | |
148 responses_.find(request_data.url.spec()); | |
149 if (it == responses_.end()) { | |
150 headers = "HTTP/1.1 404 Not Found\0Content-Type:text/html\0\0"; | |
151 body = "content not found"; | |
152 } else { | |
153 headers = "HTTP/1.1 200 OK\0Content-Type:text/html\0\0"; | |
154 body = it->second; | |
155 } | |
156 | |
157 ResourceResponseHead response_head; | |
158 response_head.headers = new net::HttpResponseHeaders(headers); | |
159 response_head.mime_type = "text/html"; | |
160 ASSERT_TRUE(channel_->Send(new ResourceMsg_ReceivedResponse( | |
161 message.routing_id(), request_id, response_head))); | |
162 | |
163 base::SharedMemory shared_memory; | |
164 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(body.size())); | |
165 memcpy(shared_memory.memory(), body.data(), body.size()); | |
166 | |
167 base::SharedMemoryHandle handle; | |
168 ASSERT_TRUE(shared_memory.GiveToProcess(base::Process::Current().handle(), | |
169 &handle)); | |
170 ASSERT_TRUE(channel_->Send(new ResourceMsg_DataReceived( | |
171 message.routing_id(), | |
172 request_id, | |
173 handle, | |
174 body.size(), | |
175 body.size()))); | |
176 | |
177 ASSERT_TRUE(channel_->Send(new ResourceMsg_RequestComplete( | |
178 message.routing_id(), | |
179 request_id, | |
180 net::URLRequestStatus(), | |
181 std::string(), | |
182 base::TimeTicks()))); | |
183 } | |
184 | |
185 void RenderViewFakeResourcesTest::OnRenderViewReady() { | |
186 // Grab a pointer to the new view using RenderViewVisitor. | |
187 ASSERT_TRUE(!view_); | |
188 RenderView::ForEach(this); | |
189 ASSERT_TRUE(view_); | |
190 message_loop_.Quit(); | |
191 } | |
192 | |
193 void RenderViewFakeResourcesTest::GoToOffset( | |
194 int offset, | |
195 const WebKit::WebHistoryItem& history_item) { | |
196 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); | |
197 ViewMsg_Navigate_Params params; | |
198 params.page_id = impl->GetPageId() + offset; | |
199 params.pending_history_list_offset = | |
200 impl->history_list_offset() + offset; | |
201 params.current_history_list_offset = impl->history_list_offset(); | |
202 params.current_history_list_length = (impl->historyBackListCount() + | |
203 impl->historyForwardListCount() + 1); | |
204 params.url = GURL(history_item.urlString()); | |
205 params.transition = PAGE_TRANSITION_FORWARD_BACK; | |
206 params.state = webkit_glue::HistoryItemToString(history_item); | |
207 params.navigation_type = ViewMsg_Navigate_Type::NORMAL; | |
208 params.request_time = base::Time::Now(); | |
209 channel_->Send(new ViewMsg_Navigate(impl->routing_id(), params)); | |
210 message_loop_.Run(); | |
211 } | |
212 | |
213 } // namespace content | |
OLD | NEW |