OLD | NEW |
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 <set> | 5 #include <set> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 *child_id = child_id_; | 54 *child_id = child_id_; |
55 return true; | 55 return true; |
56 } | 56 } |
57 | 57 |
58 virtual bool GetRouteId(int* route_id) const OVERRIDE { | 58 virtual bool GetRouteId(int* route_id) const OVERRIDE { |
59 *route_id = route_id_; | 59 *route_id = route_id_; |
60 return true; | 60 return true; |
61 } | 61 } |
62 | 62 |
63 void Start() { | 63 void Start() { |
64 AddObserver(prerender_manager()->prerender_tracker()); | |
65 prerendering_has_started_ = true; | 64 prerendering_has_started_ = true; |
66 NotifyPrerenderStart(); | 65 NotifyPrerenderStart(); |
67 } | 66 } |
68 | 67 |
69 void Cancel() { | 68 void Cancel() { |
70 Destroy(FINAL_STATUS_CANCELLED); | 69 Destroy(FINAL_STATUS_CANCELLED); |
71 } | 70 } |
72 | 71 |
73 void Use() { | 72 void Use() { |
74 SetFinalStatus(FINAL_STATUS_USED); | 73 SetFinalStatus(FINAL_STATUS_USED); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 205 |
207 private: | 206 private: |
208 base::MessageLoopForIO message_loop_; | 207 base::MessageLoopForIO message_loop_; |
209 content::TestBrowserThread ui_thread_; | 208 content::TestBrowserThread ui_thread_; |
210 content::TestBrowserThread io_thread_; | 209 content::TestBrowserThread io_thread_; |
211 | 210 |
212 TestPrerenderManager prerender_manager_; | 211 TestPrerenderManager prerender_manager_; |
213 TestPrerenderContents test_contents_; | 212 TestPrerenderContents test_contents_; |
214 }; | 213 }; |
215 | 214 |
216 TEST_F(PrerenderTrackerTest, IsPrerenderingOnIOThread) { | |
217 EXPECT_FALSE(prerender_tracker()->IsPrerenderingOnIOThread( | |
218 kDefaultChildId, kDefaultRouteId)); | |
219 | |
220 test_contents()->Start(); | |
221 // This calls AddPrerenderOnIOThreadTask(). | |
222 RunEvents(); | |
223 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | |
224 kDefaultChildId, kDefaultRouteId)); | |
225 | |
226 test_contents()->Cancel(); | |
227 // This calls RemovePrerenderOnIOThreadTask(). | |
228 RunEvents(); | |
229 EXPECT_FALSE(prerender_tracker()->IsPrerenderingOnIOThread( | |
230 kDefaultChildId, kDefaultRouteId)); | |
231 } | |
232 | |
233 // Checks that deferred redirects are throttled and resumed correctly. | 215 // Checks that deferred redirects are throttled and resumed correctly. |
234 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectResume) { | 216 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectResume) { |
235 const base::FilePath::CharType kRedirectPath[] = | 217 const base::FilePath::CharType kRedirectPath[] = |
236 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 218 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
237 | 219 |
238 test_contents()->Start(); | 220 test_contents()->Start(); |
239 // This calls AddPrerenderOnIOThreadTask(). | |
240 RunEvents(); | 221 RunEvents(); |
241 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | |
242 kDefaultChildId, kDefaultRouteId)); | |
243 | 222 |
244 // Fake a request. | 223 // Fake a request. |
245 net::TestURLRequestContext url_request_context; | 224 net::TestURLRequestContext url_request_context; |
246 DeferredRedirectDelegate delegate; | 225 DeferredRedirectDelegate delegate; |
247 net::URLRequest request( | 226 net::URLRequest request( |
248 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 227 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
249 net::DEFAULT_PRIORITY, | 228 net::DEFAULT_PRIORITY, |
250 &delegate, | 229 &delegate, |
251 &url_request_context); | 230 &url_request_context); |
252 content::ResourceRequestInfo::AllocateForTesting( | 231 content::ResourceRequestInfo::AllocateForTesting( |
253 &request, ResourceType::IMAGE, NULL, | 232 &request, ResourceType::IMAGE, NULL, |
254 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, true); | 233 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, true); |
255 | 234 |
256 // Install a prerender throttle. | 235 // Install a prerender throttle. |
257 PrerenderResourceThrottle throttle(&request, prerender_tracker()); | 236 PrerenderResourceThrottle throttle(&request); |
258 delegate.SetThrottle(&throttle); | 237 delegate.SetThrottle(&throttle); |
259 | 238 |
260 // Start the request and wait for a redirect. | 239 // Start the request and wait for a redirect. |
261 request.Start(); | 240 request.Start(); |
262 delegate.Run(); | 241 delegate.Run(); |
263 EXPECT_TRUE(delegate.was_deferred()); | 242 EXPECT_TRUE(delegate.was_deferred()); |
264 // This calls WillRedirectRequestOnUI(). | 243 // This calls WillRedirectRequestOnUI(). |
265 RunEvents(); | 244 RunEvents(); |
266 | 245 |
267 // Display the prerendered RenderView and wait for the throttle to | 246 // Display the prerendered RenderView and wait for the throttle to |
268 // notice. | 247 // notice. |
269 test_contents()->Use(); | 248 test_contents()->Use(); |
270 delegate.Run(); | 249 delegate.Run(); |
271 EXPECT_TRUE(delegate.resume_called()); | 250 EXPECT_TRUE(delegate.resume_called()); |
272 EXPECT_FALSE(delegate.cancel_called()); | 251 EXPECT_FALSE(delegate.cancel_called()); |
273 } | 252 } |
274 | 253 |
275 // Checks that deferred redirects are cancelled on prerender cancel. | |
276 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectCancel) { | |
277 const base::FilePath::CharType kRedirectPath[] = | |
278 FILE_PATH_LITERAL("prerender/image-deferred.png"); | |
279 | |
280 test_contents()->Start(); | |
281 // This calls AddPrerenderOnIOThreadTask(). | |
282 RunEvents(); | |
283 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | |
284 kDefaultChildId, kDefaultRouteId)); | |
285 | |
286 // Fake a request. | |
287 net::TestURLRequestContext url_request_context; | |
288 DeferredRedirectDelegate delegate; | |
289 net::URLRequest request( | |
290 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | |
291 net::DEFAULT_PRIORITY, | |
292 &delegate, | |
293 &url_request_context); | |
294 content::ResourceRequestInfo::AllocateForTesting( | |
295 &request, ResourceType::IMAGE, NULL, | |
296 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, true); | |
297 | |
298 // Install a prerender throttle. | |
299 PrerenderResourceThrottle throttle(&request, prerender_tracker()); | |
300 delegate.SetThrottle(&throttle); | |
301 | |
302 // Start the request and wait for a redirect. | |
303 request.Start(); | |
304 delegate.Run(); | |
305 EXPECT_TRUE(delegate.was_deferred()); | |
306 // This calls WillRedirectRequestOnUI(). | |
307 RunEvents(); | |
308 | |
309 // Display the prerendered RenderView and wait for the throttle to | |
310 // notice. | |
311 test_contents()->Cancel(); | |
312 delegate.Run(); | |
313 EXPECT_FALSE(delegate.resume_called()); | |
314 EXPECT_TRUE(delegate.cancel_called()); | |
315 } | |
316 | |
317 // Checks that redirects in main frame loads are not deferred. | 254 // Checks that redirects in main frame loads are not deferred. |
318 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectMainFrame) { | 255 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectMainFrame) { |
319 const base::FilePath::CharType kRedirectPath[] = | 256 const base::FilePath::CharType kRedirectPath[] = |
320 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 257 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
321 | 258 |
322 test_contents()->Start(); | 259 test_contents()->Start(); |
323 // This calls AddPrerenderOnIOThreadTask(). | |
324 RunEvents(); | 260 RunEvents(); |
325 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | |
326 kDefaultChildId, kDefaultRouteId)); | |
327 | 261 |
328 // Fake a request. | 262 // Fake a request. |
329 net::TestURLRequestContext url_request_context; | 263 net::TestURLRequestContext url_request_context; |
330 DeferredRedirectDelegate delegate; | 264 DeferredRedirectDelegate delegate; |
331 net::URLRequest request( | 265 net::URLRequest request( |
332 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 266 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
333 net::DEFAULT_PRIORITY, | 267 net::DEFAULT_PRIORITY, |
334 &delegate, | 268 &delegate, |
335 &url_request_context); | 269 &url_request_context); |
336 content::ResourceRequestInfo::AllocateForTesting( | 270 content::ResourceRequestInfo::AllocateForTesting( |
337 &request, ResourceType::MAIN_FRAME, NULL, | 271 &request, ResourceType::MAIN_FRAME, NULL, |
338 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, true); | 272 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, true); |
339 | 273 |
340 // Install a prerender throttle. | 274 // Install a prerender throttle. |
341 PrerenderResourceThrottle throttle(&request, prerender_tracker()); | 275 PrerenderResourceThrottle throttle(&request); |
342 delegate.SetThrottle(&throttle); | 276 delegate.SetThrottle(&throttle); |
343 | 277 |
344 // Start the request and wait for a redirect. This time, it should | 278 // Start the request and wait for a redirect. This time, it should |
345 // not be deferred. | 279 // not be deferred. |
346 request.Start(); | 280 request.Start(); |
347 delegate.Run(); | 281 delegate.Run(); |
348 // This calls WillRedirectRequestOnUI(). | 282 // This calls WillRedirectRequestOnUI(). |
349 RunEvents(); | 283 RunEvents(); |
350 | 284 |
351 // Cleanup work so the prerender is gone. | 285 // Cleanup work so the prerender is gone. |
352 test_contents()->Cancel(); | 286 test_contents()->Cancel(); |
353 RunEvents(); | 287 RunEvents(); |
354 } | 288 } |
355 | 289 |
356 // Checks that attempting to defer a synchronous request aborts the | 290 // Checks that attempting to defer a synchronous request aborts the |
357 // prerender. | 291 // prerender. |
358 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectSyncXHR) { | 292 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectSyncXHR) { |
359 const base::FilePath::CharType kRedirectPath[] = | 293 const base::FilePath::CharType kRedirectPath[] = |
360 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 294 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
361 | 295 |
362 test_contents()->Start(); | 296 test_contents()->Start(); |
363 // This calls AddPrerenderOnIOThreadTask(). | |
364 RunEvents(); | 297 RunEvents(); |
365 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | |
366 kDefaultChildId, kDefaultRouteId)); | |
367 | 298 |
368 // Fake a request. | 299 // Fake a request. |
369 net::TestURLRequestContext url_request_context; | 300 net::TestURLRequestContext url_request_context; |
370 DeferredRedirectDelegate delegate; | 301 DeferredRedirectDelegate delegate; |
371 net::URLRequest request( | 302 net::URLRequest request( |
372 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 303 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
373 net::DEFAULT_PRIORITY, | 304 net::DEFAULT_PRIORITY, |
374 &delegate, | 305 &delegate, |
375 &url_request_context); | 306 &url_request_context); |
376 content::ResourceRequestInfo::AllocateForTesting( | 307 content::ResourceRequestInfo::AllocateForTesting( |
377 &request, ResourceType::XHR, NULL, | 308 &request, ResourceType::XHR, NULL, |
378 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, false); | 309 kDefaultChildId, kDefaultRouteId, MSG_ROUTING_NONE, false); |
379 | 310 |
380 // Install a prerender throttle. | 311 // Install a prerender throttle. |
381 PrerenderResourceThrottle throttle(&request, prerender_tracker()); | 312 PrerenderResourceThrottle throttle(&request); |
382 delegate.SetThrottle(&throttle); | 313 delegate.SetThrottle(&throttle); |
383 | 314 |
384 // Start the request and wait for a redirect. | 315 // Start the request and wait for a redirect. |
385 request.Start(); | 316 request.Start(); |
386 delegate.Run(); | 317 delegate.Run(); |
387 // This calls WillRedirectRequestOnUI(). | 318 // This calls WillRedirectRequestOnUI(). |
388 RunEvents(); | 319 RunEvents(); |
389 | 320 |
390 // We should have cancelled the prerender. | 321 // We should have cancelled the prerender. |
391 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT, | 322 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT, |
392 test_contents()->final_status()); | 323 test_contents()->final_status()); |
393 | 324 |
394 // Cleanup work so the prerender is gone. | 325 // Cleanup work so the prerender is gone. |
395 test_contents()->Cancel(); | 326 test_contents()->Cancel(); |
396 RunEvents(); | 327 RunEvents(); |
397 } | 328 } |
398 | 329 |
399 } // namespace prerender | 330 } // namespace prerender |
OLD | NEW |