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