Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(793)

Side by Side Diff: components/ntp_snippets/content_suggestions_service_unittest.cc

Issue 2259873002: Add ContentSuggestionsService tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/content_suggestions_service.h" 5 #include "components/ntp_snippets/content_suggestions_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 result.emplace_back(CreateSuggestion(number)); 51 result.emplace_back(CreateSuggestion(number));
52 } 52 }
53 return result; 53 return result;
54 } 54 }
55 55
56 class MockProvider : public ContentSuggestionsProvider { 56 class MockProvider : public ContentSuggestionsProvider {
57 public: 57 public:
58 MockProvider(Observer* observer, 58 MockProvider(Observer* observer,
59 CategoryFactory* category_factory, 59 CategoryFactory* category_factory,
60 std::vector<Category> provided_categories) 60 std::vector<Category> provided_categories)
61 : ContentSuggestionsProvider(observer, category_factory), 61 : ContentSuggestionsProvider(observer, category_factory) {
62 provided_categories_(provided_categories) { 62 SetProvidedCategories(provided_categories);
63 }
64
65 void SetProvidedCategories(std::vector<Category> provided_categories) {
66 statuses_.clear();
67 provided_categories_ = provided_categories;
63 for (Category category : provided_categories) { 68 for (Category category : provided_categories) {
64 statuses_[category.id()] = CategoryStatus::AVAILABLE; 69 statuses_[category.id()] = CategoryStatus::AVAILABLE;
65 } 70 }
66 } 71 }
67 72
68 std::vector<Category> GetProvidedCategories() override { 73 std::vector<Category> GetProvidedCategories() override {
69 return provided_categories_; 74 return provided_categories_;
70 } 75 }
71 76
72 CategoryStatus GetCategoryStatus(Category category) { 77 CategoryStatus GetCategoryStatus(Category category) {
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); 361 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>());
357 Mock::VerifyAndClearExpectations(&observer); 362 Mock::VerifyAndClearExpectations(&observer);
358 363
359 // Shutdown the service 364 // Shutdown the service
360 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); 365 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown());
361 service()->Shutdown(); 366 service()->Shutdown();
362 service()->RemoveObserver(&observer); 367 service()->RemoveObserver(&observer);
363 // The service will receive two Shutdown() calls. 368 // The service will receive two Shutdown() calls.
364 } 369 }
365 370
371 TEST_F(ContentSuggestionsServiceTest,
372 ShouldNotReturnCategoryInfoForNonexistentCategory) {
373 Category category = FromKnownCategory(KnownCategories::BOOKMARKS);
374 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category);
375 EXPECT_FALSE(result.has_value());
376 }
377
378 TEST_F(ContentSuggestionsServiceTest, ShouldReturnCategoryInfo) {
379 Category category = FromKnownCategory(KnownCategories::BOOKMARKS);
380 MockProvider* provider = MakeProvider(category);
381 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category);
382 EXPECT_TRUE(result.has_value());
383 CategoryInfo expected = provider->GetCategoryInfo(category);
384 const CategoryInfo& actual = result.value();
385 EXPECT_EQ(expected.title(), actual.title());
386 EXPECT_EQ(expected.card_layout(), actual.card_layout());
387 EXPECT_EQ(expected.has_more_button(), actual.has_more_button());
388 }
389
390 TEST_F(ContentSuggestionsServiceTest,
391 ShouldRegisterNewCategoryOnNewSuggestions) {
Philipp Keck 2016/08/18 12:51:58 OnCategoryStatusChanged has the same ability of ad
vitaliii 2016/08/18 13:12:35 Done.
392 Category category = FromKnownCategory(KnownCategories::BOOKMARKS);
393 MockProvider* provider = MakeProvider(category);
394 MockServiceObserver observer;
395 service()->AddObserver(&observer);
396
397 // Provider starts providing |new_category| without calling
398 // |OnCategoryStatusChanged|.
399 Category new_category = FromKnownCategory(KnownCategories::ARTICLES);
400 provider->SetProvidedCategories(
401 std::vector<Category>({category, new_category}));
402
403 EXPECT_CALL(observer, OnNewSuggestions(new_category)).Times(1);
404 EXPECT_CALL(observer,
405 OnCategoryStatusChanged(new_category, CategoryStatus::AVAILABLE))
406 .Times(1);
407 provider->FireSuggestionsChanged(new_category, {1, 2});
408
409 ExpectThatSuggestionsAre(new_category, {1, 2});
410 EXPECT_THAT(providers().at(category), Eq(provider));
411 EXPECT_THAT(service()->GetCategoryStatus(category),
412 Eq(CategoryStatus::AVAILABLE));
413 EXPECT_THAT(providers().at(new_category), Eq(provider));
414 EXPECT_THAT(service()->GetCategoryStatus(new_category),
415 Eq(CategoryStatus::AVAILABLE));
416
417 service()->RemoveObserver(&observer);
418 }
419
420 TEST_F(ContentSuggestionsServiceTest, ShouldRemoveCategoryWhenNotProvided) {
421 Category category = FromKnownCategory(KnownCategories::BOOKMARKS);
422 MockProvider* provider = MakeProvider(category);
423 MockServiceObserver observer;
424 service()->AddObserver(&observer);
425
426 provider->FireSuggestionsChanged(category, {1, 2});
427 ExpectThatSuggestionsAre(category, {1, 2});
428
429 EXPECT_CALL(observer,
430 OnCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED));
431 provider->FireCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED);
432
433 EXPECT_THAT(service()->GetCategoryStatus(category),
434 Eq(CategoryStatus::NOT_PROVIDED));
435 EXPECT_TRUE(service()->GetCategories().empty());
436 ExpectThatSuggestionsAre(category, std::vector<int>());
437
438 service()->RemoveObserver(&observer);
439 }
440
366 } // namespace ntp_snippets 441 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698