| 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" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> |
| 6 | 9 |
| 7 #include "base/macros.h" | 10 #include "base/macros.h" |
| 8 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 11 #include "chrome/browser/predictors/resource_prefetcher.h" | |
| 12 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 14 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 15 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 16 #include "net/url_request/redirect_info.h" | 18 #include "net/url_request/redirect_info.h" |
| 17 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 23 |
| 22 using testing::Eq; | 24 using testing::Eq; |
| 23 using testing::Property; | 25 using testing::Property; |
| 24 | 26 |
| 25 namespace predictors { | 27 namespace predictors { |
| 26 | 28 |
| 27 // Wrapper over the ResourcePrefetcher that stubs out the StartURLRequest call | 29 // Wrapper over the ResourcePrefetcher that stubs out the StartURLRequest call |
| 28 // since we do not want to do network fetches in this unittest. | 30 // since we do not want to do network fetches in this unittest. |
| 29 class TestResourcePrefetcher : public ResourcePrefetcher { | 31 class TestResourcePrefetcher : public ResourcePrefetcher { |
| 30 public: | 32 public: |
| 31 TestResourcePrefetcher(ResourcePrefetcher::Delegate* delegate, | 33 TestResourcePrefetcher(ResourcePrefetcher::Delegate* delegate, |
| 32 const ResourcePrefetchPredictorConfig& config, | 34 const ResourcePrefetchPredictorConfig& config, |
| 33 const NavigationID& navigation_id, | 35 const NavigationID& navigation_id, |
| 34 PrefetchKeyType key_type, | 36 PrefetchKeyType key_type, |
| 35 scoped_ptr<RequestVector> requests) | 37 scoped_ptr<RequestVector> requests) |
| 36 : ResourcePrefetcher(delegate, config, navigation_id, | 38 : ResourcePrefetcher(delegate, |
| 37 key_type, requests.Pass()) { } | 39 config, |
| 40 navigation_id, |
| 41 key_type, |
| 42 std::move(requests)) {} |
| 38 | 43 |
| 39 ~TestResourcePrefetcher() override {} | 44 ~TestResourcePrefetcher() override {} |
| 40 | 45 |
| 41 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); | 46 MOCK_METHOD1(StartURLRequest, void(net::URLRequest* request)); |
| 42 | 47 |
| 43 void ReadFullResponse(net::URLRequest* request) override { | 48 void ReadFullResponse(net::URLRequest* request) override { |
| 44 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); | 49 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); |
| 45 FinishRequest(request, Request::PREFETCH_STATUS_FROM_CACHE); | 50 FinishRequest(request, Request::PREFETCH_STATUS_FROM_CACHE); |
| 46 } | 51 } |
| 47 | 52 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 "http://yahoo.com/resource5.png"))); | 189 "http://yahoo.com/resource5.png"))); |
| 185 | 190 |
| 186 NavigationID navigation_id; | 191 NavigationID navigation_id; |
| 187 navigation_id.render_process_id = 1; | 192 navigation_id.render_process_id = 1; |
| 188 navigation_id.render_frame_id = 2; | 193 navigation_id.render_frame_id = 2; |
| 189 navigation_id.main_frame_url = GURL("http://www.google.com"); | 194 navigation_id.main_frame_url = GURL("http://www.google.com"); |
| 190 | 195 |
| 191 // Needed later for comparison. | 196 // Needed later for comparison. |
| 192 ResourcePrefetcher::RequestVector* requests_ptr = requests.get(); | 197 ResourcePrefetcher::RequestVector* requests_ptr = requests.get(); |
| 193 | 198 |
| 194 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, | 199 prefetcher_.reset( |
| 195 config_, | 200 new TestResourcePrefetcher(&prefetcher_delegate_, config_, navigation_id, |
| 196 navigation_id, | 201 PREFETCH_KEY_TYPE_URL, std::move(requests))); |
| 197 PREFETCH_KEY_TYPE_URL, | |
| 198 requests.Pass())); | |
| 199 | 202 |
| 200 // Starting the prefetcher maxes out the number of possible requests. | 203 // Starting the prefetcher maxes out the number of possible requests. |
| 201 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 204 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
| 202 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 205 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 203 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 206 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
| 204 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 207 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
| 205 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 208 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
| 206 | 209 |
| 207 prefetcher_->Start(); | 210 prefetcher_->Start(); |
| 208 CheckPrefetcherState(5, 7, 3); | 211 CheckPrefetcherState(5, 7, 3); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 "http://m.google.com/resource1.jpg"))); | 310 "http://m.google.com/resource1.jpg"))); |
| 308 | 311 |
| 309 NavigationID navigation_id; | 312 NavigationID navigation_id; |
| 310 navigation_id.render_process_id = 1; | 313 navigation_id.render_process_id = 1; |
| 311 navigation_id.render_frame_id = 2; | 314 navigation_id.render_frame_id = 2; |
| 312 navigation_id.main_frame_url = GURL("http://www.google.com"); | 315 navigation_id.main_frame_url = GURL("http://www.google.com"); |
| 313 | 316 |
| 314 // Needed later for comparison. | 317 // Needed later for comparison. |
| 315 ResourcePrefetcher::RequestVector* requests_ptr = requests.get(); | 318 ResourcePrefetcher::RequestVector* requests_ptr = requests.get(); |
| 316 | 319 |
| 317 prefetcher_.reset(new TestResourcePrefetcher(&prefetcher_delegate_, | 320 prefetcher_.reset( |
| 318 config_, | 321 new TestResourcePrefetcher(&prefetcher_delegate_, config_, navigation_id, |
| 319 navigation_id, | 322 PREFETCH_KEY_TYPE_HOST, std::move(requests))); |
| 320 PREFETCH_KEY_TYPE_HOST, | |
| 321 requests.Pass())); | |
| 322 | 323 |
| 323 // Starting the prefetcher maxes out the number of possible requests. | 324 // Starting the prefetcher maxes out the number of possible requests. |
| 324 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); | 325 AddStartUrlRequestExpectation("http://www.google.com/resource1.html"); |
| 325 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); | 326 AddStartUrlRequestExpectation("http://www.google.com/resource2.png"); |
| 326 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); | 327 AddStartUrlRequestExpectation("http://yahoo.com/resource1.png"); |
| 327 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); | 328 AddStartUrlRequestExpectation("http://yahoo.com/resource2.png"); |
| 328 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); | 329 AddStartUrlRequestExpectation("http://m.google.com/resource1.jpg"); |
| 329 | 330 |
| 330 prefetcher_->Start(); | 331 prefetcher_->Start(); |
| 331 CheckPrefetcherState(5, 1, 3); | 332 CheckPrefetcherState(5, 1, 3); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 355 // We need to delete requests_ptr here, though it looks to be managed by the | 356 // We need to delete requests_ptr here, though it looks to be managed by the |
| 356 // scoped_ptr requests. The scoped_ptr requests releases itself and the raw | 357 // scoped_ptr requests. The scoped_ptr requests releases itself and the raw |
| 357 // pointer requests_ptr is passed to ResourcePrefetcherFinished(). In the | 358 // pointer requests_ptr is passed to ResourcePrefetcherFinished(). In the |
| 358 // test, ResourcePrefetcherFinished() is a mock function and does not handle | 359 // test, ResourcePrefetcherFinished() is a mock function and does not handle |
| 359 // the raw pointer properly. In the real code, requests_ptr will eventually be | 360 // the raw pointer properly. In the real code, requests_ptr will eventually be |
| 360 // passed to and managed by ResourcePrefetchPredictor::Result::Result. | 361 // passed to and managed by ResourcePrefetchPredictor::Result::Result. |
| 361 delete requests_ptr; | 362 delete requests_ptr; |
| 362 } | 363 } |
| 363 | 364 |
| 364 } // namespace predictors | 365 } // namespace predictors |
| OLD | NEW |