Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/ntp_snippets/download_suggestions_provider.h" | 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 return os; | 83 return os; |
| 84 } | 84 } |
| 85 | 85 |
| 86 } // namespace ntp_snippets | 86 } // namespace ntp_snippets |
| 87 | 87 |
| 88 namespace { | 88 namespace { |
| 89 | 89 |
| 90 const int kDefaultMaxDownloadAgeHours = 6 * 7 * 24; | 90 const int kDefaultMaxDownloadAgeHours = 6 * 7 * 24; |
| 91 | 91 |
| 92 // Tue, 31 Jan 2017 13:00:00 UTC | 92 // Tue, 31 Jan 2017 13:00:00 UTC |
| 93 const base::Time now = base::Time::FromJavaTime(1485867600000); | 93 const base::Time kDummyNow = base::Time::FromInternalValue(13130341200000000); |
|
miu
2017/05/10 21:43:02
nit: Rather than FromInternalValue, how about some
| |
| 94 | |
| 95 base::Time GetDummyNotOutdatedTime() { | |
| 96 return kDummyNow - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | |
| 97 base::TimeDelta::FromSeconds(1); | |
| 98 } | |
| 99 | |
| 100 base::Time GetDummyOutdatedTime() { | |
| 101 return kDummyNow - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
| 102 base::TimeDelta::FromSeconds(1); | |
| 103 } | |
| 94 | 104 |
| 95 // TODO(vitaliii): Move this and outputting functions above to common file and | 105 // TODO(vitaliii): Move this and outputting functions above to common file and |
| 96 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. | 106 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. |
| 97 // See crbug.com/655513. | 107 // See crbug.com/655513. |
| 98 MATCHER_P(HasUrl, url, "") { | 108 MATCHER_P(HasUrl, url, "") { |
| 99 *result_listener << "expected URL: " << url | 109 *result_listener << "expected URL: " << url |
| 100 << "has URL: " << arg.url().spec(); | 110 << "has URL: " << arg.url().spec(); |
| 101 return arg.url().spec() == url; | 111 return arg.url().spec() == url; |
| 102 } | 112 } |
| 103 | 113 |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 FireDownloadCreated(asset_downloads[1].get()); | 474 FireDownloadCreated(asset_downloads[1].get()); |
| 465 } | 475 } |
| 466 | 476 |
| 467 TEST_F(DownloadSuggestionsProviderTest, ShouldSortSuggestions) { | 477 TEST_F(DownloadSuggestionsProviderTest, ShouldSortSuggestions) { |
| 468 IgnoreOnCategoryStatusChangedToAvailable(); | 478 IgnoreOnCategoryStatusChangedToAvailable(); |
| 469 IgnoreOnSuggestionInvalidated(); | 479 IgnoreOnSuggestionInvalidated(); |
| 470 | 480 |
| 471 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); | 481 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); |
| 472 | 482 |
| 473 offline_pages[0].url = GURL("http://dummy.com/0"); | 483 offline_pages[0].url = GURL("http://dummy.com/0"); |
| 474 offline_pages[0].creation_time = now - base::TimeDelta::FromMinutes(10); | 484 offline_pages[0].creation_time = kDummyNow - base::TimeDelta::FromMinutes(10); |
| 475 offline_pages[0].last_access_time = offline_pages[0].creation_time; | 485 offline_pages[0].last_access_time = offline_pages[0].creation_time; |
| 476 | 486 |
| 477 offline_pages[1].url = GURL("http://dummy.com/1"); | 487 offline_pages[1].url = GURL("http://dummy.com/1"); |
| 478 offline_pages[1].creation_time = now - base::TimeDelta::FromMinutes(5); | 488 offline_pages[1].creation_time = kDummyNow - base::TimeDelta::FromMinutes(5); |
| 479 offline_pages[1].last_access_time = offline_pages[1].creation_time; | 489 offline_pages[1].last_access_time = offline_pages[1].creation_time; |
| 480 | 490 |
| 481 *(offline_pages_model()->mutable_items()) = offline_pages; | 491 *(offline_pages_model()->mutable_items()) = offline_pages; |
| 482 | 492 |
| 483 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 493 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 484 test_clock->SetNow(now); | 494 test_clock->SetNow(kDummyNow); |
| 485 EXPECT_CALL(*observer(), | 495 EXPECT_CALL(*observer(), |
| 486 OnNewSuggestions(_, downloads_category(), | 496 OnNewSuggestions(_, downloads_category(), |
| 487 ElementsAre(HasUrl("http://dummy.com/1"), | 497 ElementsAre(HasUrl("http://dummy.com/1"), |
| 488 HasUrl("http://dummy.com/0")))); | 498 HasUrl("http://dummy.com/0")))); |
| 489 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, | 499 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 490 std::move(test_clock)); | 500 std::move(test_clock)); |
| 491 | 501 |
| 492 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 502 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 493 CreateDummyAssetDownloads({2, 3}); | 503 CreateDummyAssetDownloads({2, 3}); |
| 494 | 504 |
| 495 asset_downloads[0]->SetURL(GURL("http://download.com/2")); | 505 asset_downloads[0]->SetURL(GURL("http://download.com/2")); |
| 496 asset_downloads[0]->SetStartTime(now - base::TimeDelta::FromMinutes(3)); | 506 asset_downloads[0]->SetStartTime(kDummyNow - base::TimeDelta::FromMinutes(3)); |
| 497 | 507 |
| 498 asset_downloads[1]->SetURL(GURL("http://download.com/3")); | 508 asset_downloads[1]->SetURL(GURL("http://download.com/3")); |
| 499 asset_downloads[1]->SetStartTime(now - base::TimeDelta::FromMinutes(7)); | 509 asset_downloads[1]->SetStartTime(kDummyNow - base::TimeDelta::FromMinutes(7)); |
| 500 | 510 |
| 501 EXPECT_CALL(*observer(), | 511 EXPECT_CALL(*observer(), |
| 502 OnNewSuggestions(_, downloads_category(), | 512 OnNewSuggestions(_, downloads_category(), |
| 503 ElementsAre(HasUrl("http://download.com/2"), | 513 ElementsAre(HasUrl("http://download.com/2"), |
| 504 HasUrl("http://dummy.com/1"), | 514 HasUrl("http://dummy.com/1"), |
| 505 HasUrl("http://dummy.com/0")))); | 515 HasUrl("http://dummy.com/0")))); |
| 506 FireDownloadCreated(asset_downloads[0].get()); | 516 FireDownloadCreated(asset_downloads[0].get()); |
| 507 | 517 |
| 508 EXPECT_CALL(*observer(), | 518 EXPECT_CALL(*observer(), |
| 509 OnNewSuggestions(_, downloads_category(), | 519 OnNewSuggestions(_, downloads_category(), |
| (...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1028 | 1038 |
| 1029 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 1039 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 1030 // Once the manager has been loaded, the ids should be pruned. | 1040 // Once the manager has been loaded, the ids should be pruned. |
| 1031 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); | 1041 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); |
| 1032 } | 1042 } |
| 1033 | 1043 |
| 1034 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { | 1044 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { |
| 1035 IgnoreOnCategoryStatusChangedToAvailable(); | 1045 IgnoreOnCategoryStatusChangedToAvailable(); |
| 1036 IgnoreOnSuggestionInvalidated(); | 1046 IgnoreOnSuggestionInvalidated(); |
| 1037 | 1047 |
| 1038 const base::Time not_outdated = | 1048 const base::Time not_outdated = GetDummyNotOutdatedTime(); |
| 1039 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | 1049 const base::Time outdated = GetDummyOutdatedTime(); |
| 1040 base::TimeDelta::FromSeconds(1); | |
| 1041 const base::Time outdated = | |
| 1042 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
| 1043 base::TimeDelta::FromSeconds(1); | |
| 1044 | 1050 |
| 1045 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); | 1051 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); |
| 1046 | 1052 |
| 1047 offline_pages_model()->mutable_items()->at(0).url = | 1053 offline_pages_model()->mutable_items()->at(0).url = |
| 1048 GURL("http://dummy.com/0"); | 1054 GURL("http://dummy.com/0"); |
| 1049 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; | 1055 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; |
| 1050 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated; | 1056 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated; |
| 1051 | 1057 |
| 1052 offline_pages_model()->mutable_items()->at(1).url = | 1058 offline_pages_model()->mutable_items()->at(1).url = |
| 1053 GURL("http://dummy.com/1"); | 1059 GURL("http://dummy.com/1"); |
| 1054 offline_pages_model()->mutable_items()->at(1).creation_time = outdated; | 1060 offline_pages_model()->mutable_items()->at(1).creation_time = outdated; |
| 1055 offline_pages_model()->mutable_items()->at(1).last_access_time = outdated; | 1061 offline_pages_model()->mutable_items()->at(1).last_access_time = outdated; |
| 1056 | 1062 |
| 1057 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); | 1063 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
| 1058 | 1064 |
| 1059 downloads_manager()->mutable_items()->at(0)->SetURL( | 1065 downloads_manager()->mutable_items()->at(0)->SetURL( |
| 1060 GURL("http://download.com/0")); | 1066 GURL("http://download.com/0")); |
| 1061 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated); | 1067 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated); |
| 1062 | 1068 |
| 1063 downloads_manager()->mutable_items()->at(1)->SetURL( | 1069 downloads_manager()->mutable_items()->at(1)->SetURL( |
| 1064 GURL("http://download.com/1")); | 1070 GURL("http://download.com/1")); |
| 1065 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); | 1071 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); |
| 1066 | 1072 |
| 1067 EXPECT_CALL( | 1073 EXPECT_CALL( |
| 1068 *observer(), | 1074 *observer(), |
| 1069 OnNewSuggestions(_, downloads_category(), | 1075 OnNewSuggestions(_, downloads_category(), |
| 1070 UnorderedElementsAre(HasUrl("http://dummy.com/0"), | 1076 UnorderedElementsAre(HasUrl("http://dummy.com/0"), |
| 1071 HasUrl("http://download.com/0")))); | 1077 HasUrl("http://download.com/0")))); |
| 1072 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1078 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1073 test_clock->SetNow(now); | 1079 test_clock->SetNow(kDummyNow); |
| 1074 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, | 1080 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 1075 std::move(test_clock)); | 1081 std::move(test_clock)); |
| 1076 } | 1082 } |
| 1077 | 1083 |
| 1078 TEST_F(DownloadSuggestionsProviderTest, | 1084 TEST_F(DownloadSuggestionsProviderTest, |
| 1079 ShouldShowRecentlyVisitedOfflinePageDownloads) { | 1085 ShouldShowRecentlyVisitedOfflinePageDownloads) { |
| 1080 IgnoreOnCategoryStatusChangedToAvailable(); | 1086 IgnoreOnCategoryStatusChangedToAvailable(); |
| 1081 IgnoreOnSuggestionInvalidated(); | 1087 IgnoreOnSuggestionInvalidated(); |
| 1082 | 1088 |
| 1083 const base::Time not_outdated = | 1089 const base::Time not_outdated = GetDummyNotOutdatedTime(); |
| 1084 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | 1090 const base::Time outdated = GetDummyOutdatedTime(); |
| 1085 base::TimeDelta::FromSeconds(1); | |
| 1086 const base::Time outdated = | |
| 1087 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
| 1088 base::TimeDelta::FromSeconds(1); | |
| 1089 | 1091 |
| 1090 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); | 1092 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); |
| 1091 | 1093 |
| 1092 offline_pages[0].url = GURL("http://dummy.com/0"); | 1094 offline_pages[0].url = GURL("http://dummy.com/0"); |
| 1093 offline_pages[0].creation_time = outdated; | 1095 offline_pages[0].creation_time = outdated; |
| 1094 offline_pages[0].last_access_time = not_outdated; | 1096 offline_pages[0].last_access_time = not_outdated; |
| 1095 | 1097 |
| 1096 offline_pages[1].url = GURL("http://dummy.com/1"); | 1098 offline_pages[1].url = GURL("http://dummy.com/1"); |
| 1097 offline_pages[1].creation_time = outdated; | 1099 offline_pages[1].creation_time = outdated; |
| 1098 offline_pages[1].last_access_time = offline_pages[1].creation_time; | 1100 offline_pages[1].last_access_time = offline_pages[1].creation_time; |
| 1099 | 1101 |
| 1100 *(offline_pages_model()->mutable_items()) = offline_pages; | 1102 *(offline_pages_model()->mutable_items()) = offline_pages; |
| 1101 | 1103 |
| 1102 // Even though page 0 was created long time ago, it should be reported because | 1104 // Even though page 0 was created long time ago, it should be reported because |
| 1103 // it has been visited recently. | 1105 // it has been visited recently. |
| 1104 EXPECT_CALL( | 1106 EXPECT_CALL( |
| 1105 *observer(), | 1107 *observer(), |
| 1106 OnNewSuggestions(_, downloads_category(), | 1108 OnNewSuggestions(_, downloads_category(), |
| 1107 UnorderedElementsAre(HasUrl("http://dummy.com/0")))); | 1109 UnorderedElementsAre(HasUrl("http://dummy.com/0")))); |
| 1108 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1110 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1109 test_clock->SetNow(now); | 1111 test_clock->SetNow(kDummyNow); |
| 1110 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, | 1112 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, |
| 1111 std::move(test_clock)); | 1113 std::move(test_clock)); |
| 1112 } | 1114 } |
| 1113 | 1115 |
| 1114 TEST_F(DownloadSuggestionsProviderTest, | 1116 TEST_F(DownloadSuggestionsProviderTest, |
| 1115 ShouldShowRecentlyVisitedAssetDownloads) { | 1117 ShouldShowRecentlyVisitedAssetDownloads) { |
| 1116 IgnoreOnCategoryStatusChangedToAvailable(); | 1118 IgnoreOnCategoryStatusChangedToAvailable(); |
| 1117 IgnoreOnSuggestionInvalidated(); | 1119 IgnoreOnSuggestionInvalidated(); |
| 1118 | 1120 |
| 1119 const base::Time not_outdated = | 1121 const base::Time not_outdated = GetDummyNotOutdatedTime(); |
| 1120 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | 1122 const base::Time outdated = GetDummyOutdatedTime(); |
| 1121 base::TimeDelta::FromSeconds(1); | |
| 1122 const base::Time outdated = | |
| 1123 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
| 1124 base::TimeDelta::FromSeconds(1); | |
| 1125 | 1123 |
| 1126 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); | 1124 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
| 1127 | 1125 |
| 1128 downloads_manager()->mutable_items()->at(0)->SetURL( | 1126 downloads_manager()->mutable_items()->at(0)->SetURL( |
| 1129 GURL("http://download.com/0")); | 1127 GURL("http://download.com/0")); |
| 1130 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated); | 1128 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated); |
| 1131 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated); | 1129 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated); |
| 1132 | 1130 |
| 1133 downloads_manager()->mutable_items()->at(1)->SetURL( | 1131 downloads_manager()->mutable_items()->at(1)->SetURL( |
| 1134 GURL("http://download.com/1")); | 1132 GURL("http://download.com/1")); |
| 1135 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); | 1133 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); |
| 1136 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( | 1134 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( |
| 1137 downloads_manager()->mutable_items()->at(1)->GetStartTime()); | 1135 downloads_manager()->mutable_items()->at(1)->GetStartTime()); |
| 1138 | 1136 |
| 1139 // Even though asset download 0 was downloaded long time ago, it should be | 1137 // Even though asset download 0 was downloaded long time ago, it should be |
| 1140 // reported because it has been visited recently. | 1138 // reported because it has been visited recently. |
| 1141 EXPECT_CALL( | 1139 EXPECT_CALL( |
| 1142 *observer(), | 1140 *observer(), |
| 1143 OnNewSuggestions(_, downloads_category(), | 1141 OnNewSuggestions(_, downloads_category(), |
| 1144 UnorderedElementsAre(HasUrl("http://download.com/0")))); | 1142 UnorderedElementsAre(HasUrl("http://download.com/0")))); |
| 1145 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1143 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1146 test_clock->SetNow(now); | 1144 test_clock->SetNow(kDummyNow); |
| 1147 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, | 1145 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
| 1148 std::move(test_clock)); | 1146 std::move(test_clock)); |
| 1149 } | 1147 } |
| OLD | NEW |