Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 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_switches.h" | 29 #include "components/precache/core/precache_switches.h" |
| 30 #include "components/precache/core/proto/unfinished_work.pb.h" | 30 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/test/test_browser_context.h" | 32 #include "content/public/test/test_browser_context.h" |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
| 34 #include "net/http/http_response_headers.h" | |
| 35 #include "net/http/http_response_info.h" | |
| 34 #include "net/http/http_status_code.h" | 36 #include "net/http/http_status_code.h" |
| 35 #include "net/url_request/test_url_fetcher_factory.h" | 37 #include "net/url_request/test_url_fetcher_factory.h" |
| 36 #include "net/url_request/url_request_status.h" | 38 #include "net/url_request/url_request_status.h" |
| 37 #include "net/url_request/url_request_test_util.h" | 39 #include "net/url_request/url_request_test_util.h" |
| 38 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 40 #include "url/gurl.h" | 42 #include "url/gurl.h" |
| 41 | 43 |
| 42 namespace precache { | 44 namespace precache { |
| 43 | 45 |
| 44 namespace { | 46 namespace { |
| 45 | 47 |
| 46 using ::testing::_; | 48 using ::testing::_; |
| 47 using ::testing::ContainerEq; | 49 using ::testing::ContainerEq; |
| 48 using ::testing::ElementsAre; | 50 using ::testing::ElementsAre; |
| 51 using ::testing::UnorderedElementsAre; | |
| 49 using ::testing::Invoke; | 52 using ::testing::Invoke; |
| 50 using ::testing::IsEmpty; | 53 using ::testing::IsEmpty; |
| 51 using ::testing::Pair; | 54 using ::testing::Pair; |
| 52 using ::testing::SaveArg; | 55 using ::testing::SaveArg; |
| 53 | 56 |
| 54 const char kConfigURL[] = "http://config-url.com"; | 57 const char kConfigURL[] = "http://config-url.com"; |
| 55 const char kManifestURLPrefix[] = "http://manifest-url-prefix.com/"; | 58 const char kManifestURLPrefix[] = "http://manifest-url-prefix.com/"; |
| 56 const char kGoodManifestURL[] = | 59 const char kGoodManifestURL[] = |
| 57 "http://manifest-url-prefix.com/good-manifest.com"; | 60 "http://manifest-url-prefix.com/good-manifest.com"; |
| 58 const char kEvilManifestURL[] = | 61 const char kEvilManifestURL[] = |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 // Make the fetch of the precache configuration settings fail. Precaching | 185 // Make the fetch of the precache configuration settings fail. Precaching |
| 183 // should still complete normally in this case. | 186 // should still complete normally in this case. |
| 184 factory_.SetFakeResponse(GURL(kConfigURL), "", | 187 factory_.SetFakeResponse(GURL(kConfigURL), "", |
| 185 net::HTTP_INTERNAL_SERVER_ERROR, | 188 net::HTTP_INTERNAL_SERVER_ERROR, |
| 186 net::URLRequestStatus::FAILED); | 189 net::URLRequestStatus::FAILED); |
| 187 precache_manager_.reset( | 190 precache_manager_.reset( |
| 188 new PrecacheManagerUnderTest( | 191 new PrecacheManagerUnderTest( |
| 189 &browser_context_, nullptr /* sync_service */, | 192 &browser_context_, nullptr /* sync_service */, |
| 190 &history_service_, db_path, std::move(precache_database))); | 193 &history_service_, db_path, std::move(precache_database))); |
| 191 base::RunLoop().RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
| 195 | |
| 196 info_.headers = new net::HttpResponseHeaders(""); | |
| 192 } | 197 } |
| 193 | 198 |
| 194 // Must be declared first so that it is destroyed last. | 199 // Must be declared first so that it is destroyed last. |
| 195 content::TestBrowserThreadBundle test_browser_thread_bundle_; | 200 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 196 base::ScopedTempDir scoped_temp_dir_; | 201 base::ScopedTempDir scoped_temp_dir_; |
| 197 PrecacheDatabase* precache_database_; | 202 PrecacheDatabase* precache_database_; |
| 198 content::TestBrowserContext browser_context_; | 203 content::TestBrowserContext browser_context_; |
| 199 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_; | 204 std::unique_ptr<PrecacheManagerUnderTest> precache_manager_; |
| 200 TestURLFetcherCallback url_callback_; | 205 TestURLFetcherCallback url_callback_; |
| 201 net::FakeURLFetcherFactory factory_; | 206 net::FakeURLFetcherFactory factory_; |
| 202 TestPrecacheCompletionCallback precache_callback_; | 207 TestPrecacheCompletionCallback precache_callback_; |
| 203 testing::NiceMock<MockHistoryService> history_service_; | 208 testing::NiceMock<MockHistoryService> history_service_; |
| 204 base::HistogramTester histograms_; | 209 base::HistogramTester histograms_; |
| 210 net::HttpResponseInfo info_; | |
| 205 }; | 211 }; |
| 206 | 212 |
| 207 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { | 213 TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { |
| 208 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 214 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 209 | 215 |
| 210 MockHistoryService::TopHostsCallback top_hosts_callback; | 216 MockHistoryService::TopHostsCallback top_hosts_callback; |
| 211 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) | 217 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) |
| 212 .WillOnce(SaveArg<1>(&top_hosts_callback)); | 218 .WillOnce(SaveArg<1>(&top_hosts_callback)); |
| 213 | 219 |
| 214 factory_.SetFakeResponse(GURL(kGoodManifestURL), "", net::HTTP_OK, | 220 factory_.SetFakeResponse(GURL(kGoodManifestURL), "", net::HTTP_OK, |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 std::multiset<GURL> expected_requested_urls; | 348 std::multiset<GURL> expected_requested_urls; |
| 343 expected_requested_urls.insert(GURL(kConfigURL)); | 349 expected_requested_urls.insert(GURL(kConfigURL)); |
| 344 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 350 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 345 } | 351 } |
| 346 | 352 |
| 347 // TODO(rajendrant): Add unittests for | 353 // TODO(rajendrant): Add unittests for |
| 348 // PrecacheUtil::UpdatePrecacheMetricsAndState() for more test coverage. | 354 // PrecacheUtil::UpdatePrecacheMetricsAndState() for more test coverage. |
| 349 TEST_F(PrecacheManagerTest, RecordStatsForFetchWithSizeZero) { | 355 TEST_F(PrecacheManagerTest, RecordStatsForFetchWithSizeZero) { |
| 350 // Fetches with size 0 should be ignored. | 356 // Fetches with size 0 should be ignored. |
| 351 precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(), | 357 precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(), |
| 352 base::TimeDelta(), base::Time(), 0, | 358 base::TimeDelta(), base::Time(), info_, |
| 353 false); | 359 0); |
| 354 base::RunLoop().RunUntilIdle(); | 360 base::RunLoop().RunUntilIdle(); |
| 355 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); | 361 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); |
| 356 } | 362 } |
| 357 | 363 |
| 358 TEST_F(PrecacheManagerTest, RecordStatsForFetchWithNonHTTP) { | 364 TEST_F(PrecacheManagerTest, RecordStatsForFetchWithNonHTTP) { |
| 359 // Fetches for URLs with schemes other than HTTP or HTTPS should be ignored. | 365 // Fetches for URLs with schemes other than HTTP or HTTPS should be ignored. |
| 360 precache_manager_->RecordStatsForFetch(GURL("ftp://ftp.com"), GURL(), | 366 precache_manager_->RecordStatsForFetch(GURL("ftp://ftp.com"), GURL(), |
| 361 base::TimeDelta(), base::Time(), 1000, | 367 base::TimeDelta(), base::Time(), info_, |
| 362 false); | 368 1000); |
| 363 base::RunLoop().RunUntilIdle(); | 369 base::RunLoop().RunUntilIdle(); |
| 364 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); | 370 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); |
| 365 } | 371 } |
| 366 | 372 |
| 367 TEST_F(PrecacheManagerTest, RecordStatsForFetchWithEmptyURL) { | 373 TEST_F(PrecacheManagerTest, RecordStatsForFetchWithEmptyURL) { |
| 368 // Fetches for empty URLs should be ignored. | 374 // Fetches for empty URLs should be ignored. |
| 369 precache_manager_->RecordStatsForFetch(GURL(), GURL(), base::TimeDelta(), | 375 precache_manager_->RecordStatsForFetch(GURL(), GURL(), base::TimeDelta(), |
| 370 base::Time(), 1000, false); | 376 base::Time(), info_, 1000); |
| 371 base::RunLoop().RunUntilIdle(); | 377 base::RunLoop().RunUntilIdle(); |
| 372 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); | 378 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), IsEmpty()); |
| 373 } | 379 } |
| 374 | 380 |
| 375 TEST_F(PrecacheManagerTest, RecordStatsForFetchDuringPrecaching) { | 381 TEST_F(PrecacheManagerTest, RecordStatsForFetchDuringPrecaching) { |
| 376 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) | 382 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) |
| 377 .WillOnce(ReturnHosts(history::TopHostsList())); | 383 .WillOnce(ReturnHosts(history::TopHostsList())); |
| 378 | 384 |
| 379 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); | 385 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); |
| 380 | 386 |
| 381 EXPECT_TRUE(precache_manager_->IsPrecaching()); | 387 EXPECT_TRUE(precache_manager_->IsPrecaching()); |
| 382 precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(), | 388 precache_manager_->RecordStatsForFetch(GURL("http://url.com"), GURL(), |
| 383 base::TimeDelta(), base::Time(), 1000, | 389 base::TimeDelta(), base::Time(), info_, |
| 384 false); | 390 1000); |
| 385 base::RunLoop().RunUntilIdle(); | 391 base::RunLoop().RunUntilIdle(); |
| 386 precache_manager_->CancelPrecaching(); | 392 precache_manager_->CancelPrecaching(); |
| 387 | 393 |
| 388 // For PrecacheFetcher and RecordURLPrecached. | 394 // For PrecacheFetcher and RecordURLPrecached. |
| 389 base::RunLoop().RunUntilIdle(); | 395 base::RunLoop().RunUntilIdle(); |
| 390 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 396 EXPECT_THAT( |
| 391 ElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1), | 397 histograms_.GetTotalCountsForPrefix("Precache."), |
| 392 Pair("Precache.Fetch.PercentCompleted", 1), | 398 UnorderedElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1), |
| 393 Pair("Precache.Fetch.ResponseBytes.Network", 1), | 399 Pair("Precache.Fetch.PercentCompleted", 1), |
| 394 Pair("Precache.Fetch.ResponseBytes.Total", 1), | 400 Pair("Precache.Fetch.ResponseBytes.Network", 1), |
| 395 Pair("Precache.Fetch.TimeToComplete", 1), | 401 Pair("Precache.Fetch.ResponseBytes.Total", 1), |
| 396 Pair("Precache.Latency.Prefetch", 1))); | 402 Pair("Precache.Fetch.TimeToComplete", 1), |
| 403 Pair("Precache.Latency.Prefetch", 1), | |
| 404 Pair("Precache.Freshness.Prefetch", 1))); | |
| 397 } | 405 } |
| 398 | 406 |
| 399 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) { | 407 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) { |
| 400 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(), | 408 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(), |
| 401 base::TimeDelta(), base::Time(), 1000, | 409 base::TimeDelta(), base::Time(), info_, |
| 402 false); | 410 1000); |
| 403 base::RunLoop().RunUntilIdle(); | 411 base::RunLoop().RunUntilIdle(); |
| 404 | 412 |
| 405 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 413 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 406 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), | 414 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), |
| 407 Pair("Precache.Latency.NonPrefetch", 1), | 415 Pair("Precache.Latency.NonPrefetch", 1), |
| 408 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); | 416 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); |
| 409 } | 417 } |
| 410 | 418 |
| 411 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTPS) { | 419 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTPS) { |
| 412 precache_manager_->RecordStatsForFetch(GURL("https://https-url.com"), GURL(), | 420 precache_manager_->RecordStatsForFetch(GURL("https://https-url.com"), GURL(), |
| 413 base::TimeDelta(), base::Time(), 1000, | 421 base::TimeDelta(), base::Time(), info_, |
| 414 false); | 422 1000); |
| 415 base::RunLoop().RunUntilIdle(); | 423 base::RunLoop().RunUntilIdle(); |
| 416 | 424 |
| 417 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 425 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 418 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), | 426 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), |
| 419 Pair("Precache.Latency.NonPrefetch", 1), | 427 Pair("Precache.Latency.NonPrefetch", 1), |
| 420 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); | 428 Pair("Precache.Latency.NonPrefetch.NonTopHosts", 1))); |
| 421 } | 429 } |
| 422 | 430 |
| 423 TEST_F(PrecacheManagerTest, RecordStatsForFetchInTopHosts) { | 431 TEST_F(PrecacheManagerTest, RecordStatsForFetchInTopHosts) { |
| 424 EXPECT_CALL(history_service_, | 432 EXPECT_CALL(history_service_, |
| 425 HostRankIfAvailable(GURL("http://referrer.com"), _)) | 433 HostRankIfAvailable(GURL("http://referrer.com"), _)) |
| 426 .WillOnce(Invoke( | 434 .WillOnce(Invoke( |
| 427 [](const GURL& url, const base::Callback<void(int)>& callback) { | 435 [](const GURL& url, const base::Callback<void(int)>& callback) { |
| 428 callback.Run(0); | 436 callback.Run(0); |
| 429 })); | 437 })); |
| 430 precache_manager_->RecordStatsForFetch( | 438 precache_manager_->RecordStatsForFetch( |
| 431 GURL("http://http-url.com"), GURL("http://referrer.com"), | 439 GURL("http://http-url.com"), GURL("http://referrer.com"), |
| 432 base::TimeDelta(), base::Time(), 1000, false); | 440 base::TimeDelta(), base::Time(), info_, 1000); |
| 433 base::RunLoop().RunUntilIdle(); | 441 base::RunLoop().RunUntilIdle(); |
| 434 | 442 |
| 435 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 443 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 436 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), | 444 ElementsAre(Pair("Precache.DownloadedNonPrecache", 1), |
| 437 Pair("Precache.Latency.NonPrefetch", 1), | 445 Pair("Precache.Latency.NonPrefetch", 1), |
| 438 Pair("Precache.Latency.NonPrefetch.TopHosts", 1))); | 446 Pair("Precache.Latency.NonPrefetch.TopHosts", 1))); |
| 439 } | 447 } |
| 440 | 448 |
| 441 TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) { | 449 TEST_F(PrecacheManagerTest, DeleteExpiredPrecacheHistory) { |
| 442 // TODO(twifkak): Split this into multiple tests. | 450 // TODO(twifkak): Split this into multiple tests. |
| 443 base::HistogramTester::CountsMap expected_histogram_count_map; | 451 base::HistogramTester::CountsMap expected_histogram_count_map; |
| 444 | 452 |
| 445 // This test has to use Time::Now() because StartPrecaching uses Time::Now(). | 453 // This test has to use Time::Now() because StartPrecaching uses Time::Now(). |
| 446 const base::Time kCurrentTime = base::Time::Now(); | 454 const base::Time kCurrentTime = base::Time::Now(); |
| 447 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) | 455 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) |
| 448 .Times(2) | 456 .Times(2) |
| 449 .WillRepeatedly(ReturnHosts(history::TopHostsList())); | 457 .WillRepeatedly(ReturnHosts(history::TopHostsList())); |
| 450 | 458 |
| 451 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); | 459 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); |
| 452 EXPECT_TRUE(precache_manager_->IsPrecaching()); | 460 EXPECT_TRUE(precache_manager_->IsPrecaching()); |
| 453 | 461 |
| 454 // Precache a bunch of URLs, with different fetch times. | 462 // Precache a bunch of URLs, with different fetch times. |
| 455 precache_manager_->RecordStatsForFetch( | 463 precache_manager_->RecordStatsForFetch( |
| 456 GURL("http://old-fetch.com"), GURL(), base::TimeDelta(), | 464 GURL("http://old-fetch.com"), GURL(), base::TimeDelta(), |
| 457 kCurrentTime - base::TimeDelta::FromDays(61), 1000, false); | 465 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000); |
| 458 precache_manager_->RecordStatsForFetch( | 466 precache_manager_->RecordStatsForFetch( |
| 459 GURL("http://recent-fetch.com"), GURL(), base::TimeDelta(), | 467 GURL("http://recent-fetch.com"), GURL(), base::TimeDelta(), |
| 460 kCurrentTime - base::TimeDelta::FromDays(59), 1000, false); | 468 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000); |
| 461 precache_manager_->RecordStatsForFetch( | 469 precache_manager_->RecordStatsForFetch( |
| 462 GURL("http://yesterday-fetch.com"), GURL(), base::TimeDelta(), | 470 GURL("http://yesterday-fetch.com"), GURL(), base::TimeDelta(), |
| 463 kCurrentTime - base::TimeDelta::FromDays(1), 1000, false); | 471 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000); |
| 464 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3; | 472 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3; |
| 465 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; | 473 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; |
| 466 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; | 474 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; |
| 467 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; | 475 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; |
| 468 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; | 476 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; |
| 469 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3; | 477 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3; |
| 478 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3; | |
| 470 base::RunLoop().RunUntilIdle(); | 479 base::RunLoop().RunUntilIdle(); |
| 471 | 480 |
| 472 precache_manager_->CancelPrecaching(); | 481 precache_manager_->CancelPrecaching(); |
| 473 base::RunLoop().RunUntilIdle(); | 482 base::RunLoop().RunUntilIdle(); |
| 474 | 483 |
| 475 // Disable pause-resume. | 484 // Disable pause-resume. |
| 476 precache_database_->DeleteUnfinishedWork(); | 485 precache_database_->DeleteUnfinishedWork(); |
| 477 base::RunLoop().RunUntilIdle(); | 486 base::RunLoop().RunUntilIdle(); |
| 478 | 487 |
| 479 // For PrecacheFetcher and RecordURLPrecached. | 488 // For PrecacheFetcher and RecordURLPrecached. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 492 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; | 501 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; |
| 493 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; | 502 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; |
| 494 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; | 503 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; |
| 495 // For PrecacheFetcher and RecordURLPrecached. | 504 // For PrecacheFetcher and RecordURLPrecached. |
| 496 base::RunLoop().RunUntilIdle(); | 505 base::RunLoop().RunUntilIdle(); |
| 497 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 506 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 498 | 507 |
| 499 // A fetch for the same URL as the expired precache was served from the cache, | 508 // A fetch for the same URL as the expired precache was served from the cache, |
| 500 // but it isn't reported as saved bytes because it had expired in the precache | 509 // but it isn't reported as saved bytes because it had expired in the precache |
| 501 // history. | 510 // history. |
| 511 info_.was_cached = true; // From now on all fetches are cached. | |
| 502 precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(), | 512 precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(), |
| 503 base::TimeDelta(), kCurrentTime, 1000, | 513 base::TimeDelta(), kCurrentTime, info_, |
| 504 true); | 514 1000); |
| 505 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; | 515 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; |
| 506 expected_histogram_count_map["Precache.Latency.NonPrefetch"]++; | 516 expected_histogram_count_map["Precache.Latency.NonPrefetch"]++; |
| 507 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++; | 517 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++; |
| 508 | 518 |
| 509 base::RunLoop().RunUntilIdle(); | 519 base::RunLoop().RunUntilIdle(); |
| 510 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 520 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 511 ContainerEq(expected_histogram_count_map)); | 521 ContainerEq(expected_histogram_count_map)); |
| 512 | 522 |
| 513 // The other precaches should not have expired, so the following fetches from | 523 // The other precaches should not have expired, so the following fetches from |
| 514 // the cache should count as saved bytes. | 524 // the cache should count as saved bytes. |
| 515 precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), GURL() , | 525 precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), |
| 516 base::TimeDelta(), kCurrentTime, 1000, | 526 GURL(), base::TimeDelta(), |
|
bengr
2016/07/21 00:03:27
#include "base/time/time.h"
| |
| 517 true); | 527 kCurrentTime, info_, 1000); |
| 518 precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"), | 528 precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"), |
| 519 GURL(), base::TimeDelta(), kCurrentTime, | 529 GURL(), base::TimeDelta(), |
| 520 1000, true); | 530 kCurrentTime, info_, 1000); |
| 521 expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2; | 531 expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2; |
| 522 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2; | 532 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2; |
| 523 expected_histogram_count_map["Precache.Saved"] += 2; | 533 expected_histogram_count_map["Precache.Saved"] += 2; |
| 534 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; | |
| 524 | 535 |
| 525 base::RunLoop().RunUntilIdle(); | 536 base::RunLoop().RunUntilIdle(); |
| 526 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 537 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 527 ContainerEq(expected_histogram_count_map)); | 538 ContainerEq(expected_histogram_count_map)); |
| 528 } | 539 } |
| 529 | 540 |
| 530 } // namespace precache | 541 } // namespace precache |
| OLD | NEW |