Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc

Issue 2892553005: Avoid base::Time::FromJavaTime() when not dealing with Java. (Closed)
Patch Set: address comment Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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();
miu 2017/05/22 21:45:31 Global static initializer here. So, this goes agai
Lei Zhang 2017/05/22 22:03:57 Yes, which is what I was avoiding in patch set 1.
100
101 base::Time GetDummyNotOutdatedTime() {
102 return now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) +
103 base::TimeDelta::FromSeconds(1);
104 }
105
106 base::Time GetDummyOutdatedTime() {
107 return now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) -
108 base::TimeDelta::FromSeconds(1);
109 }
94 110
95 // TODO(vitaliii): Move this and outputting functions above to common file and 111 // TODO(vitaliii): Move this and outputting functions above to common file and
96 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|. 112 // replace remaining |Property(&ContentSuggestion::url, GURL("some_url"))|.
97 // See crbug.com/655513. 113 // See crbug.com/655513.
98 MATCHER_P(HasUrl, url, "") { 114 MATCHER_P(HasUrl, url, "") {
99 *result_listener << "expected URL: " << url 115 *result_listener << "expected URL: " << url
100 << "has URL: " << arg.url().spec(); 116 << "has URL: " << arg.url().spec();
101 return arg.url().spec() == url; 117 return arg.url().spec() == url;
102 } 118 }
103 119
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 1044
1029 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 1045 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
1030 // Once the manager has been loaded, the ids should be pruned. 1046 // Once the manager has been loaded, the ids should be pruned.
1031 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); 1047 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty());
1032 } 1048 }
1033 1049
1034 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { 1050 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) {
1035 IgnoreOnCategoryStatusChangedToAvailable(); 1051 IgnoreOnCategoryStatusChangedToAvailable();
1036 IgnoreOnSuggestionInvalidated(); 1052 IgnoreOnSuggestionInvalidated();
1037 1053
1038 const base::Time not_outdated = 1054 const base::Time not_outdated = GetDummyNotOutdatedTime();
1039 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + 1055 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 1056
1045 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); 1057 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1});
1046 1058
1047 offline_pages_model()->mutable_items()->at(0).url = 1059 offline_pages_model()->mutable_items()->at(0).url =
1048 GURL("http://dummy.com/0"); 1060 GURL("http://dummy.com/0");
1049 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; 1061 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated;
1050 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated; 1062 offline_pages_model()->mutable_items()->at(0).last_access_time = not_outdated;
1051 1063
1052 offline_pages_model()->mutable_items()->at(1).url = 1064 offline_pages_model()->mutable_items()->at(1).url =
1053 GURL("http://dummy.com/1"); 1065 GURL("http://dummy.com/1");
(...skipping 19 matching lines...) Expand all
1073 test_clock->SetNow(now); 1085 test_clock->SetNow(now);
1074 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, 1086 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
1075 std::move(test_clock)); 1087 std::move(test_clock));
1076 } 1088 }
1077 1089
1078 TEST_F(DownloadSuggestionsProviderTest, 1090 TEST_F(DownloadSuggestionsProviderTest,
1079 ShouldShowRecentlyVisitedOfflinePageDownloads) { 1091 ShouldShowRecentlyVisitedOfflinePageDownloads) {
1080 IgnoreOnCategoryStatusChangedToAvailable(); 1092 IgnoreOnCategoryStatusChangedToAvailable();
1081 IgnoreOnSuggestionInvalidated(); 1093 IgnoreOnSuggestionInvalidated();
1082 1094
1083 const base::Time not_outdated = 1095 const base::Time not_outdated = GetDummyNotOutdatedTime();
1084 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + 1096 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 1097
1090 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1}); 1098 std::vector<OfflinePageItem> offline_pages = CreateDummyOfflinePages({0, 1});
1091 1099
1092 offline_pages[0].url = GURL("http://dummy.com/0"); 1100 offline_pages[0].url = GURL("http://dummy.com/0");
1093 offline_pages[0].creation_time = outdated; 1101 offline_pages[0].creation_time = outdated;
1094 offline_pages[0].last_access_time = not_outdated; 1102 offline_pages[0].last_access_time = not_outdated;
1095 1103
1096 offline_pages[1].url = GURL("http://dummy.com/1"); 1104 offline_pages[1].url = GURL("http://dummy.com/1");
1097 offline_pages[1].creation_time = outdated; 1105 offline_pages[1].creation_time = outdated;
1098 offline_pages[1].last_access_time = offline_pages[1].creation_time; 1106 offline_pages[1].last_access_time = offline_pages[1].creation_time;
(...skipping 10 matching lines...) Expand all
1109 test_clock->SetNow(now); 1117 test_clock->SetNow(now);
1110 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, 1118 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
1111 std::move(test_clock)); 1119 std::move(test_clock));
1112 } 1120 }
1113 1121
1114 TEST_F(DownloadSuggestionsProviderTest, 1122 TEST_F(DownloadSuggestionsProviderTest,
1115 ShouldShowRecentlyVisitedAssetDownloads) { 1123 ShouldShowRecentlyVisitedAssetDownloads) {
1116 IgnoreOnCategoryStatusChangedToAvailable(); 1124 IgnoreOnCategoryStatusChangedToAvailable();
1117 IgnoreOnSuggestionInvalidated(); 1125 IgnoreOnSuggestionInvalidated();
1118 1126
1119 const base::Time not_outdated = 1127 const base::Time not_outdated = GetDummyNotOutdatedTime();
1120 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + 1128 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 1129
1126 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); 1130 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1});
1127 1131
1128 downloads_manager()->mutable_items()->at(0)->SetURL( 1132 downloads_manager()->mutable_items()->at(0)->SetURL(
1129 GURL("http://download.com/0")); 1133 GURL("http://download.com/0"));
1130 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated); 1134 downloads_manager()->mutable_items()->at(0)->SetStartTime(outdated);
1131 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated); 1135 downloads_manager()->mutable_items()->at(0)->SetLastAccessTime(not_outdated);
1132 1136
1133 downloads_manager()->mutable_items()->at(1)->SetURL( 1137 downloads_manager()->mutable_items()->at(1)->SetURL(
1134 GURL("http://download.com/1")); 1138 GURL("http://download.com/1"));
1135 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); 1139 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated);
1136 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime( 1140 downloads_manager()->mutable_items()->at(1)->SetLastAccessTime(
1137 downloads_manager()->mutable_items()->at(1)->GetStartTime()); 1141 downloads_manager()->mutable_items()->at(1)->GetStartTime());
1138 1142
1139 // Even though asset download 0 was downloaded long time ago, it should be 1143 // Even though asset download 0 was downloaded long time ago, it should be
1140 // reported because it has been visited recently. 1144 // reported because it has been visited recently.
1141 EXPECT_CALL( 1145 EXPECT_CALL(
1142 *observer(), 1146 *observer(),
1143 OnNewSuggestions(_, downloads_category(), 1147 OnNewSuggestions(_, downloads_category(),
1144 UnorderedElementsAre(HasUrl("http://download.com/0")))); 1148 UnorderedElementsAre(HasUrl("http://download.com/0"))));
1145 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); 1149 auto test_clock = base::MakeUnique<base::SimpleTestClock>();
1146 test_clock->SetNow(now); 1150 test_clock->SetNow(now);
1147 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, 1151 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
1148 std::move(test_clock)); 1152 std::move(test_clock));
1149 } 1153 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698