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 } // namespace |
| 63 |
| 64 class GetRecentlyVisitedBookmarksTest : public testing::Test { |
| 65 public: |
| 66 GetRecentlyVisitedBookmarksTest() { |
| 67 base::TimeDelta week = base::TimeDelta::FromDays(7); |
| 68 threshold_time_ = base::Time::UnixEpoch() + 52 * week; |
| 69 } |
| 70 |
| 71 const base::Time& threshold_time() const { return threshold_time_; } |
| 72 |
| 73 private: |
| 74 base::Time threshold_time_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(GetRecentlyVisitedBookmarksTest); |
| 77 }; |
| 78 |
| 79 TEST_F(GetRecentlyVisitedBookmarksTest, |
| 80 WithoutDateFallbackShouldNotReturnNonRecent) { |
| 81 const int number_of_recent = 0; |
| 82 const int number_of_bookmarks = 3; |
| 83 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( |
| 84 number_of_bookmarks, number_of_recent, threshold_time()); |
| 85 |
| 86 std::vector<const bookmarks::BookmarkNode*> result = |
| 87 GetRecentlyVisitedBookmarks(model.get(), 0, number_of_bookmarks, |
| 88 threshold_time(), |
| 89 /*creation_date_fallback=*/false); |
| 90 EXPECT_THAT(result, IsEmpty()); |
| 91 } |
| 92 |
| 93 TEST_F(GetRecentlyVisitedBookmarksTest, |
| 94 WithDateFallbackShouldReturnNonRecentUpToMinCount) { |
| 95 const int number_of_recent = 0; |
| 96 const int number_of_bookmarks = 3; |
| 97 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( |
| 98 number_of_bookmarks, number_of_recent, threshold_time()); |
| 99 |
| 100 const int min_count = number_of_bookmarks - 1; |
| 101 const int max_count = min_count + 10; |
| 102 std::vector<const bookmarks::BookmarkNode*> result = |
| 103 GetRecentlyVisitedBookmarks(model.get(), min_count, max_count, |
| 104 threshold_time(), |
| 105 /*creation_date_fallback=*/true); |
| 106 EXPECT_THAT(result, SizeIs(min_count)); |
| 107 } |
| 108 |
| 109 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnNotMoreThanMaxCount) { |
| 110 const int number_of_recent = 3; |
| 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 const int max_count = number_of_recent - 1; |
| 116 std::vector<const bookmarks::BookmarkNode*> result = |
| 117 GetRecentlyVisitedBookmarks(model.get(), max_count, max_count, |
| 118 threshold_time(), |
| 119 /*creation_date_fallback=*/false); |
| 120 EXPECT_THAT(result, SizeIs(max_count)); |
| 121 } |
| 122 |
| 123 } // namespace ntp_snippets |
OLD | NEW |