Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: content/renderer/resource_fetcher_browsertest.cc

Issue 2869663002: ResourceFetcher::Start should be given a WebLocalFrame (Closed)
Patch Set: fix Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/mojo_context_state.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/public/renderer/resource_fetcher.h" 5 #include "content/public/renderer/resource_fetcher.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "base/timer/timer.h" 13 #include "base/timer/timer.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "content/public/browser/render_view_host.h" 15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_switches.h" 17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/url_constants.h" 18 #include "content/public/common/url_constants.h"
19 #include "content/public/renderer/render_view.h" 19 #include "content/public/renderer/render_view.h"
20 #include "content/public/test/content_browser_test.h" 20 #include "content/public/test/content_browser_test.h"
21 #include "content/public/test/content_browser_test_utils.h" 21 #include "content/public/test/content_browser_test_utils.h"
22 #include "content/public/test/test_utils.h" 22 #include "content/public/test/test_utils.h"
23 #include "content/shell/browser/shell.h" 23 #include "content/shell/browser/shell.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h" 24 #include "net/test/embedded_test_server/embedded_test_server.h"
25 #include "third_party/WebKit/public/platform/WebURLResponse.h" 25 #include "third_party/WebKit/public/platform/WebURLResponse.h"
26 #include "third_party/WebKit/public/web/WebFrame.h" 26 #include "third_party/WebKit/public/web/WebFrame.h"
27 #include "third_party/WebKit/public/web/WebView.h" 27 #include "third_party/WebKit/public/web/WebView.h"
28 28
29 using blink::WebFrame;
30 using blink::WebURLRequest; 29 using blink::WebURLRequest;
31 using blink::WebURLResponse; 30 using blink::WebURLResponse;
32 31
33 namespace content { 32 namespace content {
34 33
35 static const int kMaxWaitTimeMs = 5000; 34 static const int kMaxWaitTimeMs = 5000;
36 35
37 class FetcherDelegate { 36 class FetcherDelegate {
38 public: 37 public:
39 FetcherDelegate() 38 FetcherDelegate()
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 void SetUpOnMainThread() override { 141 void SetUpOnMainThread() override {
143 render_view_routing_id_ = 142 render_view_routing_id_ =
144 shell()->web_contents()->GetRenderViewHost()->GetRoutingID(); 143 shell()->web_contents()->GetRenderViewHost()->GetRoutingID();
145 } 144 }
146 145
147 RenderView* GetRenderView() { 146 RenderView* GetRenderView() {
148 return RenderView::FromRoutingID(render_view_routing_id_); 147 return RenderView::FromRoutingID(render_view_routing_id_);
149 } 148 }
150 149
151 void ResourceFetcherDownloadOnRenderer(const GURL& url) { 150 void ResourceFetcherDownloadOnRenderer(const GURL& url) {
152 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 151 blink::WebLocalFrame* frame =
152 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
153 153
154 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); 154 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate);
155 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 155 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
156 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 156 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
157 delegate->NewCallback()); 157 delegate->NewCallback());
158 158
159 delegate->WaitForResponse(); 159 delegate->WaitForResponse();
160 160
161 ASSERT_TRUE(delegate->completed()); 161 ASSERT_TRUE(delegate->completed());
162 EXPECT_EQ(delegate->response().HttpStatusCode(), 200); 162 EXPECT_EQ(delegate->response().HttpStatusCode(), 200);
163 std::string text = delegate->data(); 163 std::string text = delegate->data();
164 EXPECT_TRUE(text.find("Basic html test.") != std::string::npos); 164 EXPECT_TRUE(text.find("Basic html test.") != std::string::npos);
165 } 165 }
166 166
167 void ResourceFetcher404OnRenderer(const GURL& url) { 167 void ResourceFetcher404OnRenderer(const GURL& url) {
168 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 168 blink::WebLocalFrame* frame =
169 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
169 170
170 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); 171 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate);
171 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 172 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
172 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 173 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
173 delegate->NewCallback()); 174 delegate->NewCallback());
174 175
175 delegate->WaitForResponse(); 176 delegate->WaitForResponse();
176 177
177 ASSERT_TRUE(delegate->completed()); 178 ASSERT_TRUE(delegate->completed());
178 EXPECT_EQ(delegate->response().HttpStatusCode(), 404); 179 EXPECT_EQ(delegate->response().HttpStatusCode(), 404);
179 } 180 }
180 181
181 void ResourceFetcherDidFailOnRenderer() { 182 void ResourceFetcherDidFailOnRenderer() {
182 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 183 blink::WebLocalFrame* frame =
184 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
183 185
184 // Try to fetch a page on a site that doesn't exist. 186 // Try to fetch a page on a site that doesn't exist.
185 GURL url("http://localhost:1339/doesnotexist"); 187 GURL url("http://localhost:1339/doesnotexist");
186 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); 188 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate);
187 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 189 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
188 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 190 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
189 delegate->NewCallback()); 191 delegate->NewCallback());
190 192
191 delegate->WaitForResponse(); 193 delegate->WaitForResponse();
192 194
193 // When we fail, we still call the Delegate callback but we pass in empty 195 // When we fail, we still call the Delegate callback but we pass in empty
194 // values. 196 // values.
195 EXPECT_TRUE(delegate->completed()); 197 EXPECT_TRUE(delegate->completed());
196 EXPECT_TRUE(delegate->response().IsNull()); 198 EXPECT_TRUE(delegate->response().IsNull());
197 EXPECT_EQ(delegate->data(), std::string()); 199 EXPECT_EQ(delegate->data(), std::string());
198 EXPECT_FALSE(delegate->timed_out()); 200 EXPECT_FALSE(delegate->timed_out());
199 } 201 }
200 202
201 void ResourceFetcherTimeoutOnRenderer(const GURL& url) { 203 void ResourceFetcherTimeoutOnRenderer(const GURL& url) {
202 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 204 blink::WebLocalFrame* frame =
205 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
203 206
204 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); 207 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate);
205 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 208 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
206 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 209 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
207 delegate->NewCallback()); 210 delegate->NewCallback());
208 fetcher->SetTimeout(base::TimeDelta()); 211 fetcher->SetTimeout(base::TimeDelta());
209 212
210 delegate->WaitForResponse(); 213 delegate->WaitForResponse();
211 214
212 // When we timeout, we still call the Delegate callback but we pass in empty 215 // When we timeout, we still call the Delegate callback but we pass in empty
213 // values. 216 // values.
214 EXPECT_TRUE(delegate->completed()); 217 EXPECT_TRUE(delegate->completed());
215 EXPECT_TRUE(delegate->response().IsNull()); 218 EXPECT_TRUE(delegate->response().IsNull());
216 EXPECT_EQ(delegate->data(), std::string()); 219 EXPECT_EQ(delegate->data(), std::string());
217 EXPECT_FALSE(delegate->timed_out()); 220 EXPECT_FALSE(delegate->timed_out());
218 } 221 }
219 222
220 void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) { 223 void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) {
221 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 224 blink::WebLocalFrame* frame =
225 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
222 226
223 std::unique_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); 227 std::unique_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate);
224 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 228 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
225 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 229 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
226 delegate->NewCallback()); 230 delegate->NewCallback());
227 fetcher->SetTimeout(base::TimeDelta()); 231 fetcher->SetTimeout(base::TimeDelta());
228 delegate->SetFetcher(fetcher.release()); 232 delegate->SetFetcher(fetcher.release());
229 233
230 delegate->WaitForResponse(); 234 delegate->WaitForResponse();
231 EXPECT_FALSE(delegate->timed_out()); 235 EXPECT_FALSE(delegate->timed_out());
232 } 236 }
233 237
234 void ResourceFetcherPost(const GURL& url) { 238 void ResourceFetcherPost(const GURL& url) {
235 const char* kBody = "Really nifty POST body!"; 239 const char* kBody = "Really nifty POST body!";
236 240
237 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 241 blink::WebLocalFrame* frame =
242 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
238 243
239 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); 244 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate);
240 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 245 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
241 fetcher->SetMethod("POST"); 246 fetcher->SetMethod("POST");
242 fetcher->SetBody(kBody); 247 fetcher->SetBody(kBody);
243 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 248 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
244 delegate->NewCallback()); 249 delegate->NewCallback());
245 250
246 delegate->WaitForResponse(); 251 delegate->WaitForResponse();
247 ASSERT_TRUE(delegate->completed()); 252 ASSERT_TRUE(delegate->completed());
248 EXPECT_EQ(delegate->response().HttpStatusCode(), 200); 253 EXPECT_EQ(delegate->response().HttpStatusCode(), 200);
249 EXPECT_EQ(kBody, delegate->data()); 254 EXPECT_EQ(kBody, delegate->data());
250 } 255 }
251 256
252 void ResourceFetcherSetHeader(const GURL& url) { 257 void ResourceFetcherSetHeader(const GURL& url) {
253 const char* kHeader = "Rather boring header."; 258 const char* kHeader = "Rather boring header.";
254 259
255 WebFrame* frame = GetRenderView()->GetWebView()->MainFrame(); 260 blink::WebLocalFrame* frame =
261 GetRenderView()->GetWebView()->MainFrame()->ToWebLocalFrame();
256 262
257 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); 263 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate);
258 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); 264 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
259 fetcher->SetHeader("header", kHeader); 265 fetcher->SetHeader("header", kHeader);
260 fetcher->Start(frame, WebURLRequest::kRequestContextInternal, 266 fetcher->Start(frame, WebURLRequest::kRequestContextInternal,
261 delegate->NewCallback()); 267 delegate->NewCallback());
262 268
263 delegate->WaitForResponse(); 269 delegate->WaitForResponse();
264 ASSERT_TRUE(delegate->completed()); 270 ASSERT_TRUE(delegate->completed());
265 EXPECT_EQ(delegate->response().HttpStatusCode(), 200); 271 EXPECT_EQ(delegate->response().HttpStatusCode(), 200);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 ASSERT_TRUE(embedded_test_server()->Start()); 367 ASSERT_TRUE(embedded_test_server()->Start());
362 GURL url(embedded_test_server()->GetURL("/echoheader?header")); 368 GURL url(embedded_test_server()->GetURL("/echoheader?header"));
363 369
364 PostTaskToInProcessRendererAndWait( 370 PostTaskToInProcessRendererAndWait(
365 base::Bind( 371 base::Bind(
366 &ResourceFetcherTests::ResourceFetcherSetHeader, 372 &ResourceFetcherTests::ResourceFetcherSetHeader,
367 base::Unretained(this), url)); 373 base::Unretained(this), url));
368 } 374 }
369 375
370 } // namespace content 376 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/mojo_context_state.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698