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/core/precache_database.h" | 5 #include "components/precache/core/precache_database.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
(...skipping 20 matching lines...) Expand all Loading... |
31 using base::Bucket; | 31 using base::Bucket; |
32 using net::HttpResponseInfo; | 32 using net::HttpResponseInfo; |
33 | 33 |
34 const GURL kURL("http://url.com"); | 34 const GURL kURL("http://url.com"); |
35 const int kReferrerID = 1; | 35 const int kReferrerID = 1; |
36 const base::TimeDelta kLatency = base::TimeDelta::FromMilliseconds(5); | 36 const base::TimeDelta kLatency = base::TimeDelta::FromMilliseconds(5); |
37 const base::Time kFetchTime = base::Time() + base::TimeDelta::FromHours(1000); | 37 const base::Time kFetchTime = base::Time() + base::TimeDelta::FromHours(1000); |
38 const base::Time kOldFetchTime = kFetchTime - base::TimeDelta::FromDays(1); | 38 const base::Time kOldFetchTime = kFetchTime - base::TimeDelta::FromDays(1); |
39 const base::Time kNewFetchTime = | 39 const base::Time kNewFetchTime = |
40 base::Time() + base::TimeDelta::FromHours(2000); | 40 base::Time() + base::TimeDelta::FromHours(2000); |
| 41 const base::Time kPrecacheTime = |
| 42 base::Time() + base::TimeDelta::FromHours(3000); |
41 const int64_t kSize = 5000; | 43 const int64_t kSize = 5000; |
42 const int64_t kFreshnessBucket10K = 9089; | 44 const int64_t kFreshnessBucket10K = 9089; |
43 // One of the possible CacheEntryStatus for when the fetch was served from the | 45 // One of the possible CacheEntryStatus for when the fetch was served from the |
44 // network. | 46 // network. |
45 const HttpResponseInfo::CacheEntryStatus kFromNetwork = | 47 const HttpResponseInfo::CacheEntryStatus kFromNetwork = |
46 HttpResponseInfo::CacheEntryStatus::ENTRY_UPDATED; | 48 HttpResponseInfo::CacheEntryStatus::ENTRY_UPDATED; |
47 | 49 |
48 std::map<GURL, base::Time> BuildURLTableMap(const GURL& url, | 50 std::map<GURL, base::Time> BuildURLTableMap(const GURL& url, |
49 const base::Time& precache_time) { | 51 const base::Time& precache_time) { |
50 std::map<GURL, base::Time> url_table_map; | 52 std::map<GURL, base::Time> url_table_map; |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 | 520 |
519 TEST_F(PrecacheDatabaseTest, GetURLListForReferrerHost) { | 521 TEST_F(PrecacheDatabaseTest, GetURLListForReferrerHost) { |
520 precache_database_->UpdatePrecacheReferrerHost("foo.com", 1, kFetchTime); | 522 precache_database_->UpdatePrecacheReferrerHost("foo.com", 1, kFetchTime); |
521 precache_database_->UpdatePrecacheReferrerHost("bar.com", 2, kNewFetchTime); | 523 precache_database_->UpdatePrecacheReferrerHost("bar.com", 2, kNewFetchTime); |
522 precache_database_->UpdatePrecacheReferrerHost("foobar.com", 3, | 524 precache_database_->UpdatePrecacheReferrerHost("foobar.com", 3, |
523 kNewFetchTime); | 525 kNewFetchTime); |
524 precache_database_->UpdatePrecacheReferrerHost("empty.com", 3, kNewFetchTime); | 526 precache_database_->UpdatePrecacheReferrerHost("empty.com", 3, kNewFetchTime); |
525 | 527 |
526 struct test_resource_info { | 528 struct test_resource_info { |
527 std::string url; | 529 std::string url; |
| 530 bool is_user_browsed; |
528 bool is_network_fetched; | 531 bool is_network_fetched; |
529 bool is_cellular_fetched; | 532 bool is_cellular_fetched; |
530 bool expected_in_used; | 533 bool expected_in_used; |
531 }; | 534 }; |
532 | 535 |
533 const struct { | 536 const struct { |
534 std::string hostname; | 537 std::string hostname; |
535 std::vector<test_resource_info> resource_info; | 538 std::vector<test_resource_info> resource_info; |
536 } tests[] = { | 539 } tests[] = { |
537 { | 540 { |
538 "foo.com", | 541 "foo.com", |
539 { | 542 { |
540 {"http://foo.com/from-cache.js", false, false, true}, | 543 {"http://foo.com/from-cache.js", true, false, false, true}, |
541 {"http://some-cdn.com/from-network.js", true, false, false}, | 544 {"http://some-cdn.com/from-network.js", true, true, false, false}, |
542 {"http://foo.com/from-cache-cellular.js", false, true, true}, | 545 {"http://foo.com/from-cache-cellular.js", true, false, true, |
543 {"http://foo.com/from-network-cellular.js", true, true, false}, | 546 true}, |
| 547 {"http://foo.com/from-network-cellular.js", true, true, true, |
| 548 false}, |
| 549 {"http://foo.com/not-browsed.js", false, false, false, false}, |
544 }, | 550 }, |
545 }, | 551 }, |
546 { | 552 { |
547 "bar.com", | 553 "bar.com", |
548 { | 554 { |
549 {"http://bar.com/a.js", false, false, true}, | 555 {"http://bar.com/a.js", true, false, false, true}, |
550 {"http://some-cdn.com/b.js", false, true, true}, | 556 {"http://some-cdn.com/b.js", true, false, true, true}, |
| 557 {"http://bar.com/not-browsed.js", false, false, false, false}, |
551 }, | 558 }, |
552 }, | 559 }, |
553 { | 560 { |
554 "foobar.com", | 561 "foobar.com", |
555 { | 562 { |
556 {"http://some-cdn.com/not-used.js", true, true, false}, | 563 {"http://some-cdn.com/not-used.js", true, true, true, false}, |
557 }, | 564 }, |
558 }, | 565 }, |
559 { | 566 { |
560 "empty.com", std::vector<test_resource_info>{}, | 567 "empty.com", std::vector<test_resource_info>{}, |
561 }, | 568 }, |
562 }; | 569 }; |
563 // Add precached resources. | 570 // Add precached resources. |
564 for (const auto& test : tests) { | 571 for (const auto& test : tests) { |
565 for (const auto& resource : test.resource_info) { | 572 for (const auto& resource : test.resource_info) { |
566 precache_database_->RecordURLPrefetch(GURL(resource.url), test.hostname, | 573 precache_database_->RecordURLPrefetch(GURL(resource.url), test.hostname, |
567 base::Time(), false, kSize); | 574 kPrecacheTime, false, kSize); |
568 } | 575 } |
569 } | 576 } |
570 // Update some resources as used due to user browsing. | 577 // Update some resources as used due to user browsing. |
571 for (const auto& test : tests) { | 578 for (const auto& test : tests) { |
572 for (const auto& resource : test.resource_info) { | 579 for (const auto& resource : test.resource_info) { |
| 580 if (!resource.is_user_browsed) |
| 581 continue; |
573 if (!resource.is_network_fetched && !resource.is_cellular_fetched) { | 582 if (!resource.is_network_fetched && !resource.is_cellular_fetched) { |
574 RecordFetchFromCache(GURL(resource.url), kFetchTime, kSize); | 583 RecordFetchFromCache(GURL(resource.url), kFetchTime, kSize); |
575 } else if (!resource.is_network_fetched && resource.is_cellular_fetched) { | 584 } else if (!resource.is_network_fetched && resource.is_cellular_fetched) { |
576 RecordFetchFromCacheCellular(GURL(resource.url), kFetchTime, kSize); | 585 RecordFetchFromCacheCellular(GURL(resource.url), kFetchTime, kSize); |
577 } else if (resource.is_network_fetched && !resource.is_cellular_fetched) { | 586 } else if (resource.is_network_fetched && !resource.is_cellular_fetched) { |
578 RecordFetchFromNetwork(GURL(resource.url), kLatency, kFetchTime, kSize); | 587 RecordFetchFromNetwork(GURL(resource.url), kLatency, kFetchTime, kSize); |
579 } else if (resource.is_network_fetched && resource.is_cellular_fetched) { | 588 } else if (resource.is_network_fetched && resource.is_cellular_fetched) { |
580 RecordFetchFromNetworkCellular(GURL(resource.url), kLatency, kFetchTime, | 589 RecordFetchFromNetworkCellular(GURL(resource.url), kLatency, kFetchTime, |
581 kSize); | 590 kSize); |
582 } | 591 } |
(...skipping 24 matching lines...) Expand all Loading... |
607 precache_database_->UpdatePrecacheReferrerHost(test.hostname, 100, | 616 precache_database_->UpdatePrecacheReferrerHost(test.hostname, 100, |
608 kNewFetchTime); | 617 kNewFetchTime); |
609 Flush(); | 618 Flush(); |
610 std::vector<GURL> actual_used_urls, actual_unused_urls; | 619 std::vector<GURL> actual_used_urls, actual_unused_urls; |
611 auto referrer_id = precache_database_->GetReferrerHost(test.hostname).id; | 620 auto referrer_id = precache_database_->GetReferrerHost(test.hostname).id; |
612 EXPECT_NE(PrecacheReferrerHostEntry::kInvalidId, referrer_id); | 621 EXPECT_NE(PrecacheReferrerHostEntry::kInvalidId, referrer_id); |
613 precache_database_->GetURLListForReferrerHost( | 622 precache_database_->GetURLListForReferrerHost( |
614 referrer_id, &actual_used_urls, &actual_unused_urls); | 623 referrer_id, &actual_used_urls, &actual_unused_urls); |
615 EXPECT_TRUE(actual_used_urls.empty()); | 624 EXPECT_TRUE(actual_used_urls.empty()); |
616 } | 625 } |
| 626 // Resources that were precached previously and not seen in user browsing |
| 627 // should be still marked as precached. |
| 628 std::map<GURL, base::Time> expected_url_table_map; |
| 629 for (const auto& test : tests) { |
| 630 for (const auto& resource : test.resource_info) { |
| 631 if (!resource.is_user_browsed) |
| 632 expected_url_table_map[GURL(resource.url)] = kPrecacheTime; |
| 633 } |
| 634 } |
| 635 EXPECT_EQ(expected_url_table_map, GetActualURLTableMap()); |
617 } | 636 } |
618 | 637 |
619 } // namespace | 638 } // namespace |
620 | 639 |
621 } // namespace precache | 640 } // namespace precache |
OLD | NEW |