| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <cstring> | 9 #include <cstring> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
| 20 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
| 21 #include "base/files/scoped_temp_dir.h" | 21 #include "base/files/scoped_temp_dir.h" |
| 22 #include "base/memory/ptr_util.h" | 22 #include "base/memory/ptr_util.h" |
| 23 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 24 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
| 25 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
| 26 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
| 27 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
| 28 #include "base/test/histogram_tester.h" | 28 #include "base/test/histogram_tester.h" |
| 29 #include "base/test/scoped_task_environment.h" |
| 29 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
| 30 #include "components/precache/core/precache_database.h" | 31 #include "components/precache/core/precache_database.h" |
| 31 #include "components/precache/core/precache_switches.h" | 32 #include "components/precache/core/precache_switches.h" |
| 32 #include "components/precache/core/proto/precache.pb.h" | 33 #include "components/precache/core/proto/precache.pb.h" |
| 33 #include "components/precache/core/proto/unfinished_work.pb.h" | 34 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 34 #include "net/base/escape.h" | 35 #include "net/base/escape.h" |
| 35 #include "net/base/load_flags.h" | 36 #include "net/base/load_flags.h" |
| 36 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
| 37 #include "net/http/http_response_info.h" | 38 #include "net/http/http_response_info.h" |
| 38 #include "net/http/http_status_code.h" | 39 #include "net/http/http_status_code.h" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 private: | 204 private: |
| 204 std::string body_; | 205 std::string body_; |
| 205 F modifier_; | 206 F modifier_; |
| 206 net::FakeURLFetcher** fetcher_; | 207 net::FakeURLFetcher** fetcher_; |
| 207 }; | 208 }; |
| 208 }; | 209 }; |
| 209 | 210 |
| 210 class PrecacheFetcherFetcherTest : public testing::Test { | 211 class PrecacheFetcherFetcherTest : public testing::Test { |
| 211 public: | 212 public: |
| 212 PrecacheFetcherFetcherTest() | 213 PrecacheFetcherFetcherTest() |
| 213 : request_context_(new net::TestURLRequestContextGetter( | 214 : scoped_task_environment_( |
| 215 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 216 request_context_(new net::TestURLRequestContextGetter( |
| 214 base::ThreadTaskRunnerHandle::Get())), | 217 base::ThreadTaskRunnerHandle::Get())), |
| 215 scoped_url_fetcher_factory_(&factory_), | 218 scoped_url_fetcher_factory_(&factory_), |
| 216 callback_(base::Bind(&PrecacheFetcherFetcherTest::Callback, | 219 callback_(base::Bind(&PrecacheFetcherFetcherTest::Callback, |
| 217 base::Unretained(this))) {} | 220 base::Unretained(this))) {} |
| 218 | 221 |
| 219 MOCK_METHOD1(Callback, void(const PrecacheFetcher::Fetcher&)); | 222 MOCK_METHOD1(Callback, void(const PrecacheFetcher::Fetcher&)); |
| 220 | 223 |
| 221 protected: | 224 protected: |
| 222 base::MessageLoopForUI loop_; | 225 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 223 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 226 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 224 MockURLFetcherFactory factory_; | 227 MockURLFetcherFactory factory_; |
| 225 net::ScopedURLFetcherFactory scoped_url_fetcher_factory_; | 228 net::ScopedURLFetcherFactory scoped_url_fetcher_factory_; |
| 226 base::Callback<void(const PrecacheFetcher::Fetcher&)> callback_; | 229 base::Callback<void(const PrecacheFetcher::Fetcher&)> callback_; |
| 227 }; | 230 }; |
| 228 | 231 |
| 229 void CacheMiss(net::FakeURLFetcher* fetcher) { | 232 void CacheMiss(net::FakeURLFetcher* fetcher) { |
| 230 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 233 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 231 net::ERR_CACHE_MISS)); | 234 net::ERR_CACHE_MISS)); |
| 232 } | 235 } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 request_context_.get(), url, url.host(), callback_, | 421 request_context_.get(), url, url.host(), callback_, |
| 419 true /* is_resource_request */, 99 /* max_bytes */, | 422 true /* is_resource_request */, 99 /* max_bytes */, |
| 420 false /* revalidation_only */); | 423 false /* revalidation_only */); |
| 421 | 424 |
| 422 base::RunLoop().RunUntilIdle(); | 425 base::RunLoop().RunUntilIdle(); |
| 423 } | 426 } |
| 424 | 427 |
| 425 class PrecacheFetcherTest : public testing::Test { | 428 class PrecacheFetcherTest : public testing::Test { |
| 426 public: | 429 public: |
| 427 PrecacheFetcherTest() | 430 PrecacheFetcherTest() |
| 428 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 431 : scoped_task_environment_( |
| 432 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 433 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 429 request_context_(new net::TestURLRequestContextGetter( | 434 request_context_(new net::TestURLRequestContextGetter( |
| 430 base::ThreadTaskRunnerHandle::Get())), | 435 base::ThreadTaskRunnerHandle::Get())), |
| 431 factory_(NULL, | 436 factory_(NULL, |
| 432 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, | 437 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, |
| 433 base::Unretained(&url_callback_))), | 438 base::Unretained(&url_callback_))), |
| 434 expected_total_response_bytes_(0), | 439 expected_total_response_bytes_(0), |
| 435 parallel_fetches_beyond_capacity_(false) {} | 440 parallel_fetches_beyond_capacity_(false) {} |
| 436 | 441 |
| 437 void UpdatePrecacheReferrerHost(const std::string& hostname, | 442 void UpdatePrecacheReferrerHost(const std::string& hostname, |
| 438 int64_t manifest_id) { | 443 int64_t manifest_id) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 } | 495 } |
| 491 | 496 |
| 492 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const { | 497 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const { |
| 493 return task_runner_; | 498 return task_runner_; |
| 494 } | 499 } |
| 495 | 500 |
| 496 // To allow friend access. | 501 // To allow friend access. |
| 497 void Flush() { precache_database_.Flush(); } | 502 void Flush() { precache_database_.Flush(); } |
| 498 | 503 |
| 499 // Must be declared first so that it is destroyed last. | 504 // Must be declared first so that it is destroyed last. |
| 500 base::MessageLoopForUI loop_; | 505 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 501 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 506 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 502 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 507 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 503 TestURLFetcherCallback url_callback_; | 508 TestURLFetcherCallback url_callback_; |
| 504 net::FakeURLFetcherFactory factory_; | 509 net::FakeURLFetcherFactory factory_; |
| 505 TestPrecacheDelegate precache_delegate_; | 510 TestPrecacheDelegate precache_delegate_; |
| 506 base::ScopedTempDir scoped_temp_dir_; | 511 base::ScopedTempDir scoped_temp_dir_; |
| 507 PrecacheDatabase precache_database_; | 512 PrecacheDatabase precache_database_; |
| 508 int expected_total_response_bytes_; | 513 int expected_total_response_bytes_; |
| 509 | 514 |
| 510 // True if more parallel fetches were attempted beyond the fetcher pool | 515 // True if more parallel fetches were attempted beyond the fetcher pool |
| (...skipping 1597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2108 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 2113 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 2109 EXPECT_TRUE(precache_delegate_.get_manifest_hosts().empty()); | 2114 EXPECT_TRUE(precache_delegate_.get_manifest_hosts().empty()); |
| 2110 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 2115 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
| 2111 | 2116 |
| 2112 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); | 2117 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); |
| 2113 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); | 2118 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); |
| 2114 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); | 2119 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); |
| 2115 } | 2120 } |
| 2116 | 2121 |
| 2117 } // namespace precache | 2122 } // namespace precache |
| OLD | NEW |