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

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc

Issue 2820393002: predictors: Use several hosts in resource_prefetch_predictor tests. (Closed)
Patch Set: Address comments.' Created 3 years, 8 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 | « no previous file | chrome/test/data/predictors/html_subresources.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm>
7 #include <set> 8 #include <set>
8 9
9 #include "base/command_line.h" 10 #include "base/command_line.h"
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
12 #include "base/test/histogram_tester.h" 13 #include "base/test/histogram_tester.h"
13 #include "chrome/browser/browsing_data/browsing_data_helper.h" 14 #include "chrome/browser/browsing_data/browsing_data_helper.h"
14 #include "chrome/browser/browsing_data/browsing_data_remover.h" 15 #include "chrome/browser/browsing_data/browsing_data_remover.h"
15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 16 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
16 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" 17 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
17 #include "chrome/browser/predictors/resource_prefetch_predictor.h" 18 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
18 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" 19 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
19 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" 20 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h"
20 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/in_process_browser_test.h" 25 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h" 26 #include "chrome/test/base/ui_test_utils.h"
26 #include "net/base/host_port_pair.h" 27 #include "net/base/host_port_pair.h"
28 #include "net/base/url_util.h"
27 #include "net/dns/mock_host_resolver.h" 29 #include "net/dns/mock_host_resolver.h"
28 #include "net/test/embedded_test_server/http_request.h" 30 #include "net/test/embedded_test_server/http_request.h"
29 #include "net/test/embedded_test_server/http_response.h" 31 #include "net/test/embedded_test_server/http_response.h"
32 #include "net/test/embedded_test_server/request_handler_util.h"
30 #include "testing/gmock/include/gmock/gmock.h" 33 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
32 35
33 namespace predictors { 36 namespace predictors {
34 37
35 namespace { 38 namespace {
36 39
40 const char kLocalHost[] = "127.0.0.1";
37 const char kFooHost[] = "foo.com"; 41 const char kFooHost[] = "foo.com";
38 const char kBarHost[] = "bar.com"; 42 const char kBarHost[] = "bar.com";
39 const char kBazHost[] = "baz.com"; 43 const char kBazHost[] = "baz.com";
40 44
41 const char kImageMime[] = "image/png"; 45 const char kImageMime[] = "image/png";
42 const char kStyleMime[] = "text/css"; 46 const char kStyleMime[] = "text/css";
43 const char kJavascriptMime[] = "application/javascript"; 47 const char kJavascriptMime[] = "application/javascript";
44 48
45 // Paths to resources handled by a custom request handler. They return empty 49 // Paths to resources handled by a custom request handler. They return empty
46 // responses with controllable response headers. 50 // responses with controllable response headers.
(...skipping 15 matching lines...) Expand all
62 const char kHtmlAppendChildPath[] = "/predictors/append_child.html"; 66 const char kHtmlAppendChildPath[] = "/predictors/append_child.html";
63 const char kScriptAppendChildPath[] = "/predictors/append_child.js"; 67 const char kScriptAppendChildPath[] = "/predictors/append_child.js";
64 const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html"; 68 const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html";
65 const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js"; 69 const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js";
66 const char kHtmlXHRPath[] = "/predictors/xhr.html"; 70 const char kHtmlXHRPath[] = "/predictors/xhr.html";
67 const char kScriptXHRPath[] = "/predictors/xhr.js"; 71 const char kScriptXHRPath[] = "/predictors/xhr.js";
68 const char kHtmlIframePath[] = "/predictors/html_iframe.html"; 72 const char kHtmlIframePath[] = "/predictors/html_iframe.html";
69 const char kHtmlJavascriptRedirectPath[] = 73 const char kHtmlJavascriptRedirectPath[] =
70 "/predictors/javascript_redirect.html"; 74 "/predictors/javascript_redirect.html";
71 75
76 // Host, path.
77 const char* kScript[2] = {kFooHost, kScriptPath};
78 const char* kImage[2] = {kBarHost, kImagePath};
79 const char* kFont[2] = {kFooHost, kFontPath};
80 const char* kStyle[2] = {kBazHost, kStylePath};
81
72 struct ResourceSummary { 82 struct ResourceSummary {
73 ResourceSummary() 83 ResourceSummary()
74 : version(0), 84 : version(0),
75 is_no_store(false), 85 is_no_store(false),
76 is_external(false), 86 is_external(false),
77 is_observable(true), 87 is_observable(true),
78 is_prohibited(false) {} 88 is_prohibited(false) {}
79 89
80 ResourcePrefetchPredictor::URLRequestSummary request; 90 ResourcePrefetchPredictor::URLRequestSummary request;
81 // Allows to update HTTP ETag. 91 // Allows to update HTTP ETag.
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); 321 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter);
312 std::string enabled_feature = base::StringPrintf( 322 std::string enabled_feature = base::StringPrintf(
313 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); 323 "%s<trial", kSpeculativeResourcePrefetchingFeatureName);
314 command_line->AppendSwitchASCII("enable-features", enabled_feature); 324 command_line->AppendSwitchASCII("enable-features", enabled_feature);
315 } 325 }
316 326
317 void SetUpOnMainThread() override { 327 void SetUpOnMainThread() override {
318 // Resolving all hosts to local allows us to have 328 // Resolving all hosts to local allows us to have
319 // cross domains navigations (matching url_visit_count_, etc). 329 // cross domains navigations (matching url_visit_count_, etc).
320 host_resolver()->AddRule("*", "127.0.0.1"); 330 host_resolver()->AddRule("*", "127.0.0.1");
321 embedded_test_server()->RegisterRequestHandler( 331
322 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, 332 https_server_ = base::MakeUnique<net::EmbeddedTestServer>(
323 base::Unretained(this))); 333 net::EmbeddedTestServer::TYPE_HTTPS);
324 embedded_test_server()->RegisterRequestHandler( 334
325 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, 335 for (auto* server : {embedded_test_server(), https_server()}) {
326 base::Unretained(this))); 336 server->AddDefaultHandlers(
327 embedded_test_server()->RegisterRequestMonitor(base::Bind( 337 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
328 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, 338 server->RegisterRequestHandler(base::Bind(
329 base::Unretained(this))); 339 &ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest,
330 ASSERT_TRUE(embedded_test_server()->Start()); 340 base::Unretained(this)));
341 server->RegisterRequestHandler(base::Bind(
342 &ResourcePrefetchPredictorBrowserTest::HandleResourceRequest,
343 base::Unretained(this)));
344 server->RegisterRequestMonitor(base::Bind(
345 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest,
346 base::Unretained(this)));
347 ASSERT_TRUE(server->Start());
348 }
349
331 predictor_ = 350 predictor_ =
332 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); 351 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile());
333 ASSERT_TRUE(predictor_); 352 ASSERT_TRUE(predictor_);
334 // URLs from the test server contain a port number. 353 // URLs from the test server contain a port number.
335 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(true); 354 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(true);
336 EnsurePredictorInitialized(); 355 EnsurePredictorInitialized();
337 histogram_tester_.reset(new base::HistogramTester()); 356 histogram_tester_.reset(new base::HistogramTester());
338 } 357 }
339 358
340 void TearDownOnMainThread() override { 359 void TearDownOnMainThread() override {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 BrowsingDataRemoverObserver observer(remover); 496 BrowsingDataRemoverObserver observer(remover);
478 remover->RemoveAndReply( 497 remover->RemoveAndReply(
479 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_CACHE, 498 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_CACHE,
480 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &observer); 499 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &observer);
481 observer.Wait(); 500 observer.Wait();
482 501
483 for (auto& kv : resources_) 502 for (auto& kv : resources_)
484 kv.second.request.was_cached = false; 503 kv.second.request.was_cached = false;
485 } 504 }
486 505
487 // Shortcut for convenience. 506 // Shortcuts for convenience.
488 GURL GetURL(const std::string& path) const { 507 GURL GetURL(const std::string& path) const {
489 return embedded_test_server()->GetURL(path); 508 return embedded_test_server()->GetURL(path);
490 } 509 }
491 510
492 void EnableHttpsServer() { 511 GURL GetURLWithHost(const std::string& host,
493 ASSERT_FALSE(https_server_); 512 const std::string& path,
494 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( 513 bool https = false) const {
495 net::EmbeddedTestServer::TYPE_HTTPS); 514 auto* server = https ? https_server() : embedded_test_server();
496 https_server()->AddDefaultHandlers( 515 return server->GetURL(host, path);
497 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
498 https_server()->RegisterRequestHandler(
499 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest,
500 base::Unretained(this)));
501 https_server()->RegisterRequestHandler(
502 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest,
503 base::Unretained(this)));
504 https_server()->RegisterRequestMonitor(base::Bind(
505 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest,
506 base::Unretained(this)));
507 ASSERT_TRUE(https_server()->Start());
508 } 516 }
509 517
510 // Returns the embedded test server working over HTTPS. Must be enabled by 518 // Only works if the resulting URL is served from a file.
511 // calling EnableHttpsServer() before use. 519 std::string GetPathWithReplacements(const std::string& path) const {
520 std::string port = base::StringPrintf("%d", embedded_test_server()->port());
521 std::string path_with_replacements;
522 net::test_server::GetFilePathWithReplacements(
523 path, {{"REPLACE_WITH_PORT", port}}, &path_with_replacements);
524 return path_with_replacements;
525 }
526
527 GURL GetPageURLWithReplacements(const std::string& host,
528 const std::string& path,
529 bool https = false) const {
530 std::string path_with_replacements = GetPathWithReplacements(path);
531 return GetURLWithHost(host, path_with_replacements, https);
532 }
533
534 // Returns the embedded test server working over HTTPS.
512 const net::EmbeddedTestServer* https_server() const { 535 const net::EmbeddedTestServer* https_server() const {
513 return https_server_.get(); 536 return https_server_.get();
514 } 537 }
515 538
516 net::EmbeddedTestServer* https_server() { return https_server_.get(); } 539 net::EmbeddedTestServer* https_server() { return https_server_.get(); }
517 540
518 size_t navigation_ids_history_size() const { 541 size_t navigation_ids_history_size() const {
519 return navigation_id_history_.size(); 542 return navigation_id_history_.size();
520 } 543 }
521 544
545 // Expects the resources from kHtmlSubresourcesPath.
546 std::vector<ResourceSummary*> AddResourcesFromSubresourceHtml() {
547 // These resources have default priorities that correspond to
548 // blink::TypeToPriority().
549 return {AddResource(GetURLWithHost(kImage[0], kImage[1]),
550 content::RESOURCE_TYPE_IMAGE, net::LOWEST),
551 AddResource(GetURLWithHost(kStyle[0], kStyle[1]),
552 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST),
553 AddResource(GetURLWithHost(kScript[0], kScript[1]),
554 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM),
555 AddResource(GetURLWithHost(kFont[0], kFont[1]),
556 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST)};
557 }
558
522 std::unique_ptr<base::HistogramTester> histogram_tester_; 559 std::unique_ptr<base::HistogramTester> histogram_tester_;
523 560
524 private: 561 private:
525 // ResourcePrefetchPredictor needs to be initialized before the navigation 562 // ResourcePrefetchPredictor needs to be initialized before the navigation
526 // happens otherwise this navigation will be ignored by predictor. 563 // happens otherwise this navigation will be ignored by predictor.
527 void EnsurePredictorInitialized() { 564 void EnsurePredictorInitialized() {
528 if (predictor_->initialization_state_ == 565 if (predictor_->initialization_state_ ==
529 ResourcePrefetchPredictor::INITIALIZED) { 566 ResourcePrefetchPredictor::INITIALIZED) {
530 return; 567 return;
531 } 568 }
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 std::string parameter = base::StringPrintf( 714 std::string parameter = base::StringPrintf(
678 "trial.group:%s/%s", kModeParamName, kPrefetchingMode); 715 "trial.group:%s/%s", kModeParamName, kPrefetchingMode);
679 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); 716 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter);
680 std::string enabled_feature = base::StringPrintf( 717 std::string enabled_feature = base::StringPrintf(
681 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); 718 "%s<trial", kSpeculativeResourcePrefetchingFeatureName);
682 command_line->AppendSwitchASCII("enable-features", enabled_feature); 719 command_line->AppendSwitchASCII("enable-features", enabled_feature);
683 } 720 }
684 }; 721 };
685 722
686 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { 723 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) {
687 // These resources have default priorities that correspond to 724 AddResourcesFromSubresourceHtml();
688 // blink::typeToPriority function. 725 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
689 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 726 TestLearningAndPrefetching(url);
690 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
691 net::HIGHEST);
692 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
693 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
694 net::HIGHEST);
695 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath));
696 727
697 // The local cache is cleared. 728 // The local cache is cleared.
698 histogram_tester_->ExpectBucketCount( 729 histogram_tester_->ExpectBucketCount(
699 internal::kResourcePrefetchPredictorPrefetchMissesCountCached, 0, 1); 730 internal::kResourcePrefetchPredictorPrefetchMissesCountCached, 0, 1);
700 histogram_tester_->ExpectBucketCount( 731 histogram_tester_->ExpectBucketCount(
701 internal::kResourcePrefetchPredictorPrefetchMissesCountNotCached, 0, 1); 732 internal::kResourcePrefetchPredictorPrefetchMissesCountNotCached, 0, 1);
702 histogram_tester_->ExpectBucketCount( 733 histogram_tester_->ExpectBucketCount(
703 internal::kResourcePrefetchPredictorPrefetchHitsCountCached, 0, 1); 734 internal::kResourcePrefetchPredictorPrefetchHitsCountCached, 0, 1);
704 histogram_tester_->ExpectBucketCount( 735 histogram_tester_->ExpectBucketCount(
705 internal::kResourcePrefetchPredictorPrefetchHitsCountNotCached, 4, 1); 736 internal::kResourcePrefetchPredictorPrefetchHitsCountNotCached, 4, 1);
706 737
707 histogram_tester_->ExpectBucketCount( 738 histogram_tester_->ExpectBucketCount(
708 internal::kResourcePrefetchPredictorPrefetchMissesSize, 0, 1); 739 internal::kResourcePrefetchPredictorPrefetchMissesSize, 0, 1);
709 // Each request is ~1k, see HandleResourceRequest() above. 740 // Each request is ~1k, see HandleResourceRequest() above.
710 histogram_tester_->ExpectBucketCount( 741 histogram_tester_->ExpectBucketCount(
711 internal::kResourcePrefetchPredictorPrefetchHitsSize, 4, 1); 742 internal::kResourcePrefetchPredictorPrefetchHitsSize, 4, 1);
712 } 743 }
713 744
714 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) { 745 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) {
715 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); 746 GURL initial_url = GetURLWithHost(kFooHost, kRedirectPath);
716 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, 747 GURL redirected_url =
717 GetURL(kHtmlSubresourcesPath)}}); 748 GetPageURLWithReplacements(kBarHost, kHtmlSubresourcesPath);
718 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 749 AddRedirectChain(initial_url,
719 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, 750 {{net::HTTP_MOVED_PERMANENTLY, redirected_url}});
720 net::HIGHEST); 751 AddResourcesFromSubresourceHtml();
721 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
722 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
723 net::HIGHEST);
724 TestLearningAndPrefetching(initial_url); 752 TestLearningAndPrefetching(initial_url);
725 } 753 }
726 754
727 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, RedirectChain) { 755 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, RedirectChain) {
728 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); 756 GURL initial_url = GetURLWithHost(kFooHost, kRedirectPath);
729 AddRedirectChain(initial_url, 757 GURL redirected_url =
730 {{net::HTTP_FOUND, 758 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
731 embedded_test_server()->GetURL(kBarHost, kRedirectPath2)}, 759 AddRedirectChain(
732 {net::HTTP_MOVED_PERMANENTLY, 760 initial_url,
733 embedded_test_server()->GetURL(kBazHost, kRedirectPath3)}, 761 {{net::HTTP_FOUND, GetURLWithHost(kBarHost, kRedirectPath2)},
734 {net::HTTP_FOUND, GetURL(kHtmlSubresourcesPath)}}); 762 {net::HTTP_MOVED_PERMANENTLY, GetURLWithHost(kBazHost, kRedirectPath3)},
735 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 763 {net::HTTP_FOUND, redirected_url}});
736 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, 764 AddResourcesFromSubresourceHtml();
737 net::HIGHEST);
738 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
739 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
740 net::HIGHEST);
741 TestLearningAndPrefetching(initial_url); 765 TestLearningAndPrefetching(initial_url);
742 } 766 }
743 767
744 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, 768 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
745 HttpToHttpsRedirect) { 769 HttpToHttpsRedirect) {
746 EnableHttpsServer(); 770 GURL initial_url = GetURLWithHost(kFooHost, kRedirectPath);
747 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); 771 GURL redirected_url =
772 GetPageURLWithReplacements(kLocalHost, kHtmlSubresourcesPath, true);
773
774 // Only one resource, as HTTP images in HTTPS pages are only a warning, not
775 // an error.
776 AddResource(GetURLWithHost(kImage[0], kImage[1]),
777 content::RESOURCE_TYPE_IMAGE, net::LOWEST);
778
748 AddRedirectChain(initial_url, 779 AddRedirectChain(initial_url,
749 {{net::HTTP_MOVED_PERMANENTLY, 780 {{net::HTTP_MOVED_PERMANENTLY, redirected_url}});
750 https_server()->GetURL(kHtmlSubresourcesPath)}});
751 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE,
752 net::LOWEST);
753 AddResource(https_server()->GetURL(kStylePath),
754 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST);
755 AddResource(https_server()->GetURL(kScriptPath),
756 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
757 AddResource(https_server()->GetURL(kFontPath),
758 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST);
759 TestLearningAndPrefetching(initial_url); 781 TestLearningAndPrefetching(initial_url);
760 } 782 }
761 783
762 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, 784 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
763 JavascriptDocumentWrite) { 785 JavascriptDocumentWrite) {
764 auto* externalScript = 786 auto* externalScript =
765 AddExternalResource(GetURL(kScriptDocumentWritePath), 787 AddExternalResource(GetURL(kScriptDocumentWritePath),
766 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); 788 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
767 externalScript->request.mime_type = kJavascriptMime; 789 externalScript->request.mime_type = kJavascriptMime;
768 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 790 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); 850 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
829 // Included from <iframe src="html_subresources.html"> and shouldn't be 851 // Included from <iframe src="html_subresources.html"> and shouldn't be
830 // observed. 852 // observed.
831 AddUnobservableResources({GetURL(kImagePath), GetURL(kStylePath), 853 AddUnobservableResources({GetURL(kImagePath), GetURL(kStylePath),
832 GetURL(kScriptPath), GetURL(kFontPath)}); 854 GetURL(kScriptPath), GetURL(kFontPath)});
833 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); 855 TestLearningAndPrefetching(GetURL(kHtmlIframePath));
834 } 856 }
835 857
836 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, 858 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
837 CrossSiteNavigation) { 859 CrossSiteNavigation) {
838 AddResource(embedded_test_server()->GetURL(kFooHost, kImagePath), 860 AddResourcesFromSubresourceHtml();
839 content::RESOURCE_TYPE_IMAGE, net::LOWEST); 861 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
840 AddResource(embedded_test_server()->GetURL(kFooHost, kStylePath), 862 TestLearningAndPrefetching(url);
841 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); 863
842 AddResource(embedded_test_server()->GetURL(kFooHost, kScriptPath),
843 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
844 AddResource(embedded_test_server()->GetURL(kFooHost, kFontPath),
845 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST);
846 TestLearningAndPrefetching(
847 embedded_test_server()->GetURL(kFooHost, kHtmlSubresourcesPath));
848 ClearResources(); 864 ClearResources();
849 865 ClearCache();
850 AddResource(embedded_test_server()->GetURL(kBarHost, kImagePath), 866 AddResourcesFromSubresourceHtml();
851 content::RESOURCE_TYPE_IMAGE, net::LOWEST);
852 AddResource(embedded_test_server()->GetURL(kBarHost, kStylePath),
853 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST);
854 AddResource(embedded_test_server()->GetURL(kBarHost, kScriptPath),
855 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
856 AddResource(embedded_test_server()->GetURL(kBarHost, kFontPath),
857 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST);
858 // Navigating to kBarHost, although done in the same tab, will generate a new 867 // Navigating to kBarHost, although done in the same tab, will generate a new
859 // process. 868 // process.
860 TestLearningAndPrefetching( 869 url = GetPageURLWithReplacements(kBarHost, kHtmlSubresourcesPath);
861 embedded_test_server()->GetURL(kBarHost, kHtmlSubresourcesPath)); 870 TestLearningAndPrefetching(url);
862 } 871 }
863 872
864 // In this test we are trying to assess if : 873 // In this test we are trying to assess if :
865 // - Reloading in the same tab is using the same NavigationID. 874 // - Reloading in the same tab is using the same NavigationID.
866 // - Navigation into a new tab, window or popup yields different NavigationID's. 875 // - Navigation into a new tab, window or popup yields different NavigationID's.
867 // - Navigating twice to a new browser/popup yields different NavigationID's. 876 // - Navigating twice to a new browser/popup yields different NavigationID's.
868 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, 877 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
869 TabIdBehavingAsExpected) { 878 TabIdBehavingAsExpected) {
870 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 879 AddResourcesFromSubresourceHtml();
871 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, 880 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
872 net::HIGHEST); 881 NavigateToURLAndCheckSubresources(url);
873 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
874 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
875 net::HIGHEST);
876 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath));
877 EXPECT_EQ(navigation_ids_history_size(), 1U); 882 EXPECT_EQ(navigation_ids_history_size(), 1U);
878 ClearCache(); 883 ClearCache();
879 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); 884 NavigateToURLAndCheckSubresources(url);
880 EXPECT_EQ(navigation_ids_history_size(), 1U); 885 EXPECT_EQ(navigation_ids_history_size(), 1U);
881 ClearCache(); 886 ClearCache();
882 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), 887 NavigateToURLAndCheckSubresources(url,
883 WindowOpenDisposition::NEW_BACKGROUND_TAB); 888 WindowOpenDisposition::NEW_BACKGROUND_TAB);
884 EXPECT_EQ(navigation_ids_history_size(), 2U); 889 EXPECT_EQ(navigation_ids_history_size(), 2U);
885 ClearCache(); 890 ClearCache();
886 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), 891 NavigateToURLAndCheckSubresources(url, WindowOpenDisposition::NEW_WINDOW);
887 WindowOpenDisposition::NEW_WINDOW);
888 EXPECT_EQ(navigation_ids_history_size(), 3U); 892 EXPECT_EQ(navigation_ids_history_size(), 3U);
889 ClearCache(); 893 ClearCache();
890 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), 894 NavigateToURLAndCheckSubresources(url, WindowOpenDisposition::NEW_POPUP);
891 WindowOpenDisposition::NEW_POPUP);
892 EXPECT_EQ(navigation_ids_history_size(), 4U); 895 EXPECT_EQ(navigation_ids_history_size(), 4U);
893 } 896 }
894 897
895 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, AlwaysRevalidate) { 898 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, AlwaysRevalidate) {
896 std::vector<ResourceSummary*> resources = { 899 auto resources = AddResourcesFromSubresourceHtml();
897 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, 900 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
898 net::LOWEST),
899 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
900 net::HIGHEST),
901 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT,
902 net::MEDIUM),
903 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
904 net::HIGHEST),
905 };
906 for (auto* resource : resources) 901 for (auto* resource : resources)
907 resource->request.always_revalidate = true; 902 resource->request.always_revalidate = true;
908 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); 903 TestLearningAndPrefetching(url);
909 } 904 }
910 905
911 // Client-side redirects currently aren't tracked by ResourcePrefetchPredictor. 906 // Client-side redirects currently aren't tracked by ResourcePrefetchPredictor.
912 // A client-side redirect initiates a new navigation to the redirect destination 907 // A client-side redirect initiates a new navigation to the redirect destination
913 // URL and aborts the current navigation so that the OnLoad event is not fired. 908 // URL and aborts the current navigation so that the OnLoad event is not fired.
914 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, 909 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
915 JavascriptRedirectsAreNotHandled) { 910 JavascriptRedirectsAreNotHandled) {
916 std::string redirect_path_with_query = 911 GURL redirected_url =
917 std::string(kHtmlJavascriptRedirectPath) + "?url=" + 912 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
918 GetURL(kHtmlSubresourcesPath).spec(); 913 GURL initial_url = GetURLWithHost(kBarHost, kHtmlJavascriptRedirectPath);
919 GURL initial_url = 914 initial_url =
920 embedded_test_server()->GetURL(kBarHost, redirect_path_with_query); 915 net::AppendQueryParameter(initial_url, "url", redirected_url.spec());
921 AddRedirectChain(initial_url, {{net::HTTP_TEMPORARY_REDIRECT, 916
922 GetURL(kHtmlSubresourcesPath), true}}); 917 AddRedirectChain(initial_url,
923 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 918 {{net::HTTP_TEMPORARY_REDIRECT, redirected_url, true}});
924 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, 919 AddResourcesFromSubresourceHtml();
925 net::HIGHEST);
926 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
927 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
928 net::HIGHEST);
929 920
930 // Two navigations will occur. LearningObserver will get events only for the 921 // Two navigations will occur. LearningObserver will get events only for the
931 // second navigation because the first one will be aborted. 922 // second navigation because the first one will be aborted.
932 NavigateToURLAndCheckSubresources(initial_url); 923 NavigateToURLAndCheckSubresources(initial_url);
933 ClearCache(); 924 ClearCache();
934 // It is needed to have at least two resource hits to trigger prefetch. 925 // It is needed to have at least two resource hits to trigger prefetch.
935 NavigateToURLAndCheckSubresources(initial_url); 926 NavigateToURLAndCheckSubresources(initial_url);
936 ClearCache(); 927 ClearCache();
937 // Prefetching of |initial_url| has no effect because there is no entry in 928 // Prefetching of |initial_url| has no effect because there is no entry in
938 // the predictor database corresponding the client-side redirect. 929 // the predictor database corresponding the client-side redirect.
939 TryToPrefetchURL(initial_url); 930 TryToPrefetchURL(initial_url);
940 NavigateToURLAndCheckSubresources(initial_url); 931 NavigateToURLAndCheckSubresources(initial_url);
941 ClearCache(); 932 ClearCache();
942 // But the predictor database contains all subresources for the endpoint url 933 // But the predictor database contains all subresources for the endpoint url
943 // so this prefetch works. 934 // so this prefetch works.
944 PrefetchURL(GetURL(kHtmlSubresourcesPath)); 935 PrefetchURL(redirected_url);
945 NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath)); 936 NavigateToURLAndCheckSubresourcesAllCached(redirected_url);
946 } 937 }
947 938
948 // Makes sure that {Stop,Start}Prefetching are called with the same argument. 939 // Makes sure that {Stop,Start}Prefetching are called with the same argument.
949 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, 940 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest,
950 Simple) { 941 Simple) {
951 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 942 AddResourcesFromSubresourceHtml();
952 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
953 net::HIGHEST);
954 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
955 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
956 net::HIGHEST);
957 943
958 GURL main_frame_url = GetURL(kHtmlSubresourcesPath); 944 GURL main_frame_url =
945 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath);
959 NavigateToURLAndCheckSubresources(main_frame_url); 946 NavigateToURLAndCheckSubresources(main_frame_url);
960 ClearCache(); 947 ClearCache();
961 NavigateToURLAndCheckSubresources(main_frame_url); 948 NavigateToURLAndCheckSubresources(main_frame_url);
962 ClearCache(); 949 ClearCache();
963 NavigateToURLAndCheckPrefetching(main_frame_url); 950 NavigateToURLAndCheckPrefetching(main_frame_url);
964 } 951 }
965 952
966 // Makes sure that {Stop,Start}Prefetching are called with the same argument in 953 // Makes sure that {Stop,Start}Prefetching are called with the same argument in
967 // presence of redirect. 954 // presence of redirect.
968 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, 955 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest,
969 Redirect) { 956 Redirect) {
970 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); 957 GURL initial_url = GetURLWithHost(kFooHost, kRedirectPath);
971 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, 958 GURL redirected_url =
972 GetURL(kHtmlSubresourcesPath)}}); 959 GetPageURLWithReplacements(kBarHost, kHtmlSubresourcesPath);
973 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 960 AddRedirectChain(initial_url,
974 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, 961 {{net::HTTP_MOVED_PERMANENTLY, redirected_url}});
975 net::HIGHEST); 962 AddResourcesFromSubresourceHtml();
976 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
977 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
978 net::HIGHEST);
979 963
980 NavigateToURLAndCheckSubresources(initial_url); 964 NavigateToURLAndCheckSubresources(initial_url);
981 ClearCache(); 965 ClearCache();
982 NavigateToURLAndCheckSubresources(initial_url); 966 NavigateToURLAndCheckSubresources(initial_url);
983 ClearCache(); 967 ClearCache();
984 NavigateToURLAndCheckPrefetching(initial_url); 968 NavigateToURLAndCheckPrefetching(initial_url);
985 } 969 }
986 970
987 } // namespace predictors 971 } // namespace predictors
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/predictors/html_subresources.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698