OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide r.h" | 5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide r.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 OfflinePageItem CreateDummyRecentTab(int id, base::Time time) { | 68 OfflinePageItem CreateDummyRecentTab(int id, base::Time time) { |
69 OfflinePageItem item = CreateDummyRecentTab(id); | 69 OfflinePageItem item = CreateDummyRecentTab(id); |
70 item.last_access_time = time; | 70 item.last_access_time = time; |
71 return item; | 71 return item; |
72 } | 72 } |
73 | 73 |
74 OfflinePageItem CreateDummyDownload(int id) { | 74 OfflinePageItem CreateDummyDownload(int id) { |
75 return CreateDummyItem(offline_pages::kAsyncNamespace, id); | 75 return CreateDummyItem(offline_pages::kAsyncNamespace, id); |
76 } | 76 } |
77 | 77 |
78 void CaptureDismissedSuggestions( | |
79 std::vector<ContentSuggestion>* captured_suggestions, | |
80 std::vector<ContentSuggestion> dismissed_suggestions) { | |
81 std::move(dismissed_suggestions.begin(), dismissed_suggestions.end(), | |
82 std::back_inserter(*captured_suggestions)); | |
83 } | |
84 | |
78 } // namespace | 85 } // namespace |
79 | 86 |
80 class MockOfflinePageModel : public StubOfflinePageModel { | 87 class MockOfflinePageModel : public StubOfflinePageModel { |
tschumann
2016/08/29 15:47:00
nit: while we're at it. This is more a fake than a
Philipp Keck
2016/08/29 17:04:15
Done.
| |
81 public: | 88 public: |
82 MockOfflinePageModel() {} | 89 MockOfflinePageModel() {} |
83 | 90 |
84 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { | 91 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { |
85 callback.Run(items_); | 92 callback.Run(items_); |
86 } | 93 } |
87 | 94 |
88 std::vector<OfflinePageItem>* items() { return &items_; } | 95 std::vector<OfflinePageItem>* items() { return &items_; } |
tschumann
2016/08/29 15:47:00
a proto-inspired convention is to call these funct
Philipp Keck
2016/08/29 17:04:15
Done.
| |
89 | 96 |
90 private: | 97 private: |
91 std::vector<OfflinePageItem> items_; | 98 std::vector<OfflinePageItem> items_; |
92 }; | 99 }; |
93 | 100 |
94 class OfflinePageSuggestionsProviderTest : public testing::Test { | 101 class OfflinePageSuggestionsProviderTest : public testing::Test { |
95 public: | 102 public: |
96 OfflinePageSuggestionsProviderTest() | 103 OfflinePageSuggestionsProviderTest() |
97 : pref_service_(new TestingPrefServiceSimple()) { | 104 : pref_service_(new TestingPrefServiceSimple()) { |
98 OfflinePageSuggestionsProvider::RegisterProfilePrefs( | 105 OfflinePageSuggestionsProvider::RegisterProfilePrefs( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 } | 153 } |
147 | 154 |
148 void FireOfflinePageDeleted(const OfflinePageItem& item) { | 155 void FireOfflinePageDeleted(const OfflinePageItem& item) { |
149 provider_->OfflinePageDeleted(item.offline_id, item.client_id); | 156 provider_->OfflinePageDeleted(item.offline_id, item.client_id); |
150 } | 157 } |
151 | 158 |
152 std::set<std::string> ReadDismissedIDsFromPrefs(Category category) { | 159 std::set<std::string> ReadDismissedIDsFromPrefs(Category category) { |
153 return provider_->ReadDismissedIDsFromPrefs(category); | 160 return provider_->ReadDismissedIDsFromPrefs(category); |
154 } | 161 } |
155 | 162 |
156 // Workaround to realize a DismissedSuggestionsCallback. Because gMock can't | |
157 // handle non-movable parameters, a helper method is needed to forward the | |
158 // call to the actual MOCK_METHOD. | |
159 void DismissedSuggestionsHelper( | |
160 std::vector<ContentSuggestion> dismissed_suggestions) { | |
161 ReceivedDismissedSuggestions(dismissed_suggestions); | |
162 } | |
163 MOCK_METHOD1( | |
164 ReceivedDismissedSuggestions, | |
165 void(const std::vector<ContentSuggestion>& dismissed_suggestions)); | |
166 | |
167 ContentSuggestionsProvider* provider() { return provider_.get(); } | 163 ContentSuggestionsProvider* provider() { return provider_.get(); } |
168 MockOfflinePageModel* model() { return &model_; } | 164 MockOfflinePageModel* model() { return &model_; } |
169 MockContentSuggestionsProviderObserver* observer() { return &observer_; } | 165 MockContentSuggestionsProviderObserver* observer() { return &observer_; } |
170 TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } | 166 TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } |
171 | 167 |
172 private: | 168 private: |
173 MockOfflinePageModel model_; | 169 MockOfflinePageModel model_; |
174 MockContentSuggestionsProviderObserver observer_; | 170 MockContentSuggestionsProviderObserver observer_; |
175 CategoryFactory category_factory_; | 171 CategoryFactory category_factory_; |
176 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 172 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 Property(&ContentSuggestion::url, | 297 Property(&ContentSuggestion::url, |
302 GURL("file:///some/folder/test1.mhtml")), | 298 GURL("file:///some/folder/test1.mhtml")), |
303 Property(&ContentSuggestion::url, | 299 Property(&ContentSuggestion::url, |
304 GURL("file:///some/folder/test4.mhtml"))))); | 300 GURL("file:///some/folder/test4.mhtml"))))); |
305 EXPECT_CALL(*observer(), | 301 EXPECT_CALL(*observer(), |
306 OnNewSuggestions(_, downloads_category(), IsEmpty())); | 302 OnNewSuggestions(_, downloads_category(), IsEmpty())); |
307 FireOfflinePageModelChanged(); | 303 FireOfflinePageModelChanged(); |
308 Mock::VerifyAndClearExpectations(observer()); | 304 Mock::VerifyAndClearExpectations(observer()); |
309 | 305 |
310 // And appear in the dismissed suggestions for the right category. | 306 // And appear in the dismissed suggestions for the right category. |
311 EXPECT_CALL(*this, ReceivedDismissedSuggestions(UnorderedElementsAre( | 307 std::vector<ContentSuggestion> dismissed_suggestions; |
312 Property(&ContentSuggestion::url, | |
313 GURL("file:///some/folder/test2.mhtml")), | |
314 Property(&ContentSuggestion::url, | |
315 GURL("file:///some/folder/test3.mhtml"))))); | |
316 provider()->GetDismissedSuggestionsForDebugging( | 308 provider()->GetDismissedSuggestionsForDebugging( |
317 recent_tabs_category(), | 309 recent_tabs_category(), |
318 base::Bind( | 310 base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions)); |
319 &OfflinePageSuggestionsProviderTest::DismissedSuggestionsHelper, | 311 EXPECT_THAT( |
320 base::Unretained(this))); | 312 dismissed_suggestions, |
321 Mock::VerifyAndClearExpectations(this); | 313 UnorderedElementsAre(Property(&ContentSuggestion::url, |
314 GURL("file:///some/folder/test2.mhtml")), | |
315 Property(&ContentSuggestion::url, | |
316 GURL("file:///some/folder/test3.mhtml")))); | |
322 | 317 |
323 // The other category should have no dismissed suggestions. | 318 // The other category should have no dismissed suggestions. |
324 EXPECT_CALL(*this, ReceivedDismissedSuggestions(IsEmpty())); | 319 dismissed_suggestions.clear(); |
325 provider()->GetDismissedSuggestionsForDebugging( | 320 provider()->GetDismissedSuggestionsForDebugging( |
326 downloads_category(), | 321 downloads_category(), |
327 base::Bind( | 322 base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions)); |
328 &OfflinePageSuggestionsProviderTest::DismissedSuggestionsHelper, | 323 EXPECT_THAT(dismissed_suggestions, IsEmpty()); |
329 base::Unretained(this))); | |
330 Mock::VerifyAndClearExpectations(this); | |
331 | 324 |
332 // Clear dismissed suggestions. | 325 // Clear dismissed suggestions. |
333 provider()->ClearDismissedSuggestionsForDebugging(recent_tabs_category()); | 326 provider()->ClearDismissedSuggestionsForDebugging(recent_tabs_category()); |
334 | 327 |
335 // They should be gone from the dismissed suggestions. | 328 // They should be gone from the dismissed suggestions. |
336 EXPECT_CALL(*this, ReceivedDismissedSuggestions(IsEmpty())); | 329 dismissed_suggestions.clear(); |
337 provider()->GetDismissedSuggestionsForDebugging( | 330 provider()->GetDismissedSuggestionsForDebugging( |
338 recent_tabs_category(), | 331 recent_tabs_category(), |
339 base::Bind( | 332 base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions)); |
340 &OfflinePageSuggestionsProviderTest::DismissedSuggestionsHelper, | 333 EXPECT_THAT(dismissed_suggestions, IsEmpty()); |
341 base::Unretained(this))); | |
342 Mock::VerifyAndClearExpectations(this); | |
343 | 334 |
344 // And appear in the reported suggestions for the category again. | 335 // And appear in the reported suggestions for the category again. |
345 EXPECT_CALL(*observer(), | 336 EXPECT_CALL(*observer(), |
346 OnNewSuggestions(_, recent_tabs_category(), SizeIs(4))); | 337 OnNewSuggestions(_, recent_tabs_category(), SizeIs(4))); |
347 EXPECT_CALL(*observer(), | 338 EXPECT_CALL(*observer(), |
348 OnNewSuggestions(_, downloads_category(), IsEmpty())); | 339 OnNewSuggestions(_, downloads_category(), IsEmpty())); |
349 FireOfflinePageModelChanged(); | 340 FireOfflinePageModelChanged(); |
350 Mock::VerifyAndClearExpectations(observer()); | 341 Mock::VerifyAndClearExpectations(observer()); |
351 } | 342 } |
352 | 343 |
(...skipping 24 matching lines...) Expand all Loading... | |
377 GetDummySuggestionId(recent_tabs_category(), 2)); | 368 GetDummySuggestionId(recent_tabs_category(), 2)); |
378 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), SizeIs(1)); | 369 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), SizeIs(1)); |
379 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), IsEmpty()); | 370 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), IsEmpty()); |
380 | 371 |
381 FireOfflinePageDeleted(model()->items()->at(1)); | 372 FireOfflinePageDeleted(model()->items()->at(1)); |
382 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); | 373 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); |
383 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), IsEmpty()); | 374 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), IsEmpty()); |
384 } | 375 } |
385 | 376 |
386 TEST_F(OfflinePageSuggestionsProviderTest, ShouldClearDismissedOnFetch) { | 377 TEST_F(OfflinePageSuggestionsProviderTest, ShouldClearDismissedOnFetch) { |
387 AddItem(CreateDummyDownload(1)); | 378 AddItem(CreateDummyDownload(1)); |
tschumann
2016/08/29 15:47:00
for symmetry, I'd also call model() here and inlin
Philipp Keck
2016/08/29 17:04:15
Done.
| |
388 AddItem(CreateDummyDownload(2)); | 379 AddItem(CreateDummyDownload(2)); |
389 AddItem(CreateDummyDownload(3)); | 380 AddItem(CreateDummyDownload(3)); |
390 FireOfflinePageModelChanged(); | 381 FireOfflinePageModelChanged(); |
391 | 382 |
392 provider()->DismissSuggestion(GetDummySuggestionId(downloads_category(), 2)); | 383 provider()->DismissSuggestion(GetDummySuggestionId(downloads_category(), 2)); |
393 provider()->DismissSuggestion(GetDummySuggestionId(downloads_category(), 3)); | 384 provider()->DismissSuggestion(GetDummySuggestionId(downloads_category(), 3)); |
394 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); | 385 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); |
395 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), SizeIs(2)); | 386 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), SizeIs(2)); |
396 | 387 |
397 model()->items()->clear(); | 388 model()->items()->clear(); |
398 AddItem(CreateDummyDownload(2)); | 389 AddItem(CreateDummyDownload(2)); |
399 FireOfflinePageModelChanged(); | 390 FireOfflinePageModelChanged(); |
400 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); | 391 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); |
401 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), SizeIs(1)); | 392 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), SizeIs(1)); |
402 | 393 |
403 model()->items()->clear(); | 394 model()->items()->clear(); |
404 FireOfflinePageModelChanged(); | 395 FireOfflinePageModelChanged(); |
405 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); | 396 EXPECT_THAT(ReadDismissedIDsFromPrefs(recent_tabs_category()), IsEmpty()); |
406 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), IsEmpty()); | 397 EXPECT_THAT(ReadDismissedIDsFromPrefs(downloads_category()), IsEmpty()); |
407 } | 398 } |
408 | 399 |
409 } // namespace ntp_snippets | 400 } // namespace ntp_snippets |
OLD | NEW |