| 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> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/test/histogram_tester.h" |
| 16 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 17 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 17 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 18 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 20 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 21 #include "net/url_request/redirect_info.h" | 22 #include "net/url_request/redirect_info.h" |
| 22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 38 const GURL& main_frame_url, | 39 const GURL& main_frame_url, |
| 39 const std::vector<GURL>& urls) | 40 const std::vector<GURL>& urls) |
| 40 : ResourcePrefetcher(delegate, config, main_frame_url, urls) {} | 41 : ResourcePrefetcher(delegate, config, main_frame_url, urls) {} |
| 41 | 42 |
| 42 ~TestResourcePrefetcher() override {} | 43 ~TestResourcePrefetcher() override {} |
| 43 | 44 |
| 44 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); | 45 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); |
| 45 | 46 |
| 46 void ReadFullResponse(net::URLRequest* request) override { | 47 void ReadFullResponse(net::URLRequest* request) override { |
| 47 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); | 48 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); |
| 49 RequestComplete(request); |
| 48 FinishRequest(request); | 50 FinishRequest(request); |
| 49 } | 51 } |
| 50 | 52 |
| 51 private: | 53 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcher); | 54 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcher); |
| 53 }; | 55 }; |
| 54 | 56 |
| 55 | 57 |
| 56 // Delegate for ResourcePrefetcher. | 58 // Delegate for ResourcePrefetcher. |
| 57 class TestResourcePrefetcherDelegate : public ResourcePrefetcher::Delegate { | 59 class TestResourcePrefetcherDelegate : public ResourcePrefetcher::Delegate { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 GURL("http://yahoo.com/resource2.png"), | 160 GURL("http://yahoo.com/resource2.png"), |
| 159 GURL("http://yahoo.com/resource3.png"), | 161 GURL("http://yahoo.com/resource3.png"), |
| 160 GURL("http://m.google.com/resource1.jpg"), | 162 GURL("http://m.google.com/resource1.jpg"), |
| 161 GURL("http://www.google.com/resource3.html"), | 163 GURL("http://www.google.com/resource3.html"), |
| 162 GURL("http://m.google.com/resource2.html"), | 164 GURL("http://m.google.com/resource2.html"), |
| 163 GURL("http://m.google.com/resource3.css"), | 165 GURL("http://m.google.com/resource3.css"), |
| 164 GURL("http://m.google.com/resource4.png"), | 166 GURL("http://m.google.com/resource4.png"), |
| 165 GURL("http://yahoo.com/resource4.png"), | 167 GURL("http://yahoo.com/resource4.png"), |
| 166 GURL("http://yahoo.com/resource5.png")}; | 168 GURL("http://yahoo.com/resource5.png")}; |
| 167 | 169 |
| 168 NavigationID navigation_id = CreateNavigationID(1, main_frame_url.spec()); | |
| 169 | |
| 170 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, config_, | 170 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, config_, |
| 171 main_frame_url, urls)); | 171 main_frame_url, urls)); |
| 172 | 172 |
| 173 // Starting the prefetcher maxes out the number of possible requests. | 173 // Starting the prefetcher maxes out the number of possible requests. |
| 174 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 174 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
| 175 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 175 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 176 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 176 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
| 177 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 177 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
| 178 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 178 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
| 179 | 179 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 230 |
| 231 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { | 231 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { |
| 232 GURL main_frame_url("http://www.google.com"); | 232 GURL main_frame_url("http://www.google.com"); |
| 233 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), | 233 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), |
| 234 GURL("http://www.google.com/resource2.png"), | 234 GURL("http://www.google.com/resource2.png"), |
| 235 GURL("http://yahoo.com/resource1.png"), | 235 GURL("http://yahoo.com/resource1.png"), |
| 236 GURL("http://yahoo.com/resource2.png"), | 236 GURL("http://yahoo.com/resource2.png"), |
| 237 GURL("http://yahoo.com/resource3.png"), | 237 GURL("http://yahoo.com/resource3.png"), |
| 238 GURL("http://m.google.com/resource1.jpg")}; | 238 GURL("http://m.google.com/resource1.jpg")}; |
| 239 | 239 |
| 240 NavigationID navigation_id = CreateNavigationID(1, main_frame_url.spec()); | |
| 241 | |
| 242 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, config_, | 240 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, config_, |
| 243 main_frame_url, urls)); | 241 main_frame_url, urls)); |
| 244 | 242 |
| 245 // Starting the prefetcher maxes out the number of possible requests. | 243 // Starting the prefetcher maxes out the number of possible requests. |
| 246 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 244 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
| 247 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 245 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 248 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 246 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
| 249 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 247 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
| 250 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 248 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
| 251 | 249 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 267 CheckPrefetcherState(1, 1, 1); | 265 CheckPrefetcherState(1, 1, 1); |
| 268 | 266 |
| 269 // Expect the final call. | 267 // Expect the final call. |
| 270 EXPECT_CALL(prefetcher_delegate_, | 268 EXPECT_CALL(prefetcher_delegate_, |
| 271 ResourcePrefetcherFinished(Eq(prefetcher_.get()))); | 269 ResourcePrefetcherFinished(Eq(prefetcher_.get()))); |
| 272 | 270 |
| 273 OnResponse("http://m.google.com/resource1.jpg"); | 271 OnResponse("http://m.google.com/resource1.jpg"); |
| 274 CheckPrefetcherState(0, 1, 0); | 272 CheckPrefetcherState(0, 1, 0); |
| 275 } | 273 } |
| 276 | 274 |
| 275 TEST_F(ResourcePrefetcherTest, TestHistogramsCollected) { |
| 276 base::HistogramTester histogram_tester; |
| 277 GURL main_frame_url("http://www.google.com"); |
| 278 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), |
| 279 GURL("http://www.google.com/resource2.png"), |
| 280 GURL("http://www.google.com/resource3.png"), |
| 281 GURL("http://www.google.com/resource4.png"), |
| 282 GURL("http://www.google.com/resource5.png"), |
| 283 GURL("http://www.google.com/resource6.png")}; |
| 284 |
| 285 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
| 286 &prefetcher_delegate_, config_, main_frame_url, urls); |
| 287 |
| 288 // Starting the prefetcher maxes out the number of possible requests. |
| 289 AddStartUrlRequestExpectation("http://www.google.com/resource1.png"); |
| 290 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 291 AddStartUrlRequestExpectation("http://www.google.com/resource3.png"); |
| 292 |
| 293 prefetcher_->Start(); |
| 294 |
| 295 AddStartUrlRequestExpectation("http://www.google.com/resource4.png"); |
| 296 OnResponse("http://www.google.com/resource1.png"); |
| 297 histogram_tester.ExpectTotalCount( |
| 298 internal::kResourcePrefetchPredictorCachePatternHistogram, 1); |
| 299 |
| 300 // Failed prefetches aren't counted. |
| 301 AddStartUrlRequestExpectation("http://www.google.com/resource5.png"); |
| 302 OnReceivedRedirect("http://www.google.com/resource2.png"); |
| 303 |
| 304 AddStartUrlRequestExpectation("http://www.google.com/resource6.png"); |
| 305 OnAuthRequired("http://www.google.com/resource3.png"); |
| 306 |
| 307 OnCertificateRequested("http://www.google.com/resource4.png"); |
| 308 |
| 309 OnSSLCertificateError("http://www.google.com/resource5.png"); |
| 310 histogram_tester.ExpectTotalCount( |
| 311 internal::kResourcePrefetchPredictorCachePatternHistogram, 1); |
| 312 |
| 313 // Expect the final call. |
| 314 EXPECT_CALL(prefetcher_delegate_, |
| 315 ResourcePrefetcherFinished(Eq(prefetcher_.get()))); |
| 316 |
| 317 OnResponse("http://www.google.com/resource6.png"); |
| 318 histogram_tester.ExpectTotalCount( |
| 319 internal::kResourcePrefetchPredictorCachePatternHistogram, 2); |
| 320 histogram_tester.ExpectBucketCount( |
| 321 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, 2, 1); |
| 322 histogram_tester.ExpectTotalCount( |
| 323 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, 1); |
| 324 } |
| 325 |
| 277 } // namespace predictors | 326 } // namespace predictors |
| OLD | NEW |