OLD | NEW |
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_prefetcher.h" | 5 #include "chrome/browser/predictors/resource_prefetcher.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
24 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 using testing::Eq; | 28 using testing::Eq; |
29 using testing::Property; | 29 using testing::Property; |
30 | 30 |
31 namespace predictors { | 31 namespace predictors { |
32 | 32 |
| 33 constexpr size_t kMaxConcurrentRequests = 5; |
| 34 constexpr size_t kMaxConcurrentRequestsPerHost = 2; |
| 35 |
33 // Wrapper over the ResourcePrefetcher that stubs out the StartURLRequest call | 36 // Wrapper over the ResourcePrefetcher that stubs out the StartURLRequest call |
34 // since we do not want to do network fetches in this unittest. | 37 // since we do not want to do network fetches in this unittest. |
35 class TestResourcePrefetcher : public ResourcePrefetcher { | 38 class TestResourcePrefetcher : public ResourcePrefetcher { |
36 public: | 39 public: |
37 TestResourcePrefetcher(ResourcePrefetcher::Delegate* delegate, | 40 TestResourcePrefetcher(ResourcePrefetcher::Delegate* delegate, |
38 const ResourcePrefetchPredictorConfig& config, | 41 size_t max_concurrent_requests, |
| 42 size_t max_concurrent_requests_per_host, |
39 const GURL& main_frame_url, | 43 const GURL& main_frame_url, |
40 const std::vector<GURL>& urls) | 44 const std::vector<GURL>& urls) |
41 : ResourcePrefetcher(delegate, config, main_frame_url, urls) {} | 45 : ResourcePrefetcher(delegate, |
| 46 max_concurrent_requests, |
| 47 max_concurrent_requests_per_host, |
| 48 main_frame_url, |
| 49 urls) {} |
42 | 50 |
43 ~TestResourcePrefetcher() override {} | 51 ~TestResourcePrefetcher() override {} |
44 | 52 |
45 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); | 53 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); |
46 | 54 |
47 void ReadFullResponse(net::URLRequest* request) override { | 55 void ReadFullResponse(net::URLRequest* request) override { |
48 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); | 56 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); |
49 RequestComplete(request); | 57 RequestComplete(request); |
50 FinishRequest(request); | 58 FinishRequest(request); |
51 } | 59 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 void OnSSLCertificateError(const std::string& url) { | 145 void OnSSLCertificateError(const std::string& url) { |
138 prefetcher_->OnSSLCertificateError(GetInFlightRequest(url), | 146 prefetcher_->OnSSLCertificateError(GetInFlightRequest(url), |
139 net::SSLInfo(), false); | 147 net::SSLInfo(), false); |
140 } | 148 } |
141 void OnResponse(const std::string& url) { | 149 void OnResponse(const std::string& url) { |
142 prefetcher_->OnResponseStarted(GetInFlightRequest(url), net::OK); | 150 prefetcher_->OnResponseStarted(GetInFlightRequest(url), net::OK); |
143 } | 151 } |
144 | 152 |
145 base::MessageLoop loop_; | 153 base::MessageLoop loop_; |
146 content::TestBrowserThread io_thread_; | 154 content::TestBrowserThread io_thread_; |
147 ResourcePrefetchPredictorConfig config_; | |
148 TestResourcePrefetcherDelegate prefetcher_delegate_; | 155 TestResourcePrefetcherDelegate prefetcher_delegate_; |
149 std::unique_ptr<TestResourcePrefetcher> prefetcher_; | 156 std::unique_ptr<TestResourcePrefetcher> prefetcher_; |
150 | 157 |
151 private: | 158 private: |
152 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcherTest); | 159 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcherTest); |
153 }; | 160 }; |
154 | 161 |
155 ResourcePrefetcherTest::ResourcePrefetcherTest() | 162 ResourcePrefetcherTest::ResourcePrefetcherTest() |
156 : loop_(base::MessageLoop::TYPE_IO), | 163 : loop_(base::MessageLoop::TYPE_IO), |
157 io_thread_(content::BrowserThread::IO, &loop_), | 164 io_thread_(content::BrowserThread::IO, &loop_), |
158 prefetcher_delegate_(&loop_) { | 165 prefetcher_delegate_(&loop_) {} |
159 config_.max_prefetches_inflight_per_host_per_navigation = 2; | |
160 } | |
161 | 166 |
162 ResourcePrefetcherTest::~ResourcePrefetcherTest() { | 167 ResourcePrefetcherTest::~ResourcePrefetcherTest() { |
163 } | 168 } |
164 | 169 |
165 TEST_F(ResourcePrefetcherTest, TestPrefetcherFinishes) { | 170 TEST_F(ResourcePrefetcherTest, TestPrefetcherFinishes) { |
166 GURL main_frame_url("http://www.google.com"); | 171 GURL main_frame_url("http://www.google.com"); |
167 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), | 172 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), |
168 GURL("http://www.google.com/resource2.png"), | 173 GURL("http://www.google.com/resource2.png"), |
169 GURL("http://yahoo.com/resource1.png"), | 174 GURL("http://yahoo.com/resource1.png"), |
170 GURL("http://yahoo.com/resource2.png"), | 175 GURL("http://yahoo.com/resource2.png"), |
171 GURL("http://yahoo.com/resource3.png"), | 176 GURL("http://yahoo.com/resource3.png"), |
172 GURL("http://m.google.com/resource1.jpg"), | 177 GURL("http://m.google.com/resource1.jpg"), |
173 GURL("http://www.google.com/resource3.html"), | 178 GURL("http://www.google.com/resource3.html"), |
174 GURL("http://m.google.com/resource2.html"), | 179 GURL("http://m.google.com/resource2.html"), |
175 GURL("http://m.google.com/resource3.css"), | 180 GURL("http://m.google.com/resource3.css"), |
176 GURL("http://m.google.com/resource4.png"), | 181 GURL("http://m.google.com/resource4.png"), |
177 GURL("http://yahoo.com/resource4.png"), | 182 GURL("http://yahoo.com/resource4.png"), |
178 GURL("http://yahoo.com/resource5.png")}; | 183 GURL("http://yahoo.com/resource5.png")}; |
179 | 184 |
180 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, config_, | 185 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
181 main_frame_url, urls)); | 186 &prefetcher_delegate_, kMaxConcurrentRequests, |
| 187 kMaxConcurrentRequestsPerHost, main_frame_url, urls); |
182 | 188 |
183 // Starting the prefetcher maxes out the number of possible requests. | 189 // Starting the prefetcher maxes out the number of possible requests. |
184 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 190 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
185 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 191 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
186 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 192 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
187 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 193 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
188 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 194 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
189 | 195 |
190 prefetcher_->Start(); | 196 prefetcher_->Start(); |
191 CheckPrefetcherState(5, 7, 3); | 197 CheckPrefetcherState(5, 7, 3); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 246 |
241 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { | 247 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { |
242 GURL main_frame_url("http://www.google.com"); | 248 GURL main_frame_url("http://www.google.com"); |
243 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), | 249 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), |
244 GURL("http://www.google.com/resource2.png"), | 250 GURL("http://www.google.com/resource2.png"), |
245 GURL("http://yahoo.com/resource1.png"), | 251 GURL("http://yahoo.com/resource1.png"), |
246 GURL("http://yahoo.com/resource2.png"), | 252 GURL("http://yahoo.com/resource2.png"), |
247 GURL("http://yahoo.com/resource3.png"), | 253 GURL("http://yahoo.com/resource3.png"), |
248 GURL("http://m.google.com/resource1.jpg")}; | 254 GURL("http://m.google.com/resource1.jpg")}; |
249 | 255 |
250 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, config_, | 256 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
251 main_frame_url, urls)); | 257 &prefetcher_delegate_, kMaxConcurrentRequests, |
| 258 kMaxConcurrentRequestsPerHost, main_frame_url, urls); |
252 | 259 |
253 // Starting the prefetcher maxes out the number of possible requests. | 260 // Starting the prefetcher maxes out the number of possible requests. |
254 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 261 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
255 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 262 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
256 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 263 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
257 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 264 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
258 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 265 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
259 | 266 |
260 prefetcher_->Start(); | 267 prefetcher_->Start(); |
261 CheckPrefetcherState(5, 1, 3); | 268 CheckPrefetcherState(5, 1, 3); |
(...skipping 24 matching lines...) Expand all Loading... |
286 base::HistogramTester histogram_tester; | 293 base::HistogramTester histogram_tester; |
287 GURL main_frame_url("http://www.google.com"); | 294 GURL main_frame_url("http://www.google.com"); |
288 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), | 295 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), |
289 GURL("http://www.google.com/resource2.png"), | 296 GURL("http://www.google.com/resource2.png"), |
290 GURL("http://www.google.com/resource3.png"), | 297 GURL("http://www.google.com/resource3.png"), |
291 GURL("http://www.google.com/resource4.png"), | 298 GURL("http://www.google.com/resource4.png"), |
292 GURL("http://www.google.com/resource5.png"), | 299 GURL("http://www.google.com/resource5.png"), |
293 GURL("http://www.google.com/resource6.png")}; | 300 GURL("http://www.google.com/resource6.png")}; |
294 | 301 |
295 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( | 302 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
296 &prefetcher_delegate_, config_, main_frame_url, urls); | 303 &prefetcher_delegate_, kMaxConcurrentRequests, |
| 304 kMaxConcurrentRequestsPerHost, main_frame_url, urls); |
297 | 305 |
298 // Starting the prefetcher maxes out the number of possible requests. | 306 // Starting the prefetcher maxes out the number of possible requests. |
299 AddStartUrlRequestExpectation("http://www.google.com/resource1.png"); | 307 AddStartUrlRequestExpectation("http://www.google.com/resource1.png"); |
300 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 308 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
301 AddStartUrlRequestExpectation("http://www.google.com/resource3.png"); | 309 AddStartUrlRequestExpectation("http://www.google.com/resource3.png"); |
302 | 310 |
303 prefetcher_->Start(); | 311 prefetcher_->Start(); |
304 | 312 |
305 AddStartUrlRequestExpectation("http://www.google.com/resource4.png"); | 313 AddStartUrlRequestExpectation("http://www.google.com/resource4.png"); |
306 OnResponse("http://www.google.com/resource1.png"); | 314 OnResponse("http://www.google.com/resource1.png"); |
(...skipping 27 matching lines...) Expand all Loading... |
334 } | 342 } |
335 | 343 |
336 TEST_F(ResourcePrefetcherTest, TestReferrer) { | 344 TEST_F(ResourcePrefetcherTest, TestReferrer) { |
337 std::string url = "https://www.notgoogle.com/cats.html"; | 345 std::string url = "https://www.notgoogle.com/cats.html"; |
338 std::string https_resource = "https://www.google.com/resource1.png"; | 346 std::string https_resource = "https://www.google.com/resource1.png"; |
339 std::string http_resource = "http://www.google.com/resource1.png"; | 347 std::string http_resource = "http://www.google.com/resource1.png"; |
340 | 348 |
341 std::vector<GURL> urls = {GURL(https_resource), GURL(http_resource)}; | 349 std::vector<GURL> urls = {GURL(https_resource), GURL(http_resource)}; |
342 | 350 |
343 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( | 351 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
344 &prefetcher_delegate_, config_, GURL(url), urls); | 352 &prefetcher_delegate_, kMaxConcurrentRequests, |
| 353 kMaxConcurrentRequestsPerHost, GURL(url), urls); |
345 | 354 |
346 AddStartUrlRequestExpectation(https_resource); | 355 AddStartUrlRequestExpectation(https_resource); |
347 AddStartUrlRequestExpectation(http_resource); | 356 AddStartUrlRequestExpectation(http_resource); |
348 prefetcher_->Start(); | 357 prefetcher_->Start(); |
349 | 358 |
350 net::URLRequest* request = GetInFlightRequest(https_resource); | 359 net::URLRequest* request = GetInFlightRequest(https_resource); |
351 EXPECT_TRUE(request); | 360 EXPECT_TRUE(request); |
352 EXPECT_EQ(url, request->referrer()); | 361 EXPECT_EQ(url, request->referrer()); |
353 | 362 |
354 request = GetInFlightRequest(http_resource); | 363 request = GetInFlightRequest(http_resource); |
355 EXPECT_TRUE(request); | 364 EXPECT_TRUE(request); |
356 EXPECT_EQ("", request->referrer()); | 365 EXPECT_EQ("", request->referrer()); |
357 | 366 |
358 OnResponse(https_resource); | 367 OnResponse(https_resource); |
359 OnResponse(http_resource); | 368 OnResponse(http_resource); |
360 | 369 |
361 // Expect the final call. | 370 // Expect the final call. |
362 EXPECT_TRUE( | 371 EXPECT_TRUE( |
363 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); | 372 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
364 } | 373 } |
365 | 374 |
366 } // namespace predictors | 375 } // namespace predictors |
OLD | NEW |