Chromium Code Reviews| 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/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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 } | 64 } |
| 65 | 65 |
| 66 void SetProvidedCategories(const std::vector<Category>& provided_categories) { | 66 void SetProvidedCategories(const std::vector<Category>& provided_categories) { |
| 67 statuses_.clear(); | 67 statuses_.clear(); |
| 68 provided_categories_ = provided_categories; | 68 provided_categories_ = provided_categories; |
| 69 for (Category category : provided_categories) { | 69 for (Category category : provided_categories) { |
| 70 statuses_[category.id()] = CategoryStatus::AVAILABLE; | 70 statuses_[category.id()] = CategoryStatus::AVAILABLE; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 std::vector<Category> GetProvidedCategories() override { | |
| 75 return provided_categories_; | |
| 76 } | |
| 77 | |
| 78 CategoryStatus GetCategoryStatus(Category category) { | 74 CategoryStatus GetCategoryStatus(Category category) { |
| 79 return statuses_[category.id()]; | 75 return statuses_[category.id()]; |
| 80 } | 76 } |
| 81 | 77 |
| 82 CategoryInfo GetCategoryInfo(Category category) { | 78 CategoryInfo GetCategoryInfo(Category category) { |
| 83 return CategoryInfo(base::ASCIIToUTF16("Section title"), | 79 return CategoryInfo(base::ASCIIToUTF16("Section title"), |
| 84 ContentSuggestionsCardLayout::FULL_CARD, true); | 80 ContentSuggestionsCardLayout::FULL_CARD, true); |
| 85 } | 81 } |
| 86 | 82 |
| 87 void FireSuggestionsChanged(Category category, | 83 void FireSuggestionsChanged(Category category, |
| 88 const std::vector<int>& numbers) { | 84 const std::vector<int>& numbers) { |
| 89 observer()->OnNewSuggestions(this, category, CreateSuggestions(numbers)); | 85 observer()->OnNewSuggestions(this, category, CreateSuggestions(numbers)); |
| 90 } | 86 } |
| 91 | 87 |
| 92 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { | 88 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { |
| 93 statuses_[category.id()] = new_status; | 89 statuses_[category.id()] = new_status; |
| 94 observer()->OnCategoryStatusChanged(this, category, new_status); | 90 observer()->OnCategoryStatusChanged(this, category, new_status); |
| 95 } | 91 } |
| 96 | 92 |
| 93 void FireCategoryStatusChangedWithCurrentStatus(Category category) { | |
| 94 observer()->OnCategoryStatusChanged(this, category, | |
|
Philipp Keck
2016/08/19 14:14:13
Couldn't this just go in the RegisterProvider meth
vitaliii
2016/08/19 16:14:14
Then it is not possible to do ShouldRegisterNewCat
| |
| 95 statuses_[category.id()]); | |
| 96 } | |
| 97 | |
| 97 void FireSuggestionInvalidated(Category category, | 98 void FireSuggestionInvalidated(Category category, |
| 98 const std::string& suggestion_id) { | 99 const std::string& suggestion_id) { |
| 99 observer()->OnSuggestionInvalidated(this, category, suggestion_id); | 100 observer()->OnSuggestionInvalidated(this, category, suggestion_id); |
| 100 } | 101 } |
| 101 | 102 |
| 102 MOCK_METHOD1(ClearCachedSuggestionsForDebugging, void(Category category)); | 103 MOCK_METHOD1(ClearCachedSuggestionsForDebugging, void(Category category)); |
| 103 MOCK_METHOD1(GetDismissedSuggestionsForDebugging, | 104 MOCK_METHOD1(GetDismissedSuggestionsForDebugging, |
| 104 std::vector<ContentSuggestion>(Category category)); | 105 std::vector<ContentSuggestion>(Category category)); |
| 105 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category category)); | 106 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category category)); |
| 106 MOCK_METHOD1(DismissSuggestion, void(const std::string& suggestion_id)); | 107 MOCK_METHOD1(DismissSuggestion, void(const std::string& suggestion_id)); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 Category offline_pages_category = | 223 Category offline_pages_category = |
| 223 FromKnownCategory(KnownCategories::BOOKMARKS); | 224 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 224 ASSERT_THAT(providers(), IsEmpty()); | 225 ASSERT_THAT(providers(), IsEmpty()); |
| 225 EXPECT_THAT(service()->GetCategories(), IsEmpty()); | 226 EXPECT_THAT(service()->GetCategories(), IsEmpty()); |
| 226 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 227 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 227 Eq(CategoryStatus::NOT_PROVIDED)); | 228 Eq(CategoryStatus::NOT_PROVIDED)); |
| 228 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 229 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 229 Eq(CategoryStatus::NOT_PROVIDED)); | 230 Eq(CategoryStatus::NOT_PROVIDED)); |
| 230 | 231 |
| 231 MockProvider* provider1 = RegisterProvider(articles_category); | 232 MockProvider* provider1 = RegisterProvider(articles_category); |
| 233 provider1->FireCategoryStatusChangedWithCurrentStatus(articles_category); | |
| 232 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul)); | 234 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul)); |
| 235 ASSERT_THAT(providers().count(articles_category), Eq(1ul)); | |
| 233 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 236 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
| 234 EXPECT_THAT(providers().size(), Eq(1ul)); | 237 EXPECT_THAT(providers().size(), Eq(1ul)); |
| 235 EXPECT_THAT(service()->GetCategories(), ElementsAre(articles_category)); | 238 EXPECT_THAT(service()->GetCategories(), ElementsAre(articles_category)); |
| 236 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 239 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 237 Eq(CategoryStatus::AVAILABLE)); | 240 Eq(CategoryStatus::AVAILABLE)); |
| 238 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 241 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 239 Eq(CategoryStatus::NOT_PROVIDED)); | 242 Eq(CategoryStatus::NOT_PROVIDED)); |
| 240 | 243 |
| 241 MockProvider* provider2 = RegisterProvider(offline_pages_category); | 244 MockProvider* provider2 = RegisterProvider(offline_pages_category); |
| 245 provider2->FireCategoryStatusChangedWithCurrentStatus(offline_pages_category); | |
| 246 ASSERT_THAT(providers().count(offline_pages_category), Eq(1ul)); | |
| 242 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 247 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
| 248 ASSERT_THAT(providers().count(articles_category), Eq(1ul)); | |
| 243 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); | 249 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); |
| 244 EXPECT_THAT(providers().size(), Eq(2ul)); | 250 EXPECT_THAT(providers().size(), Eq(2ul)); |
| 245 EXPECT_THAT(service()->GetCategories(), | 251 EXPECT_THAT(service()->GetCategories(), |
| 246 ElementsAre(offline_pages_category, articles_category)); | 252 ElementsAre(offline_pages_category, articles_category)); |
| 247 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 253 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 248 Eq(CategoryStatus::AVAILABLE)); | 254 Eq(CategoryStatus::AVAILABLE)); |
| 249 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 255 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 250 Eq(CategoryStatus::AVAILABLE)); | 256 Eq(CategoryStatus::AVAILABLE)); |
| 251 } | 257 } |
| 252 | 258 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 service()->RemoveObserver(&observer); | 349 service()->RemoveObserver(&observer); |
| 344 } | 350 } |
| 345 | 351 |
| 346 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { | 352 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { |
| 347 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 353 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 348 Category offline_pages_category = | 354 Category offline_pages_category = |
| 349 FromKnownCategory(KnownCategories::BOOKMARKS); | 355 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 350 | 356 |
| 351 // Create and register providers | 357 // Create and register providers |
| 352 MockProvider* provider1 = RegisterProvider(articles_category); | 358 MockProvider* provider1 = RegisterProvider(articles_category); |
| 359 provider1->FireCategoryStatusChangedWithCurrentStatus(articles_category); | |
| 353 MockProvider* provider2 = RegisterProvider(offline_pages_category); | 360 MockProvider* provider2 = RegisterProvider(offline_pages_category); |
| 361 provider2->FireCategoryStatusChangedWithCurrentStatus(offline_pages_category); | |
| 362 ASSERT_THAT(providers().count(articles_category), Eq(1ul)); | |
| 354 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 363 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
| 364 ASSERT_THAT(providers().count(offline_pages_category), Eq(1ul)); | |
| 355 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); | 365 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); |
| 356 | 366 |
| 357 // Create and register observer | 367 // Create and register observer |
| 358 MockServiceObserver observer; | 368 MockServiceObserver observer; |
| 359 service()->AddObserver(&observer); | 369 service()->AddObserver(&observer); |
| 360 | 370 |
| 361 // Send suggestions 1 and 2 | 371 // Send suggestions 1 and 2 |
| 362 EXPECT_CALL(observer, OnNewSuggestions(articles_category)); | 372 EXPECT_CALL(observer, OnNewSuggestions(articles_category)); |
| 363 provider1->FireSuggestionsChanged(articles_category, {1, 2}); | 373 provider1->FireSuggestionsChanged(articles_category, {1, 2}); |
| 364 ExpectThatSuggestionsAre(articles_category, {1, 2}); | 374 ExpectThatSuggestionsAre(articles_category, {1, 2}); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 TEST_F(ContentSuggestionsServiceTest, | 419 TEST_F(ContentSuggestionsServiceTest, |
| 410 ShouldNotReturnCategoryInfoForNonexistentCategory) { | 420 ShouldNotReturnCategoryInfoForNonexistentCategory) { |
| 411 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); | 421 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); |
| 412 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category); | 422 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category); |
| 413 EXPECT_FALSE(result.has_value()); | 423 EXPECT_FALSE(result.has_value()); |
| 414 } | 424 } |
| 415 | 425 |
| 416 TEST_F(ContentSuggestionsServiceTest, ShouldReturnCategoryInfo) { | 426 TEST_F(ContentSuggestionsServiceTest, ShouldReturnCategoryInfo) { |
| 417 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); | 427 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); |
| 418 MockProvider* provider = RegisterProvider(category); | 428 MockProvider* provider = RegisterProvider(category); |
| 429 provider->FireCategoryStatusChangedWithCurrentStatus(category); | |
| 419 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category); | 430 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category); |
| 420 ASSERT_TRUE(result.has_value()); | 431 ASSERT_TRUE(result.has_value()); |
| 421 CategoryInfo expected = provider->GetCategoryInfo(category); | 432 CategoryInfo expected = provider->GetCategoryInfo(category); |
| 422 const CategoryInfo& actual = result.value(); | 433 const CategoryInfo& actual = result.value(); |
| 423 EXPECT_THAT(expected.title(), Eq(actual.title())); | 434 EXPECT_THAT(expected.title(), Eq(actual.title())); |
| 424 EXPECT_THAT(expected.card_layout(), Eq(actual.card_layout())); | 435 EXPECT_THAT(expected.card_layout(), Eq(actual.card_layout())); |
| 425 EXPECT_THAT(expected.has_more_button(), Eq(actual.has_more_button())); | 436 EXPECT_THAT(expected.has_more_button(), Eq(actual.has_more_button())); |
| 426 } | 437 } |
| 427 | 438 |
| 428 TEST_F(ContentSuggestionsServiceTest, | 439 TEST_F(ContentSuggestionsServiceTest, |
| 429 ShouldRegisterNewCategoryOnNewSuggestions) { | 440 ShouldRegisterNewCategoryOnNewSuggestions) { |
| 430 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); | 441 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); |
| 431 MockProvider* provider = RegisterProvider(category); | 442 MockProvider* provider = RegisterProvider(category); |
| 443 provider->FireCategoryStatusChangedWithCurrentStatus(category); | |
| 432 MockServiceObserver observer; | 444 MockServiceObserver observer; |
| 433 service()->AddObserver(&observer); | 445 service()->AddObserver(&observer); |
| 434 | 446 |
| 435 // Provider starts providing |new_category| without calling | 447 // Provider starts providing |new_category| without calling |
| 436 // |OnCategoryStatusChanged|. This is supported for now until further | 448 // |OnCategoryStatusChanged|. This is supported for now until further |
| 437 // reconsideration. | 449 // reconsideration. |
| 438 Category new_category = FromKnownCategory(KnownCategories::ARTICLES); | 450 Category new_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 439 provider->SetProvidedCategories( | 451 provider->SetProvidedCategories( |
| 440 std::vector<Category>({category, new_category})); | 452 std::vector<Category>({category, new_category})); |
| 441 | 453 |
| 442 EXPECT_CALL(observer, OnNewSuggestions(new_category)); | 454 EXPECT_CALL(observer, OnNewSuggestions(new_category)); |
| 443 EXPECT_CALL(observer, | 455 EXPECT_CALL(observer, |
| 444 OnCategoryStatusChanged(new_category, CategoryStatus::AVAILABLE)); | 456 OnCategoryStatusChanged(new_category, CategoryStatus::AVAILABLE)); |
| 445 provider->FireSuggestionsChanged(new_category, {1, 2}); | 457 provider->FireSuggestionsChanged(new_category, {1, 2}); |
| 446 | 458 |
| 447 ExpectThatSuggestionsAre(new_category, {1, 2}); | 459 ExpectThatSuggestionsAre(new_category, {1, 2}); |
| 460 ASSERT_THAT(providers().count(category), Eq(1ul)); | |
| 448 EXPECT_THAT(providers().at(category), Eq(provider)); | 461 EXPECT_THAT(providers().at(category), Eq(provider)); |
| 449 EXPECT_THAT(service()->GetCategoryStatus(category), | 462 EXPECT_THAT(service()->GetCategoryStatus(category), |
| 450 Eq(CategoryStatus::AVAILABLE)); | 463 Eq(CategoryStatus::AVAILABLE)); |
| 464 ASSERT_THAT(providers().count(new_category), Eq(1ul)); | |
| 451 EXPECT_THAT(providers().at(new_category), Eq(provider)); | 465 EXPECT_THAT(providers().at(new_category), Eq(provider)); |
| 452 EXPECT_THAT(service()->GetCategoryStatus(new_category), | 466 EXPECT_THAT(service()->GetCategoryStatus(new_category), |
| 453 Eq(CategoryStatus::AVAILABLE)); | 467 Eq(CategoryStatus::AVAILABLE)); |
| 454 | 468 |
| 455 service()->RemoveObserver(&observer); | 469 service()->RemoveObserver(&observer); |
| 456 } | 470 } |
| 457 | 471 |
| 458 TEST_F(ContentSuggestionsServiceTest, | 472 TEST_F(ContentSuggestionsServiceTest, |
| 459 ShouldRegisterNewCategoryOnCategoryStatusChanged) { | 473 ShouldRegisterNewCategoryOnCategoryStatusChanged) { |
| 460 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); | 474 Category category = FromKnownCategory(KnownCategories::BOOKMARKS); |
| 461 MockProvider* provider = RegisterProvider(category); | 475 MockProvider* provider = RegisterProvider(category); |
| 476 provider->FireCategoryStatusChangedWithCurrentStatus(category); | |
| 462 MockServiceObserver observer; | 477 MockServiceObserver observer; |
| 463 service()->AddObserver(&observer); | 478 service()->AddObserver(&observer); |
| 464 | 479 |
| 465 // Provider starts providing |new_category| and calls | 480 // Provider starts providing |new_category| and calls |
| 466 // |OnCategoryStatusChanged|, but the category is not yet available. | 481 // |OnCategoryStatusChanged|, but the category is not yet available. |
| 467 Category new_category = FromKnownCategory(KnownCategories::ARTICLES); | 482 Category new_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 468 provider->SetProvidedCategories( | 483 provider->SetProvidedCategories( |
| 469 std::vector<Category>({category, new_category})); | 484 std::vector<Category>({category, new_category})); |
| 470 EXPECT_CALL(observer, OnCategoryStatusChanged(new_category, | 485 EXPECT_CALL(observer, OnCategoryStatusChanged(new_category, |
| 471 CategoryStatus::INITIALIZING)); | 486 CategoryStatus::INITIALIZING)); |
| 472 provider->FireCategoryStatusChanged(new_category, | 487 provider->FireCategoryStatusChanged(new_category, |
| 473 CategoryStatus::INITIALIZING); | 488 CategoryStatus::INITIALIZING); |
| 474 | 489 |
| 490 ASSERT_THAT(providers().count(new_category), Eq(1ul)); | |
| 475 EXPECT_THAT(providers().at(new_category), Eq(provider)); | 491 EXPECT_THAT(providers().at(new_category), Eq(provider)); |
| 476 ExpectThatSuggestionsAre(new_category, std::vector<int>()); | 492 ExpectThatSuggestionsAre(new_category, std::vector<int>()); |
| 477 EXPECT_THAT(service()->GetCategoryStatus(new_category), | 493 EXPECT_THAT(service()->GetCategoryStatus(new_category), |
| 478 Eq(CategoryStatus::INITIALIZING)); | 494 Eq(CategoryStatus::INITIALIZING)); |
| 479 EXPECT_THAT(service()->GetCategories(), | 495 EXPECT_THAT(service()->GetCategories(), |
| 480 Eq(std::vector<Category>({category, new_category}))); | 496 Eq(std::vector<Category>({category, new_category}))); |
| 481 | 497 |
| 482 service()->RemoveObserver(&observer); | 498 service()->RemoveObserver(&observer); |
| 483 } | 499 } |
| 484 | 500 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 497 | 513 |
| 498 EXPECT_THAT(service()->GetCategoryStatus(category), | 514 EXPECT_THAT(service()->GetCategoryStatus(category), |
| 499 Eq(CategoryStatus::NOT_PROVIDED)); | 515 Eq(CategoryStatus::NOT_PROVIDED)); |
| 500 EXPECT_TRUE(service()->GetCategories().empty()); | 516 EXPECT_TRUE(service()->GetCategories().empty()); |
| 501 ExpectThatSuggestionsAre(category, std::vector<int>()); | 517 ExpectThatSuggestionsAre(category, std::vector<int>()); |
| 502 | 518 |
| 503 service()->RemoveObserver(&observer); | 519 service()->RemoveObserver(&observer); |
| 504 } | 520 } |
| 505 | 521 |
| 506 } // namespace ntp_snippets | 522 } // namespace ntp_snippets |
| OLD | NEW |