Chromium Code Reviews| 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 base::Time threshold_time; | |
|
Marc Treib
2016/09/01 11:16:32
threshold_time_ (trailing underscore)
Also this s
vitaliii
2016/09/01 11:38:43
Done.
| |
| 72 | |
| 73 private: | |
| 74 DISALLOW_COPY_AND_ASSIGN(GetRecentlyVisitedBookmarksTest); | |
| 75 }; | |
| 76 | |
| 77 TEST_F(GetRecentlyVisitedBookmarksTest, | |
| 78 WithoutDateFallbackShouldNotReturnNonRecent) { | |
| 79 const int number_of_recent = 0; | |
| 80 const int number_of_bookmarks = 3; | |
| 81 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( | |
| 82 number_of_bookmarks, number_of_recent, threshold_time); | |
| 83 | |
| 84 std::vector<const bookmarks::BookmarkNode*> result = | |
| 85 GetRecentlyVisitedBookmarks(model.get(), 0, number_of_bookmarks, | |
| 86 threshold_time, | |
| 87 /*creation_date_fallback=*/false); | |
| 88 EXPECT_THAT(result, IsEmpty()); | |
| 89 } | |
| 90 | |
| 91 TEST_F(GetRecentlyVisitedBookmarksTest, ShouldReturnNotMoreThanMaxCount) { | |
| 92 const int number_of_recent = 3; | |
| 93 const int number_of_bookmarks = number_of_recent; | |
| 94 std::unique_ptr<BookmarkModel> model = CreateModelWithRecentBookmarks( | |
| 95 number_of_bookmarks, number_of_recent, threshold_time); | |
| 96 | |
| 97 std::vector<const bookmarks::BookmarkNode*> result = | |
| 98 GetRecentlyVisitedBookmarks(model.get(), number_of_recent - 1, | |
|
Marc Treib
2016/09/01 11:16:32
nit: make a max_count variable, so it's clear what
vitaliii
2016/09/01 11:38:43
Done.
| |
| 99 number_of_recent - 1, threshold_time, | |
| 100 /*creation_date_fallback=*/false); | |
| 101 EXPECT_THAT(result, SizeIs(number_of_recent - 1)); | |
| 102 } | |
| 103 | |
| 104 } // namespace ntp_snippets | |
| OLD | NEW |