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 CalculateDummyNowTime() { |
93 const base::Time now = base::Time::FromJavaTime(1485867600000); | 94 base::Time now; |
| 95 CHECK(base::Time::FromUTCString("2017-01-31 13:00:00", &now)); |
| 96 return now; |
| 97 } |
| 98 |
| 99 const base::Time now = CalculateDummyNowTime(); |
| 100 |
| 101 const base::Time kNotOutdatedTime = |
| 102 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + |
| 103 base::TimeDelta::FromSeconds(1); |
| 104 |
| 105 const base::Time kOutdatedTime = |
| 106 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - |
| 107 base::TimeDelta::FromSeconds(1); |
94 | 108 |
95 // TODO(vitaliii): Move this and outputting functions above to common file and | 109 // TODO(vitaliii): Move this and outputting functions above to common file and |
96 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. | 110 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. |
97 // See crbug.com/655513. | 111 // See crbug.com/655513. |
98 MATCHER_P(HasUrl, url, "") { | 112 MATCHER_P(HasUrl, url, "") { |
99 *result_listener << "expected URL: " << url | 113 *result_listener << "expected URL: " << url |
100 << "has URL: " << arg.url().spec(); | 114 << "has URL: " << arg.url().spec(); |
101 return arg.url().spec() == url; | 115 return arg.url().spec() == url; |
102 } | 116 } |
103 | 117 |
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 | 1042 |
1029 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 1043 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
1030 // Once the manager has been loaded, the ids should be pruned. | 1044 // Once the manager has been loaded, the ids should be pruned. |
1031 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); | 1045 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); |
1032 } | 1046 } |
1033 | 1047 |
1034 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { | 1048 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { |
1035 IgnoreOnCategoryStatusChangedToAvailable(); | 1049 IgnoreOnCategoryStatusChangedToAvailable(); |
1036 IgnoreOnSuggestionInvalidated(); | 1050 IgnoreOnSuggestionInvalidated(); |
1037 | 1051 |
1038 const base::Time not_outdated = | |
1039 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | |
1040 base::TimeDelta::FromSeconds(1); | |
1041 const base::Time outdated = | |
1042 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
1043 base::TimeDelta::FromSeconds(1); | |
1044 | |
1045 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); | 1052 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); |
1046 | 1053 |
1047 offline_pages_model()->mutable_items()->at(0).url = | 1054 offline_pages_model()->mutable_items()->at(0).url = |
1048 GURL("http://dummy.com/0"); | 1055 GURL("http://dummy.com/0"); |
1049 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; | 1056 offline_pages_model()->mutable_items()->at(0).creation_time = |
1050 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated; | 1057 kNotOutdatedTime; |
| 1058 offline_pages_model()->mutable_items()->at(0).last_access_time = |
| 1059 kNotOutdatedTime; |
1051 | 1060 |
1052 offline_pages_model()->mutable_items()->at(1).url = | 1061 offline_pages_model()->mutable_items()->at(1).url = |
1053 GURL("http://dummy.com/1"); | 1062 GURL("http://dummy.com/1"); |
1054 offline_pages_model()->mutable_items()->at(1).creation_time = outdated; | 1063 offline_pages_model()->mutable_items()->at(1).creation_time = kOutdatedTime; |
1055 offline_pages_model()->mutable_items()->at(1).last_access_time = outdated; | 1064 offline_pages_model()->mutable_items()->at(1).last_access_time = |
| 1065 kOutdatedTime; |
1056 | 1066 |
1057 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); | 1067 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
1058 | 1068 |
1059 downloads_manager()->mutable_items()->at(0)->SetURL( | 1069 downloads_manager()->mutable_items()->at(0)->SetURL( |
1060 GURL("http://download.com/0")); | 1070 GURL("http://download.com/0")); |
1061 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated); | 1071 downloads_manager()->mutable_items()->at(0)->SetStartTime(kNotOutdatedTime); |
1062 | 1072 |
1063 downloads_manager()->mutable_items()->at(1)->SetURL( | 1073 downloads_manager()->mutable_items()->at(1)->SetURL( |
1064 GURL("http://download.com/1")); | 1074 GURL("http://download.com/1")); |
1065 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); | 1075 downloads_manager()->mutable_items()->at(1)->SetStartTime(kOutdatedTime); |
1066 | 1076 |
1067 EXPECT_CALL( | 1077 EXPECT_CALL( |
1068 *observer(), | 1078 *observer(), |
1069 OnNewSuggestions(_, downloads_category(), | 1079 OnNewSuggestions(_, downloads_category(), |
1070 UnorderedElementsAre(HasUrl("http://dummy.com/0"), | 1080 UnorderedElementsAre(HasUrl("http://dummy.com/0"), |
1071 HasUrl("http://download.com/0")))); | 1081 HasUrl("http://download.com/0")))); |
1072 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1082 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
1073 test_clock->SetNow(now); | 1083 test_clock->SetNow(now); |
1074 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, | 1084 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
1075 std::move(test_clock)); | 1085 std::move(test_clock)); |
1076 } | 1086 } |
1077 | 1087 |
1078 TEST_F(DownloadSuggestionsProviderTest, | 1088 TEST_F(DownloadSuggestionsProviderTest, |
1079 ShouldShowRecentlyVisitedOfflinePageDownloads) { | 1089 ShouldShowRecentlyVisitedOfflinePageDownloads) { |
1080 IgnoreOnCategoryStatusChangedToAvailable(); | 1090 IgnoreOnCategoryStatusChangedToAvailable(); |
1081 IgnoreOnSuggestionInvalidated(); | 1091 IgnoreOnSuggestionInvalidated(); |
1082 | 1092 |
1083 const base::Time not_outdated = | |
1084 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | |
1085 base::TimeDelta::FromSeconds(1); | |
1086 const base::Time outdated = | |
1087 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
1088 base::TimeDelta::FromSeconds(1); | |
1089 | |
1090 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); | 1093 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); |
1091 | 1094 |
1092 offline_pages[0].url = GURL("http://dummy.com/0"); | 1095 offline_pages[0].url = GURL("http://dummy.com/0"); |
1093 offline_pages[0].creation_time = outdated; | 1096 offline_pages[0].creation_time = kOutdatedTime; |
1094 offline_pages[0].last_access_time = not_outdated; | 1097 offline_pages[0].last_access_time = kNotOutdatedTime; |
1095 | 1098 |
1096 offline_pages[1].url = GURL("http://dummy.com/1"); | 1099 offline_pages[1].url = GURL("http://dummy.com/1"); |
1097 offline_pages[1].creation_time = outdated; | 1100 offline_pages[1].creation_time = kOutdatedTime; |
1098 offline_pages[1].last_access_time = offline_pages[1].creation_time; | 1101 offline_pages[1].last_access_time = offline_pages[1].creation_time; |
1099 | 1102 |
1100 *(offline_pages_model()->mutable_items()) = offline_pages; | 1103 *(offline_pages_model()->mutable_items()) = offline_pages; |
1101 | 1104 |
1102 // Even though page 0 was created long time ago, it should be reported because | 1105 // Even though page 0 was created long time ago, it should be reported because |
1103 // it has been visited recently. | 1106 // it has been visited recently. |
1104 EXPECT_CALL( | 1107 EXPECT_CALL( |
1105 *observer(), | 1108 *observer(), |
1106 OnNewSuggestions(_, downloads_category(), | 1109 OnNewSuggestions(_, downloads_category(), |
1107 UnorderedElementsAre(HasUrl("http://dummy.com/0")))); | 1110 UnorderedElementsAre(HasUrl("http://dummy.com/0")))); |
1108 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1111 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
1109 test_clock->SetNow(now); | 1112 test_clock->SetNow(now); |
1110 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, | 1113 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, |
1111 std::move(test_clock)); | 1114 std::move(test_clock)); |
1112 } | 1115 } |
1113 | 1116 |
1114 TEST_F(DownloadSuggestionsProviderTest, | 1117 TEST_F(DownloadSuggestionsProviderTest, |
1115 ShouldShowRecentlyVisitedAssetDownloads) { | 1118 ShouldShowRecentlyVisitedAssetDownloads) { |
1116 IgnoreOnCategoryStatusChangedToAvailable(); | 1119 IgnoreOnCategoryStatusChangedToAvailable(); |
1117 IgnoreOnSuggestionInvalidated(); | 1120 IgnoreOnSuggestionInvalidated(); |
1118 | 1121 |
1119 const base::Time not_outdated = | |
1120 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + | |
1121 base::TimeDelta::FromSeconds(1); | |
1122 const base::Time outdated = | |
1123 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - | |
1124 base::TimeDelta::FromSeconds(1); | |
1125 | |
1126 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); | 1122 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
1127 | 1123 |
1128 downloads_manager()->mutable_items()->at(0)->SetURL( | 1124 downloads_manager()->mutable_items()->at(0)->SetURL( |
1129 GURL("http://download.com/0")); | 1125 GURL("http://download.com/0")); |
1130 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated); | 1126 downloads_manager()->mutable_items()->at(0)->SetStartTime(kOutdatedTime); |
1131 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated); | 1127 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime( |
| 1128 kNotOutdatedTime); |
1132 | 1129 |
1133 downloads_manager()->mutable_items()->at(1)->SetURL( | 1130 downloads_manager()->mutable_items()->at(1)->SetURL( |
1134 GURL("http://download.com/1")); | 1131 GURL("http://download.com/1")); |
1135 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); | 1132 downloads_manager()->mutable_items()->at(1)->SetStartTime(kOutdatedTime); |
1136 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( | 1133 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( |
1137 downloads_manager()->mutable_items()->at(1)->GetStartTime()); | 1134 downloads_manager()->mutable_items()->at(1)->GetStartTime()); |
1138 | 1135 |
1139 // Even though asset download 0 was downloaded long time ago, it should be | 1136 // Even though asset download 0 was downloaded long time ago, it should be |
1140 // reported because it has been visited recently. | 1137 // reported because it has been visited recently. |
1141 EXPECT_CALL( | 1138 EXPECT_CALL( |
1142 *observer(), | 1139 *observer(), |
1143 OnNewSuggestions(_, downloads_category(), | 1140 OnNewSuggestions(_, downloads_category(), |
1144 UnorderedElementsAre(HasUrl("http://download.com/0")))); | 1141 UnorderedElementsAre(HasUrl("http://download.com/0")))); |
1145 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1142 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
1146 test_clock->SetNow(now); | 1143 test_clock->SetNow(now); |
1147 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, | 1144 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
1148 std::move(test_clock)); | 1145 std::move(test_clock)); |
1149 } | 1146 } |
OLD | NEW |