OLD | NEW |
| (Empty) |
1 // Copyright 2017 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/offline_pages/content/suggested_articles_observer.h" | |
6 | |
7 #include "base/run_loop.h" | |
8 #include "base/test/test_simple_task_runner.h" | |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #include "components/offline_pages/core/client_namespace_constants.h" | |
11 #include "components/offline_pages/core/stub_offline_page_model.h" | |
12 #include "content/public/test/test_browser_context.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "url/gurl.h" | |
15 | |
16 using ntp_snippets::Category; | |
17 using ntp_snippets::ContentSuggestion; | |
18 | |
19 namespace offline_pages { | |
20 | |
21 namespace { | |
22 | |
23 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { | |
24 auto category = | |
25 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | |
26 return ContentSuggestion(category, test_url.spec(), test_url); | |
27 } | |
28 | |
29 class TestingPrefetchService : public PrefetchService { | |
30 public: | |
31 TestingPrefetchService() = default; | |
32 | |
33 void AddCandidatePrefetchURLs( | |
34 const std::vector<PrefetchURL>& suggested_urls) override { | |
35 latest_prefetch_urls = suggested_urls; | |
36 new_suggestions_count++; | |
37 } | |
38 | |
39 void RemoveAllUnprocessedPrefetchURLs( | |
40 const std::string& name_space) override { | |
41 DCHECK_EQ(name_space, kSuggestedArticlesNamespace); | |
42 latest_prefetch_urls.clear(); | |
43 remove_all_suggestions_count++; | |
44 } | |
45 | |
46 void RemovePrefetchURLsByClientId(const ClientId& client_id) override { | |
47 DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); | |
48 remove_by_client_id_count++; | |
49 last_removed_client_id = base::MakeUnique<ClientId>(client_id); | |
50 } | |
51 | |
52 void BeginBackgroundTask( | |
53 std::unique_ptr<ScopedBackgroundTask> task) override {} | |
54 void StopBackgroundTask(ScopedBackgroundTask* task) override {} | |
55 | |
56 std::vector<PrefetchURL> latest_prefetch_urls; | |
57 std::unique_ptr<ClientId> last_removed_client_id; | |
58 | |
59 int new_suggestions_count = 0; | |
60 int remove_all_suggestions_count = 0; | |
61 int remove_by_client_id_count = 0; | |
62 }; | |
63 | |
64 class TestDelegate : public SuggestedArticlesObserver::Delegate { | |
65 public: | |
66 TestDelegate() = default; | |
67 ~TestDelegate() override = default; | |
68 | |
69 const std::vector<ContentSuggestion>& GetSuggestions( | |
70 const Category& category) override { | |
71 get_suggestions_count++; | |
72 return suggestions; | |
73 } | |
74 | |
75 PrefetchService* GetPrefetchService( | |
76 content::BrowserContext* context) override { | |
77 return &prefetch_service; | |
78 } | |
79 | |
80 TestingPrefetchService prefetch_service; | |
81 | |
82 // Public for test manipulation. | |
83 std::vector<ContentSuggestion> suggestions; | |
84 | |
85 // Signals that delegate was called. | |
86 int get_suggestions_count = 0; | |
87 }; | |
88 | |
89 } // namespace | |
90 | |
91 class OfflinePageSuggestedArticlesObserverTest : public testing::Test { | |
92 public: | |
93 OfflinePageSuggestedArticlesObserverTest() = default; | |
94 | |
95 void SetUp() override { | |
96 observer_ = | |
97 base::MakeUnique<SuggestedArticlesObserver>(&context_, MakeDelegate()); | |
98 } | |
99 | |
100 virtual std::unique_ptr<SuggestedArticlesObserver::Delegate> MakeDelegate() { | |
101 auto delegate_ptr = base::MakeUnique<TestDelegate>(); | |
102 test_delegate_ = delegate_ptr.get(); | |
103 return std::move(delegate_ptr); | |
104 } | |
105 | |
106 SuggestedArticlesObserver* observer() { return observer_.get(); } | |
107 | |
108 TestDelegate* test_delegate() { return test_delegate_; } | |
109 TestingPrefetchService* test_prefetch_service() { | |
110 return &(test_delegate()->prefetch_service); | |
111 } | |
112 | |
113 protected: | |
114 Category category = | |
115 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | |
116 content::TestBrowserContext context_; | |
117 | |
118 private: | |
119 std::unique_ptr<SuggestedArticlesObserver> observer_; | |
120 TestDelegate* test_delegate_; | |
121 }; | |
122 | |
123 TEST_F(OfflinePageSuggestedArticlesObserverTest, | |
124 CallsDelegateOnNewSuggestions) { | |
125 // We should not do anything if the category is not loaded. | |
126 observer()->OnNewSuggestions(category); | |
127 EXPECT_EQ(0, test_delegate()->get_suggestions_count); | |
128 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); | |
129 | |
130 // Once the category becomes available, new suggestions should cause us to ask | |
131 // the delegate for suggestion URLs. | |
132 observer()->OnCategoryStatusChanged(category, | |
133 ntp_snippets::CategoryStatus::AVAILABLE); | |
134 observer()->OnNewSuggestions(category); | |
135 EXPECT_EQ(1, test_delegate()->get_suggestions_count); | |
136 | |
137 // We expect that no pages were forwarded to the prefetch service since no | |
138 // pages were prepopulated. | |
139 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); | |
140 } | |
141 | |
142 TEST_F(OfflinePageSuggestedArticlesObserverTest, | |
143 ForwardsSuggestionsToPrefetchService) { | |
144 const GURL test_url_1("https://www.example.com/1"); | |
145 test_delegate()->suggestions.push_back( | |
146 ContentSuggestionFromTestURL(test_url_1)); | |
147 | |
148 observer()->OnCategoryStatusChanged(category, | |
149 ntp_snippets::CategoryStatus::AVAILABLE); | |
150 observer()->OnNewSuggestions(category); | |
151 EXPECT_EQ(1, test_prefetch_service()->new_suggestions_count); | |
152 EXPECT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); | |
153 EXPECT_EQ(test_url_1, test_prefetch_service()->latest_prefetch_urls[0].url); | |
154 EXPECT_EQ( | |
155 kSuggestedArticlesNamespace, | |
156 test_prefetch_service()->latest_prefetch_urls[0].client_id.name_space); | |
157 } | |
158 | |
159 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { | |
160 const GURL test_url_1("https://www.example.com/1"); | |
161 const GURL test_url_2("https://www.example.com/2"); | |
162 test_delegate()->suggestions.push_back( | |
163 ContentSuggestionFromTestURL(test_url_1)); | |
164 test_delegate()->suggestions.push_back( | |
165 ContentSuggestionFromTestURL(test_url_2)); | |
166 | |
167 observer()->OnCategoryStatusChanged(category, | |
168 ntp_snippets::CategoryStatus::AVAILABLE); | |
169 observer()->OnNewSuggestions(category); | |
170 ASSERT_EQ(2U, test_prefetch_service()->latest_prefetch_urls.size()); | |
171 | |
172 observer()->OnCategoryStatusChanged( | |
173 category, ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); | |
174 EXPECT_EQ(1, test_prefetch_service()->remove_all_suggestions_count); | |
175 observer()->OnCategoryStatusChanged( | |
176 category, | |
177 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED); | |
178 EXPECT_EQ(2, test_prefetch_service()->remove_all_suggestions_count); | |
179 } | |
180 | |
181 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesClientIdOnInvalidated) { | |
182 const GURL test_url_1("https://www.example.com/1"); | |
183 test_delegate()->suggestions.push_back( | |
184 ContentSuggestionFromTestURL(test_url_1)); | |
185 observer()->OnCategoryStatusChanged(category, | |
186 ntp_snippets::CategoryStatus::AVAILABLE); | |
187 observer()->OnNewSuggestions(category); | |
188 ASSERT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); | |
189 | |
190 observer()->OnSuggestionInvalidated( | |
191 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); | |
192 | |
193 EXPECT_EQ(1, test_prefetch_service()->remove_by_client_id_count); | |
194 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), | |
195 *test_prefetch_service()->last_removed_client_id); | |
196 } | |
197 | |
198 } // namespace offline_pages | |
OLD | NEW |