| 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> |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 42 #include "url/gurl.h" | 42 #include "url/gurl.h" |
| 43 | 43 |
| 44 namespace precache { | 44 namespace precache { |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 using ::testing::_; | 48 using ::testing::_; |
| 49 using ::testing::ContainerEq; | 49 using ::testing::ContainerEq; |
| 50 using ::testing::ElementsAre; | |
| 51 using ::testing::UnorderedElementsAre; | 50 using ::testing::UnorderedElementsAre; |
| 52 using ::testing::Invoke; | 51 using ::testing::Invoke; |
| 53 using ::testing::IsEmpty; | 52 using ::testing::IsEmpty; |
| 54 using ::testing::Pair; | 53 using ::testing::Pair; |
| 55 using ::testing::SaveArg; | 54 using ::testing::SaveArg; |
| 56 | 55 |
| 57 const char kConfigURL[] = "http://config-url.com"; | 56 const char kConfigURL[] = "http://config-url.com"; |
| 58 const char kManifestURLPrefix[] = "http://manifest-url-prefix.com/"; | 57 const char kManifestURLPrefix[] = "http://manifest-url-prefix.com/"; |
| 59 const char kGoodManifestURL[] = | 58 const char kGoodManifestURL[] = |
| 60 "http://manifest-url-prefix.com/good-manifest.com"; | 59 "http://manifest-url-prefix.com/good-manifest.com"; |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 Pair("Precache.Freshness.Prefetch", 1))); | 408 Pair("Precache.Freshness.Prefetch", 1))); |
| 410 } | 409 } |
| 411 | 410 |
| 412 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) { | 411 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) { |
| 413 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(), | 412 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(), |
| 414 base::TimeDelta(), base::Time(), info_, | 413 base::TimeDelta(), base::Time(), info_, |
| 415 1000); | 414 1000); |
| 416 base::RunLoop().RunUntilIdle(); | 415 base::RunLoop().RunUntilIdle(); |
| 417 | 416 |
| 418 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 417 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 419 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), | 418 UnorderedElementsAre( |
| 420 Pair("Precache.Latency.NonPrefetch", 1), | 419 Pair("Precache.DownloadedNonPrecache", 1), |
| 421 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); | 420 Pair("Precache.CacheStatus.NonPrefetch", 1), |
| 421 Pair("Precache.Latency.NonPrefetch", 1), |
| 422 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); |
| 422 } | 423 } |
| 423 | 424 |
| 424 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTPS) { | 425 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTPS) { |
| 425 precache_manager_->RecordStatsForFetch(GURL("https://https-url.com"), GURL(), | 426 precache_manager_->RecordStatsForFetch(GURL("https://https-url.com"), GURL(), |
| 426 base::TimeDelta(), base::Time(), info_, | 427 base::TimeDelta(), base::Time(), info_, |
| 427 1000); | 428 1000); |
| 428 base::RunLoop().RunUntilIdle(); | 429 base::RunLoop().RunUntilIdle(); |
| 429 | 430 |
| 430 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 431 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 431 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), | 432 UnorderedElementsAre( |
| 432 Pair("Precache.Latency.NonPrefetch", 1), | 433 Pair("Precache.DownloadedNonPrecache", 1), |
| 433 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); | 434 Pair("Precache.CacheStatus.NonPrefetch", 1), |
| 435 Pair("Precache.Latency.NonPrefetch", 1), |
| 436 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); |
| 434 } | 437 } |
| 435 | 438 |
| 436 TEST_F(PrecacheManagerTest, RecordStatsForFetchInTopHosts) { | 439 TEST_F(PrecacheManagerTest, RecordStatsForFetchInTopHosts) { |
| 437 EXPECT_CALL(history_service_, | 440 EXPECT_CALL(history_service_, |
| 438 HostRankIfAvailable(GURL("http://referrer.com"), _)) | 441 HostRankIfAvailable(GURL("http://referrer.com"), _)) |
| 439 .WillOnce(Invoke( | 442 .WillOnce(Invoke( |
| 440 [](const GURL& url, const base::Callback<void(int)>& callback) { | 443 [](const GURL& url, const base::Callback<void(int)>& callback) { |
| 441 callback.Run(0); | 444 callback.Run(0); |
| 442 })); | 445 })); |
| 443 precache_manager_->RecordStatsForFetch( | 446 precache_manager_->RecordStatsForFetch( |
| 444 GURL("http://http-url.com"), GURL("http://referrer.com"), | 447 GURL("http://http-url.com"), GURL("http://referrer.com"), |
| 445 base::TimeDelta(), base::Time(), info_, 1000); | 448 base::TimeDelta(), base::Time(), info_, 1000); |
| 446 base::RunLoop().RunUntilIdle(); | 449 base::RunLoop().RunUntilIdle(); |
| 447 | 450 |
| 448 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 451 EXPECT_THAT( |
| 449 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), | 452 histograms_.GetTotalCountsForPrefix("Precache."), |
| 450 Pair("Precache.Latency.NonPrefetch", 1), | 453 UnorderedElementsAre(Pair("Precache.DownloadedNonPrecache", 1), |
| 451 Pair("Precache.Latency.NonPrefetch.TopHosts", 1))); | 454 Pair("Precache.CacheStatus.NonPrefetch", 1), |
| 455 Pair("Precache.Latency.NonPrefetch", 1), |
| 456 Pair("Precache.Latency.NonPrefetch.TopHosts", 1))); |
| 452 } | 457 } |
| 453 | 458 |
| 454 TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) { | 459 TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) { |
| 455 // TODO(twifkak): Split this into multiple tests. | 460 // TODO(twifkak): Split this into multiple tests. |
| 456 base::HistogramTester::CountsMap expected_histogram_count_map; | 461 base::HistogramTester::CountsMap expected_histogram_count_map; |
| 457 | 462 |
| 458 // This test has to use Time::Now() because StartPrecaching uses Time::Now(). | 463 // This test has to use Time::Now() because StartPrecaching uses Time::Now(). |
| 459 const base::Time kCurrentTime = base::Time::Now(); | 464 const base::Time kCurrentTime = base::Time::Now(); |
| 460 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) | 465 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) |
| 461 .Times(2) | 466 .Times(2) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 516 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 512 | 517 |
| 513 // A fetch for the same URL as the expired precache was served from the cache, | 518 // A fetch for the same URL as the expired precache was served from the cache, |
| 514 // but it isn't reported as saved bytes because it had expired in the precache | 519 // but it isn't reported as saved bytes because it had expired in the precache |
| 515 // history. | 520 // history. |
| 516 info_.was_cached = true; // From now on all fetches are cached. | 521 info_.was_cached = true; // From now on all fetches are cached. |
| 517 precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(), | 522 precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(), |
| 518 base::TimeDelta(), kCurrentTime, info_, | 523 base::TimeDelta(), kCurrentTime, info_, |
| 519 1000); | 524 1000); |
| 520 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; | 525 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; |
| 526 expected_histogram_count_map["Precache.CacheStatus.NonPrefetch"]++; |
| 521 expected_histogram_count_map["Precache.Latency.NonPrefetch"]++; | 527 expected_histogram_count_map["Precache.Latency.NonPrefetch"]++; |
| 522 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++; | 528 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++; |
| 523 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 1; | 529 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 1; |
| 524 | 530 |
| 525 base::RunLoop().RunUntilIdle(); | 531 base::RunLoop().RunUntilIdle(); |
| 526 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 532 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 527 ContainerEq(expected_histogram_count_map)); | 533 ContainerEq(expected_histogram_count_map)); |
| 528 | 534 |
| 529 // The other precaches should not have expired, so the following fetches from | 535 // The other precaches should not have expired, so the following fetches from |
| 530 // the cache should count as saved bytes. | 536 // the cache should count as saved bytes. |
| 531 precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), | 537 precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), |
| 532 GURL(), base::TimeDelta(), | 538 GURL(), base::TimeDelta(), |
| 533 kCurrentTime, info_, 1000); | 539 kCurrentTime, info_, 1000); |
| 534 precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"), | 540 precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"), |
| 535 GURL(), base::TimeDelta(), | 541 GURL(), base::TimeDelta(), |
| 536 kCurrentTime, info_, 1000); | 542 kCurrentTime, info_, 1000); |
| 543 expected_histogram_count_map["Precache.CacheStatus.NonPrefetch"] += 2; |
| 537 expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2; | 544 expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2; |
| 538 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2; | 545 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2; |
| 539 expected_histogram_count_map["Precache.Saved"] += 2; | 546 expected_histogram_count_map["Precache.Saved"] += 2; |
| 540 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; | 547 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; |
| 541 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; | 548 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; |
| 542 | 549 |
| 543 base::RunLoop().RunUntilIdle(); | 550 base::RunLoop().RunUntilIdle(); |
| 544 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 551 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 545 ContainerEq(expected_histogram_count_map)); | 552 ContainerEq(expected_histogram_count_map)); |
| 546 } | 553 } |
| 547 | 554 |
| 548 } // namespace precache | 555 } // namespace precache |
| OLD | NEW |