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 #include <utility> | |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
11 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
14 #include "base/time/default_clock.h" | 15 #include "base/time/default_clock.h" |
15 #include "components/ntp_snippets/category.h" | 16 #include "components/ntp_snippets/category.h" |
16 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" | 17 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
17 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" | 18 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 } | 83 } |
83 return os; | 84 return os; |
84 } | 85 } |
85 | 86 |
86 } // namespace ntp_snippets | 87 } // namespace ntp_snippets |
87 | 88 |
88 namespace { | 89 namespace { |
89 | 90 |
90 const int kDefaultMaxDownloadAgeHours = 6 * 7 * 24; | 91 const int kDefaultMaxDownloadAgeHours = 6 * 7 * 24; |
91 | 92 |
92 // Tue, 31 Jan 2017 13:00:00 UTC | 93 base::Time GetDummyNowTime() { |
vitaliii
2017/05/19 07:09:07
How about
== CODE STARTS ==
base::Time GetTimeFro
| |
93 const base::Time now = base::Time::FromJavaTime(1485867600000); | 94 static base::Time now; |
95 static bool now_set = false; | |
96 if (!now_set) { | |
97 CHECK(base::Time::FromUTCString("2017-01-31 13:00:00", &now)); | |
98 now_set = true; | |
99 } | |
100 return now; | |
101 } | |
102 | |
103 base::Time GetDummyNotOutdatedTime() { | |
104 return GetDummyNowTime() - | |
105 base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | |
106 base::TimeDelta::FromSeconds(1); | |
107 } | |
108 | |
109 base::Time GetDummyOutdatedTime() { | |
110 return GetDummyNowTime() - | |
111 base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
112 base::TimeDelta::FromSeconds(1); | |
113 } | |
94 | 114 |
95 // TODO(vitaliii): Move this and outputting functions above to common file and | 115 // TODO(vitaliii): Move this and outputting functions above to common file and |
96 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. | 116 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. |
97 // See crbug.com/655513. | 117 // See crbug.com/655513. |
98 MATCHER_P(HasUrl, url, "") { | 118 MATCHER_P(HasUrl, url, "") { |
99 *result_listener << "expected URL: " << url | 119 *result_listener << "expected URL: " << url |
100 << "has URL: " << arg.url().spec(); | 120 << "has URL: " << arg.url().spec(); |
101 return arg.url().spec() == url; | 121 return arg.url().spec() == url; |
102 } | 122 } |
103 | 123 |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
464 FireDownloadCreated(asset_downloads[1].get()); | 484 FireDownloadCreated(asset_downloads[1].get()); |
465 } | 485 } |
466 | 486 |
467 TEST_F(DownloadSuggestionsProviderTest, ShouldSortSuggestions) { | 487 TEST_F(DownloadSuggestionsProviderTest, ShouldSortSuggestions) { |
468 IgnoreOnCategoryStatusChangedToAvailable(); | 488 IgnoreOnCategoryStatusChangedToAvailable(); |
469 IgnoreOnSuggestionInvalidated(); | 489 IgnoreOnSuggestionInvalidated(); |
470 | 490 |
471 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); | 491 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); |
472 | 492 |
473 offline_pages[0].url = GURL("http://dummy.com/0"); | 493 offline_pages[0].url = GURL("http://dummy.com/0"); |
474 offline_pages[0].creation_time = now - base::TimeDelta::FromMinutes(10); | 494 offline_pages[0].creation_time = |
495 GetDummyNowTime() - base::TimeDelta::FromMinutes(10); | |
475 offline_pages[0].last_access_time = offline_pages[0].creation_time; | 496 offline_pages[0].last_access_time = offline_pages[0].creation_time; |
476 | 497 |
477 offline_pages[1].url = GURL("http://dummy.com/1"); | 498 offline_pages[1].url = GURL("http://dummy.com/1"); |
478 offline_pages[1].creation_time = now - base::TimeDelta::FromMinutes(5); | 499 offline_pages[1].creation_time = |
500 GetDummyNowTime() - base::TimeDelta::FromMinutes(5); | |
479 offline_pages[1].last_access_time = offline_pages[1].creation_time; | 501 offline_pages[1].last_access_time = offline_pages[1].creation_time; |
480 | 502 |
481 *(offline_pages_model()->mutable_items()) = offline_pages; | 503 *(offline_pages_model()->mutable_items()) = offline_pages; |
482 | 504 |
483 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 505 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
484 test_clock->SetNow(now); | 506 test_clock->SetNow(GetDummyNowTime()); |
485 EXPECT_CALL(*observer(), | 507 EXPECT_CALL(*observer(), |
486 OnNewSuggestions(_, downloads_category(), | 508 OnNewSuggestions(_, downloads_category(), |
487 ElementsAre(HasUrl("http://dummy.com/1"), | 509 ElementsAre(HasUrl("http://dummy.com/1"), |
488 HasUrl("http://dummy.com/0")))); | 510 HasUrl("http://dummy.com/0")))); |
489 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, | 511 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
490 std::move(test_clock)); | 512 std::move(test_clock)); |
491 | 513 |
492 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 514 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
493 CreateDummyAssetDownloads({2, 3}); | 515 CreateDummyAssetDownloads({2, 3}); |
494 | 516 |
495 asset_downloads[0]->SetURL(GURL("http://download.com/2")); | 517 asset_downloads[0]->SetURL(GURL("http://download.com/2")); |
496 asset_downloads[0]->SetStartTime(now - base::TimeDelta::FromMinutes(3)); | 518 asset_downloads[0]->SetStartTime(GetDummyNowTime() - |
519 base::TimeDelta::FromMinutes(3)); | |
497 | 520 |
498 asset_downloads[1]->SetURL(GURL("http://download.com/3")); | 521 asset_downloads[1]->SetURL(GURL("http://download.com/3")); |
499 asset_downloads[1]->SetStartTime(now - base::TimeDelta::FromMinutes(7)); | 522 asset_downloads[1]->SetStartTime(GetDummyNowTime() - |
523 base::TimeDelta::FromMinutes(7)); | |
500 | 524 |
501 EXPECT_CALL(*observer(), | 525 EXPECT_CALL(*observer(), |
502 OnNewSuggestions(_, downloads_category(), | 526 OnNewSuggestions(_, downloads_category(), |
503 ElementsAre(HasUrl("http://download.com/2"), | 527 ElementsAre(HasUrl("http://download.com/2"), |
504 HasUrl("http://dummy.com/1"), | 528 HasUrl("http://dummy.com/1"), |
505 HasUrl("http://dummy.com/0")))); | 529 HasUrl("http://dummy.com/0")))); |
506 FireDownloadCreated(asset_downloads[0].get()); | 530 FireDownloadCreated(asset_downloads[0].get()); |
507 | 531 |
508 EXPECT_CALL(*observer(), | 532 EXPECT_CALL(*observer(), |
509 OnNewSuggestions(_, downloads_category(), | 533 OnNewSuggestions(_, downloads_category(), |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1028 | 1052 |
1029 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 1053 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
1030 // Once the manager has been loaded, the ids should be pruned. | 1054 // Once the manager has been loaded, the ids should be pruned. |
1031 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); | 1055 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); |
1032 } | 1056 } |
1033 | 1057 |
1034 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { | 1058 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { |
1035 IgnoreOnCategoryStatusChangedToAvailable(); | 1059 IgnoreOnCategoryStatusChangedToAvailable(); |
1036 IgnoreOnSuggestionInvalidated(); | 1060 IgnoreOnSuggestionInvalidated(); |
1037 | 1061 |
1038 const base::Time not_outdated = | 1062 const base::Time not_outdated = GetDummyNotOutdatedTime(); |
1039 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | 1063 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 | 1064 |
1045 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); | 1065 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); |
1046 | 1066 |
1047 offline_pages_model()->mutable_items()->at(0).url = | 1067 offline_pages_model()->mutable_items()->at(0).url = |
1048 GURL("http://dummy.com/0"); | 1068 GURL("http://dummy.com/0"); |
1049 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; | 1069 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; |
1050 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated; | 1070 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated; |
1051 | 1071 |
1052 offline_pages_model()->mutable_items()->at(1).url = | 1072 offline_pages_model()->mutable_items()->at(1).url = |
1053 GURL("http://dummy.com/1"); | 1073 GURL("http://dummy.com/1"); |
1054 offline_pages_model()->mutable_items()->at(1).creation_time = outdated; | 1074 offline_pages_model()->mutable_items()->at(1).creation_time = outdated; |
1055 offline_pages_model()->mutable_items()->at(1).last_access_time = outdated; | 1075 offline_pages_model()->mutable_items()->at(1).last_access_time = outdated; |
1056 | 1076 |
1057 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); | 1077 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
1058 | 1078 |
1059 downloads_manager()->mutable_items()->at(0)->SetURL( | 1079 downloads_manager()->mutable_items()->at(0)->SetURL( |
1060 GURL("http://download.com/0")); | 1080 GURL("http://download.com/0")); |
1061 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated); | 1081 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated); |
1062 | 1082 |
1063 downloads_manager()->mutable_items()->at(1)->SetURL( | 1083 downloads_manager()->mutable_items()->at(1)->SetURL( |
1064 GURL("http://download.com/1")); | 1084 GURL("http://download.com/1")); |
1065 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); | 1085 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); |
1066 | 1086 |
1067 EXPECT_CALL( | 1087 EXPECT_CALL( |
1068 *observer(), | 1088 *observer(), |
1069 OnNewSuggestions(_, downloads_category(), | 1089 OnNewSuggestions(_, downloads_category(), |
1070 UnorderedElementsAre(HasUrl("http://dummy.com/0"), | 1090 UnorderedElementsAre(HasUrl("http://dummy.com/0"), |
1071 HasUrl("http://download.com/0")))); | 1091 HasUrl("http://download.com/0")))); |
1072 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1092 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
1073 test_clock->SetNow(now); | 1093 test_clock->SetNow(GetDummyNowTime()); |
1074 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, | 1094 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
1075 std::move(test_clock)); | 1095 std::move(test_clock)); |
1076 } | 1096 } |
1077 | 1097 |
1078 TEST_F(DownloadSuggestionsProviderTest, | 1098 TEST_F(DownloadSuggestionsProviderTest, |
1079 ShouldShowRecentlyVisitedOfflinePageDownloads) { | 1099 ShouldShowRecentlyVisitedOfflinePageDownloads) { |
1080 IgnoreOnCategoryStatusChangedToAvailable(); | 1100 IgnoreOnCategoryStatusChangedToAvailable(); |
1081 IgnoreOnSuggestionInvalidated(); | 1101 IgnoreOnSuggestionInvalidated(); |
1082 | 1102 |
1083 const base::Time not_outdated = | 1103 const base::Time not_outdated = GetDummyNotOutdatedTime(); |
1084 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | 1104 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 | 1105 |
1090 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); | 1106 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); |
1091 | 1107 |
1092 offline_pages[0].url = GURL("http://dummy.com/0"); | 1108 offline_pages[0].url = GURL("http://dummy.com/0"); |
1093 offline_pages[0].creation_time = outdated; | 1109 offline_pages[0].creation_time = outdated; |
1094 offline_pages[0].last_access_time = not_outdated; | 1110 offline_pages[0].last_access_time = not_outdated; |
1095 | 1111 |
1096 offline_pages[1].url = GURL("http://dummy.com/1"); | 1112 offline_pages[1].url = GURL("http://dummy.com/1"); |
1097 offline_pages[1].creation_time = outdated; | 1113 offline_pages[1].creation_time = outdated; |
1098 offline_pages[1].last_access_time = offline_pages[1].creation_time; | 1114 offline_pages[1].last_access_time = offline_pages[1].creation_time; |
1099 | 1115 |
1100 *(offline_pages_model()->mutable_items()) = offline_pages; | 1116 *(offline_pages_model()->mutable_items()) = offline_pages; |
1101 | 1117 |
1102 // Even though page 0 was created long time ago, it should be reported because | 1118 // Even though page 0 was created long time ago, it should be reported because |
1103 // it has been visited recently. | 1119 // it has been visited recently. |
1104 EXPECT_CALL( | 1120 EXPECT_CALL( |
1105 *observer(), | 1121 *observer(), |
1106 OnNewSuggestions(_, downloads_category(), | 1122 OnNewSuggestions(_, downloads_category(), |
1107 UnorderedElementsAre(HasUrl("http://dummy.com/0")))); | 1123 UnorderedElementsAre(HasUrl("http://dummy.com/0")))); |
1108 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1124 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
1109 test_clock->SetNow(now); | 1125 test_clock->SetNow(GetDummyNowTime()); |
1110 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, | 1126 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, |
1111 std::move(test_clock)); | 1127 std::move(test_clock)); |
1112 } | 1128 } |
1113 | 1129 |
1114 TEST_F(DownloadSuggestionsProviderTest, | 1130 TEST_F(DownloadSuggestionsProviderTest, |
1115 ShouldShowRecentlyVisitedAssetDownloads) { | 1131 ShouldShowRecentlyVisitedAssetDownloads) { |
1116 IgnoreOnCategoryStatusChangedToAvailable(); | 1132 IgnoreOnCategoryStatusChangedToAvailable(); |
1117 IgnoreOnSuggestionInvalidated(); | 1133 IgnoreOnSuggestionInvalidated(); |
1118 | 1134 |
1119 const base::Time not_outdated = | 1135 const base::Time not_outdated = GetDummyNotOutdatedTime(); |
1120 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | 1136 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 | 1137 |
1126 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); | 1138 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
1127 | 1139 |
1128 downloads_manager()->mutable_items()->at(0)->SetURL( | 1140 downloads_manager()->mutable_items()->at(0)->SetURL( |
1129 GURL("http://download.com/0")); | 1141 GURL("http://download.com/0")); |
1130 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated); | 1142 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated); |
1131 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated); | 1143 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated); |
1132 | 1144 |
1133 downloads_manager()->mutable_items()->at(1)->SetURL( | 1145 downloads_manager()->mutable_items()->at(1)->SetURL( |
1134 GURL("http://download.com/1")); | 1146 GURL("http://download.com/1")); |
1135 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); | 1147 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); |
1136 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( | 1148 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( |
1137 downloads_manager()->mutable_items()->at(1)->GetStartTime()); | 1149 downloads_manager()->mutable_items()->at(1)->GetStartTime()); |
1138 | 1150 |
1139 // Even though asset download 0 was downloaded long time ago, it should be | 1151 // Even though asset download 0 was downloaded long time ago, it should be |
1140 // reported because it has been visited recently. | 1152 // reported because it has been visited recently. |
1141 EXPECT_CALL( | 1153 EXPECT_CALL( |
1142 *observer(), | 1154 *observer(), |
1143 OnNewSuggestions(_, downloads_category(), | 1155 OnNewSuggestions(_, downloads_category(), |
1144 UnorderedElementsAre(HasUrl("http://download.com/0")))); | 1156 UnorderedElementsAre(HasUrl("http://download.com/0")))); |
1145 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1157 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
1146 test_clock->SetNow(now); | 1158 test_clock->SetNow(GetDummyNowTime()); |
1147 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, | 1159 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
1148 std::move(test_clock)); | 1160 std::move(test_clock)); |
1149 } | 1161 } |
OLD | NEW |