| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "components/bookmarks/browser/bookmark_model.h" |
| 13 #include "components/bookmarks/browser/bookmark_node.h" |
| 14 #include "components/bookmarks/test/test_bookmark_client.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 using bookmarks::BookmarkModel; |
| 20 using bookmarks::BookmarkNode; |
| 21 |
| 22 using testing::IsEmpty; |
| 23 using testing::SizeIs; |
| 24 using testing::UnorderedElementsAre; |
| 25 |
| 26 namespace ntp_snippets { |
| 27 |
| 28 namespace { |
| 29 |
| 30 const char kBookmarkLastVisitDateKey[] = "last_visited"; |
| 31 |
| 32 std::unique_ptr<BookmarkModel> CreateModelWithRecentBookmarks( |
| 33 int number_of_bookmarks, |
| 34 int number_of_recent, |
| 35 const base::Time& threshold_time) { |
| 36 std::unique_ptr<BookmarkModel> model = |
| 37 bookmarks::TestBookmarkClient::CreateModel(); |
| 38 |
| 39 base::TimeDelta week = base::TimeDelta::FromDays(7); |
| 40 base::Time recent_time = threshold_time + week; |
| 41 std::string recent_time_string = |
| 42 base::Int64ToString(recent_time.ToInternalValue()); |
| 43 base::Time nonrecent_time = threshold_time - week; |
| 44 std::string nonrecent_time_string = |
| 45 base::Int64ToString(nonrecent_time.ToInternalValue()); |
| 46 |
| 47 for (int index = 0; index < number_of_bookmarks; ++index) { |
| 48 base::string16 title = |
| 49 base::ASCIIToUTF16(base::StringPrintf("title%d", index)); |
| 50 GURL url(base::StringPrintf("http://url%d.com", index)); |
| 51 const BookmarkNode* node = |
| 52 model->AddURL(model->bookmark_bar_node(), index, title, url); |
| 53 |
| 54 model->SetNodeMetaInfo( |
| 55 node, kBookmarkLastVisitDateKey, |
| 56 index < number_of_recent ? recent_time_string : nonrecent_time_string); |
| 57 } |
| 58 |
| 59 return model; |
| 60 } |
| 61 |
| 62 MATCHER_P(HasURL, url, "") { |
| 63 *result_listener << "expected URL: " << url |
| 64 << "has URL: " << arg->url().spec(); |
| 65 return arg->url().spec() == url; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 class GetRecentlyVisitedBookmarksTest : public testing::Test { |
| 71 public: |
| 72 GetRecentlyVisitedBookmarksTest() { |
| 73 base::TimeDelta week = base::TimeDelta::FromDays(7); |
| 74 threshold_time = base::Time::UnixEpoch() + 52 * week; |
| 75 } |
| 76 |
| 77 base::Time threshold_time; |
| 78 |
| 79 private: |
| 80 DISALLOW_COPY_AND_ASSIGN(GetRecentlyVisitedBookmarksTest); |
| 81 }; |
| 82 |
| 83 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnNothingIfNotEnoughRecent) { |
| 84 const int number_of_recent = 2; |
| 85 const int number_of_bookmarks = number_of_recent; |
| 86 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( |
| 87 number_of_bookmarks, number_of_recent, threshold_time); |
| 88 |
| 89 std::vector<const bookmarks::BookmarkNode*> result = |
| 90 GetRecentlyVisitedBookmarks(model.get(), number_of_recent + 1, |
| 91 number_of_recent + 2, threshold_time, |
| 92 /* creation_date_fallback */ false); |
| 93 EXPECT_THAT(result, IsEmpty()); |
| 94 } |
| 95 |
| 96 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldNotReturnNonRecent) { |
| 97 const int number_of_recent = 0; |
| 98 const int number_of_bookmarks = 3; |
| 99 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( |
| 100 number_of_bookmarks, number_of_recent, threshold_time); |
| 101 |
| 102 std::vector<const bookmarks::BookmarkNode*> result = |
| 103 GetRecentlyVisitedBookmarks(model.get(), 0, number_of_bookmarks, |
| 104 threshold_time, |
| 105 /* creation_date_fallback */ false); |
| 106 EXPECT_THAT(result, IsEmpty()); |
| 107 } |
| 108 |
| 109 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnIfEnoughRecent) { |
| 110 const int number_of_recent = 2; |
| 111 const int number_of_bookmarks = number_of_recent; |
| 112 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( |
| 113 number_of_bookmarks, number_of_recent, threshold_time); |
| 114 |
| 115 std::vector<const bookmarks::BookmarkNode*> result = |
| 116 GetRecentlyVisitedBookmarks(model.get(), 0, number_of_recent, |
| 117 threshold_time, |
| 118 /* creation_date_fallback */ false); |
| 119 EXPECT_THAT(result, UnorderedElementsAre(HasURL("http://url0.com/"), |
| 120 HasURL("http://url1.com/"))); |
| 121 } |
| 122 |
| 123 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnNotMoreThanMaxCount) { |
| 124 const int number_of_recent = 3; |
| 125 const int number_of_bookmarks = number_of_recent; |
| 126 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( |
| 127 number_of_bookmarks, number_of_recent, threshold_time); |
| 128 |
| 129 std::vector<const bookmarks::BookmarkNode*> result = |
| 130 GetRecentlyVisitedBookmarks(model.get(), number_of_recent - 1, |
| 131 number_of_recent - 1, threshold_time, |
| 132 /* creation_date_fallback */ false); |
| 133 EXPECT_THAT(result, SizeIs(number_of_recent - 1)); |
| 134 } |
| 135 |
| 136 } // namespace ntp_snippets |
| OLD | NEW |