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 "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | 5 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "components/bookmarks/browser/bookmark_model.h" | 13 #include "components/bookmarks/browser/bookmark_model.h" |
13 #include "components/bookmarks/browser/bookmark_node.h" | 14 #include "components/bookmarks/browser/bookmark_node.h" |
14 #include "components/bookmarks/test/test_bookmark_client.h" | 15 #include "components/bookmarks/test/test_bookmark_client.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
(...skipping 22 matching lines...) Expand all Loading... | |
41 model->AddURL(model->bookmark_bar_node(), 0, title, url); | 42 model->AddURL(model->bookmark_bar_node(), 0, title, url); |
42 | 43 |
43 if (!meta_key.empty()) { | 44 if (!meta_key.empty()) { |
44 model->SetNodeMetaInfo(node, meta_key, meta_value); | 45 model->SetNodeMetaInfo(node, meta_key, meta_value); |
45 } | 46 } |
46 } | 47 } |
47 } | 48 } |
48 | 49 |
49 void AddBookmarksRecentOnMobile(BookmarkModel* model, | 50 void AddBookmarksRecentOnMobile(BookmarkModel* model, |
50 int num, | 51 int num, |
51 const base::Time& threshold_time) { | 52 const base::Time& visit_time) { |
52 base::TimeDelta week = base::TimeDelta::FromDays(7); | |
53 base::Time recent_time = threshold_time + week; | |
54 std::string recent_time_string = | |
55 base::Int64ToString(recent_time.ToInternalValue()); | |
56 | |
57 AddBookmarks(model, num, kBookmarkLastVisitDateOnMobileKey, | 53 AddBookmarks(model, num, kBookmarkLastVisitDateOnMobileKey, |
58 recent_time_string); | 54 base::Int64ToString(visit_time.ToInternalValue())); |
59 } | 55 } |
60 | 56 |
61 void AddBookmarksRecentOnDesktop(BookmarkModel* model, | 57 void AddBookmarksRecentOnDesktop(BookmarkModel* model, |
62 int num, | 58 int num, |
63 const base::Time& threshold_time) { | 59 const base::Time& visit_time) { |
64 base::TimeDelta week = base::TimeDelta::FromDays(7); | |
65 base::Time recent_time = threshold_time + week; | |
66 std::string recent_time_string = | |
67 base::Int64ToString(recent_time.ToInternalValue()); | |
68 | |
69 AddBookmarks(model, num, kBookmarkLastVisitDateOnDesktopKey, | 60 AddBookmarks(model, num, kBookmarkLastVisitDateOnDesktopKey, |
70 recent_time_string); | 61 base::Int64ToString(visit_time.ToInternalValue())); |
71 } | 62 } |
72 | 63 |
73 void AddBookmarksNonVisited(BookmarkModel* model, int num) { | 64 void AddBookmarksNonVisited(BookmarkModel* model, int num) { |
74 AddBookmarks(model, num, std::string(), std::string()); | 65 AddBookmarks(model, num, std::string(), std::string()); |
75 } | 66 } |
76 | 67 |
68 const BookmarkNode* AddSingleBookmark(BookmarkModel* model, | |
69 const std::string& url, | |
70 const std::string& last_visit_key, | |
71 const base::Time& visit_time) { | |
72 base::string16 title = | |
73 base::ASCIIToUTF16(base::StringPrintf("title-%s", url.c_str())); | |
74 const BookmarkNode* node = | |
75 model->AddURL(model->bookmark_bar_node(), 0, title, GURL(url)); | |
76 model->SetNodeMetaInfo(node, last_visit_key, | |
77 base::Int64ToString(visit_time.ToInternalValue())); | |
78 return node; | |
79 } | |
80 | |
77 } // namespace | 81 } // namespace |
78 | 82 |
79 class GetRecentlyVisitedBookmarksTest : public testing::Test { | 83 class GetRecentlyVisitedBookmarksTest : public testing::Test { |
80 public: | 84 public: |
81 GetRecentlyVisitedBookmarksTest() { | 85 GetRecentlyVisitedBookmarksTest() { |
82 base::TimeDelta week = base::TimeDelta::FromDays(7); | 86 base::TimeDelta week = base::TimeDelta::FromDays(7); |
83 threshold_time_ = base::Time::UnixEpoch() + 52 * week; | 87 threshold_time_ = base::Time::UnixEpoch() + 52 * week; |
84 } | 88 } |
85 | 89 |
86 const base::Time& threshold_time() const { return threshold_time_; } | 90 const base::Time& threshold_time() const { return threshold_time_; } |
87 | 91 |
92 base::Time GetRecentTime() const { | |
93 return threshold_time_ + base::TimeDelta::FromDays(7); | |
94 } | |
95 | |
88 private: | 96 private: |
89 base::Time threshold_time_; | 97 base::Time threshold_time_; |
90 | 98 |
91 DISALLOW_COPY_AND_ASSIGN(GetRecentlyVisitedBookmarksTest); | 99 DISALLOW_COPY_AND_ASSIGN(GetRecentlyVisitedBookmarksTest); |
92 }; | 100 }; |
93 | 101 |
94 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldNotReturnMissing) { | 102 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldNotReturnMissing) { |
95 const int number_of_bookmarks = 3; | 103 const int number_of_bookmarks = 3; |
96 std::unique_ptr<BookmarkModel> model = | 104 std::unique_ptr<BookmarkModel> model = |
97 bookmarks::TestBookmarkClient::CreateModel(); | 105 bookmarks::TestBookmarkClient::CreateModel(); |
98 AddBookmarksNonVisited(model.get(), number_of_bookmarks); | 106 AddBookmarksNonVisited(model.get(), number_of_bookmarks); |
99 | 107 |
100 std::vector<const bookmarks::BookmarkNode*> result = | 108 std::vector<const bookmarks::BookmarkNode*> result = |
101 GetRecentlyVisitedBookmarks(model.get(), number_of_bookmarks, | 109 GetRecentlyVisitedBookmarks(model.get(), number_of_bookmarks, |
102 threshold_time(), | 110 threshold_time(), |
103 /*consider_visits_from_desktop=*/false); | 111 /*consider_visits_from_desktop=*/false); |
104 EXPECT_THAT(result, IsEmpty()); | 112 EXPECT_THAT(result, IsEmpty()); |
105 } | 113 } |
106 | 114 |
107 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldNotConsiderDesktopVisits) { | 115 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldNotConsiderDesktopVisits) { |
108 const int number_of_bookmarks = 3; | 116 const int number_of_bookmarks = 3; |
109 std::unique_ptr<BookmarkModel> model = | 117 std::unique_ptr<BookmarkModel> model = |
110 bookmarks::TestBookmarkClient::CreateModel(); | 118 bookmarks::TestBookmarkClient::CreateModel(); |
111 AddBookmarksRecentOnDesktop(model.get(), number_of_bookmarks, | 119 AddBookmarksRecentOnDesktop(model.get(), number_of_bookmarks, |
112 threshold_time()); | 120 GetRecentTime()); |
113 | 121 |
114 std::vector<const bookmarks::BookmarkNode*> result = | 122 std::vector<const bookmarks::BookmarkNode*> result = |
115 GetRecentlyVisitedBookmarks(model.get(), number_of_bookmarks, | 123 GetRecentlyVisitedBookmarks(model.get(), number_of_bookmarks, |
116 threshold_time(), | 124 threshold_time(), |
117 /*consider_visits_from_desktop=*/false); | 125 /*consider_visits_from_desktop=*/false); |
118 EXPECT_THAT(result, IsEmpty()); | 126 EXPECT_THAT(result, IsEmpty()); |
119 } | 127 } |
120 | 128 |
121 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldConsiderDesktopVisits) { | 129 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldConsiderDesktopVisits) { |
122 const int number_of_bookmarks = 3; | 130 const int number_of_bookmarks = 3; |
123 const int number_of_recent_desktop = 2; | 131 const int number_of_recent_desktop = 2; |
124 std::unique_ptr<BookmarkModel> model = | 132 std::unique_ptr<BookmarkModel> model = |
125 bookmarks::TestBookmarkClient::CreateModel(); | 133 bookmarks::TestBookmarkClient::CreateModel(); |
126 AddBookmarksRecentOnDesktop(model.get(), number_of_recent_desktop, | 134 AddBookmarksRecentOnDesktop(model.get(), number_of_recent_desktop, |
127 threshold_time()); | 135 GetRecentTime()); |
128 AddBookmarksNonVisited(model.get(), | 136 AddBookmarksNonVisited(model.get(), |
129 number_of_bookmarks - number_of_recent_desktop); | 137 number_of_bookmarks - number_of_recent_desktop); |
130 | 138 |
131 std::vector<const bookmarks::BookmarkNode*> result = | 139 std::vector<const bookmarks::BookmarkNode*> result = |
132 GetRecentlyVisitedBookmarks(model.get(), number_of_bookmarks, | 140 GetRecentlyVisitedBookmarks(model.get(), number_of_bookmarks, |
133 threshold_time(), | 141 threshold_time(), |
134 /*consider_visits_from_desktop=*/true); | 142 /*consider_visits_from_desktop=*/true); |
135 EXPECT_THAT(result, SizeIs(number_of_recent_desktop)); | 143 EXPECT_THAT(result, SizeIs(number_of_recent_desktop)); |
136 } | 144 } |
137 | 145 |
138 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnNotMoreThanMaxCount) { | 146 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnNotMoreThanMaxCount) { |
139 const int number_of_bookmarks = 3; | 147 const int number_of_bookmarks = 3; |
140 std::unique_ptr<BookmarkModel> model = | 148 std::unique_ptr<BookmarkModel> model = |
141 bookmarks::TestBookmarkClient::CreateModel(); | 149 bookmarks::TestBookmarkClient::CreateModel(); |
142 AddBookmarksRecentOnMobile(model.get(), number_of_bookmarks, | 150 AddBookmarksRecentOnMobile(model.get(), number_of_bookmarks, GetRecentTime()); |
143 threshold_time()); | |
144 | 151 |
145 const int max_count = number_of_bookmarks - 1; | 152 const int max_count = number_of_bookmarks - 1; |
146 std::vector<const bookmarks::BookmarkNode*> result = | 153 std::vector<const bookmarks::BookmarkNode*> result = |
147 GetRecentlyVisitedBookmarks(model.get(), max_count, threshold_time(), | 154 GetRecentlyVisitedBookmarks(model.get(), max_count, threshold_time(), |
148 /*consider_visits_from_desktop=*/false); | 155 /*consider_visits_from_desktop=*/false); |
149 EXPECT_THAT(result, SizeIs(max_count)); | 156 EXPECT_THAT(result, SizeIs(max_count)); |
150 } | 157 } |
151 | 158 |
159 TEST(RemoveLastVisitedDatesBetween, ShouldRemoveTimestampsWithinTimeRange) { | |
160 const base::Time delete_begin = | |
161 base::Time::Now() - base::TimeDelta::FromDays(2); | |
162 const base::Time delete_end = base::Time::Max(); | |
163 | |
164 std::unique_ptr<BookmarkModel> model = | |
165 bookmarks::TestBookmarkClient::CreateModel(); | |
166 AddSingleBookmark(model.get(), "http://url-1.com", | |
167 kBookmarkLastVisitDateOnMobileKey, | |
168 delete_begin + base::TimeDelta::FromSeconds(1)); | |
169 AddSingleBookmark(model.get(), "http://url-1.com", | |
170 kBookmarkLastVisitDateOnDesktopKey, | |
171 delete_begin + base::TimeDelta::FromSeconds(1)); | |
172 ASSERT_THAT( | |
173 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
174 /*consider_visits_from_desktop=*/true), | |
175 SizeIs(1)); | |
176 | |
177 base::Callback<bool(const GURL& url)> filter; | |
178 RemoveLastVisitedDatesBetween(delete_begin, delete_end, filter, model.get()); | |
179 | |
180 EXPECT_THAT( | |
181 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
182 /*consider_visits_from_desktop=*/true), | |
183 IsEmpty()); | |
184 // Verify that the bookmark model nodes themselve still exist. | |
185 std::vector<const BookmarkNode*> remaining_nodes; | |
186 model->GetNodesByURL(GURL("http://url-1.com"), &remaining_nodes); | |
187 EXPECT_THAT(remaining_nodes, SizeIs(2)); | |
188 } | |
189 | |
190 TEST(RemoveLastVisitedDatesBetween, | |
191 ShouldAlsoRemoveAnyTimestampFromOtherDevices) { | |
192 const base::Time delete_begin = | |
193 base::Time::Now() - base::TimeDelta::FromDays(2); | |
194 const base::Time delete_end = base::Time::Max(); | |
195 | |
196 std::unique_ptr<BookmarkModel> model = | |
197 bookmarks::TestBookmarkClient::CreateModel(); | |
198 // Create a bookmark with last visited times from both, mobile and desktop. | |
199 // The mobile one is within the deletion interval, the desktop one outside. | |
200 // Both should get removed. | |
jkrcal
2017/01/04 12:14:22
This is more a question to the implementation: are
tschumann
2017/01/04 13:32:32
This was my understanding of Dominic's comment: ht
jkrcal
2017/01/04 14:09:25
Interesting :)
My interpretation of Dominic's co
msramek
2017/01/04 14:19:07
I agree with Jan. Definitely take timestamps into
| |
201 const BookmarkNode* node = AddSingleBookmark( | |
202 model.get(), "http://url-1.com", kBookmarkLastVisitDateOnMobileKey, | |
203 delete_begin + base::TimeDelta::FromSeconds(1)); | |
204 model->SetNodeMetaInfo( | |
205 node, kBookmarkLastVisitDateOnDesktopKey, | |
206 base::Int64ToString( | |
207 (delete_begin - base::TimeDelta::FromSeconds(1)).ToInternalValue())); | |
208 ASSERT_THAT( | |
209 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
210 /*consider_visits_from_desktop=*/true), | |
211 SizeIs(1)); | |
212 | |
213 base::Callback<bool(const GURL& url)> filter; | |
214 RemoveLastVisitedDatesBetween(delete_begin, delete_end, filter, model.get()); | |
215 | |
216 EXPECT_THAT( | |
217 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
218 /*consider_visits_from_desktop=*/true), | |
219 IsEmpty()); | |
220 } | |
221 | |
222 TEST(RemoveLastVisitedDatesBetween, | |
223 ShouldKeepSeparateBookmarksForDifferentDeviceTypes) { | |
224 // This test verifies that if we see two bookmark nodes for the same URL, | |
225 // where one is only visited from mobile devices and the other one only from | |
226 // desktop devices, clearing one should not affect the other one. | |
227 const base::Time delete_begin = | |
228 base::Time::Now() - base::TimeDelta::FromDays(2); | |
229 const base::Time delete_end = base::Time::Max(); | |
230 | |
231 std::unique_ptr<BookmarkModel> model = | |
232 bookmarks::TestBookmarkClient::CreateModel(); | |
233 AddSingleBookmark( | |
234 model.get(), "http://url-1.com", kBookmarkLastVisitDateOnMobileKey, | |
235 delete_begin + base::TimeDelta::FromSeconds(1)); | |
236 AddSingleBookmark(model.get(), "http://url-1.com", | |
237 kBookmarkLastVisitDateOnDesktopKey, | |
238 delete_begin - base::TimeDelta::FromSeconds(1)); | |
239 ASSERT_THAT( | |
240 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
241 /*consider_visits_from_desktop=*/true), | |
242 SizeIs(1)); | |
243 | |
244 base::Callback<bool(const GURL& url)> filter; | |
245 RemoveLastVisitedDatesBetween(delete_begin, delete_end, filter, model.get()); | |
246 | |
247 EXPECT_THAT( | |
248 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
249 /*consider_visits_from_desktop=*/false), | |
250 IsEmpty()); | |
251 EXPECT_THAT( | |
252 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
253 /*consider_visits_from_desktop=*/true), | |
254 SizeIs(1)); | |
255 } | |
256 | |
257 TEST(RemoveLastVisitedDatesBetween, ShouldNotRemoveTimestampsOutsideTimeRange) { | |
258 const base::Time delete_begin = | |
259 base::Time::Now() - base::TimeDelta::FromDays(2); | |
260 const base::Time delete_end = delete_begin + base::TimeDelta::FromDays(5); | |
261 | |
262 std::unique_ptr<BookmarkModel> model = | |
263 bookmarks::TestBookmarkClient::CreateModel(); | |
264 AddSingleBookmark(model.get(), "http://url-1.com", | |
265 kBookmarkLastVisitDateOnMobileKey, | |
266 delete_begin - base::TimeDelta::FromSeconds(1)); | |
267 AddSingleBookmark(model.get(), "http://url-2.com", | |
268 kBookmarkLastVisitDateOnDesktopKey, | |
269 delete_end + base::TimeDelta::FromSeconds(1)); | |
270 ASSERT_THAT( | |
271 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
272 /*consider_visits_from_desktop=*/true), | |
273 SizeIs(2)); | |
274 | |
275 base::Callback<bool(const GURL& url)> filter; | |
276 RemoveLastVisitedDatesBetween(delete_begin, delete_end, filter, model.get()); | |
277 | |
278 EXPECT_THAT( | |
279 GetRecentlyVisitedBookmarks(model.get(), 20, base::Time(), | |
280 /*consider_visits_from_desktop=*/true), | |
281 SizeIs(2)); | |
282 } | |
283 | |
152 } // namespace ntp_snippets | 284 } // namespace ntp_snippets |
OLD | NEW |