| 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/content/precache_manager.h" | 5 #include "components/precache/content/precache_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 18 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 19 #include "base/files/scoped_temp_dir.h" | 19 #include "base/files/scoped_temp_dir.h" |
| 20 #include "base/location.h" | 20 #include "base/location.h" |
| 21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 22 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
| 23 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
| 24 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "components/history/core/browser/history_constants.h" | 25 #include "components/history/core/browser/history_constants.h" |
| 26 #include "components/history/core/browser/history_service.h" | 26 #include "components/history/core/browser/history_service.h" |
| 27 #include "components/history/core/browser/history_types.h" | 27 #include "components/history/core/browser/history_types.h" |
| 28 #include "components/precache/core/precache_database.h" | 28 #include "components/precache/core/precache_database.h" |
| 29 #include "components/precache/core/precache_manifest_delegate.h" |
| 29 #include "components/precache/core/precache_switches.h" | 30 #include "components/precache/core/precache_switches.h" |
| 30 #include "components/precache/core/proto/unfinished_work.pb.h" | 31 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 31 #include "components/variations/variations_params_manager.h" | 32 #include "components/variations/variations_params_manager.h" |
| 32 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/storage_partition.h" | 34 #include "content/public/browser/storage_partition.h" |
| 34 #include "content/public/test/test_browser_context.h" | 35 #include "content/public/test/test_browser_context.h" |
| 35 #include "content/public/test/test_browser_thread_bundle.h" | 36 #include "content/public/test/test_browser_thread_bundle.h" |
| 36 #include "net/base/test_completion_callback.h" | 37 #include "net/base/test_completion_callback.h" |
| 37 #include "net/disk_cache/simple/simple_backend_impl.h" | 38 #include "net/disk_cache/simple/simple_backend_impl.h" |
| 38 #include "net/http/http_response_headers.h" | 39 #include "net/http/http_response_headers.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 101 } |
| 101 | 102 |
| 102 MOCK_CONST_METHOD2(TopHosts, | 103 MOCK_CONST_METHOD2(TopHosts, |
| 103 void(size_t num_hosts, const TopHostsCallback& callback)); | 104 void(size_t num_hosts, const TopHostsCallback& callback)); |
| 104 | 105 |
| 105 MOCK_CONST_METHOD2(HostRankIfAvailable, | 106 MOCK_CONST_METHOD2(HostRankIfAvailable, |
| 106 void(const GURL& url, | 107 void(const GURL& url, |
| 107 const base::Callback<void(int)>& callback)); | 108 const base::Callback<void(int)>& callback)); |
| 108 }; | 109 }; |
| 109 | 110 |
| 111 class MockPrecacheManifestDelegate : public PrecacheManifestDelegate { |
| 112 public: |
| 113 MOCK_METHOD2(OnManifestFetched, |
| 114 void(const std::string& host, const PrecacheManifest& manifest)); |
| 115 }; |
| 116 |
| 110 ACTION_P(ReturnHosts, starting_hosts) { | 117 ACTION_P(ReturnHosts, starting_hosts) { |
| 111 arg1.Run(starting_hosts); | 118 arg1.Run(starting_hosts); |
| 112 } | 119 } |
| 113 | 120 |
| 114 class TestPrecacheCompletionCallback { | 121 class TestPrecacheCompletionCallback { |
| 115 public: | 122 public: |
| 116 TestPrecacheCompletionCallback() : was_on_done_called_(false) {} | 123 TestPrecacheCompletionCallback() : was_on_done_called_(false) {} |
| 117 | 124 |
| 118 void OnDone(bool precaching_started) { was_on_done_called_ = true; } | 125 void OnDone(bool precaching_started) { was_on_done_called_ = true; } |
| 119 | 126 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 131 }; | 138 }; |
| 132 | 139 |
| 133 class PrecacheManagerUnderTest : public PrecacheManager { | 140 class PrecacheManagerUnderTest : public PrecacheManager { |
| 134 public: | 141 public: |
| 135 PrecacheManagerUnderTest( | 142 PrecacheManagerUnderTest( |
| 136 content::BrowserContext* browser_context, | 143 content::BrowserContext* browser_context, |
| 137 const syncer::SyncService* sync_service, | 144 const syncer::SyncService* sync_service, |
| 138 const history::HistoryService* history_service, | 145 const history::HistoryService* history_service, |
| 139 const data_reduction_proxy::DataReductionProxySettings* | 146 const data_reduction_proxy::DataReductionProxySettings* |
| 140 data_reduction_proxy_settings, | 147 data_reduction_proxy_settings, |
| 148 PrecacheManifestDelegate* precache_manifest_delegate_, |
| 141 const base::FilePath& db_path, | 149 const base::FilePath& db_path, |
| 142 std::unique_ptr<PrecacheDatabase> precache_database) | 150 std::unique_ptr<PrecacheDatabase> precache_database) |
| 143 : PrecacheManager(browser_context, | 151 : PrecacheManager(browser_context, |
| 144 sync_service, | 152 sync_service, |
| 145 history_service, | 153 history_service, |
| 146 data_reduction_proxy_settings, | 154 data_reduction_proxy_settings, |
| 155 precache_manifest_delegate_, |
| 147 db_path, | 156 db_path, |
| 148 std::move(precache_database)), | 157 std::move(precache_database)), |
| 149 control_group_(false) {} | 158 control_group_(false) {} |
| 150 bool IsInExperimentGroup() const override { return !control_group_; } | 159 bool IsInExperimentGroup() const override { return !control_group_; } |
| 151 bool IsInControlGroup() const override { return control_group_; } | 160 bool IsInControlGroup() const override { return control_group_; } |
| 152 bool IsPrecachingAllowed() const override { return true; } | 161 bool IsPrecachingAllowed() const override { return true; } |
| 153 void SetInControlGroup(bool in_control_group) { | 162 void SetInControlGroup(bool in_control_group) { |
| 154 control_group_ = in_control_group; | 163 control_group_ = in_control_group; |
| 155 } | 164 } |
| 156 | 165 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 net::URLRequestStatus::FAILED); | 204 net::URLRequestStatus::FAILED); |
| 196 info_.headers = new net::HttpResponseHeaders(""); | 205 info_.headers = new net::HttpResponseHeaders(""); |
| 197 } | 206 } |
| 198 | 207 |
| 199 // precache_manager_ assumes ownership of precache_database. | 208 // precache_manager_ assumes ownership of precache_database. |
| 200 void Reset(PrecacheDatabase* precache_database) { | 209 void Reset(PrecacheDatabase* precache_database) { |
| 201 base::FilePath db_path = scoped_temp_dir_.GetPath().Append( | 210 base::FilePath db_path = scoped_temp_dir_.GetPath().Append( |
| 202 base::FilePath(FILE_PATH_LITERAL("precache_database"))); | 211 base::FilePath(FILE_PATH_LITERAL("precache_database"))); |
| 203 precache_manager_.reset(new PrecacheManagerUnderTest( | 212 precache_manager_.reset(new PrecacheManagerUnderTest( |
| 204 &browser_context_, nullptr /* sync_service */, &history_service_, | 213 &browser_context_, nullptr /* sync_service */, &history_service_, |
| 205 nullptr /* data_reduction_proxy_settings */, db_path, | 214 nullptr /* data_reduction_proxy_settings */, |
| 215 &precache_manifest_delegate_, db_path, |
| 206 base::WrapUnique(precache_database))); | 216 base::WrapUnique(precache_database))); |
| 207 } | 217 } |
| 208 | 218 |
| 209 void Flush() { precache_database_->Flush(); } | 219 void Flush() { precache_database_->Flush(); } |
| 210 | 220 |
| 211 void RecordStatsForFetch(const GURL& url, | 221 void RecordStatsForFetch(const GURL& url, |
| 212 const std::string& referrer_host, | 222 const std::string& referrer_host, |
| 213 const base::Time& fetch_time, | 223 const base::Time& fetch_time, |
| 214 const net::HttpResponseInfo& info, | 224 const net::HttpResponseInfo& info, |
| 215 int64_t size, | 225 int64_t size, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 239 // Must be declared first so that it is destroyed last. | 249 // Must be declared first so that it is destroyed last. |
| 240 content::TestBrowserThreadBundle test_browser_thread_bundle_; | 250 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 241 base::ScopedTempDir scoped_temp_dir_; | 251 base::ScopedTempDir scoped_temp_dir_; |
| 242 PrecacheDatabase* precache_database_; | 252 PrecacheDatabase* precache_database_; |
| 243 content::TestBrowserContext browser_context_; | 253 content::TestBrowserContext browser_context_; |
| 244 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_; | 254 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_; |
| 245 TestURLFetcherCallback url_callback_; | 255 TestURLFetcherCallback url_callback_; |
| 246 net::FakeURLFetcherFactory factory_; | 256 net::FakeURLFetcherFactory factory_; |
| 247 TestPrecacheCompletionCallback precache_callback_; | 257 TestPrecacheCompletionCallback precache_callback_; |
| 248 testing::NiceMock<MockHistoryService> history_service_; | 258 testing::NiceMock<MockHistoryService> history_service_; |
| 259 testing::NiceMock<MockPrecacheManifestDelegate> precache_manifest_delegate_; |
| 249 base::HistogramTester histograms_; | 260 base::HistogramTester histograms_; |
| 250 net::HttpResponseInfo info_; | 261 net::HttpResponseInfo info_; |
| 251 variations::testing::VariationParamsManager variation_params_; | 262 variations::testing::VariationParamsManager variation_params_; |
| 252 }; | 263 }; |
| 253 | 264 |
| 254 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { | 265 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { |
| 255 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 266 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 256 | 267 |
| 257 MockHistoryService::TopHostsCallback top_hosts_callback; | 268 MockHistoryService::TopHostsCallback top_hosts_callback; |
| 258 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) | 269 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 expected_histogram_count_map["Precache.Saved"] += 2; | 684 expected_histogram_count_map["Precache.Saved"] += 2; |
| 674 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; | 685 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; |
| 675 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; | 686 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; |
| 676 | 687 |
| 677 base::RunLoop().RunUntilIdle(); | 688 base::RunLoop().RunUntilIdle(); |
| 678 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 689 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 679 ContainerEq(expected_histogram_count_map)); | 690 ContainerEq(expected_histogram_count_map)); |
| 680 } | 691 } |
| 681 | 692 |
| 682 } // namespace precache | 693 } // namespace precache |
| OLD | NEW |