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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcher); | 54 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcher); |
55 }; | 55 }; |
56 | 56 |
57 | 57 |
58 // Delegate for ResourcePrefetcher. | 58 // Delegate for ResourcePrefetcher. |
59 class TestResourcePrefetcherDelegate : public ResourcePrefetcher::Delegate { | 59 class TestResourcePrefetcherDelegate : public ResourcePrefetcher::Delegate { |
60 public: | 60 public: |
61 explicit TestResourcePrefetcherDelegate(base::MessageLoop* loop) | 61 explicit TestResourcePrefetcherDelegate(base::MessageLoop* loop) |
62 : request_context_getter_( | 62 : request_context_getter_( |
63 new net::TestURLRequestContextGetter(loop->task_runner())) {} | 63 new net::TestURLRequestContextGetter(loop->task_runner())) {} |
64 ~TestResourcePrefetcherDelegate() { } | 64 ~TestResourcePrefetcherDelegate() override {} |
65 | 65 |
66 net::URLRequestContext* GetURLRequestContext() override { | 66 net::URLRequestContext* GetURLRequestContext() override { |
67 return request_context_getter_->GetURLRequestContext(); | 67 return request_context_getter_->GetURLRequestContext(); |
68 } | 68 } |
69 | 69 |
70 MOCK_METHOD1(ResourcePrefetcherFinished, | 70 void ResourcePrefetcherFinished( |
71 void(ResourcePrefetcher* prefetcher)); | 71 ResourcePrefetcher* prefetcher, |
| 72 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) override { |
| 73 prefetcher_ = prefetcher; |
| 74 } |
| 75 |
| 76 bool ResourcePrefetcherFinishedCalled(ResourcePrefetcher* for_prefetcher) { |
| 77 ResourcePrefetcher* prefetcher = prefetcher_; |
| 78 prefetcher_ = nullptr; |
| 79 return prefetcher == for_prefetcher; |
| 80 } |
72 | 81 |
73 private: | 82 private: |
74 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 83 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 84 ResourcePrefetcher* prefetcher_; |
75 | 85 |
76 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcherDelegate); | 86 DISALLOW_COPY_AND_ASSIGN(TestResourcePrefetcherDelegate); |
77 }; | 87 }; |
78 | 88 |
79 | 89 |
80 // The following unittest tests most of the ResourcePrefetcher except for: | 90 // The following unittest tests most of the ResourcePrefetcher except for: |
81 // 1. Call to ReadFullResponse. There does not seem to be a good way to test the | 91 // 1. Call to ReadFullResponse. There does not seem to be a good way to test the |
82 // function in a unittest, and probably requires a browser_test. | 92 // function in a unittest, and probably requires a browser_test. |
83 // 2. Setting of the Prefetch status for cache vs non cache. | 93 // 2. Setting of the Prefetch status for cache vs non cache. |
84 class ResourcePrefetcherTest : public testing::Test { | 94 class ResourcePrefetcherTest : public testing::Test { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 223 |
214 OnResponse("http://yahoo.com/resource5.png"); | 224 OnResponse("http://yahoo.com/resource5.png"); |
215 CheckPrefetcherState(3, 0, 2); | 225 CheckPrefetcherState(3, 0, 2); |
216 | 226 |
217 OnCertificateRequested("http://m.google.com/resource4.png"); | 227 OnCertificateRequested("http://m.google.com/resource4.png"); |
218 CheckPrefetcherState(2, 0, 2); | 228 CheckPrefetcherState(2, 0, 2); |
219 | 229 |
220 OnAuthRequired("http://m.google.com/resource3.css"); | 230 OnAuthRequired("http://m.google.com/resource3.css"); |
221 CheckPrefetcherState(1, 0, 1); | 231 CheckPrefetcherState(1, 0, 1); |
222 | 232 |
223 // Expect the final call. | |
224 EXPECT_CALL(prefetcher_delegate_, | |
225 ResourcePrefetcherFinished(Eq(prefetcher_.get()))); | |
226 | |
227 OnResponse("http://yahoo.com/resource3.png"); | 233 OnResponse("http://yahoo.com/resource3.png"); |
228 CheckPrefetcherState(0, 0, 0); | 234 CheckPrefetcherState(0, 0, 0); |
| 235 |
| 236 // Expect the final call. |
| 237 EXPECT_TRUE( |
| 238 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
229 } | 239 } |
230 | 240 |
231 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { | 241 TEST_F(ResourcePrefetcherTest, TestPrefetcherStopped) { |
232 GURL main_frame_url("http://www.google.com"); | 242 GURL main_frame_url("http://www.google.com"); |
233 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), | 243 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.html"), |
234 GURL("http://www.google.com/resource2.png"), | 244 GURL("http://www.google.com/resource2.png"), |
235 GURL("http://yahoo.com/resource1.png"), | 245 GURL("http://yahoo.com/resource1.png"), |
236 GURL("http://yahoo.com/resource2.png"), | 246 GURL("http://yahoo.com/resource2.png"), |
237 GURL("http://yahoo.com/resource3.png"), | 247 GURL("http://yahoo.com/resource3.png"), |
238 GURL("http://m.google.com/resource1.jpg")}; | 248 GURL("http://m.google.com/resource1.jpg")}; |
(...skipping 18 matching lines...) Expand all Loading... |
257 | 267 |
258 OnResponse("http://www.google.com/resource2.png"); | 268 OnResponse("http://www.google.com/resource2.png"); |
259 CheckPrefetcherState(3, 1, 2); | 269 CheckPrefetcherState(3, 1, 2); |
260 | 270 |
261 OnResponse("http://yahoo.com/resource1.png"); | 271 OnResponse("http://yahoo.com/resource1.png"); |
262 CheckPrefetcherState(2, 1, 2); | 272 CheckPrefetcherState(2, 1, 2); |
263 | 273 |
264 OnResponse("http://yahoo.com/resource2.png"); | 274 OnResponse("http://yahoo.com/resource2.png"); |
265 CheckPrefetcherState(1, 1, 1); | 275 CheckPrefetcherState(1, 1, 1); |
266 | 276 |
267 // Expect the final call. | |
268 EXPECT_CALL(prefetcher_delegate_, | |
269 ResourcePrefetcherFinished(Eq(prefetcher_.get()))); | |
270 | |
271 OnResponse("http://m.google.com/resource1.jpg"); | 277 OnResponse("http://m.google.com/resource1.jpg"); |
272 CheckPrefetcherState(0, 1, 0); | 278 CheckPrefetcherState(0, 1, 0); |
| 279 |
| 280 // Expect the final call. |
| 281 EXPECT_TRUE( |
| 282 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
273 } | 283 } |
274 | 284 |
275 TEST_F(ResourcePrefetcherTest, TestHistogramsCollected) { | 285 TEST_F(ResourcePrefetcherTest, TestHistogramsCollected) { |
276 base::HistogramTester histogram_tester; | 286 base::HistogramTester histogram_tester; |
277 GURL main_frame_url("http://www.google.com"); | 287 GURL main_frame_url("http://www.google.com"); |
278 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), | 288 std::vector<GURL> urls = {GURL("http://www.google.com/resource1.png"), |
279 GURL("http://www.google.com/resource2.png"), | 289 GURL("http://www.google.com/resource2.png"), |
280 GURL("http://www.google.com/resource3.png"), | 290 GURL("http://www.google.com/resource3.png"), |
281 GURL("http://www.google.com/resource4.png"), | 291 GURL("http://www.google.com/resource4.png"), |
282 GURL("http://www.google.com/resource5.png"), | 292 GURL("http://www.google.com/resource5.png"), |
(...skipping 20 matching lines...) Expand all Loading... |
303 | 313 |
304 AddStartUrlRequestExpectation("http://www.google.com/resource6.png"); | 314 AddStartUrlRequestExpectation("http://www.google.com/resource6.png"); |
305 OnAuthRequired("http://www.google.com/resource3.png"); | 315 OnAuthRequired("http://www.google.com/resource3.png"); |
306 | 316 |
307 OnCertificateRequested("http://www.google.com/resource4.png"); | 317 OnCertificateRequested("http://www.google.com/resource4.png"); |
308 | 318 |
309 OnSSLCertificateError("http://www.google.com/resource5.png"); | 319 OnSSLCertificateError("http://www.google.com/resource5.png"); |
310 histogram_tester.ExpectTotalCount( | 320 histogram_tester.ExpectTotalCount( |
311 internal::kResourcePrefetchPredictorCachePatternHistogram, 1); | 321 internal::kResourcePrefetchPredictorCachePatternHistogram, 1); |
312 | 322 |
313 // Expect the final call. | |
314 EXPECT_CALL(prefetcher_delegate_, | |
315 ResourcePrefetcherFinished(Eq(prefetcher_.get()))); | |
316 | |
317 OnResponse("http://www.google.com/resource6.png"); | 323 OnResponse("http://www.google.com/resource6.png"); |
318 histogram_tester.ExpectTotalCount( | 324 histogram_tester.ExpectTotalCount( |
319 internal::kResourcePrefetchPredictorCachePatternHistogram, 2); | 325 internal::kResourcePrefetchPredictorCachePatternHistogram, 2); |
320 histogram_tester.ExpectBucketCount( | 326 histogram_tester.ExpectBucketCount( |
321 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, 2, 1); | 327 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, 2, 1); |
322 histogram_tester.ExpectTotalCount( | 328 histogram_tester.ExpectTotalCount( |
323 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, 1); | 329 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, 1); |
| 330 |
| 331 // Expect the final call. |
| 332 EXPECT_TRUE( |
| 333 prefetcher_delegate_.ResourcePrefetcherFinishedCalled(prefetcher_.get())); |
324 } | 334 } |
325 | 335 |
326 } // namespace predictors | 336 } // namespace predictors |
OLD | NEW |