| Index: components/precache/content/precache_manager_unittest.cc
|
| diff --git a/components/precache/content/precache_manager_unittest.cc b/components/precache/content/precache_manager_unittest.cc
|
| index 7e62d23fc9525f91064b0b33aaf22847aa9da249..247d559bbc285199b36519076b8c5c750d48c544 100644
|
| --- a/components/precache/content/precache_manager_unittest.cc
|
| +++ b/components/precache/content/precache_manager_unittest.cc
|
| @@ -31,6 +31,8 @@
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/test/test_browser_context.h"
|
| #include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "net/http/http_response_headers.h"
|
| +#include "net/http/http_response_info.h"
|
| #include "net/http/http_status_code.h"
|
| #include "net/url_request/test_url_fetcher_factory.h"
|
| #include "net/url_request/url_request_status.h"
|
| @@ -46,6 +48,7 @@ namespace {
|
| using ::testing::_;
|
| using ::testing::ContainerEq;
|
| using ::testing::ElementsAre;
|
| +using ::testing::UnorderedElementsAre;
|
| using ::testing::Invoke;
|
| using ::testing::IsEmpty;
|
| using ::testing::Pair;
|
| @@ -189,6 +192,8 @@ class PrecacheManagerTest : public testing::Test {
|
| &browser_context_, nullptr /* sync_service */,
|
| &history_service_, db_path, std::move(precache_database)));
|
| base::RunLoop().RunUntilIdle();
|
| +
|
| + info_.headers = new net::HttpResponseHeaders("");
|
| }
|
|
|
| // Must be declared first so that it is destroyed last.
|
| @@ -202,6 +207,7 @@ class PrecacheManagerTest : public testing::Test {
|
| TestPrecacheCompletionCallback precache_callback_;
|
| testing::NiceMock<MockHistoryService> history_service_;
|
| base::HistogramTester histograms_;
|
| + net::HttpResponseInfo info_;
|
| };
|
|
|
| TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) {
|
| @@ -354,8 +360,8 @@ TEST_F(PrecacheManagerTest, StartAndCancelPrecachingAfterURLsReceived) {
|
| TEST_F(PrecacheManagerTest, RecordStatsForFetchWithSizeZero) {
|
| // Fetches with size 0 should be ignored.
|
| precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(),
|
| - base::TimeDelta(), base::Time(), 0,
|
| - false);
|
| + base::TimeDelta(), base::Time(), info_,
|
| + 0);
|
| base::RunLoop().RunUntilIdle();
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty());
|
| }
|
| @@ -363,8 +369,8 @@ TEST_F(PrecacheManagerTest, RecordStatsForFetchWithSizeZero) {
|
| TEST_F(PrecacheManagerTest, RecordStatsForFetchWithNonHTTP) {
|
| // Fetches for URLs with schemes other than HTTP or HTTPS should be ignored.
|
| precache_manager_->RecordStatsForFetch(GURL("ftp://ftp.com"), GURL(),
|
| - base::TimeDelta(), base::Time(), 1000,
|
| - false);
|
| + base::TimeDelta(), base::Time(), info_,
|
| + 1000);
|
| base::RunLoop().RunUntilIdle();
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty());
|
| }
|
| @@ -372,7 +378,7 @@ TEST_F(PrecacheManagerTest, RecordStatsForFetchWithNonHTTP) {
|
| TEST_F(PrecacheManagerTest, RecordStatsForFetchWithEmptyURL) {
|
| // Fetches for empty URLs should be ignored.
|
| precache_manager_->RecordStatsForFetch(GURL(), GURL(), base::TimeDelta(),
|
| - base::Time(), 1000, false);
|
| + base::Time(), info_, 1000);
|
| base::RunLoop().RunUntilIdle();
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty());
|
| }
|
| @@ -385,26 +391,28 @@ TEST_F(PrecacheManagerTest, RecordStatsForFetchDuringPrecaching) {
|
|
|
| EXPECT_TRUE(precache_manager_->IsPrecaching());
|
| precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(),
|
| - base::TimeDelta(), base::Time(), 1000,
|
| - false);
|
| + base::TimeDelta(), base::Time(), info_,
|
| + 1000);
|
| base::RunLoop().RunUntilIdle();
|
| precache_manager_->CancelPrecaching();
|
|
|
| // For PrecacheFetcher and RecordURLPrecached.
|
| base::RunLoop().RunUntilIdle();
|
| - EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
|
| - ElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1),
|
| - Pair("Precache.Fetch.PercentCompleted", 1),
|
| - Pair("Precache.Fetch.ResponseBytes.Network", 1),
|
| - Pair("Precache.Fetch.ResponseBytes.Total", 1),
|
| - Pair("Precache.Fetch.TimeToComplete", 1),
|
| - Pair("Precache.Latency.Prefetch", 1)));
|
| + EXPECT_THAT(
|
| + histograms_.GetTotalCountsForPrefix("Precache."),
|
| + UnorderedElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1),
|
| + Pair("Precache.Fetch.PercentCompleted", 1),
|
| + Pair("Precache.Fetch.ResponseBytes.Network", 1),
|
| + Pair("Precache.Fetch.ResponseBytes.Total", 1),
|
| + Pair("Precache.Fetch.TimeToComplete", 1),
|
| + Pair("Precache.Latency.Prefetch", 1),
|
| + Pair("Precache.Freshness.Prefetch", 1)));
|
| }
|
|
|
| TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) {
|
| precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(),
|
| - base::TimeDelta(), base::Time(), 1000,
|
| - false);
|
| + base::TimeDelta(), base::Time(), info_,
|
| + 1000);
|
| base::RunLoop().RunUntilIdle();
|
|
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
|
| @@ -415,8 +423,8 @@ TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) {
|
|
|
| TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTPS) {
|
| precache_manager_->RecordStatsForFetch(GURL("https://https-url.com"), GURL(),
|
| - base::TimeDelta(), base::Time(), 1000,
|
| - false);
|
| + base::TimeDelta(), base::Time(), info_,
|
| + 1000);
|
| base::RunLoop().RunUntilIdle();
|
|
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
|
| @@ -434,7 +442,7 @@ TEST_F(PrecacheManagerTest, RecordStatsForFetchInTopHosts) {
|
| }));
|
| precache_manager_->RecordStatsForFetch(
|
| GURL("http://http-url.com"), GURL("http://referrer.com"),
|
| - base::TimeDelta(), base::Time(), 1000, false);
|
| + base::TimeDelta(), base::Time(), info_, 1000);
|
| base::RunLoop().RunUntilIdle();
|
|
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
|
| @@ -459,19 +467,20 @@ TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) {
|
| // Precache a bunch of URLs, with different fetch times.
|
| precache_manager_->RecordStatsForFetch(
|
| GURL("http://old-fetch.com"), GURL(), base::TimeDelta(),
|
| - kCurrentTime - base::TimeDelta::FromDays(61), 1000, false);
|
| + kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000);
|
| precache_manager_->RecordStatsForFetch(
|
| GURL("http://recent-fetch.com"), GURL(), base::TimeDelta(),
|
| - kCurrentTime - base::TimeDelta::FromDays(59), 1000, false);
|
| + kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000);
|
| precache_manager_->RecordStatsForFetch(
|
| GURL("http://yesterday-fetch.com"), GURL(), base::TimeDelta(),
|
| - kCurrentTime - base::TimeDelta::FromDays(1), 1000, false);
|
| + kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000);
|
| expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3;
|
| expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++;
|
| expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++;
|
| expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++;
|
| expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++;
|
| expected_histogram_count_map["Precache.Latency.Prefetch"] += 3;
|
| + expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3;
|
| base::RunLoop().RunUntilIdle();
|
|
|
| precache_manager_->CancelPrecaching();
|
| @@ -504,9 +513,10 @@ TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) {
|
| // A fetch for the same URL as the expired precache was served from the cache,
|
| // but it isn't reported as saved bytes because it had expired in the precache
|
| // history.
|
| + info_.was_cached = true; // From now on all fetches are cached.
|
| precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(),
|
| - base::TimeDelta(), kCurrentTime, 1000,
|
| - true);
|
| + base::TimeDelta(), kCurrentTime, info_,
|
| + 1000);
|
| expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++;
|
| expected_histogram_count_map["Precache.Latency.NonPrefetch"]++;
|
| expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++;
|
| @@ -518,16 +528,17 @@ TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) {
|
|
|
| // The other precaches should not have expired, so the following fetches from
|
| // the cache should count as saved bytes.
|
| - precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), GURL(),
|
| - base::TimeDelta(), kCurrentTime, 1000,
|
| - true);
|
| + precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"),
|
| + GURL(), base::TimeDelta(),
|
| + kCurrentTime, info_, 1000);
|
| precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"),
|
| - GURL(), base::TimeDelta(), kCurrentTime,
|
| - 1000, true);
|
| + GURL(), base::TimeDelta(),
|
| + kCurrentTime, info_, 1000);
|
| expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2;
|
| expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2;
|
| expected_histogram_count_map["Precache.Saved"] += 2;
|
| expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2;
|
| + expected_histogram_count_map["Precache.Saved.Freshness"] = 2;
|
|
|
| base::RunLoop().RunUntilIdle();
|
| EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
|
|
|