Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/predictors/loading_data_collector.h" | |
| 6 | |
| 7 #include <iostream> | |
| 8 #include <memory> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/test/histogram_tester.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "chrome/browser/history/history_service_factory.h" | |
| 17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | |
|
Benoit L
2017/06/02 12:43:59
Is this include needed?
Please run "git cl lint",
trevordixon
2017/06/06 13:08:03
I think I got rid of all the unnecessary ones.
| |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | |
| 19 #include "chrome/test/base/testing_profile.h" | |
| 20 #include "components/history/core/browser/history_service.h" | |
| 21 #include "components/history/core/browser/history_types.h" | |
| 22 #include "components/sessions/core/session_id.h" | |
| 23 #include "content/public/browser/resource_request_info.h" | |
| 24 #include "content/public/test/test_browser_thread_bundle.h" | |
| 25 #include "net/http/http_response_headers.h" | |
| 26 #include "net/url_request/url_request_context.h" | |
| 27 #include "net/url_request/url_request_job.h" | |
| 28 #include "net/url_request/url_request_test_util.h" | |
| 29 #include "testing/gmock/include/gmock/gmock.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 | |
| 32 using testing::StrictMock; | |
| 33 | |
| 34 namespace predictors { | |
| 35 | |
| 36 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 37 | |
| 38 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { | |
| 39 void OnResponseStarted(net::URLRequest* request, int net_error) override {} | |
| 40 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} | |
| 41 }; | |
| 42 | |
| 43 class LoadingDataCollectorTest : public testing::Test { | |
| 44 public: | |
| 45 LoadingDataCollectorTest(); | |
| 46 ~LoadingDataCollectorTest() override; | |
| 47 void SetUp() override; | |
| 48 void TearDown() override; | |
| 49 | |
| 50 protected: | |
| 51 std::unique_ptr<net::URLRequest> CreateURLRequest( | |
|
Benoit L
2017/06/02 12:43:59
See comment below, is it possible to avoid duplica
| |
| 52 const GURL& url, | |
| 53 net::RequestPriority priority, | |
| 54 content::ResourceType resource_type, | |
| 55 bool is_main_frame) { | |
| 56 std::unique_ptr<net::URLRequest> request = | |
| 57 url_request_context_.CreateRequest(url, priority, | |
| 58 &url_request_delegate_); | |
| 59 request->set_first_party_for_cookies(url); | |
| 60 content::ResourceRequestInfo::AllocateForTesting( | |
| 61 request.get(), resource_type, nullptr, -1, -1, -1, is_main_frame, false, | |
| 62 false, true, content::PREVIEWS_OFF); | |
| 63 request->Start(); | |
| 64 return request; | |
| 65 } | |
| 66 | |
| 67 void InitializePredictor() { | |
| 68 predictor_->StartInitialization(); | |
| 69 base::RunLoop loop; | |
| 70 loop.RunUntilIdle(); // Runs the DB lookup. | |
| 71 profile_->BlockUntilHistoryProcessesPendingRequests(); | |
| 72 } | |
| 73 | |
| 74 void ResetPredictor() { | |
| 75 LoadingPredictorConfig config; | |
|
Benoit L
2017/06/02 12:43:59
There is now PopulateTestConfig() to replace this
trevordixon
2017/06/06 13:08:04
Done.
| |
| 76 config.max_urls_to_track = 3; | |
| 77 config.max_hosts_to_track = 2; | |
| 78 config.min_url_visit_count = 2; | |
| 79 config.max_resources_per_entry = 4; | |
| 80 config.max_consecutive_misses = 2; | |
| 81 config.max_redirect_consecutive_misses = 2; | |
| 82 config.min_resource_confidence_to_trigger_prefetch = 0.5; | |
| 83 config.is_url_learning_enabled = true; | |
| 84 config.is_manifests_enabled = true; | |
| 85 config.is_origin_learning_enabled = true; | |
| 86 | |
| 87 config.mode |= LoadingPredictorConfig::LEARNING; | |
| 88 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); | |
|
Benoit L
2017/06/02 12:43:59
nit:
predictor_ = base::MakeUnique<>()...
trevordixon
2017/06/06 13:08:04
Done.
| |
| 89 } | |
| 90 | |
| 91 content::TestBrowserThreadBundle thread_bundle_; | |
| 92 std::unique_ptr<TestingProfile> profile_; | |
| 93 net::TestURLRequestContext url_request_context_; | |
| 94 | |
| 95 std::unique_ptr<ResourcePrefetchPredictor> predictor_; | |
| 96 | |
| 97 MockURLRequestJobFactory url_request_job_factory_; | |
| 98 EmptyURLRequestDelegate url_request_delegate_; | |
| 99 | |
| 100 std::unique_ptr<base::HistogramTester> histogram_tester_; | |
| 101 }; | |
| 102 | |
| 103 LoadingDataCollectorTest::LoadingDataCollectorTest() | |
| 104 : thread_bundle_(), profile_(new TestingProfile()) {} | |
| 105 | |
| 106 LoadingDataCollectorTest::~LoadingDataCollectorTest() { | |
| 107 profile_.reset(NULL); | |
| 108 base::RunLoop().RunUntilIdle(); | |
| 109 } | |
| 110 | |
| 111 void LoadingDataCollectorTest::SetUp() { | |
| 112 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); | |
| 113 profile_->BlockUntilHistoryProcessesPendingRequests(); | |
| 114 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( | |
| 115 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); | |
| 116 ResetPredictor(); | |
|
Benoit L
2017/06/02 12:43:59
nit: These two functions are called only once, inl
trevordixon
2017/06/06 13:08:03
Done.
| |
| 117 InitializePredictor(); | |
| 118 | |
| 119 url_request_context_.set_job_factory(&url_request_job_factory_); | |
| 120 | |
| 121 histogram_tester_.reset(new base::HistogramTester()); | |
|
Benoit L
2017/06/02 12:43:59
nit:
histogram_tester_ = base::MakeUnique<base::H
trevordixon
2017/06/06 13:08:04
Done.
| |
| 122 } | |
| 123 | |
| 124 void LoadingDataCollectorTest::TearDown() { | |
| 125 predictor_.reset(NULL); | |
| 126 profile_->DestroyHistoryService(); | |
| 127 } | |
| 128 | |
| 129 TEST_F(LoadingDataCollectorTest, HandledResourceTypes) { | |
|
Benoit L
2017/06/02 12:43:59
Is it correct that all the tests below only test s
trevordixon
2017/06/06 13:08:04
You're right. At one point I had the RecordFirstCo
| |
| 130 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
| 131 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); | |
| 132 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
| 133 content::RESOURCE_TYPE_STYLESHEET, "")); | |
| 134 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
| 135 content::RESOURCE_TYPE_WORKER, "text/css")); | |
| 136 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
| 137 content::RESOURCE_TYPE_WORKER, "")); | |
| 138 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
| 139 content::RESOURCE_TYPE_PREFETCH, "text/css")); | |
| 140 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
| 141 content::RESOURCE_TYPE_PREFETCH, "bogus/mime-type")); | |
| 142 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
| 143 content::RESOURCE_TYPE_PREFETCH, "")); | |
| 144 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
| 145 content::RESOURCE_TYPE_PREFETCH, "application/font-woff")); | |
| 146 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
| 147 content::RESOURCE_TYPE_PREFETCH, "font/woff2")); | |
| 148 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
| 149 content::RESOURCE_TYPE_XHR, "")); | |
| 150 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
| 151 content::RESOURCE_TYPE_XHR, "bogus/mime-type")); | |
| 152 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
| 153 content::RESOURCE_TYPE_XHR, "application/javascript")); | |
| 154 } | |
| 155 | |
| 156 TEST_F(LoadingDataCollectorTest, ShouldRecordRequestMainFrame) { | |
| 157 std::unique_ptr<net::URLRequest> http_request = | |
| 158 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, | |
| 159 content::RESOURCE_TYPE_IMAGE, true); | |
| 160 EXPECT_TRUE(LoadingDataCollector::ShouldRecordRequest( | |
| 161 http_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 162 | |
| 163 std::unique_ptr<net::URLRequest> https_request = | |
| 164 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, | |
| 165 content::RESOURCE_TYPE_IMAGE, true); | |
| 166 EXPECT_TRUE(LoadingDataCollector::ShouldRecordRequest( | |
| 167 https_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 168 | |
| 169 std::unique_ptr<net::URLRequest> file_request = | |
| 170 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, | |
| 171 content::RESOURCE_TYPE_IMAGE, true); | |
| 172 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
| 173 file_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 174 | |
| 175 std::unique_ptr<net::URLRequest> https_request_with_port = | |
| 176 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM, | |
| 177 content::RESOURCE_TYPE_IMAGE, true); | |
| 178 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
| 179 https_request_with_port.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 180 } | |
| 181 | |
| 182 TEST_F(LoadingDataCollectorTest, ShouldRecordRequestSubResource) { | |
| 183 std::unique_ptr<net::URLRequest> http_request = | |
| 184 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 185 content::RESOURCE_TYPE_IMAGE, false); | |
| 186 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
| 187 http_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 188 | |
| 189 std::unique_ptr<net::URLRequest> https_request = | |
| 190 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, | |
| 191 content::RESOURCE_TYPE_IMAGE, false); | |
| 192 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
| 193 https_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 194 | |
| 195 std::unique_ptr<net::URLRequest> file_request = | |
| 196 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, | |
| 197 content::RESOURCE_TYPE_IMAGE, false); | |
| 198 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
| 199 file_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 200 | |
| 201 std::unique_ptr<net::URLRequest> https_request_with_port = | |
| 202 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM, | |
| 203 content::RESOURCE_TYPE_IMAGE, false); | |
| 204 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
| 205 https_request_with_port.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 206 } | |
| 207 | |
| 208 TEST_F(LoadingDataCollectorTest, ShouldRecordResponseMainFrame) { | |
| 209 net::HttpResponseInfo response_info; | |
| 210 response_info.headers = MakeResponseHeaders(""); | |
| 211 url_request_job_factory_.set_response_info(response_info); | |
| 212 | |
| 213 std::unique_ptr<net::URLRequest> http_request = | |
| 214 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, | |
| 215 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 216 EXPECT_TRUE(LoadingDataCollector::ShouldRecordResponse(http_request.get())); | |
| 217 | |
| 218 std::unique_ptr<net::URLRequest> https_request = | |
| 219 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, | |
| 220 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 221 EXPECT_TRUE(LoadingDataCollector::ShouldRecordResponse(https_request.get())); | |
| 222 | |
| 223 std::unique_ptr<net::URLRequest> file_request = | |
| 224 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, | |
| 225 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 226 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse(file_request.get())); | |
| 227 | |
| 228 std::unique_ptr<net::URLRequest> https_request_with_port = | |
| 229 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM, | |
| 230 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 231 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
| 232 https_request_with_port.get())); | |
| 233 } | |
| 234 | |
| 235 TEST_F(LoadingDataCollectorTest, ShouldRecordResponseSubresource) { | |
| 236 net::HttpResponseInfo response_info; | |
| 237 response_info.headers = | |
| 238 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); | |
| 239 response_info.was_cached = true; | |
| 240 url_request_job_factory_.set_response_info(response_info); | |
| 241 | |
| 242 // Protocol. | |
| 243 std::unique_ptr<net::URLRequest> http_image_request = | |
| 244 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 245 content::RESOURCE_TYPE_IMAGE, true); | |
| 246 EXPECT_TRUE( | |
| 247 LoadingDataCollector::ShouldRecordResponse(http_image_request.get())); | |
| 248 | |
| 249 std::unique_ptr<net::URLRequest> https_image_request = | |
| 250 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, | |
| 251 content::RESOURCE_TYPE_IMAGE, true); | |
| 252 EXPECT_TRUE( | |
| 253 LoadingDataCollector::ShouldRecordResponse(https_image_request.get())); | |
| 254 | |
| 255 std::unique_ptr<net::URLRequest> https_image_request_with_port = | |
| 256 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM, | |
| 257 content::RESOURCE_TYPE_IMAGE, true); | |
| 258 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
| 259 https_image_request_with_port.get())); | |
| 260 | |
| 261 std::unique_ptr<net::URLRequest> file_image_request = | |
| 262 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, | |
| 263 content::RESOURCE_TYPE_IMAGE, true); | |
| 264 EXPECT_FALSE( | |
| 265 LoadingDataCollector::ShouldRecordResponse(file_image_request.get())); | |
| 266 | |
| 267 // ResourceType. | |
| 268 std::unique_ptr<net::URLRequest> sub_frame_request = | |
| 269 CreateURLRequest(GURL("http://www.google.com/frame.html"), net::MEDIUM, | |
| 270 content::RESOURCE_TYPE_SUB_FRAME, true); | |
| 271 EXPECT_FALSE( | |
| 272 LoadingDataCollector::ShouldRecordResponse(sub_frame_request.get())); | |
| 273 | |
| 274 std::unique_ptr<net::URLRequest> font_request = | |
| 275 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
| 276 net::MEDIUM, content::RESOURCE_TYPE_FONT_RESOURCE, true); | |
| 277 EXPECT_TRUE(LoadingDataCollector::ShouldRecordResponse(font_request.get())); | |
| 278 | |
| 279 // From MIME Type. | |
| 280 url_request_job_factory_.set_mime_type("image/png"); | |
| 281 std::unique_ptr<net::URLRequest> prefetch_image_request = | |
| 282 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 283 content::RESOURCE_TYPE_PREFETCH, true); | |
| 284 EXPECT_TRUE( | |
| 285 LoadingDataCollector::ShouldRecordResponse(prefetch_image_request.get())); | |
| 286 | |
| 287 url_request_job_factory_.set_mime_type("image/my-wonderful-format"); | |
| 288 std::unique_ptr<net::URLRequest> prefetch_unknown_image_request = | |
| 289 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 290 content::RESOURCE_TYPE_PREFETCH, true); | |
| 291 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
| 292 prefetch_unknown_image_request.get())); | |
| 293 | |
| 294 url_request_job_factory_.set_mime_type("font/woff"); | |
| 295 std::unique_ptr<net::URLRequest> prefetch_font_request = | |
| 296 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
| 297 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true); | |
| 298 EXPECT_TRUE( | |
| 299 LoadingDataCollector::ShouldRecordResponse(prefetch_font_request.get())); | |
| 300 | |
| 301 url_request_job_factory_.set_mime_type("font/woff-woff"); | |
| 302 std::unique_ptr<net::URLRequest> prefetch_unknown_font_request = | |
| 303 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
| 304 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true); | |
| 305 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
| 306 prefetch_unknown_font_request.get())); | |
| 307 | |
| 308 // Not main frame. | |
| 309 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest( | |
| 310 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM, | |
| 311 content::RESOURCE_TYPE_FONT_RESOURCE, false); | |
| 312 EXPECT_FALSE( | |
| 313 LoadingDataCollector::ShouldRecordResponse(font_request_sub_frame.get())); | |
| 314 } | |
| 315 | |
| 316 } // namespace predictors | |
| OLD | NEW |