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

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: Address lizeb feedback. 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
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/history/history_service_factory.h" 16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/predictors/loading_predictor.h" 17 #include "chrome/browser/predictors/loading_predictor.h"
18 #include "chrome/browser/predictors/loading_test_util.h"
18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 19 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
19 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h"
20 #include "chrome/browser/predictors/resource_prefetcher_manager.h" 20 #include "chrome/browser/predictors/resource_prefetcher_manager.h"
21 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
22 #include "components/history/core/browser/history_service.h" 22 #include "components/history/core/browser/history_service.h"
23 #include "components/history/core/browser/history_types.h" 23 #include "components/history/core/browser/history_types.h"
24 #include "components/sessions/core/session_id.h" 24 #include "components/sessions/core/session_id.h"
25 #include "content/public/browser/resource_request_info.h" 25 #include "content/public/browser/resource_request_info.h"
26 #include "content/public/test/test_browser_thread_bundle.h" 26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "net/http/http_response_headers.h" 27 #include "net/http/http_response_headers.h"
28 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" 28 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
29 #include "net/url_request/url_request_context.h" 29 #include "net/url_request/url_request_context.h"
(...skipping 10 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 {
57 void OnResponseStarted(net::URLRequest* request, int net_error) override {}
58 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {}
59 };
60
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> 50 template <typename T>
131 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { 51 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> {
132 public: 52 public:
133 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} 53 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {}
134 void GetAllData(std::map<std::string, T>* data_map, 54 void GetAllData(std::map<std::string, T>* data_map,
135 sql::Connection* db) const override { 55 sql::Connection* db) const override {
136 *data_map = data_; 56 *data_map = data_;
137 } 57 }
138 void UpdateData(const std::string& key, 58 void UpdateData(const std::string& key,
139 const T& data, 59 const T& data,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 FakeGlowplugKeyValueTable<RedirectData> url_redirect_table_; 112 FakeGlowplugKeyValueTable<RedirectData> url_redirect_table_;
193 FakeGlowplugKeyValueTable<PrefetchData> host_resource_table_; 113 FakeGlowplugKeyValueTable<PrefetchData> host_resource_table_;
194 FakeGlowplugKeyValueTable<RedirectData> host_redirect_table_; 114 FakeGlowplugKeyValueTable<RedirectData> host_redirect_table_;
195 FakeGlowplugKeyValueTable<precache::PrecacheManifest> manifest_table_; 115 FakeGlowplugKeyValueTable<precache::PrecacheManifest> manifest_table_;
196 FakeGlowplugKeyValueTable<OriginData> origin_table_; 116 FakeGlowplugKeyValueTable<OriginData> origin_table_;
197 117
198 protected: 118 protected:
199 ~MockResourcePrefetchPredictorTables() override = default; 119 ~MockResourcePrefetchPredictorTables() override = default;
200 }; 120 };
201 121
202 class MockResourcePrefetchPredictorObserver : public TestObserver { 122 class MockLoadingPredictorObserver : public TestObserver {
203 public: 123 public:
204 explicit MockResourcePrefetchPredictorObserver( 124 explicit MockLoadingPredictorObserver(ResourcePrefetchPredictor* predictor)
205 ResourcePrefetchPredictor* predictor)
206 : TestObserver(predictor) {} 125 : TestObserver(predictor) {}
207 126
208 MOCK_METHOD2( 127 MOCK_METHOD2(
209 OnNavigationLearned, 128 OnNavigationLearned,
210 void(size_t url_visit_count, 129 void(size_t url_visit_count,
211 const ResourcePrefetchPredictor::PageRequestSummary& summary)); 130 const ResourcePrefetchPredictor::PageRequestSummary& summary));
212 }; 131 };
213 132
214 class ResourcePrefetchPredictorTest : public testing::Test { 133 class ResourcePrefetchPredictorTest : public testing::Test {
215 public: 134 public:
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 predictor_->RecordURLResponse(resource1); 530 predictor_->RecordURLResponse(resource1);
612 URLRequestSummary resource2 = CreateURLRequestSummary( 531 URLRequestSummary resource2 = CreateURLRequestSummary(
613 1, "https://www.google.com", "https://google.com/script1.js", 532 1, "https://www.google.com", "https://google.com/script1.js",
614 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 533 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
615 predictor_->RecordURLResponse(resource2); 534 predictor_->RecordURLResponse(resource2);
616 URLRequestSummary resource3 = CreateURLRequestSummary( 535 URLRequestSummary resource3 = CreateURLRequestSummary(
617 1, "https://www.google.com", "https://google.com/script2.js", 536 1, "https://www.google.com", "https://google.com/script2.js",
618 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 537 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
619 predictor_->RecordURLResponse(resource3); 538 predictor_->RecordURLResponse(resource3);
620 539
621 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 540 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
622 EXPECT_CALL( 541 EXPECT_CALL(
623 mock_observer, 542 mock_observer,
624 OnNavigationLearned(kVisitCount, 543 OnNavigationLearned(kVisitCount,
625 CreatePageRequestSummary( 544 CreatePageRequestSummary(
626 "https://www.google.com", "http://www.google.com", 545 "https://www.google.com", "http://www.google.com",
627 {resource1, resource2, resource3}))); 546 {resource1, resource2, resource3})));
628 547
629 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 548 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
630 profile_->BlockUntilHistoryProcessesPendingRequests(); 549 profile_->BlockUntilHistoryProcessesPendingRequests();
631 550
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 629 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
711 redirected.redirect_url = GURL("http://dev.null.google.com/style.css"); 630 redirected.redirect_url = GURL("http://dev.null.google.com/style.css");
712 631
713 predictor_->RecordURLRedirect(redirected); 632 predictor_->RecordURLRedirect(redirected);
714 redirected.is_no_store = true; 633 redirected.is_no_store = true;
715 redirected.request_url = redirected.redirect_url; 634 redirected.request_url = redirected.redirect_url;
716 redirected.redirect_url = GURL(); 635 redirected.redirect_url = GURL();
717 636
718 predictor_->RecordURLResponse(redirected); 637 predictor_->RecordURLResponse(redirected);
719 638
720 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 639 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
721 EXPECT_CALL(mock_observer, 640 EXPECT_CALL(mock_observer,
722 OnNavigationLearned( 641 OnNavigationLearned(
723 kVisitCount, CreatePageRequestSummary("http://www.google.com", 642 kVisitCount, CreatePageRequestSummary("http://www.google.com",
724 "http://www.google.com", 643 "http://www.google.com",
725 resources))); 644 resources)));
726 645
727 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 646 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
728 profile_->BlockUntilHistoryProcessesPendingRequests(); 647 profile_->BlockUntilHistoryProcessesPendingRequests();
729 648
730 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 649 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", 743 1, "http://www.google.com", "http://google.com/style2.css",
825 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 744 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
826 predictor_->RecordURLResponse(resources.back()); 745 predictor_->RecordURLResponse(resources.back());
827 auto no_store = CreateURLRequestSummary( 746 auto no_store = CreateURLRequestSummary(
828 1, "http://www.google.com", 747 1, "http://www.google.com",
829 "http://static.google.com/style2-no-store.css", 748 "http://static.google.com/style2-no-store.css",
830 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 749 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
831 no_store.is_no_store = true; 750 no_store.is_no_store = true;
832 predictor_->RecordURLResponse(no_store); 751 predictor_->RecordURLResponse(no_store);
833 752
834 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 753 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
835 EXPECT_CALL(mock_observer, 754 EXPECT_CALL(mock_observer,
836 OnNavigationLearned( 755 OnNavigationLearned(
837 kVisitCount, CreatePageRequestSummary("http://www.google.com", 756 kVisitCount, CreatePageRequestSummary("http://www.google.com",
838 "http://www.google.com", 757 "http://www.google.com",
839 resources))); 758 resources)));
840 759
841 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 760 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
842 profile_->BlockUntilHistoryProcessesPendingRequests(); 761 profile_->BlockUntilHistoryProcessesPendingRequests();
843 762
844 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 763 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 845
927 URLRequestSummary resource1 = CreateURLRequestSummary( 846 URLRequestSummary resource1 = CreateURLRequestSummary(
928 1, "http://www.nike.com", "http://nike.com/style1.css", 847 1, "http://www.nike.com", "http://nike.com/style1.css",
929 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 848 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
930 predictor_->RecordURLResponse(resource1); 849 predictor_->RecordURLResponse(resource1);
931 URLRequestSummary resource2 = CreateURLRequestSummary( 850 URLRequestSummary resource2 = CreateURLRequestSummary(
932 1, "http://www.nike.com", "http://nike.com/image2.png", 851 1, "http://www.nike.com", "http://nike.com/image2.png",
933 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 852 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
934 predictor_->RecordURLResponse(resource2); 853 predictor_->RecordURLResponse(resource2);
935 854
936 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 855 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
937 EXPECT_CALL(mock_observer, 856 EXPECT_CALL(mock_observer,
938 OnNavigationLearned( 857 OnNavigationLearned(
939 kVisitCount, CreatePageRequestSummary( 858 kVisitCount, CreatePageRequestSummary(
940 "http://www.nike.com", "http://www.nike.com", 859 "http://www.nike.com", "http://www.nike.com",
941 {resource1, resource2}))); 860 {resource1, resource2})));
942 861
943 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 862 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
944 profile_->BlockUntilHistoryProcessesPendingRequests(); 863 profile_->BlockUntilHistoryProcessesPendingRequests();
945 864
946 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); 865 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()); 915 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
997 916
998 URLRequestSummary fb2 = CreateRedirectRequestSummary( 917 URLRequestSummary fb2 = CreateRedirectRequestSummary(
999 1, "http://fb.com/google", "http://facebook.com/google"); 918 1, "http://fb.com/google", "http://facebook.com/google");
1000 predictor_->RecordURLRedirect(fb2); 919 predictor_->RecordURLRedirect(fb2);
1001 URLRequestSummary fb3 = CreateRedirectRequestSummary( 920 URLRequestSummary fb3 = CreateRedirectRequestSummary(
1002 1, "http://facebook.com/google", "https://facebook.com/google"); 921 1, "http://facebook.com/google", "https://facebook.com/google");
1003 predictor_->RecordURLRedirect(fb3); 922 predictor_->RecordURLRedirect(fb3);
1004 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google"); 923 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
1005 924
1006 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 925 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
1007 EXPECT_CALL( 926 EXPECT_CALL(
1008 mock_observer, 927 mock_observer,
1009 OnNavigationLearned(kVisitCount, CreatePageRequestSummary( 928 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
1010 "https://facebook.com/google", 929 "https://facebook.com/google",
1011 "http://fb.com/google", 930 "http://fb.com/google",
1012 std::vector<URLRequestSummary>()))); 931 std::vector<URLRequestSummary>())));
1013 932
1014 predictor_->RecordMainFrameLoadComplete(fb_end); 933 predictor_->RecordMainFrameLoadComplete(fb_end);
1015 profile_->BlockUntilHistoryProcessesPendingRequests(); 934 profile_->BlockUntilHistoryProcessesPendingRequests();
1016 935
(...skipping 29 matching lines...) Expand all
1046 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 965 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1047 966
1048 URLRequestSummary fb2 = CreateRedirectRequestSummary( 967 URLRequestSummary fb2 = CreateRedirectRequestSummary(
1049 1, "http://fb.com/google", "http://facebook.com/google"); 968 1, "http://fb.com/google", "http://facebook.com/google");
1050 predictor_->RecordURLRedirect(fb2); 969 predictor_->RecordURLRedirect(fb2);
1051 URLRequestSummary fb3 = CreateRedirectRequestSummary( 970 URLRequestSummary fb3 = CreateRedirectRequestSummary(
1052 1, "http://facebook.com/google", "https://facebook.com/google"); 971 1, "http://facebook.com/google", "https://facebook.com/google");
1053 predictor_->RecordURLRedirect(fb3); 972 predictor_->RecordURLRedirect(fb3);
1054 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google"); 973 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
1055 974
1056 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 975 StrictMock<MockLoadingPredictorObserver> mock_observer(predictor_);
1057 EXPECT_CALL( 976 EXPECT_CALL(
1058 mock_observer, 977 mock_observer,
1059 OnNavigationLearned(kVisitCount, CreatePageRequestSummary( 978 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
1060 "https://facebook.com/google", 979 "https://facebook.com/google",
1061 "http://fb.com/google", 980 "http://fb.com/google",
1062 std::vector<URLRequestSummary>()))); 981 std::vector<URLRequestSummary>())));
1063 982
1064 predictor_->RecordMainFrameLoadComplete(fb_end); 983 predictor_->RecordMainFrameLoadComplete(fb_end);
1065 profile_->BlockUntilHistoryProcessesPendingRequests(); 984 profile_->BlockUntilHistoryProcessesPendingRequests();
1066 985
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 predictor_->inflight_navigations_[main_frame1.navigation_id] 1382 predictor_->inflight_navigations_[main_frame1.navigation_id]
1464 ->subresource_requests[0]); 1383 ->subresource_requests[0]);
1465 EXPECT_EQ(resource2, 1384 EXPECT_EQ(resource2,
1466 predictor_->inflight_navigations_[main_frame1.navigation_id] 1385 predictor_->inflight_navigations_[main_frame1.navigation_id]
1467 ->subresource_requests[1]); 1386 ->subresource_requests[1]);
1468 EXPECT_EQ(resource3, 1387 EXPECT_EQ(resource3,
1469 predictor_->inflight_navigations_[main_frame1.navigation_id] 1388 predictor_->inflight_navigations_[main_frame1.navigation_id]
1470 ->subresource_requests[2]); 1389 ->subresource_requests[2]);
1471 } 1390 }
1472 1391
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) { 1392 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
1665 net::HttpResponseInfo response_info; 1393 net::HttpResponseInfo response_info;
1666 response_info.headers = 1394 response_info.headers =
1667 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); 1395 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1668 response_info.was_cached = true; 1396 response_info.was_cached = true;
1669 url_request_job_factory_.set_response_info(response_info); 1397 url_request_job_factory_.set_response_info(response_info);
1670 1398
1671 GURL url("http://www.google.com/cat.png"); 1399 GURL url("http://www.google.com/cat.png");
1672 std::unique_ptr<net::URLRequest> request = 1400 std::unique_ptr<net::URLRequest> request =
1673 CreateURLRequest(url, net::MEDIUM, content::RESOURCE_TYPE_IMAGE, true); 1401 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(); 1828 ResourceData* resource3_rd = host_data.add_resources();
2101 InitializeResourceData(resource3_rd, "http://google.com/script2.js", 1829 InitializeResourceData(resource3_rd, "http://google.com/script2.js",
2102 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, 1830 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0,
2103 net::MEDIUM, false, false); 1831 net::MEDIUM, false, false);
2104 resource3_rd->set_before_first_contentful_paint(false); 1832 resource3_rd->set_before_first_contentful_paint(false);
2105 EXPECT_EQ(mock_tables_->host_resource_table_.data_, 1833 EXPECT_EQ(mock_tables_->host_resource_table_.data_,
2106 PrefetchDataMap({{host_data.primary_key(), host_data}})); 1834 PrefetchDataMap({{host_data.primary_key(), host_data}}));
2107 } 1835 }
2108 1836
2109 } // namespace predictors 1837 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698