Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: components/precache/content/precache_manager_unittest.cc

Issue 2229983002: Send the list of used and unused resources for precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed nits Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/precache/content/precache_manager.cc ('k') | components/precache/core/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 net::URLRequestStatus::FAILED); 188 net::URLRequestStatus::FAILED);
189 precache_manager_.reset( 189 precache_manager_.reset(
190 new PrecacheManagerUnderTest( 190 new PrecacheManagerUnderTest(
191 &browser_context_, nullptr /* sync_service */, 191 &browser_context_, nullptr /* sync_service */,
192 &history_service_, db_path, std::move(precache_database))); 192 &history_service_, db_path, std::move(precache_database)));
193 base::RunLoop().RunUntilIdle(); 193 base::RunLoop().RunUntilIdle();
194 194
195 info_.headers = new net::HttpResponseHeaders(""); 195 info_.headers = new net::HttpResponseHeaders("");
196 } 196 }
197 197
198 void RecordStatsForPrecacheFetch(const GURL& url,
199 const std::string& referrer_host,
200 const base::TimeDelta& latency,
201 const base::Time& fetch_time,
202 const net::HttpResponseInfo& info,
203 int64_t size) {
204 precache_manager_->RecordStatsForFetch(url, GURL(referrer_host), latency,
205 fetch_time, info, size);
206 precache_database_->RecordURLPrefetch(url, referrer_host, fetch_time,
207 info.was_cached, size);
208 }
209
198 // Must be declared first so that it is destroyed last. 210 // Must be declared first so that it is destroyed last.
199 content::TestBrowserThreadBundle test_browser_thread_bundle_; 211 content::TestBrowserThreadBundle test_browser_thread_bundle_;
200 base::ScopedTempDir scoped_temp_dir_; 212 base::ScopedTempDir scoped_temp_dir_;
201 PrecacheDatabase* precache_database_; 213 PrecacheDatabase* precache_database_;
202 content::TestBrowserContext browser_context_; 214 content::TestBrowserContext browser_context_;
203 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_; 215 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_;
204 TestURLFetcherCallback url_callback_; 216 TestURLFetcherCallback url_callback_;
205 net::FakeURLFetcherFactory factory_; 217 net::FakeURLFetcherFactory factory_;
206 TestPrecacheCompletionCallback precache_callback_; 218 TestPrecacheCompletionCallback precache_callback_;
207 testing::NiceMock<MockHistoryService> history_service_; 219 testing::NiceMock<MockHistoryService> history_service_;
208 base::HistogramTester histograms_; 220 base::HistogramTester histograms_;
209 net::HttpResponseInfo info_; 221 net::HttpResponseInfo info_;
210 }; 222 };
211 223
212 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { 224 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) {
213 EXPECT_FALSE(precache_manager_->IsPrecaching()); 225 EXPECT_FALSE(precache_manager_->IsPrecaching());
214 226
215 MockHistoryService::TopHostsCallback top_hosts_callback; 227 MockHistoryService::TopHostsCallback top_hosts_callback;
216 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) 228 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _))
217 .WillOnce(SaveArg<1>(&top_hosts_callback)); 229 .WillOnce(SaveArg<1>(&top_hosts_callback));
218 230
231 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK,
232 net::URLRequestStatus::SUCCESS);
219 factory_.SetFakeResponse(GURL(kGoodManifestURL), "", net::HTTP_OK, 233 factory_.SetFakeResponse(GURL(kGoodManifestURL), "", net::HTTP_OK,
220 net::URLRequestStatus::SUCCESS); 234 net::URLRequestStatus::SUCCESS);
221 235
222 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 236 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
223 base::RunLoop().RunUntilIdle(); 237 base::RunLoop().RunUntilIdle();
224 EXPECT_TRUE(precache_manager_->IsPrecaching()); 238 EXPECT_TRUE(precache_manager_->IsPrecaching());
225 239
226 top_hosts_callback.Run( 240 top_hosts_callback.Run(
227 history::TopHostsList(1, std::make_pair("good-manifest.com", 1))); 241 history::TopHostsList(1, std::make_pair("good-manifest.com", 1)));
228 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. 242 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher.
229 EXPECT_FALSE(precache_manager_->IsPrecaching()); 243 EXPECT_FALSE(precache_manager_->IsPrecaching());
230 EXPECT_TRUE(precache_callback_.was_on_done_called()); 244 EXPECT_TRUE(precache_callback_.was_on_done_called());
231 245
232 std::multiset<GURL> expected_requested_urls; 246 std::multiset<GURL> expected_requested_urls;
233 expected_requested_urls.insert(GURL(kConfigURL)); 247 expected_requested_urls.insert(GURL(kConfigURL));
234 expected_requested_urls.insert(GURL(kGoodManifestURL)); 248 expected_requested_urls.insert(GURL(kGoodManifestURL));
235 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); 249 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
236 } 250 }
237 251
238 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) { 252 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) {
239 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( 253 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
240 new PrecacheUnfinishedWork()); 254 new PrecacheUnfinishedWork());
241 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); 255 unfinished_work->add_top_host()->set_hostname("evil-manifest.com");
242 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); 256 unfinished_work->set_start_time(base::Time::Now().ToInternalValue());
243 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); 257 precache_database_->SaveUnfinishedWork(std::move(unfinished_work));
244 258
245 EXPECT_FALSE(precache_manager_->IsPrecaching()); 259 EXPECT_FALSE(precache_manager_->IsPrecaching());
246 260
261 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK,
262 net::URLRequestStatus::SUCCESS);
247 factory_.SetFakeResponse( 263 factory_.SetFakeResponse(
248 GURL(kEvilManifestURL), "", 264 GURL(kEvilManifestURL), "",
249 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 265 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
250 266
251 ASSERT_TRUE(precache_database_->GetLastPrecacheTimestamp().is_null()); 267 ASSERT_TRUE(precache_database_->GetLastPrecacheTimestamp().is_null());
252 268
253 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 269 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
254 EXPECT_TRUE(precache_manager_->IsPrecaching()); 270 EXPECT_TRUE(precache_manager_->IsPrecaching());
255 271
256 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. 272 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher.
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); 398 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty());
383 } 399 }
384 400
385 TEST_F(PrecacheManagerTest, RecordStatsForFetchDuringPrecaching) { 401 TEST_F(PrecacheManagerTest, RecordStatsForFetchDuringPrecaching) {
386 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) 402 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _))
387 .WillOnce(ReturnHosts(history::TopHostsList())); 403 .WillOnce(ReturnHosts(history::TopHostsList()));
388 404
389 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 405 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
390 406
391 EXPECT_TRUE(precache_manager_->IsPrecaching()); 407 EXPECT_TRUE(precache_manager_->IsPrecaching());
392 precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(), 408 RecordStatsForPrecacheFetch(GURL("http://url.com"), std::string(),
393 base::TimeDelta(), base::Time(), info_, 409 base::TimeDelta(), base::Time(), info_, 1000);
394 1000);
395 base::RunLoop().RunUntilIdle(); 410 base::RunLoop().RunUntilIdle();
396 precache_manager_->CancelPrecaching(); 411 precache_manager_->CancelPrecaching();
397 412
398 // For PrecacheFetcher and RecordURLPrecached. 413 // For PrecacheFetcher and RecordURLPrecached.
399 base::RunLoop().RunUntilIdle(); 414 base::RunLoop().RunUntilIdle();
400 EXPECT_THAT( 415 EXPECT_THAT(
401 histograms_.GetTotalCountsForPrefix("Precache."), 416 histograms_.GetTotalCountsForPrefix("Precache."),
402 UnorderedElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1), 417 UnorderedElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1),
403 Pair("Precache.Fetch.PercentCompleted", 1), 418 Pair("Precache.Fetch.PercentCompleted", 1),
404 Pair("Precache.Fetch.ResponseBytes.Network", 1), 419 Pair("Precache.Fetch.ResponseBytes.Network", 1),
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 // This test has to use Time::Now() because StartPrecaching uses Time::Now(). 478 // This test has to use Time::Now() because StartPrecaching uses Time::Now().
464 const base::Time kCurrentTime = base::Time::Now(); 479 const base::Time kCurrentTime = base::Time::Now();
465 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) 480 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _))
466 .Times(2) 481 .Times(2)
467 .WillRepeatedly(ReturnHosts(history::TopHostsList())); 482 .WillRepeatedly(ReturnHosts(history::TopHostsList()));
468 483
469 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 484 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
470 EXPECT_TRUE(precache_manager_->IsPrecaching()); 485 EXPECT_TRUE(precache_manager_->IsPrecaching());
471 486
472 // Precache a bunch of URLs, with different fetch times. 487 // Precache a bunch of URLs, with different fetch times.
473 precache_manager_->RecordStatsForFetch( 488 RecordStatsForPrecacheFetch(
474 GURL("http://old-fetch.com"), GURL(), base::TimeDelta(), 489 GURL("http://old-fetch.com"), std::string(), base::TimeDelta(),
475 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000); 490 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000);
476 precache_manager_->RecordStatsForFetch( 491 RecordStatsForPrecacheFetch(
477 GURL("http://recent-fetch.com"), GURL(), base::TimeDelta(), 492 GURL("http://recent-fetch.com"), std::string(), base::TimeDelta(),
478 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000); 493 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000);
479 precache_manager_->RecordStatsForFetch( 494 RecordStatsForPrecacheFetch(
480 GURL("http://yesterday-fetch.com"), GURL(), base::TimeDelta(), 495 GURL("http://yesterday-fetch.com"), std::string(), base::TimeDelta(),
481 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000); 496 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000);
482 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3; 497 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3;
483 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; 498 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++;
484 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; 499 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++;
485 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; 500 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++;
486 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; 501 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++;
487 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3; 502 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3;
488 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3; 503 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3;
489 base::RunLoop().RunUntilIdle(); 504 base::RunLoop().RunUntilIdle();
490 505
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 expected_histogram_count_map["Precache.Saved"] += 2; 561 expected_histogram_count_map["Precache.Saved"] += 2;
547 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; 562 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2;
548 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; 563 expected_histogram_count_map["Precache.Saved.Freshness"] = 2;
549 564
550 base::RunLoop().RunUntilIdle(); 565 base::RunLoop().RunUntilIdle();
551 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), 566 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
552 ContainerEq(expected_histogram_count_map)); 567 ContainerEq(expected_histogram_count_map));
553 } 568 }
554 569
555 } // namespace precache 570 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/content/precache_manager.cc ('k') | components/precache/core/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698