| 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 "base/test/histogram_tester.h" |
| 17 #include "chrome/browser/predictors/loading_test_util.h" | 17 #include "chrome/browser/predictors/loading_test_util.h" |
| 18 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | |
| 19 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 22 #include "net/url_request/redirect_info.h" | 22 #include "net/url_request/redirect_info.h" |
| 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; | 33 constexpr size_t kMaxConcurrentRequests = 5; |
| 34 constexpr size_t kMaxConcurrentRequestsPerHost = 2; | 34 constexpr size_t kMaxConcurrentRequestsPerHost = 2; |
| 35 | 35 |
| 36 // Delegate for ResourcePrefetcher. |
| 37 class TestResourcePrefetcherDelegate |
| 38 : public ResourcePrefetcher::Delegate, |
| 39 public base::SupportsWeakPtr<TestResourcePrefetcherDelegate> { |
| 40 public: |
| 41 TestResourcePrefetcherDelegate() = default; |
| 42 ~TestResourcePrefetcherDelegate() override = default; |
| 43 |
| 44 void ResourcePrefetcherFinished( |
| 45 ResourcePrefetcher* prefetcher, |
| 46 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) override { |
| 47 prefetcher_ = prefetcher; |
| 48 } |
| 49 |
| 50 bool ResourcePrefetcherFinishedCalled(ResourcePrefetcher* for_prefetcher) { |
| 51 ResourcePrefetcher* prefetcher = prefetcher_; |
| 52 prefetcher_ = nullptr; |
| 53 return prefetcher == for_prefetcher; |
| 54 } |
| 55 |
| 56 private: |
| 57 ResourcePrefetcher* prefetcher_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcherDelegate); |
| 60 }; |
| 61 |
| 36 // Wrapper over the ResourcePrefetcher that stubs out the StartURLRequest call | 62 // Wrapper over the ResourcePrefetcher that stubs out the StartURLRequest call |
| 37 // since we do not want to do network fetches in this unittest. | 63 // since we do not want to do network fetches in this unittest. |
| 38 class TestResourcePrefetcher : public ResourcePrefetcher { | 64 class TestResourcePrefetcher : public ResourcePrefetcher { |
| 39 public: | 65 public: |
| 40 TestResourcePrefetcher(ResourcePrefetcher::Delegate* delegate, | 66 TestResourcePrefetcher( |
| 41 size_t max_concurrent_requests, | 67 TestResourcePrefetcherDelegate* delegate, |
| 42 size_t max_concurrent_requests_per_host, | 68 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 43 const GURL& main_frame_url, | 69 size_t max_concurrent_requests, |
| 44 const std::vector<GURL>& urls) | 70 size_t max_concurrent_requests_per_host, |
| 45 : ResourcePrefetcher(delegate, | 71 const GURL& main_frame_url, |
| 72 const std::vector<GURL>& urls) |
| 73 : ResourcePrefetcher(delegate->AsWeakPtr(), |
| 74 context_getter, |
| 46 max_concurrent_requests, | 75 max_concurrent_requests, |
| 47 max_concurrent_requests_per_host, | 76 max_concurrent_requests_per_host, |
| 48 main_frame_url, | 77 main_frame_url, |
| 49 urls) {} | 78 urls) {} |
| 50 | 79 |
| 51 ~TestResourcePrefetcher() override {} | 80 ~TestResourcePrefetcher() override {} |
| 52 | 81 |
| 53 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); | 82 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); |
| 54 | 83 |
| 55 void ReadFullResponse(net::URLRequest* request) override { | 84 void ReadFullResponse(net::URLRequest* request) override { |
| 56 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); | 85 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); |
| 57 RequestComplete(request); | 86 RequestComplete(request); |
| 58 FinishRequest(request); | 87 FinishRequest(request); |
| 59 } | 88 } |
| 60 | 89 |
| 61 private: | 90 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcher); | 91 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcher); |
| 63 }; | 92 }; |
| 64 | 93 |
| 65 | |
| 66 // Delegate for ResourcePrefetcher. | |
| 67 class TestResourcePrefetcherDelegate : public ResourcePrefetcher::Delegate { | |
| 68 public: | |
| 69 explicit TestResourcePrefetcherDelegate(base::MessageLoop* loop) | |
| 70 : request_context_getter_( | |
| 71 new net::TestURLRequestContextGetter(loop->task_runner())) {} | |
| 72 ~TestResourcePrefetcherDelegate() override {} | |
| 73 | |
| 74 net::URLRequestContext* GetURLRequestContext() override { | |
| 75 return request_context_getter_->GetURLRequestContext(); | |
| 76 } | |
| 77 | |
| 78 void ResourcePrefetcherFinished( | |
| 79 ResourcePrefetcher* prefetcher, | |
| 80 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) override { | |
| 81 prefetcher_ = prefetcher; | |
| 82 } | |
| 83 | |
| 84 bool ResourcePrefetcherFinishedCalled(ResourcePrefetcher* for_prefetcher) { | |
| 85 ResourcePrefetcher* prefetcher = prefetcher_; | |
| 86 prefetcher_ = nullptr; | |
| 87 return prefetcher == for_prefetcher; | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | |
| 92 ResourcePrefetcher* prefetcher_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcherDelegate); | |
| 95 }; | |
| 96 | |
| 97 | |
| 98 // The following unittest tests most of the ResourcePrefetcher except for: | 94 // The following unittest tests most of the ResourcePrefetcher except for: |
| 99 // 1. Call to ReadFullResponse. There does not seem to be a good way to test the | 95 // 1. Call to ReadFullResponse. There does not seem to be a good way to test the |
| 100 // function in a unittest, and probably requires a browser_test. | 96 // function in a unittest, and probably requires a browser_test. |
| 101 // 2. Setting of the Prefetch status for cache vs non cache. | 97 // 2. Setting of the Prefetch status for cache vs non cache. |
| 102 class ResourcePrefetcherTest : public testing::Test { | 98 class ResourcePrefetcherTest : public testing::Test { |
| 103 public: | 99 public: |
| 104 ResourcePrefetcherTest(); | 100 ResourcePrefetcherTest(); |
| 105 ~ResourcePrefetcherTest() override; | 101 ~ResourcePrefetcherTest() override; |
| 106 | 102 |
| 107 protected: | 103 protected: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 prefetcher_->OnCertificateRequested(GetInFlightRequest(url), NULL); | 139 prefetcher_->OnCertificateRequested(GetInFlightRequest(url), NULL); |
| 144 } | 140 } |
| 145 void OnSSLCertificateError(const std::string& url) { | 141 void OnSSLCertificateError(const std::string& url) { |
| 146 prefetcher_->OnSSLCertificateError(GetInFlightRequest(url), | 142 prefetcher_->OnSSLCertificateError(GetInFlightRequest(url), |
| 147 net::SSLInfo(), false); | 143 net::SSLInfo(), false); |
| 148 } | 144 } |
| 149 void OnResponse(const std::string& url) { | 145 void OnResponse(const std::string& url) { |
| 150 prefetcher_->OnResponseStarted(GetInFlightRequest(url), net::OK); | 146 prefetcher_->OnResponseStarted(GetInFlightRequest(url), net::OK); |
| 151 } | 147 } |
| 152 | 148 |
| 153 base::MessageLoop loop_; | 149 content::TestBrowserThreadBundle thread_bundle_; |
| 154 content::TestBrowserThread io_thread_; | |
| 155 TestResourcePrefetcherDelegate prefetcher_delegate_; | 150 TestResourcePrefetcherDelegate prefetcher_delegate_; |
| 156 std::unique_ptr<TestResourcePrefetcher> prefetcher_; | 151 std::unique_ptr<TestResourcePrefetcher> prefetcher_; |
| 152 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
| 157 | 153 |
| 158 private: | 154 private: |
| 159 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcherTest); | 155 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcherTest); |
| 160 }; | 156 }; |
| 161 | 157 |
| 162 ResourcePrefetcherTest::ResourcePrefetcherTest() | 158 ResourcePrefetcherTest::ResourcePrefetcherTest() |
| 163 : loop_(base::MessageLoop::TYPE_IO), | 159 : prefetcher_delegate_(), |
| 164 io_thread_(content::BrowserThread::IO, &loop_), | 160 context_getter_(new net::TestURLRequestContextGetter( |
| 165 prefetcher_delegate_(&loop_) {} | 161 base::ThreadTaskRunnerHandle::Get())) {} |
| 166 | 162 |
| 167 ResourcePrefetcherTest::~ResourcePrefetcherTest() { | 163 ResourcePrefetcherTest::~ResourcePrefetcherTest() { |
| 168 } | 164 } |
| 169 | 165 |
| 170 TEST_F(ResourcePrefetcherTest, TestPrefetcherFinishes) { | 166 TEST_F(ResourcePrefetcherTest, TestPrefetcherFinishes) { |
| 171 GURL main_frame_url("http://www.google.com"); | 167 GURL main_frame_url("http://www.google.com"); |
| 172 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), | 168 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), |
| 173 GURL("http://www.google.com/resource2.png"), | 169 GURL("http://www.google.com/resource2.png"), |
| 174 GURL("http://yahoo.com/resource1.png"), | 170 GURL("http://yahoo.com/resource1.png"), |
| 175 GURL("http://yahoo.com/resource2.png"), | 171 GURL("http://yahoo.com/resource2.png"), |
| 176 GURL("http://yahoo.com/resource3.png"), | 172 GURL("http://yahoo.com/resource3.png"), |
| 177 GURL("http://m.google.com/resource1.jpg"), | 173 GURL("http://m.google.com/resource1.jpg"), |
| 178 GURL("http://www.google.com/resource3.html"), | 174 GURL("http://www.google.com/resource3.html"), |
| 179 GURL("http://m.google.com/resource2.html"), | 175 GURL("http://m.google.com/resource2.html"), |
| 180 GURL("http://m.google.com/resource3.css"), | 176 GURL("http://m.google.com/resource3.css"), |
| 181 GURL("http://m.google.com/resource4.png"), | 177 GURL("http://m.google.com/resource4.png"), |
| 182 GURL("http://yahoo.com/resource4.png"), | 178 GURL("http://yahoo.com/resource4.png"), |
| 183 GURL("http://yahoo.com/resource5.png")}; | 179 GURL("http://yahoo.com/resource5.png")}; |
| 184 | 180 |
| 185 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( | 181 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
| 186 &prefetcher_delegate_, kMaxConcurrentRequests, | 182 &prefetcher_delegate_, context_getter_, kMaxConcurrentRequests, |
| 187 kMaxConcurrentRequestsPerHost, main_frame_url, urls); | 183 kMaxConcurrentRequestsPerHost, main_frame_url, urls); |
| 188 | 184 |
| 189 // Starting the prefetcher maxes out the number of possible requests. | 185 // Starting the prefetcher maxes out the number of possible requests. |
| 190 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 186 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
| 191 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 187 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 192 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 188 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
| 193 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 189 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
| 194 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 190 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
| 195 | 191 |
| 196 prefetcher_->Start(); | 192 prefetcher_->Start(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 228 |
| 233 OnCertificateRequested("http://m.google.com/resource4.png"); | 229 OnCertificateRequested("http://m.google.com/resource4.png"); |
| 234 CheckPrefetcherState(2, 0, 2); | 230 CheckPrefetcherState(2, 0, 2); |
| 235 | 231 |
| 236 OnAuthRequired("http://m.google.com/resource3.css"); | 232 OnAuthRequired("http://m.google.com/resource3.css"); |
| 237 CheckPrefetcherState(1, 0, 1); | 233 CheckPrefetcherState(1, 0, 1); |
| 238 | 234 |
| 239 OnResponse("http://yahoo.com/resource3.png"); | 235 OnResponse("http://yahoo.com/resource3.png"); |
| 240 CheckPrefetcherState(0, 0, 0); | 236 CheckPrefetcherState(0, 0, 0); |
| 241 | 237 |
| 238 base::RunLoop().RunUntilIdle(); |
| 242 // Expect the final call. | 239 // Expect the final call. |
| 243 EXPECT_TRUE( | 240 EXPECT_TRUE( |
| 244 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); | 241 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
| 245 } | 242 } |
| 246 | 243 |
| 247 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { | 244 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { |
| 248 GURL main_frame_url("http://www.google.com"); | 245 GURL main_frame_url("http://www.google.com"); |
| 249 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), | 246 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), |
| 250 GURL("http://www.google.com/resource2.png"), | 247 GURL("http://www.google.com/resource2.png"), |
| 251 GURL("http://yahoo.com/resource1.png"), | 248 GURL("http://yahoo.com/resource1.png"), |
| 252 GURL("http://yahoo.com/resource2.png"), | 249 GURL("http://yahoo.com/resource2.png"), |
| 253 GURL("http://yahoo.com/resource3.png"), | 250 GURL("http://yahoo.com/resource3.png"), |
| 254 GURL("http://m.google.com/resource1.jpg")}; | 251 GURL("http://m.google.com/resource1.jpg")}; |
| 255 | 252 |
| 256 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( | 253 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
| 257 &prefetcher_delegate_, kMaxConcurrentRequests, | 254 &prefetcher_delegate_, context_getter_, kMaxConcurrentRequests, |
| 258 kMaxConcurrentRequestsPerHost, main_frame_url, urls); | 255 kMaxConcurrentRequestsPerHost, main_frame_url, urls); |
| 259 | 256 |
| 260 // Starting the prefetcher maxes out the number of possible requests. | 257 // Starting the prefetcher maxes out the number of possible requests. |
| 261 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 258 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
| 262 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 259 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 263 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 260 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
| 264 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 261 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
| 265 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 262 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
| 266 | 263 |
| 267 prefetcher_->Start(); | 264 prefetcher_->Start(); |
| 268 CheckPrefetcherState(5, 1, 3); | 265 CheckPrefetcherState(5, 1, 3); |
| 269 | 266 |
| 270 OnResponse("http://www.google.com/resource1.html"); | 267 OnResponse("http://www.google.com/resource1.html"); |
| 271 CheckPrefetcherState(4, 1, 3); | 268 CheckPrefetcherState(4, 1, 3); |
| 272 | 269 |
| 273 prefetcher_->Stop(); // No more queueing. | 270 prefetcher_->Stop(); // No more queueing. |
| 274 | 271 |
| 275 OnResponse("http://www.google.com/resource2.png"); | 272 OnResponse("http://www.google.com/resource2.png"); |
| 276 CheckPrefetcherState(3, 1, 2); | 273 CheckPrefetcherState(3, 1, 2); |
| 277 | 274 |
| 278 OnResponse("http://yahoo.com/resource1.png"); | 275 OnResponse("http://yahoo.com/resource1.png"); |
| 279 CheckPrefetcherState(2, 1, 2); | 276 CheckPrefetcherState(2, 1, 2); |
| 280 | 277 |
| 281 OnResponse("http://yahoo.com/resource2.png"); | 278 OnResponse("http://yahoo.com/resource2.png"); |
| 282 CheckPrefetcherState(1, 1, 1); | 279 CheckPrefetcherState(1, 1, 1); |
| 283 | 280 |
| 284 OnResponse("http://m.google.com/resource1.jpg"); | 281 OnResponse("http://m.google.com/resource1.jpg"); |
| 285 CheckPrefetcherState(0, 1, 0); | 282 CheckPrefetcherState(0, 1, 0); |
| 286 | 283 |
| 284 base::RunLoop().RunUntilIdle(); |
| 287 // Expect the final call. | 285 // Expect the final call. |
| 288 EXPECT_TRUE( | 286 EXPECT_TRUE( |
| 289 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); | 287 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
| 290 } | 288 } |
| 291 | 289 |
| 292 TEST_F(ResourcePrefetcherTest, TestHistogramsCollected) { | 290 TEST_F(ResourcePrefetcherTest, TestHistogramsCollected) { |
| 293 base::HistogramTester histogram_tester; | 291 base::HistogramTester histogram_tester; |
| 294 GURL main_frame_url("http://www.google.com"); | 292 GURL main_frame_url("http://www.google.com"); |
| 295 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), | 293 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), |
| 296 GURL("http://www.google.com/resource2.png"), | 294 GURL("http://www.google.com/resource2.png"), |
| 297 GURL("http://www.google.com/resource3.png"), | 295 GURL("http://www.google.com/resource3.png"), |
| 298 GURL("http://www.google.com/resource4.png"), | 296 GURL("http://www.google.com/resource4.png"), |
| 299 GURL("http://www.google.com/resource5.png"), | 297 GURL("http://www.google.com/resource5.png"), |
| 300 GURL("http://www.google.com/resource6.png")}; | 298 GURL("http://www.google.com/resource6.png")}; |
| 301 | 299 |
| 302 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( | 300 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
| 303 &prefetcher_delegate_, kMaxConcurrentRequests, | 301 &prefetcher_delegate_, context_getter_, kMaxConcurrentRequests, |
| 304 kMaxConcurrentRequestsPerHost, main_frame_url, urls); | 302 kMaxConcurrentRequestsPerHost, main_frame_url, urls); |
| 305 | 303 |
| 306 // Starting the prefetcher maxes out the number of possible requests. | 304 // Starting the prefetcher maxes out the number of possible requests. |
| 307 AddStartUrlRequestExpectation("http://www.google.com/resource1.png"); | 305 AddStartUrlRequestExpectation("http://www.google.com/resource1.png"); |
| 308 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 306 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 309 AddStartUrlRequestExpectation("http://www.google.com/resource3.png"); | 307 AddStartUrlRequestExpectation("http://www.google.com/resource3.png"); |
| 310 | 308 |
| 311 prefetcher_->Start(); | 309 prefetcher_->Start(); |
| 312 | 310 |
| 313 AddStartUrlRequestExpectation("http://www.google.com/resource4.png"); | 311 AddStartUrlRequestExpectation("http://www.google.com/resource4.png"); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 329 internal::kResourcePrefetchPredictorCachePatternHistogram, 1); | 327 internal::kResourcePrefetchPredictorCachePatternHistogram, 1); |
| 330 | 328 |
| 331 OnResponse("http://www.google.com/resource6.png"); | 329 OnResponse("http://www.google.com/resource6.png"); |
| 332 histogram_tester.ExpectTotalCount( | 330 histogram_tester.ExpectTotalCount( |
| 333 internal::kResourcePrefetchPredictorCachePatternHistogram, 2); | 331 internal::kResourcePrefetchPredictorCachePatternHistogram, 2); |
| 334 histogram_tester.ExpectBucketCount( | 332 histogram_tester.ExpectBucketCount( |
| 335 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, 2, 1); | 333 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, 2, 1); |
| 336 histogram_tester.ExpectTotalCount( | 334 histogram_tester.ExpectTotalCount( |
| 337 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, 1); | 335 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, 1); |
| 338 | 336 |
| 337 base::RunLoop().RunUntilIdle(); |
| 339 // Expect the final call. | 338 // Expect the final call. |
| 340 EXPECT_TRUE( | 339 EXPECT_TRUE( |
| 341 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); | 340 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
| 342 } | 341 } |
| 343 | 342 |
| 344 TEST_F(ResourcePrefetcherTest, TestReferrer) { | 343 TEST_F(ResourcePrefetcherTest, TestReferrer) { |
| 345 std::string url = "https://www.notgoogle.com/cats.html"; | 344 std::string url = "https://www.notgoogle.com/cats.html"; |
| 346 std::string https_resource = "https://www.google.com/resource1.png"; | 345 std::string https_resource = "https://www.google.com/resource1.png"; |
| 347 std::string http_resource = "http://www.google.com/resource1.png"; | 346 std::string http_resource = "http://www.google.com/resource1.png"; |
| 348 | 347 |
| 349 std::vector<GURL> urls = {GURL(https_resource), GURL(http_resource)}; | 348 std::vector<GURL> urls = {GURL(https_resource), GURL(http_resource)}; |
| 350 | 349 |
| 351 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( | 350 prefetcher_ = base::MakeUnique<TestResourcePrefetcher>( |
| 352 &prefetcher_delegate_, kMaxConcurrentRequests, | 351 &prefetcher_delegate_, context_getter_, kMaxConcurrentRequests, |
| 353 kMaxConcurrentRequestsPerHost, GURL(url), urls); | 352 kMaxConcurrentRequestsPerHost, GURL(url), urls); |
| 354 | 353 |
| 355 AddStartUrlRequestExpectation(https_resource); | 354 AddStartUrlRequestExpectation(https_resource); |
| 356 AddStartUrlRequestExpectation(http_resource); | 355 AddStartUrlRequestExpectation(http_resource); |
| 357 prefetcher_->Start(); | 356 prefetcher_->Start(); |
| 358 | 357 |
| 359 net::URLRequest* request = GetInFlightRequest(https_resource); | 358 net::URLRequest* request = GetInFlightRequest(https_resource); |
| 360 EXPECT_TRUE(request); | 359 EXPECT_TRUE(request); |
| 361 EXPECT_EQ(url, request->referrer()); | 360 EXPECT_EQ(url, request->referrer()); |
| 362 | 361 |
| 363 request = GetInFlightRequest(http_resource); | 362 request = GetInFlightRequest(http_resource); |
| 364 EXPECT_TRUE(request); | 363 EXPECT_TRUE(request); |
| 365 EXPECT_EQ("", request->referrer()); | 364 EXPECT_EQ("", request->referrer()); |
| 366 | 365 |
| 367 OnResponse(https_resource); | 366 OnResponse(https_resource); |
| 368 OnResponse(http_resource); | 367 OnResponse(http_resource); |
| 369 | 368 |
| 369 base::RunLoop().RunUntilIdle(); |
| 370 // Expect the final call. | 370 // Expect the final call. |
| 371 EXPECT_TRUE( | 371 EXPECT_TRUE( |
| 372 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); | 372 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
| 373 } | 373 } |
| 374 | 374 |
| 375 } // namespace predictors | 375 } // namespace predictors |
| OLD | NEW |