| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/memory/ref_counted.h" |
| 6 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 7 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/task_scheduler/post_task.h" | 12 #include "base/task_scheduler/post_task.h" |
| 13 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
| 11 #include "chrome/browser/history/history_test_utils.h" | 15 #include "chrome/browser/history/history_test_utils.h" |
| 12 #include "chrome/browser/prerender/prerender_handle.h" | 16 #include "chrome/browser/prerender/prerender_handle.h" |
| 13 #include "chrome/browser/prerender/prerender_manager.h" | 17 #include "chrome/browser/prerender/prerender_manager.h" |
| 14 #include "chrome/browser/prerender/prerender_manager_factory.h" | 18 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 15 #include "chrome/browser/prerender/prerender_test_utils.h" | 19 #include "chrome/browser/prerender/prerender_test_utils.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 21 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
| 18 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_commands.h" | 23 #include "chrome/browser/ui/browser_commands.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/grit/generated_resources.h" | 26 #include "chrome/grit/generated_resources.h" |
| 23 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 28 #include "content/public/browser/appcache_service.h" |
| 24 #include "content/public/browser/render_process_host.h" | 29 #include "content/public/browser/render_process_host.h" |
| 30 #include "content/public/browser/storage_partition.h" |
| 25 #include "content/public/common/result_codes.h" | 31 #include "content/public/common/result_codes.h" |
| 26 #include "content/public/common/url_constants.h" | 32 #include "content/public/common/url_constants.h" |
| 27 #include "content/public/test/browser_test_utils.h" | 33 #include "content/public/test/browser_test_utils.h" |
| 28 #include "net/base/escape.h" | 34 #include "net/base/escape.h" |
| 29 #include "net/base/load_flags.h" | 35 #include "net/base/load_flags.h" |
| 30 #include "net/dns/mock_host_resolver.h" | 36 #include "net/dns/mock_host_resolver.h" |
| 31 #include "net/test/embedded_test_server/request_handler_util.h" | 37 #include "net/test/embedded_test_server/request_handler_util.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 34 | 40 |
| 35 using prerender::test_utils::DestructionWaiter; | 41 using prerender::test_utils::DestructionWaiter; |
| 36 using prerender::test_utils::RequestCounter; | 42 using prerender::test_utils::RequestCounter; |
| 37 using prerender::test_utils::TestPrerender; | 43 using prerender::test_utils::TestPrerender; |
| 38 | 44 |
| 39 namespace prerender { | 45 namespace prerender { |
| 40 | 46 |
| 41 // These URLs used for test resources must be relative with the exception of | 47 // These URLs used for test resources must be relative with the exception of |
| 42 // |kPrefetchLoaderPath|. | 48 // |kPrefetchLoaderPath|. |
| 49 const char kPrefetchAppcache[] = "prerender/prefetch_appcache.html"; |
| 50 const char kPrefetchAppcacheManifest[] = "prerender/appcache.manifest"; |
| 43 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; | 51 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; |
| 44 const char kPrefetchJpeg[] = "prerender/image.jpeg"; | 52 const char kPrefetchJpeg[] = "prerender/image.jpeg"; |
| 45 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; | 53 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; |
| 46 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; | 54 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; |
| 47 const char kPrefetchMetaCSP[] = "prerender/prefetch_meta_csp.html"; | 55 const char kPrefetchMetaCSP[] = "prerender/prefetch_meta_csp.html"; |
| 48 const char kPrefetchPage[] = "prerender/prefetch_page.html"; | 56 const char kPrefetchPage[] = "prerender/prefetch_page.html"; |
| 49 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; | 57 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; |
| 50 const char kPrefetchPng[] = "prerender/image.png"; | 58 const char kPrefetchPng[] = "prerender/image.png"; |
| 51 const char kPrefetchResponseHeaderCSP[] = | 59 const char kPrefetchResponseHeaderCSP[] = |
| 52 "prerender/prefetch_response_csp.html"; | 60 "prerender/prefetch_response_csp.html"; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 82 const std::string& path_str, | 90 const std::string& path_str, |
| 83 RequestCounter* counter) { | 91 RequestCounter* counter) { |
| 84 base::FilePath url_file = ui_test_utils::GetTestFilePath( | 92 base::FilePath url_file = ui_test_utils::GetTestFilePath( |
| 85 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); | 93 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); |
| 86 content::BrowserThread::PostTask( | 94 content::BrowserThread::PostTask( |
| 87 content::BrowserThread::IO, FROM_HERE, | 95 content::BrowserThread::IO, FROM_HERE, |
| 88 base::Bind(&prerender::test_utils::CreateCountingInterceptorOnIO, url, | 96 base::Bind(&prerender::test_utils::CreateCountingInterceptorOnIO, url, |
| 89 url_file, counter->AsWeakPtr())); | 97 url_file, counter->AsWeakPtr())); |
| 90 } | 98 } |
| 91 | 99 |
| 100 base::SimpleTestTickClock* OverridePrerenderManagerTimeTicks() { |
| 101 auto clock = base::MakeUnique<base::SimpleTestTickClock>(); |
| 102 auto* clock_ptr = clock.get(); |
| 103 // The default zero time causes the prerender manager to do strange things. |
| 104 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 105 GetPrerenderManager()->SetTickClockForTesting(std::move(clock)); |
| 106 return clock_ptr; |
| 107 } |
| 108 |
| 109 // Block until an AppCache exists for |manifest_url|. |
| 110 void WaitForAppcache(const GURL& manifest_url) { |
| 111 bool found_manifest = false; |
| 112 content::AppCacheService* appcache_service = |
| 113 content::BrowserContext::GetDefaultStoragePartition( |
| 114 current_browser()->profile()) |
| 115 ->GetAppCacheService(); |
| 116 do { |
| 117 base::RunLoop wait_loop; |
| 118 content::BrowserThread::PostTask( |
| 119 content::BrowserThread::IO, FROM_HERE, |
| 120 base::Bind(WaitForAppcacheOnIO, manifest_url, appcache_service, |
| 121 &wait_loop, &found_manifest)); |
| 122 wait_loop.Run(); |
| 123 } while (!found_manifest); |
| 124 } |
| 125 |
| 92 protected: | 126 protected: |
| 93 std::unique_ptr<TestPrerender> PrefetchFromURL( | 127 std::unique_ptr<TestPrerender> PrefetchFromURL( |
| 94 const GURL& target_url, | 128 const GURL& target_url, |
| 95 FinalStatus expected_final_status) { | 129 FinalStatus expected_final_status) { |
| 96 GURL loader_url = ServeLoaderURL( | 130 GURL loader_url = ServeLoaderURL( |
| 97 kPrefetchLoaderPath, "REPLACE_WITH_PREFETCH_URL", target_url, ""); | 131 kPrefetchLoaderPath, "REPLACE_WITH_PREFETCH_URL", target_url, ""); |
| 98 std::vector<FinalStatus> expected_final_status_queue(1, | 132 std::vector<FinalStatus> expected_final_status_queue(1, |
| 99 expected_final_status); | 133 expected_final_status); |
| 100 std::vector<std::unique_ptr<TestPrerender>> prerenders = | 134 std::vector<std::unique_ptr<TestPrerender>> prerenders = |
| 101 NavigateWithPrerenders(loader_url, expected_final_status_queue); | 135 NavigateWithPrerenders(loader_url, expected_final_status_queue); |
| 102 prerenders[0]->WaitForStop(); | 136 prerenders[0]->WaitForStop(); |
| 103 return std::move(prerenders[0]); | 137 return std::move(prerenders[0]); |
| 104 } | 138 } |
| 105 | 139 |
| 106 std::unique_ptr<TestPrerender> PrefetchFromFile( | 140 std::unique_ptr<TestPrerender> PrefetchFromFile( |
| 107 const std::string& html_file, | 141 const std::string& html_file, |
| 108 FinalStatus expected_final_status) { | 142 FinalStatus expected_final_status) { |
| 109 return PrefetchFromURL(src_server()->GetURL(MakeAbsolute(html_file)), | 143 return PrefetchFromURL(src_server()->GetURL(MakeAbsolute(html_file)), |
| 110 expected_final_status); | 144 expected_final_status); |
| 111 } | 145 } |
| 112 | 146 |
| 113 private: | 147 private: |
| 148 // Schedule a task to retrieve AppCacheInfo from |appcache_service|. This sets |
| 149 // |found_manifest| if an appcache exists for |manifest_url|. |loop| must be |
| 150 // on the UI thread, and is signaled to quit after checking for info whether |
| 151 // or not the manifest is found. |
| 152 static void WaitForAppcacheOnIO(const GURL& manifest_url, |
| 153 content::AppCacheService* appcache_service, |
| 154 base::RunLoop* loop, |
| 155 bool* found_manifest) { |
| 156 scoped_refptr<content::AppCacheInfoCollection> info_collection = |
| 157 new content::AppCacheInfoCollection(); |
| 158 appcache_service->GetAllAppCacheInfo( |
| 159 info_collection.get(), |
| 160 base::Bind(ProcessAppCacheInfo, manifest_url, loop, found_manifest, |
| 161 info_collection)); |
| 162 } |
| 163 |
| 164 // Look through |info_collection| for an entry matching |target_manifest|, |
| 165 // setting |found_manifest| appropriately. Then |loop|, which must be on the |
| 166 // UI thread, is signaled to quit. |
| 167 static void ProcessAppCacheInfo( |
| 168 const GURL& target_manifest, |
| 169 base::RunLoop* loop, |
| 170 bool* found_manifest, |
| 171 scoped_refptr<content::AppCacheInfoCollection> info_collection, |
| 172 int status) { |
| 173 if (status == net::OK) { |
| 174 for (const auto& origin_pair : info_collection->infos_by_origin) { |
| 175 for (const auto& info : origin_pair.second) { |
| 176 if (info.manifest_url == target_manifest) { |
| 177 *found_manifest = true; |
| 178 break; |
| 179 } |
| 180 } |
| 181 } |
| 182 } |
| 183 content::BrowserThread::PostTask( |
| 184 content::BrowserThread::UI, FROM_HERE, |
| 185 base::Bind([](base::RunLoop* loop) { loop->Quit(); }, loop)); |
| 186 } |
| 187 |
| 114 DISALLOW_COPY_AND_ASSIGN(NoStatePrefetchBrowserTest); | 188 DISALLOW_COPY_AND_ASSIGN(NoStatePrefetchBrowserTest); |
| 115 }; | 189 }; |
| 116 | 190 |
| 117 // Checks that a page is correctly prefetched in the case of a | 191 // Checks that a page is correctly prefetched in the case of a |
| 118 // <link rel=prerender> tag and the JavaScript on the page is not executed. | 192 // <link rel=prerender> tag and the JavaScript on the page is not executed. |
| 119 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchSimple) { | 193 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchSimple) { |
| 120 RequestCounter main_counter; | 194 RequestCounter main_counter; |
| 121 CountRequestFor(kPrefetchPage, &main_counter); | 195 CountRequestFor(kPrefetchPage, &main_counter); |
| 122 RequestCounter script_counter; | 196 RequestCounter script_counter; |
| 123 CountRequestFor(kPrefetchScript, &script_counter); | 197 CountRequestFor(kPrefetchScript, &script_counter); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 "Prerender.websame_PrefetchTTFCP.Warm.Cacheable.Visible", 1); | 252 "Prerender.websame_PrefetchTTFCP.Warm.Cacheable.Visible", 1); |
| 179 histogram_tester().ExpectTotalCount( | 253 histogram_tester().ExpectTotalCount( |
| 180 "Prerender.websame_NoStatePrefetchResponseTypes", 2); | 254 "Prerender.websame_NoStatePrefetchResponseTypes", 2); |
| 181 histogram_tester().ExpectTotalCount("Prerender.websame_PrefetchAge", 1); | 255 histogram_tester().ExpectTotalCount("Prerender.websame_PrefetchAge", 1); |
| 182 } | 256 } |
| 183 | 257 |
| 184 // Checks the prefetch of an img tag. | 258 // Checks the prefetch of an img tag. |
| 185 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchImage) { | 259 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchImage) { |
| 186 RequestCounter image_counter; | 260 RequestCounter image_counter; |
| 187 CountRequestFor(kPrefetchJpeg, &image_counter); | 261 CountRequestFor(kPrefetchJpeg, &image_counter); |
| 188 base::StringPairs replacement_text; | 262 GURL main_page_url = |
| 189 replacement_text.push_back( | 263 GetURLWithReplacement(kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", |
| 190 std::make_pair("REPLACE_WITH_IMAGE_URL", MakeAbsolute(kPrefetchJpeg))); | 264 MakeAbsolute(kPrefetchJpeg)); |
| 191 std::string main_page_path; | 265 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 192 net::test_server::GetFilePathWithReplacements( | |
| 193 kPrefetchImagePage, replacement_text, &main_page_path); | |
| 194 // Note CountRequestFor cannot be used on the main page as the test server | |
| 195 // must handling the image url replacement. | |
| 196 PrefetchFromFile(main_page_path, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | |
| 197 image_counter.WaitForCount(1); | 266 image_counter.WaitForCount(1); |
| 198 } | 267 } |
| 199 | 268 |
| 200 // Checks that a cross-domain prefetching works correctly. | 269 // Checks that a cross-domain prefetching works correctly. |
| 201 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { | 270 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { |
| 202 static const std::string secondary_domain = "www.foo.com"; | 271 static const std::string secondary_domain = "www.foo.com"; |
| 203 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 272 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 204 GURL cross_domain_url(base::StringPrintf( | 273 GURL cross_domain_url(base::StringPrintf( |
| 205 "http://%s:%d/%s", secondary_domain.c_str(), | 274 "http://%s:%d/%s", secondary_domain.c_str(), |
| 206 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); | 275 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, SSLSubresourceError) { | 467 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, SSLSubresourceError) { |
| 399 // First confirm that the image loads as expected. | 468 // First confirm that the image loads as expected. |
| 400 | 469 |
| 401 // A separate HTTPS server is started for the subresource; src_server() is | 470 // A separate HTTPS server is started for the subresource; src_server() is |
| 402 // non-HTTPS. | 471 // non-HTTPS. |
| 403 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); | 472 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 404 https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); | 473 https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 405 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); | 474 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
| 406 ASSERT_TRUE(https_server.Start()); | 475 ASSERT_TRUE(https_server.Start()); |
| 407 GURL https_url = https_server.GetURL("/prerender/image.jpeg"); | 476 GURL https_url = https_server.GetURL("/prerender/image.jpeg"); |
| 408 base::StringPairs replacement_text; | 477 GURL main_page_url = GetURLWithReplacement( |
| 409 replacement_text.push_back( | 478 kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", https_url.spec()); |
| 410 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); | |
| 411 std::string main_page_path; | |
| 412 net::test_server::GetFilePathWithReplacements( | |
| 413 kPrefetchImagePage, replacement_text, &main_page_path); | |
| 414 RequestCounter script_counter; | 479 RequestCounter script_counter; |
| 415 CountRequestFor(kPrefetchScript, &script_counter); | 480 CountRequestFor(kPrefetchScript, &script_counter); |
| 416 | 481 |
| 417 std::unique_ptr<TestPrerender> prerender = | 482 std::unique_ptr<TestPrerender> prerender = |
| 418 PrefetchFromFile(main_page_path, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 483 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 419 // Checks that the presumed failure of the image load didn't affect the script | 484 // Checks that the presumed failure of the image load didn't affect the script |
| 420 // fetch. This assumes waiting for the script load is enough to see any error | 485 // fetch. This assumes waiting for the script load is enough to see any error |
| 421 // from the image load. | 486 // from the image load. |
| 422 script_counter.WaitForCount(1); | 487 script_counter.WaitForCount(1); |
| 423 } | 488 } |
| 424 | 489 |
| 425 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Loop) { | 490 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Loop) { |
| 426 RequestCounter script_counter; | 491 RequestCounter script_counter; |
| 427 CountRequestFor(kPrefetchScript, &script_counter); | 492 CountRequestFor(kPrefetchScript, &script_counter); |
| 428 RequestCounter main_counter; | 493 RequestCounter main_counter; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 | 661 |
| 597 // The SW intercepts kPrefetchPage and replaces it with a body that contains | 662 // The SW intercepts kPrefetchPage and replaces it with a body that contains |
| 598 // an <img> tage for kPrefetchPng. This verifies that the SW ran correctly by | 663 // an <img> tage for kPrefetchPng. This verifies that the SW ran correctly by |
| 599 // observing the fetch of the image. | 664 // observing the fetch of the image. |
| 600 RequestCounter image_counter; | 665 RequestCounter image_counter; |
| 601 CountRequestFor(kPrefetchPng, &image_counter); | 666 CountRequestFor(kPrefetchPng, &image_counter); |
| 602 PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 667 PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 603 image_counter.WaitForCount(1); | 668 image_counter.WaitForCount(1); |
| 604 } | 669 } |
| 605 | 670 |
| 671 // Checks that prefetching happens if an appcache is mentioned in the html tag |
| 672 // but is uninitialized. |
| 673 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheHtmlUninitialized) { |
| 674 RequestCounter image_counter; |
| 675 CountRequestFor(kPrefetchPng, &image_counter); |
| 676 PrefetchFromFile(kPrefetchAppcache, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 677 image_counter.WaitForCount(1); |
| 678 } |
| 679 |
| 680 // Checks that prefetching does not if an initialized appcache is mentioned in |
| 681 // the html tag. |
| 682 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheHtmlInitialized) { |
| 683 base::TimeTicks current_time = GetPrerenderManager()->GetCurrentTimeTicks(); |
| 684 auto* clock = OverridePrerenderManagerTimeTicks(); |
| 685 // Some navigations have already occurred in test setup. In order to track |
| 686 // duplicate prefetches correctly the test clock needs to be beyond those |
| 687 // navigations. |
| 688 clock->SetNowTicks(current_time); |
| 689 clock->Advance(base::TimeDelta::FromSeconds(600)); |
| 690 |
| 691 // Fill manifest with the image url. The main resource will be cached |
| 692 // implicitly. |
| 693 GURL image_url = src_server()->GetURL(MakeAbsolute(kPrefetchPng)); |
| 694 GURL manifest_url = GetURLWithReplacement( |
| 695 kPrefetchAppcacheManifest, "REPLACE_WITH_URL", image_url.spec()); |
| 696 GURL appcache_page_url = GetURLWithReplacement( |
| 697 kPrefetchAppcache, "REPLACE_WITH_MANIFEST", manifest_url.spec()); |
| 698 |
| 699 // Load the page into the appcache. |
| 700 ui_test_utils::NavigateToURL(current_browser(), appcache_page_url); |
| 701 |
| 702 WaitForAppcache(manifest_url); |
| 703 |
| 704 // If a page is prefetch shortly after being loading, the prefetch is |
| 705 // canceled. Advancing the clock prevents the cancelation. |
| 706 clock->Advance(base::TimeDelta::FromSeconds(6000)); |
| 707 |
| 708 RequestCounter script_counter; |
| 709 CountRequestFor(kPrefetchScript, &script_counter); |
| 710 // While the prefetch stops when it sees the AppCache manifest, from the point |
| 711 // of view of the prerender manager the prefetch stops normally. |
| 712 PrefetchFromURL(appcache_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 713 |
| 714 // The prefetch should have been canceled before the script in |
| 715 // kPrefetchAppcache is loaded (note the script is not mentioned in the |
| 716 // manifest). |
| 717 script_counter.WaitForCount(0); |
| 718 } |
| 719 |
| 720 // If a page has been cached by another AppCache, the prefetch should be |
| 721 // canceled. |
| 722 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheRegistered) { |
| 723 base::TimeTicks current_time = GetPrerenderManager()->GetCurrentTimeTicks(); |
| 724 auto* clock = OverridePrerenderManagerTimeTicks(); |
| 725 // Some navigations have already occurred in test setup. In order to track |
| 726 // duplicate prefetches correctly the test clock needs to be beyond those |
| 727 // navigations. |
| 728 clock->SetNowTicks(current_time); |
| 729 clock->Advance(base::TimeDelta::FromSeconds(600)); |
| 730 |
| 731 // Fill manifest with kPrefetchPage so that it is cached without explicitly |
| 732 // listing a manifest. |
| 733 GURL prefetch_page_url = src_server()->GetURL(MakeAbsolute(kPrefetchPage)); |
| 734 GURL manifest_url = GetURLWithReplacement( |
| 735 kPrefetchAppcacheManifest, "REPLACE_WITH_URL", prefetch_page_url.spec()); |
| 736 |
| 737 GURL appcache_page_url = GetURLWithReplacement( |
| 738 kPrefetchAppcache, "REPLACE_WITH_MANIFEST", manifest_url.spec()); |
| 739 |
| 740 // Load the page into the appcache. |
| 741 ui_test_utils::NavigateToURL(current_browser(), appcache_page_url); |
| 742 // Load the prefetch page so it can be cached. |
| 743 ui_test_utils::NavigateToURL(current_browser(), prefetch_page_url); |
| 744 |
| 745 WaitForAppcache(manifest_url); |
| 746 |
| 747 // If a page is prefetch shortly after being loading, the prefetch is |
| 748 // canceled. Advancing the clock prevents the cancelation. |
| 749 clock->Advance(base::TimeDelta::FromSeconds(6000)); |
| 750 |
| 751 RequestCounter page_counter; |
| 752 CountRequestFor(kPrefetchPage, &page_counter); |
| 753 RequestCounter script_counter; |
| 754 CountRequestFor(kPrefetchScript, &script_counter); |
| 755 PrefetchFromURL(prefetch_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 756 // Neither the page nor the script should be prefetched. |
| 757 script_counter.WaitForCount(0); |
| 758 page_counter.WaitForCount(0); |
| 759 } |
| 760 |
| 606 } // namespace prerender | 761 } // namespace prerender |
| OLD | NEW |