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

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

Issue 2896713003: Create LoadingDataCollector class and have observers rely on it instead of ResourcePrefetchPredictor (Closed)
Patch Set: Tests + rebase Created 3 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/predictors/resource_prefetch_predictor.h" 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
6 6
7 #include <iostream> 7 #include <iostream>
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 29 matching lines...) Expand all
40 40
41 namespace predictors { 41 namespace predictors {
42 42
43 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; 43 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
44 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; 44 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
45 using PrefetchDataMap = std::map<std::string, PrefetchData>; 45 using PrefetchDataMap = std::map<std::string, PrefetchData>;
46 using RedirectDataMap = std::map<std::string, RedirectData>; 46 using RedirectDataMap = std::map<std::string, RedirectData>;
47 using OriginDataMap = std::map<std::string, OriginData>; 47 using OriginDataMap = std::map<std::string, OriginData>;
48 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>; 48 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>;
49 49
50 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders(
51 const char* headers) {
52 return make_scoped_refptr(new net::HttpResponseHeaders(
53 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
54 }
55
56 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { 50 class EmptyURLRequestDelegate : public net::URLRequest::Delegate {
Benoit L 2017/06/02 12:44:00 This class is now duplicated, can you move it to a
trevordixon 2017/06/06 13:08:04 Done.
57 void OnResponseStarted(net::URLRequest* request, int net_error) override {} 51 void OnResponseStarted(net::URLRequest* request, int net_error) override {}
58 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} 52 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {}
59 }; 53 };
60 54
61 class MockURLRequestJob : public net::URLRequestJob {
62 public:
63 MockURLRequestJob(net::URLRequest* request,
64 const net::HttpResponseInfo& response_info,
65 const std::string& mime_type)
66 : net::URLRequestJob(request, nullptr),
67 response_info_(response_info),
68 mime_type_(mime_type) {}
69
70 bool GetMimeType(std::string* mime_type) const override {
71 *mime_type = mime_type_;
72 return true;
73 }
74
75 protected:
76 void Start() override { NotifyHeadersComplete(); }
77 void GetResponseInfo(net::HttpResponseInfo* info) override {
78 *info = response_info_;
79 }
80
81 private:
82 net::HttpResponseInfo response_info_;
83 std::string mime_type_;
84 };
85
86 class MockURLRequestJobFactory : public net::URLRequestJobFactory {
87 public:
88 MockURLRequestJobFactory() {}
89 ~MockURLRequestJobFactory() override {}
90
91 net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
92 const std::string& scheme,
93 net::URLRequest* request,
94 net::NetworkDelegate* network_delegate) const override {
95 return new MockURLRequestJob(request, response_info_, mime_type_);
96 }
97
98 net::URLRequestJob* MaybeInterceptRedirect(
99 net::URLRequest* request,
100 net::NetworkDelegate* network_delegate,
101 const GURL& location) const override {
102 return nullptr;
103 }
104
105 net::URLRequestJob* MaybeInterceptResponse(
106 net::URLRequest* request,
107 net::NetworkDelegate* network_delegate) const override {
108 return nullptr;
109 }
110
111 bool IsHandledProtocol(const std::string& scheme) const override {
112 return true;
113 }
114
115 bool IsSafeRedirectTarget(const GURL& location) const override {
116 return true;
117 }
118
119 void set_response_info(const net::HttpResponseInfo& response_info) {
120 response_info_ = response_info;
121 }
122
123 void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
124
125 private:
126 net::HttpResponseInfo response_info_;
127 std::string mime_type_;
128 };
129
130 template <typename T> 55 template <typename T>
131 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { 56 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> {
132 public: 57 public:
133 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} 58 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {}
134 void GetAllData(std::map<std::string, T>* data_map, 59 void GetAllData(std::map<std::string, T>* data_map,
135 sql::Connection* db) const override { 60 sql::Connection* db) const override {
136 *data_map = data_; 61 *data_map = data_;
137 } 62 }
138 void UpdateData(const std::string& key, 63 void UpdateData(const std::string& key,
139 const T& data, 64 const T& data,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 FakeGlowplugKeyValueTable<RedirectData> url_redirect_table_; 117 FakeGlowplugKeyValueTable<RedirectData> url_redirect_table_;
193 FakeGlowplugKeyValueTable<PrefetchData> host_resource_table_; 118 FakeGlowplugKeyValueTable<PrefetchData> host_resource_table_;
194 FakeGlowplugKeyValueTable<RedirectData> host_redirect_table_; 119 FakeGlowplugKeyValueTable<RedirectData> host_redirect_table_;
195 FakeGlowplugKeyValueTable<precache::PrecacheManifest> manifest_table_; 120 FakeGlowplugKeyValueTable<precache::PrecacheManifest> manifest_table_;
196 FakeGlowplugKeyValueTable<OriginData> origin_table_; 121 FakeGlowplugKeyValueTable<OriginData> origin_table_;
197 122
198 protected: 123 protected:
199 ~MockResourcePrefetchPredictorTables() override = default; 124 ~MockResourcePrefetchPredictorTables() override = default;
200 }; 125 };
201 126
202 class MockResourcePrefetchPredictorObserver : public TestObserver { 127 class MockLoadingPredictorObserver : public TestObserver {
203 public: 128 public:
204 explicit MockResourcePrefetchPredictorObserver( 129 explicit MockLoadingPredictorObserver(ResourcePrefetchPredictor* predictor)
205 ResourcePrefetchPredictor* predictor)
206 : TestObserver(predictor) {} 130 : TestObserver(predictor) {}
207 131
208 MOCK_METHOD2( 132 MOCK_METHOD2(
209 OnNavigationLearned, 133 OnNavigationLearned,
210 void(size_t url_visit_count, 134 void(size_t url_visit_count,
211 const ResourcePrefetchPredictor::PageRequestSummary& summary)); 135 const ResourcePrefetchPredictor::PageRequestSummary& summary));
212 }; 136 };
213 137
214 class ResourcePrefetchPredictorTest : public testing::Test { 138 class ResourcePrefetchPredictorTest : public testing::Test {
215 public: 139 public:
(...skipping 20 matching lines...) Expand all
236 URLRequestSummary CreateRedirectRequestSummary( 160 URLRequestSummary CreateRedirectRequestSummary(
237 SessionID::id_type session_id, 161 SessionID::id_type session_id,
238 const std::string& main_frame_url, 162 const std::string& main_frame_url,
239 const std::string& redirect_url) { 163 const std::string& redirect_url) {
240 URLRequestSummary summary = 164 URLRequestSummary summary =
241 CreateURLRequestSummary(session_id, main_frame_url); 165 CreateURLRequestSummary(session_id, main_frame_url);
242 summary.redirect_url = GURL(redirect_url); 166 summary.redirect_url = GURL(redirect_url);
243 return summary; 167 return summary;
244 } 168 }
245 169
246 std::unique_ptr<net::URLRequest> CreateURLRequest( 170 std::unique_ptr<net::URLRequest> CreateURLRequest(
Benoit L 2017/06/02 12:44:00 This is also duplicated in the tests, is it avoida
247 const GURL& url, 171 const GURL& url,
248 net::RequestPriority priority, 172 net::RequestPriority priority,
249 content::ResourceType resource_type, 173 content::ResourceType resource_type,
250 bool is_main_frame) { 174 bool is_main_frame) {
251 std::unique_ptr<net::URLRequest> request = 175 std::unique_ptr<net::URLRequest> request =
252 url_request_context_.CreateRequest(url, priority, 176 url_request_context_.CreateRequest(url, priority,
253 &url_request_delegate_, 177 &url_request_delegate_,
254 TRAFFIC_ANNOTATION_FOR_TESTS); 178 TRAFFIC_ANNOTATION_FOR_TESTS);
255 request->set_first_party_for_cookies(url); 179 request->set_first_party_for_cookies(url);
256 content::ResourceRequestInfo::AllocateForTesting( 180 content::ResourceRequestInfo::AllocateForTesting(
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 predictor_->RecordURLResponse(resource1); 535 predictor_->RecordURLResponse(resource1);
612 URLRequestSummary resource2 = CreateURLRequestSummary( 536 URLRequestSummary resource2 = CreateURLRequestSummary(
613 1, "https://www.google.com", "https://google.com/script1.js", 537 1, "https://www.google.com", "https://google.com/script1.js",
614 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 538 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
615 predictor_->RecordURLResponse(resource2); 539 predictor_->RecordURLResponse(resource2);
616 URLRequestSummary resource3 = CreateURLRequestSummary( 540 URLRequestSummary resource3 = CreateURLRequestSummary(
617 1, "https://www.google.com", "https://google.com/script2.js", 541 1, "https://www.google.com", "https://google.com/script2.js",
618 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 542 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
619 predictor_->RecordURLResponse(resource3); 543 predictor_->RecordURLResponse(resource3);
620 544
621 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 545 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
622 EXPECT_CALL( 546 EXPECT_CALL(
623 mock_observer, 547 mock_observer,
624 OnNavigationLearned(kVisitCount, 548 OnNavigationLearned(kVisitCount,
625 CreatePageRequestSummary( 549 CreatePageRequestSummary(
626 "https://www.google.com", "http://www.google.com", 550 "https://www.google.com", "http://www.google.com",
627 {resource1, resource2, resource3}))); 551 {resource1, resource2, resource3})));
628 552
629 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 553 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
630 profile_->BlockUntilHistoryProcessesPendingRequests(); 554 profile_->BlockUntilHistoryProcessesPendingRequests();
631 555
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 634 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
711 redirected.redirect_url = GURL("http://dev.null.google.com/style.css"); 635 redirected.redirect_url = GURL("http://dev.null.google.com/style.css");
712 636
713 predictor_->RecordURLRedirect(redirected); 637 predictor_->RecordURLRedirect(redirected);
714 redirected.is_no_store = true; 638 redirected.is_no_store = true;
715 redirected.request_url = redirected.redirect_url; 639 redirected.request_url = redirected.redirect_url;
716 redirected.redirect_url = GURL(); 640 redirected.redirect_url = GURL();
717 641
718 predictor_->RecordURLResponse(redirected); 642 predictor_->RecordURLResponse(redirected);
719 643
720 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 644 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
721 EXPECT_CALL(mock_observer, 645 EXPECT_CALL(mock_observer,
722 OnNavigationLearned( 646 OnNavigationLearned(
723 kVisitCount, CreatePageRequestSummary("http://www.google.com", 647 kVisitCount, CreatePageRequestSummary("http://www.google.com",
724 "http://www.google.com", 648 "http://www.google.com",
725 resources))); 649 resources)));
726 650
727 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 651 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
728 profile_->BlockUntilHistoryProcessesPendingRequests(); 652 profile_->BlockUntilHistoryProcessesPendingRequests();
729 653
730 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 654 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
824 1, "http://www.google.com", "http://google.com/style2.css", 748 1, "http://www.google.com", "http://google.com/style2.css",
825 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 749 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
826 predictor_->RecordURLResponse(resources.back()); 750 predictor_->RecordURLResponse(resources.back());
827 auto no_store = CreateURLRequestSummary( 751 auto no_store = CreateURLRequestSummary(
828 1, "http://www.google.com", 752 1, "http://www.google.com",
829 "http://static.google.com/style2-no-store.css", 753 "http://static.google.com/style2-no-store.css",
830 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 754 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
831 no_store.is_no_store = true; 755 no_store.is_no_store = true;
832 predictor_->RecordURLResponse(no_store); 756 predictor_->RecordURLResponse(no_store);
833 757
834 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 758 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
835 EXPECT_CALL(mock_observer, 759 EXPECT_CALL(mock_observer,
836 OnNavigationLearned( 760 OnNavigationLearned(
837 kVisitCount, CreatePageRequestSummary("http://www.google.com", 761 kVisitCount, CreatePageRequestSummary("http://www.google.com",
838 "http://www.google.com", 762 "http://www.google.com",
839 resources))); 763 resources)));
840 764
841 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 765 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
842 profile_->BlockUntilHistoryProcessesPendingRequests(); 766 profile_->BlockUntilHistoryProcessesPendingRequests();
843 767
844 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 768 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 850
927 URLRequestSummary resource1 = CreateURLRequestSummary( 851 URLRequestSummary resource1 = CreateURLRequestSummary(
928 1, "http://www.nike.com", "http://nike.com/style1.css", 852 1, "http://www.nike.com", "http://nike.com/style1.css",
929 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 853 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
930 predictor_->RecordURLResponse(resource1); 854 predictor_->RecordURLResponse(resource1);
931 URLRequestSummary resource2 = CreateURLRequestSummary( 855 URLRequestSummary resource2 = CreateURLRequestSummary(
932 1, "http://www.nike.com", "http://nike.com/image2.png", 856 1, "http://www.nike.com", "http://nike.com/image2.png",
933 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 857 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
934 predictor_->RecordURLResponse(resource2); 858 predictor_->RecordURLResponse(resource2);
935 859
936 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 860 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
937 EXPECT_CALL(mock_observer, 861 EXPECT_CALL(mock_observer,
938 OnNavigationLearned( 862 OnNavigationLearned(
939 kVisitCount, CreatePageRequestSummary( 863 kVisitCount, CreatePageRequestSummary(
940 "http://www.nike.com", "http://www.nike.com", 864 "http://www.nike.com", "http://www.nike.com",
941 {resource1, resource2}))); 865 {resource1, resource2})));
942 866
943 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 867 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
944 profile_->BlockUntilHistoryProcessesPendingRequests(); 868 profile_->BlockUntilHistoryProcessesPendingRequests();
945 869
946 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); 870 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/");
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 920 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
997 921
998 URLRequestSummary fb2 = CreateRedirectRequestSummary( 922 URLRequestSummary fb2 = CreateRedirectRequestSummary(
999 1, "http://fb.com/google", "http://facebook.com/google"); 923 1, "http://fb.com/google", "http://facebook.com/google");
1000 predictor_->RecordURLRedirect(fb2); 924 predictor_->RecordURLRedirect(fb2);
1001 URLRequestSummary fb3 = CreateRedirectRequestSummary( 925 URLRequestSummary fb3 = CreateRedirectRequestSummary(
1002 1, "http://facebook.com/google", "https://facebook.com/google"); 926 1, "http://facebook.com/google", "https://facebook.com/google");
1003 predictor_->RecordURLRedirect(fb3); 927 predictor_->RecordURLRedirect(fb3);
1004 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google"); 928 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
1005 929
1006 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 930 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
1007 EXPECT_CALL( 931 EXPECT_CALL(
1008 mock_observer, 932 mock_observer,
1009 OnNavigationLearned(kVisitCount, CreatePageRequestSummary( 933 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
1010 "https://facebook.com/google", 934 "https://facebook.com/google",
1011 "http://fb.com/google", 935 "http://fb.com/google",
1012 std::vector<URLRequestSummary>()))); 936 std::vector<URLRequestSummary>())));
1013 937
1014 predictor_->RecordMainFrameLoadComplete(fb_end); 938 predictor_->RecordMainFrameLoadComplete(fb_end);
1015 profile_->BlockUntilHistoryProcessesPendingRequests(); 939 profile_->BlockUntilHistoryProcessesPendingRequests();
1016 940
(...skipping 29 matching lines...) Expand all
1046 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 970 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1047 971
1048 URLRequestSummary fb2 = CreateRedirectRequestSummary( 972 URLRequestSummary fb2 = CreateRedirectRequestSummary(
1049 1, "http://fb.com/google", "http://facebook.com/google"); 973 1, "http://fb.com/google", "http://facebook.com/google");
1050 predictor_->RecordURLRedirect(fb2); 974 predictor_->RecordURLRedirect(fb2);
1051 URLRequestSummary fb3 = CreateRedirectRequestSummary( 975 URLRequestSummary fb3 = CreateRedirectRequestSummary(
1052 1, "http://facebook.com/google", "https://facebook.com/google"); 976 1, "http://facebook.com/google", "https://facebook.com/google");
1053 predictor_->RecordURLRedirect(fb3); 977 predictor_->RecordURLRedirect(fb3);
1054 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google"); 978 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
1055 979
1056 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 980 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
1057 EXPECT_CALL( 981 EXPECT_CALL(
1058 mock_observer, 982 mock_observer,
1059 OnNavigationLearned(kVisitCount, CreatePageRequestSummary( 983 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
1060 "https://facebook.com/google", 984 "https://facebook.com/google",
1061 "http://fb.com/google", 985 "http://fb.com/google",
1062 std::vector<URLRequestSummary>()))); 986 std::vector<URLRequestSummary>())));
1063 987
1064 predictor_->RecordMainFrameLoadComplete(fb_end); 988 predictor_->RecordMainFrameLoadComplete(fb_end);
1065 profile_->BlockUntilHistoryProcessesPendingRequests(); 989 profile_->BlockUntilHistoryProcessesPendingRequests();
1066 990
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 predictor_->inflight_navigations_[main_frame1.navigation_id] 1387 predictor_->inflight_navigations_[main_frame1.navigation_id]
1464 ->subresource_requests[0]); 1388 ->subresource_requests[0]);
1465 EXPECT_EQ(resource2, 1389 EXPECT_EQ(resource2,
1466 predictor_->inflight_navigations_[main_frame1.navigation_id] 1390 predictor_->inflight_navigations_[main_frame1.navigation_id]
1467 ->subresource_requests[1]); 1391 ->subresource_requests[1]);
1468 EXPECT_EQ(resource3, 1392 EXPECT_EQ(resource3,
1469 predictor_->inflight_navigations_[main_frame1.navigation_id] 1393 predictor_->inflight_navigations_[main_frame1.navigation_id]
1470 ->subresource_requests[2]); 1394 ->subresource_requests[2]);
1471 } 1395 }
1472 1396
1473 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) {
1474 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1475 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type"));
1476 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1477 content::RESOURCE_TYPE_STYLESHEET, ""));
1478 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1479 content::RESOURCE_TYPE_WORKER, "text/css"));
1480 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1481 content::RESOURCE_TYPE_WORKER, ""));
1482 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1483 content::RESOURCE_TYPE_PREFETCH, "text/css"));
1484 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1485 content::RESOURCE_TYPE_PREFETCH, "bogus/mime-type"));
1486 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1487 content::RESOURCE_TYPE_PREFETCH, ""));
1488 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1489 content::RESOURCE_TYPE_PREFETCH, "application/font-woff"));
1490 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1491 content::RESOURCE_TYPE_PREFETCH, "font/woff2"));
1492 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1493 content::RESOURCE_TYPE_XHR, ""));
1494 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
1495 content::RESOURCE_TYPE_XHR, "bogus/mime-type"));
1496 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
1497 content::RESOURCE_TYPE_XHR, "application/javascript"));
1498 }
1499
1500 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordRequestMainFrame) {
1501 std::unique_ptr<net::URLRequest> http_request =
1502 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM,
1503 content::RESOURCE_TYPE_IMAGE, true);
1504 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordRequest(
1505 http_request.get(), content::RESOURCE_TYPE_MAIN_FRAME));
1506
1507 std::unique_ptr<net::URLRequest> https_request =
1508 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM,
1509 content::RESOURCE_TYPE_IMAGE, true);
1510 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordRequest(
1511 https_request.get(), content::RESOURCE_TYPE_MAIN_FRAME));
1512
1513 std::unique_ptr<net::URLRequest> file_request =
1514 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM,
1515 content::RESOURCE_TYPE_IMAGE, true);
1516 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
1517 file_request.get(), content::RESOURCE_TYPE_MAIN_FRAME));
1518
1519 std::unique_ptr<net::URLRequest> https_request_with_port =
1520 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM,
1521 content::RESOURCE_TYPE_IMAGE, true);
1522 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
1523 https_request_with_port.get(), content::RESOURCE_TYPE_MAIN_FRAME));
1524 }
1525
1526 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordRequestSubResource) {
1527 std::unique_ptr<net::URLRequest> http_request =
1528 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1529 content::RESOURCE_TYPE_IMAGE, false);
1530 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
1531 http_request.get(), content::RESOURCE_TYPE_IMAGE));
1532
1533 std::unique_ptr<net::URLRequest> https_request =
1534 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM,
1535 content::RESOURCE_TYPE_IMAGE, false);
1536 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
1537 https_request.get(), content::RESOURCE_TYPE_IMAGE));
1538
1539 std::unique_ptr<net::URLRequest> file_request =
1540 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM,
1541 content::RESOURCE_TYPE_IMAGE, false);
1542 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
1543 file_request.get(), content::RESOURCE_TYPE_IMAGE));
1544
1545 std::unique_ptr<net::URLRequest> https_request_with_port =
1546 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM,
1547 content::RESOURCE_TYPE_IMAGE, false);
1548 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
1549 https_request_with_port.get(), content::RESOURCE_TYPE_IMAGE));
1550 }
1551
1552 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseMainFrame) {
1553 net::HttpResponseInfo response_info;
1554 response_info.headers = MakeResponseHeaders("");
1555 url_request_job_factory_.set_response_info(response_info);
1556
1557 std::unique_ptr<net::URLRequest> http_request =
1558 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM,
1559 content::RESOURCE_TYPE_MAIN_FRAME, true);
1560 EXPECT_TRUE(
1561 ResourcePrefetchPredictor::ShouldRecordResponse(http_request.get()));
1562
1563 std::unique_ptr<net::URLRequest> https_request =
1564 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM,
1565 content::RESOURCE_TYPE_MAIN_FRAME, true);
1566 EXPECT_TRUE(
1567 ResourcePrefetchPredictor::ShouldRecordResponse(https_request.get()));
1568
1569 std::unique_ptr<net::URLRequest> file_request =
1570 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM,
1571 content::RESOURCE_TYPE_MAIN_FRAME, true);
1572 EXPECT_FALSE(
1573 ResourcePrefetchPredictor::ShouldRecordResponse(file_request.get()));
1574
1575 std::unique_ptr<net::URLRequest> https_request_with_port =
1576 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM,
1577 content::RESOURCE_TYPE_MAIN_FRAME, true);
1578 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1579 https_request_with_port.get()));
1580 }
1581
1582 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseSubresource) {
1583 net::HttpResponseInfo response_info;
1584 response_info.headers =
1585 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1586 response_info.was_cached = true;
1587 url_request_job_factory_.set_response_info(response_info);
1588
1589 // Protocol.
1590 std::unique_ptr<net::URLRequest> http_image_request =
1591 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1592 content::RESOURCE_TYPE_IMAGE, true);
1593 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
1594 http_image_request.get()));
1595
1596 std::unique_ptr<net::URLRequest> https_image_request =
1597 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM,
1598 content::RESOURCE_TYPE_IMAGE, true);
1599 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
1600 https_image_request.get()));
1601
1602 std::unique_ptr<net::URLRequest> https_image_request_with_port =
1603 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM,
1604 content::RESOURCE_TYPE_IMAGE, true);
1605 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1606 https_image_request_with_port.get()));
1607
1608 std::unique_ptr<net::URLRequest> file_image_request =
1609 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM,
1610 content::RESOURCE_TYPE_IMAGE, true);
1611 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1612 file_image_request.get()));
1613
1614 // ResourceType.
1615 std::unique_ptr<net::URLRequest> sub_frame_request =
1616 CreateURLRequest(GURL("http://www.google.com/frame.html"), net::MEDIUM,
1617 content::RESOURCE_TYPE_SUB_FRAME, true);
1618 EXPECT_FALSE(
1619 ResourcePrefetchPredictor::ShouldRecordResponse(sub_frame_request.get()));
1620
1621 std::unique_ptr<net::URLRequest> font_request =
1622 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"),
1623 net::MEDIUM, content::RESOURCE_TYPE_FONT_RESOURCE, true);
1624 EXPECT_TRUE(
1625 ResourcePrefetchPredictor::ShouldRecordResponse(font_request.get()));
1626
1627 // From MIME Type.
1628 url_request_job_factory_.set_mime_type("image/png");
1629 std::unique_ptr<net::URLRequest> prefetch_image_request =
1630 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1631 content::RESOURCE_TYPE_PREFETCH, true);
1632 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
1633 prefetch_image_request.get()));
1634
1635 url_request_job_factory_.set_mime_type("image/my-wonderful-format");
1636 std::unique_ptr<net::URLRequest> prefetch_unknown_image_request =
1637 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1638 content::RESOURCE_TYPE_PREFETCH, true);
1639 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1640 prefetch_unknown_image_request.get()));
1641
1642 url_request_job_factory_.set_mime_type("font/woff");
1643 std::unique_ptr<net::URLRequest> prefetch_font_request =
1644 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"),
1645 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true);
1646 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
1647 prefetch_font_request.get()));
1648
1649 url_request_job_factory_.set_mime_type("font/woff-woff");
1650 std::unique_ptr<net::URLRequest> prefetch_unknown_font_request =
1651 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"),
1652 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true);
1653 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1654 prefetch_unknown_font_request.get()));
1655
1656 // Not main frame.
1657 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest(
1658 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM,
1659 content::RESOURCE_TYPE_FONT_RESOURCE, false);
1660 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1661 font_request_sub_frame.get()));
1662 }
1663
1664 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) { 1397 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
1665 net::HttpResponseInfo response_info; 1398 net::HttpResponseInfo response_info;
1666 response_info.headers = 1399 response_info.headers =
1667 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); 1400 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1668 response_info.was_cached = true; 1401 response_info.was_cached = true;
1669 url_request_job_factory_.set_response_info(response_info); 1402 url_request_job_factory_.set_response_info(response_info);
1670 1403
1671 GURL url("http://www.google.com/cat.png"); 1404 GURL url("http://www.google.com/cat.png");
1672 std::unique_ptr<net::URLRequest> request = 1405 std::unique_ptr<net::URLRequest> request =
1673 CreateURLRequest(url, net::MEDIUM, content::RESOURCE_TYPE_IMAGE, true); 1406 CreateURLRequest(url, net::MEDIUM, content::RESOURCE_TYPE_IMAGE, true);
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
2100 ResourceData* resource3_rd = host_data.add_resources(); 1833 ResourceData* resource3_rd = host_data.add_resources();
2101 InitializeResourceData(resource3_rd, "http://google.com/script2.js", 1834 InitializeResourceData(resource3_rd, "http://google.com/script2.js",
2102 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, 1835 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0,
2103 net::MEDIUM, false, false); 1836 net::MEDIUM, false, false);
2104 resource3_rd->set_before_first_contentful_paint(false); 1837 resource3_rd->set_before_first_contentful_paint(false);
2105 EXPECT_EQ(mock_tables_->host_resource_table_.data_, 1838 EXPECT_EQ(mock_tables_->host_resource_table_.data_,
2106 PrefetchDataMap({{host_data.primary_key(), host_data}})); 1839 PrefetchDataMap({{host_data.primary_key(), host_data}}));
2107 } 1840 }
2108 1841
2109 } // namespace predictors 1842 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698