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

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: Added a test. 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) {
Marc Treib 2016/08/18 15:27:42 nit: const std::vector<..>& ? (Also in the ctor ab
vitaliii 2016/08/18 16:32:12 Done.
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 providers() { 160 providers() {
156 return service()->providers_by_category_; 161 return service()->providers_by_category_;
157 } 162 }
158 163
159 CategoryFactory* category_factory() { return service()->category_factory(); } 164 CategoryFactory* category_factory() { return service()->category_factory(); }
160 165
161 Category FromKnownCategory(KnownCategories known_category) { 166 Category FromKnownCategory(KnownCategories known_category) {
162 return service()->category_factory()->FromKnownCategory(known_category); 167 return service()->category_factory()->FromKnownCategory(known_category);
163 } 168 }
164 169
165 MockProvider* MakeProvider(Category provided_category) { 170 MockProvider* MakeProvider(Category provided_category) {
Marc Treib 2016/08/18 15:27:42 Not your doing, but would you mind renaming this t
vitaliii 2016/08/18 16:32:12 Done.
166 return MakeProvider(std::vector<Category>({provided_category})); 171 return MakeProvider(std::vector<Category>({provided_category}));
167 } 172 }
168 173
169 MockProvider* MakeProvider(std::vector<Category> provided_categories) { 174 MockProvider* MakeProvider(std::vector<Category> provided_categories) {
170 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>( 175 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>(
171 service(), category_factory(), provided_categories); 176 service(), category_factory(), provided_categories);
172 MockProvider* result = provider.get(); 177 MockProvider* result = provider.get();
173 service()->RegisterProvider(std::move(provider)); 178 service()->RegisterProvider(std::move(provider));
174 return result; 179 return result;
175 } 180 }
(...skipping 180 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());
Marc Treib 2016/08/18 15:27:42 nit: ASSERT_TRUE so that the test stops if it's nu
vitaliii 2016/08/18 16:32:12 Done.
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) {
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|.
Marc Treib 2016/08/18 15:27:42 [Mostly to pke:] Is this something we want to supp
Philipp Keck 2016/08/18 15:29:56 Last time we talked about this with Tim, we said w
Marc Treib 2016/08/18 15:33:55 Alright, thanks. Then I'm okay with leaving this a
vitaliii 2016/08/18 16:32:13 Done.
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);
Marc Treib 2016/08/18 15:27:42 .Times(1) isn't necessary, it doesn't do anything
vitaliii 2016/08/18 16:32:13 Done.
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));
Marc Treib 2016/08/18 15:27:43 EXPECT_EQ or EXPECT_THAT(.., Eq(..))? Either's fin
vitaliii 2016/08/18 16:32:13 Done.
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,
421 ShouldRegisterNewCategoryOnCategoryStatusChanged) {
422 Category category = FromKnownCategory(KnownCategories::BOOKMARKS);
423 MockProvider* provider = MakeProvider(category);
424 MockServiceObserver observer;
425 service()->AddObserver(&observer);
426
427 // Provider starts providing |new_category| and calls
428 // |OnCategoryStatusChanged|, but the category is not yet available.
429 Category new_category = FromKnownCategory(KnownCategories::ARTICLES);
430 provider->SetProvidedCategories(
431 std::vector<Category>({category, new_category}));
432 EXPECT_CALL(observer, OnCategoryStatusChanged(new_category,
433 CategoryStatus::INITIALIZING))
434 .Times(1);
435 provider->FireCategoryStatusChanged(new_category,
436 CategoryStatus::INITIALIZING);
437
438 EXPECT_THAT(providers().at(new_category), Eq(provider));
439 ExpectThatSuggestionsAre(new_category, std::vector<int>());
440 EXPECT_THAT(service()->GetCategoryStatus(new_category),
441 Eq(CategoryStatus::INITIALIZING));
442 EXPECT_EQ(service()->GetCategories(),
443 std::vector<Category>({category, new_category}));
444
445 service()->RemoveObserver(&observer);
446 }
447
448 TEST_F(ContentSuggestionsServiceTest, ShouldRemoveCategoryWhenNotProvided) {
449 Category category = FromKnownCategory(KnownCategories::BOOKMARKS);
450 MockProvider* provider = MakeProvider(category);
451 MockServiceObserver observer;
452 service()->AddObserver(&observer);
453
454 provider->FireSuggestionsChanged(category, {1, 2});
455 ExpectThatSuggestionsAre(category, {1, 2});
456
457 EXPECT_CALL(observer,
458 OnCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED));
459 provider->FireCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED);
460
461 EXPECT_THAT(service()->GetCategoryStatus(category),
462 Eq(CategoryStatus::NOT_PROVIDED));
463 EXPECT_TRUE(service()->GetCategories().empty());
464 ExpectThatSuggestionsAre(category, std::vector<int>());
465
466 service()->RemoveObserver(&observer);
467 }
468
366 } // namespace ntp_snippets 469 } // 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