| 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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 197 |
| 198 TestPrerenderContents* test_contents() { | 198 TestPrerenderContents* test_contents() { |
| 199 return &test_contents_; | 199 return &test_contents_; |
| 200 } | 200 } |
| 201 | 201 |
| 202 // Runs any tasks queued on either thread. | 202 // Runs any tasks queued on either thread. |
| 203 void RunEvents() { | 203 void RunEvents() { |
| 204 message_loop_.RunUntilIdle(); | 204 message_loop_.RunUntilIdle(); |
| 205 } | 205 } |
| 206 | 206 |
| 207 bool IsPrerenderingOnIOThread(int render_process_id, int render_frame_id) { |
| 208 PrerenderTracker::ChildRouteIdPair render_frame_route_id_pair( |
| 209 render_process_id, render_frame_id); |
| 210 return prerender_tracker()->resource_throttle_io_thread_map_.count( |
| 211 render_frame_route_id_pair) > 0; |
| 212 } |
| 213 |
| 207 private: | 214 private: |
| 208 base::MessageLoopForIO message_loop_; | 215 base::MessageLoopForIO message_loop_; |
| 209 content::TestBrowserThread ui_thread_; | 216 content::TestBrowserThread ui_thread_; |
| 210 content::TestBrowserThread io_thread_; | 217 content::TestBrowserThread io_thread_; |
| 211 | 218 |
| 212 TestPrerenderManager prerender_manager_; | 219 TestPrerenderManager prerender_manager_; |
| 213 TestPrerenderContents test_contents_; | 220 TestPrerenderContents test_contents_; |
| 214 }; | 221 }; |
| 215 | 222 |
| 216 TEST_F(PrerenderTrackerTest, IsPrerenderingOnIOThread) { | 223 TEST_F(PrerenderTrackerTest, IsPrerenderingOnIOThread) { |
| 217 EXPECT_FALSE(prerender_tracker()->IsPrerenderingOnIOThread( | 224 EXPECT_FALSE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 218 kDefaultChildId, kDefaultRouteId)); | |
| 219 | 225 |
| 220 test_contents()->Start(); | 226 test_contents()->Start(); |
| 221 // This calls AddPrerenderOnIOThreadTask(). | 227 // This calls AddPrerenderOnIOThreadTask(). |
| 222 RunEvents(); | 228 RunEvents(); |
| 223 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | 229 EXPECT_TRUE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 224 kDefaultChildId, kDefaultRouteId)); | |
| 225 | 230 |
| 226 test_contents()->Cancel(); | 231 test_contents()->Cancel(); |
| 227 // This calls RemovePrerenderOnIOThreadTask(). | 232 // This calls RemovePrerenderOnIOThreadTask(). |
| 228 RunEvents(); | 233 RunEvents(); |
| 229 EXPECT_FALSE(prerender_tracker()->IsPrerenderingOnIOThread( | 234 EXPECT_FALSE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 230 kDefaultChildId, kDefaultRouteId)); | |
| 231 } | 235 } |
| 232 | 236 |
| 233 // Checks that deferred redirects are throttled and resumed correctly. | 237 // Checks that deferred redirects are throttled and resumed correctly. |
| 234 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectResume) { | 238 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectResume) { |
| 235 const base::FilePath::CharType kRedirectPath[] = | 239 const base::FilePath::CharType kRedirectPath[] = |
| 236 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 240 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
| 237 | 241 |
| 238 test_contents()->Start(); | 242 test_contents()->Start(); |
| 239 // This calls AddPrerenderOnIOThreadTask(). | 243 // This calls AddPrerenderOnIOThreadTask(). |
| 240 RunEvents(); | 244 RunEvents(); |
| 241 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | 245 EXPECT_TRUE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 242 kDefaultChildId, kDefaultRouteId)); | |
| 243 | 246 |
| 244 // Fake a request. | 247 // Fake a request. |
| 245 net::TestURLRequestContext url_request_context; | 248 net::TestURLRequestContext url_request_context; |
| 246 DeferredRedirectDelegate delegate; | 249 DeferredRedirectDelegate delegate; |
| 247 net::URLRequest request( | 250 net::URLRequest request( |
| 248 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 251 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
| 249 net::DEFAULT_PRIORITY, | 252 net::DEFAULT_PRIORITY, |
| 250 &delegate, | 253 &delegate, |
| 251 &url_request_context); | 254 &url_request_context); |
| 252 content::ResourceRequestInfo::AllocateForTesting( | 255 content::ResourceRequestInfo::AllocateForTesting( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 273 } | 276 } |
| 274 | 277 |
| 275 // Checks that deferred redirects are cancelled on prerender cancel. | 278 // Checks that deferred redirects are cancelled on prerender cancel. |
| 276 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectCancel) { | 279 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectCancel) { |
| 277 const base::FilePath::CharType kRedirectPath[] = | 280 const base::FilePath::CharType kRedirectPath[] = |
| 278 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 281 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
| 279 | 282 |
| 280 test_contents()->Start(); | 283 test_contents()->Start(); |
| 281 // This calls AddPrerenderOnIOThreadTask(). | 284 // This calls AddPrerenderOnIOThreadTask(). |
| 282 RunEvents(); | 285 RunEvents(); |
| 283 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | 286 EXPECT_TRUE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 284 kDefaultChildId, kDefaultRouteId)); | |
| 285 | 287 |
| 286 // Fake a request. | 288 // Fake a request. |
| 287 net::TestURLRequestContext url_request_context; | 289 net::TestURLRequestContext url_request_context; |
| 288 DeferredRedirectDelegate delegate; | 290 DeferredRedirectDelegate delegate; |
| 289 net::URLRequest request( | 291 net::URLRequest request( |
| 290 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 292 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
| 291 net::DEFAULT_PRIORITY, | 293 net::DEFAULT_PRIORITY, |
| 292 &delegate, | 294 &delegate, |
| 293 &url_request_context); | 295 &url_request_context); |
| 294 content::ResourceRequestInfo::AllocateForTesting( | 296 content::ResourceRequestInfo::AllocateForTesting( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 315 } | 317 } |
| 316 | 318 |
| 317 // Checks that redirects in main frame loads are not deferred. | 319 // Checks that redirects in main frame loads are not deferred. |
| 318 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectMainFrame) { | 320 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectMainFrame) { |
| 319 const base::FilePath::CharType kRedirectPath[] = | 321 const base::FilePath::CharType kRedirectPath[] = |
| 320 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 322 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
| 321 | 323 |
| 322 test_contents()->Start(); | 324 test_contents()->Start(); |
| 323 // This calls AddPrerenderOnIOThreadTask(). | 325 // This calls AddPrerenderOnIOThreadTask(). |
| 324 RunEvents(); | 326 RunEvents(); |
| 325 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | 327 EXPECT_TRUE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 326 kDefaultChildId, kDefaultRouteId)); | |
| 327 | 328 |
| 328 // Fake a request. | 329 // Fake a request. |
| 329 net::TestURLRequestContext url_request_context; | 330 net::TestURLRequestContext url_request_context; |
| 330 DeferredRedirectDelegate delegate; | 331 DeferredRedirectDelegate delegate; |
| 331 net::URLRequest request( | 332 net::URLRequest request( |
| 332 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 333 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
| 333 net::DEFAULT_PRIORITY, | 334 net::DEFAULT_PRIORITY, |
| 334 &delegate, | 335 &delegate, |
| 335 &url_request_context); | 336 &url_request_context); |
| 336 content::ResourceRequestInfo::AllocateForTesting( | 337 content::ResourceRequestInfo::AllocateForTesting( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 355 | 356 |
| 356 // Checks that attempting to defer a synchronous request aborts the | 357 // Checks that attempting to defer a synchronous request aborts the |
| 357 // prerender. | 358 // prerender. |
| 358 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectSyncXHR) { | 359 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectSyncXHR) { |
| 359 const base::FilePath::CharType kRedirectPath[] = | 360 const base::FilePath::CharType kRedirectPath[] = |
| 360 FILE_PATH_LITERAL("prerender/image-deferred.png"); | 361 FILE_PATH_LITERAL("prerender/image-deferred.png"); |
| 361 | 362 |
| 362 test_contents()->Start(); | 363 test_contents()->Start(); |
| 363 // This calls AddPrerenderOnIOThreadTask(). | 364 // This calls AddPrerenderOnIOThreadTask(). |
| 364 RunEvents(); | 365 RunEvents(); |
| 365 EXPECT_TRUE(prerender_tracker()->IsPrerenderingOnIOThread( | 366 EXPECT_TRUE(IsPrerenderingOnIOThread(kDefaultChildId, kDefaultRouteId)); |
| 366 kDefaultChildId, kDefaultRouteId)); | |
| 367 | 367 |
| 368 // Fake a request. | 368 // Fake a request. |
| 369 net::TestURLRequestContext url_request_context; | 369 net::TestURLRequestContext url_request_context; |
| 370 DeferredRedirectDelegate delegate; | 370 DeferredRedirectDelegate delegate; |
| 371 net::URLRequest request( | 371 net::URLRequest request( |
| 372 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), | 372 content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)), |
| 373 net::DEFAULT_PRIORITY, | 373 net::DEFAULT_PRIORITY, |
| 374 &delegate, | 374 &delegate, |
| 375 &url_request_context); | 375 &url_request_context); |
| 376 content::ResourceRequestInfo::AllocateForTesting( | 376 content::ResourceRequestInfo::AllocateForTesting( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 390 // We should have cancelled the prerender. | 390 // We should have cancelled the prerender. |
| 391 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT, | 391 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT, |
| 392 test_contents()->final_status()); | 392 test_contents()->final_status()); |
| 393 | 393 |
| 394 // Cleanup work so the prerender is gone. | 394 // Cleanup work so the prerender is gone. |
| 395 test_contents()->Cancel(); | 395 test_contents()->Cancel(); |
| 396 RunEvents(); | 396 RunEvents(); |
| 397 } | 397 } |
| 398 | 398 |
| 399 } // namespace prerender | 399 } // namespace prerender |
| OLD | NEW |