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_suggestions_provider.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "components/bookmarks/browser/bookmark_model.h" | |
16 #include "components/bookmarks/browser/bookmark_node.h" | |
17 #include "components/bookmarks/test/test_bookmark_client.h" | |
18 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | |
19 #include "components/ntp_snippets/category.h" | |
20 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" | |
21 #include "components/prefs/testing_pref_service.h" | |
22 #include "testing/gmock/include/gmock/gmock.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 #include "url/gurl.h" | |
25 | |
26 namespace ntp_snippets { | |
27 | |
28 namespace { | |
29 | |
30 using ::testing::StrictMock; | |
31 using ::testing::_; | |
32 using ::testing::Eq; | |
33 using ::testing::IsEmpty; | |
34 using ::testing::Property; | |
35 using ::testing::UnorderedElementsAre; | |
36 | |
37 class BookmarkSuggestionsProviderTest : public ::testing::Test { | |
38 public: | |
39 BookmarkSuggestionsProviderTest() | |
40 : model_(bookmarks::TestBookmarkClient::CreateModel()) { | |
41 EXPECT_CALL(observer_, OnNewSuggestions(_, Category::FromKnownCategory( | |
42 KnownCategories::BOOKMARKS), | |
43 IsEmpty())) | |
44 .RetiresOnSaturation(); | |
45 EXPECT_CALL(observer_, | |
46 OnCategoryStatusChanged( | |
47 _, Category::FromKnownCategory(KnownCategories::BOOKMARKS), | |
48 CategoryStatus::AVAILABLE_LOADING)) | |
49 .RetiresOnSaturation(); | |
50 EXPECT_CALL(observer_, | |
51 OnCategoryStatusChanged( | |
52 _, Category::FromKnownCategory(KnownCategories::BOOKMARKS), | |
53 CategoryStatus::AVAILABLE)) | |
54 .RetiresOnSaturation(); | |
55 BookmarkSuggestionsProvider::RegisterProfilePrefs(test_prefs_.registry()); | |
56 provider_ = base::MakeUnique<BookmarkSuggestionsProvider>( | |
57 &observer_, model_.get(), &test_prefs_); | |
58 } | |
59 | |
60 protected: | |
61 std::unique_ptr<bookmarks::BookmarkModel> model_; | |
62 StrictMock<MockContentSuggestionsProviderObserver> observer_; | |
63 TestingPrefServiceSimple test_prefs_; | |
64 std::unique_ptr<BookmarkSuggestionsProvider> provider_; | |
65 }; | |
66 | |
67 TEST_F(BookmarkSuggestionsProviderTest, | |
68 ShouldProvideBookmarkSuggestions) { | |
69 GURL url("http://my-new-bookmarked.url"); | |
70 // Note, this update to the model does not trigger OnNewSuggestions() on the | |
71 // observer as the provider realizes no new nodes were added. | |
72 // don't have new data. | |
73 model_->AddURL(model_->bookmark_bar_node(), 0, | |
74 base::ASCIIToUTF16("cool page's title"), url); | |
75 | |
76 // Once we provided the last-visited meta information, an update with the | |
77 // suggestion containing the bookmark should follow. | |
78 EXPECT_CALL( | |
79 observer_, | |
80 OnNewSuggestions( | |
81 _, Category::FromKnownCategory(KnownCategories::BOOKMARKS), | |
82 UnorderedElementsAre(Property(&ContentSuggestion::url, GURL(url))))); | |
83 UpdateBookmarkOnURLVisitedInMainFrame(model_.get(), url, | |
84 /*is_mobile_platform=*/true); | |
85 } | |
86 | |
87 TEST_F(BookmarkSuggestionsProviderTest, | |
88 ShouldEnsureToBeClearedBookmarksDontAppearAfterClear) { | |
89 // Set up the provider with 2 entries: one dismissed and one active. | |
90 | |
91 // Add one bookmark (the one to be not dismissed) -- this will trigger a | |
92 // notification. | |
93 GURL active_bookmark("http://my-active-bookmarked.url"); | |
94 EXPECT_CALL(observer_, | |
95 OnNewSuggestions( | |
96 _, Category::FromKnownCategory(KnownCategories::BOOKMARKS), | |
97 UnorderedElementsAre(Property(&ContentSuggestion::url, | |
98 GURL(active_bookmark))))) | |
99 .RetiresOnSaturation(); | |
100 model_->AddURL(model_->bookmark_bar_node(), 0, | |
101 base::ASCIIToUTF16("cool page's title"), active_bookmark); | |
102 UpdateBookmarkOnURLVisitedInMainFrame(model_.get(), active_bookmark, | |
103 /*is_mobile_platform=*/true); | |
104 | |
105 // Add the other bookmark and mark it as dismissed -- this will trigger | |
106 // another notification. | |
107 GURL dismissed_bookmark("http://my-dismissed-bookmark.url"); | |
108 EXPECT_CALL( | |
109 observer_, | |
110 OnNewSuggestions( | |
111 _, Category::FromKnownCategory(KnownCategories::BOOKMARKS), | |
112 UnorderedElementsAre( | |
113 Property(&ContentSuggestion::url, GURL(active_bookmark)), | |
114 Property(&ContentSuggestion::url, GURL(dismissed_bookmark))))); | |
115 const bookmarks::BookmarkNode* dismissed_node = model_->AddURL( | |
116 model_->bookmark_bar_node(), 1, base::ASCIIToUTF16("cool page's title"), | |
117 dismissed_bookmark); | |
118 UpdateBookmarkOnURLVisitedInMainFrame(model_.get(), dismissed_bookmark, | |
119 /*is_mobile_platform=*/true); | |
120 // TODO(tschumann): This seems lik a bug. When marking a bookmark as | |
jkrcal
2017/01/12 14:50:53
This WAI. Suggestions should not be updated after
tschumann
2017/01/12 15:06:23
good to know :-) I'll re-phrase this comment then
| |
121 // dismissed the suggestions should get updated -- they don't as we didn't | |
122 // see a newer last-visited-date. | |
123 static_cast<ContentSuggestionsProvider*>(provider_.get()) | |
124 ->DismissSuggestion(ContentSuggestion::ID( | |
125 Category::FromKnownCategory(KnownCategories::BOOKMARKS), | |
126 dismissed_bookmark.spec())); | |
127 EXPECT_THAT(IsDismissedFromNTPForBookmark(*dismissed_node), Eq(true)); | |
128 | |
129 // Clear history and make sure the suggestions actually get removed. | |
130 EXPECT_CALL(observer_, OnNewSuggestions(_, Category::FromKnownCategory( | |
131 KnownCategories::BOOKMARKS), | |
132 IsEmpty())); | |
133 static_cast<ContentSuggestionsProvider*>(provider_.get()) | |
134 ->ClearHistory(base::Time(), base::Time::Max(), | |
135 base::Bind([] (const GURL& url) { return true; })); | |
136 | |
137 // Verify the dismissed marker is gone. | |
138 EXPECT_THAT(IsDismissedFromNTPForBookmark(*dismissed_node), Eq(false)); | |
139 } | |
140 | |
141 // TODO(tschumann): There are plenty of test cases missing. Most importantly: | |
142 // -- Remove a bookmark from the model | |
143 // -- verifying handling of threshold time | |
144 // -- dealing with fetches before the model is loaded. | |
145 | |
146 } // namespace | |
147 } // namespace ntp_snippets | |
148 | |
OLD | NEW |