| 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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 void SetUp() override { | 200 void SetUp() override { |
| 201 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); | 201 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); |
| 202 } | 202 } |
| 203 }; | 203 }; |
| 204 | 204 |
| 205 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { | 205 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { |
| 206 EXPECT_THAT(service()->state(), | 206 EXPECT_THAT(service()->state(), |
| 207 Eq(ContentSuggestionsService::State::ENABLED)); | 207 Eq(ContentSuggestionsService::State::ENABLED)); |
| 208 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 208 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 209 Category offline_pages_category = | 209 Category offline_pages_category = |
| 210 FromKnownCategory(KnownCategories::OFFLINE_PAGES); | 210 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 211 ASSERT_THAT(providers(), IsEmpty()); | 211 ASSERT_THAT(providers(), IsEmpty()); |
| 212 EXPECT_THAT(service()->GetCategories(), IsEmpty()); | 212 EXPECT_THAT(service()->GetCategories(), IsEmpty()); |
| 213 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 213 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 214 Eq(CategoryStatus::NOT_PROVIDED)); | 214 Eq(CategoryStatus::NOT_PROVIDED)); |
| 215 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 215 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 216 Eq(CategoryStatus::NOT_PROVIDED)); | 216 Eq(CategoryStatus::NOT_PROVIDED)); |
| 217 | 217 |
| 218 MockProvider* provider1 = MakeProvider(articles_category); | 218 MockProvider* provider1 = MakeProvider(articles_category); |
| 219 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul)); | 219 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul)); |
| 220 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 220 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 233 ElementsAre(offline_pages_category, articles_category)); | 233 ElementsAre(offline_pages_category, articles_category)); |
| 234 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 234 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 235 Eq(CategoryStatus::AVAILABLE)); | 235 Eq(CategoryStatus::AVAILABLE)); |
| 236 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 236 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 237 Eq(CategoryStatus::AVAILABLE)); | 237 Eq(CategoryStatus::AVAILABLE)); |
| 238 } | 238 } |
| 239 | 239 |
| 240 TEST_F(ContentSuggestionsServiceDisabledTest, ShouldDoNothingWhenDisabled) { | 240 TEST_F(ContentSuggestionsServiceDisabledTest, ShouldDoNothingWhenDisabled) { |
| 241 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 241 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 242 Category offline_pages_category = | 242 Category offline_pages_category = |
| 243 FromKnownCategory(KnownCategories::OFFLINE_PAGES); | 243 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 244 EXPECT_THAT(service()->state(), | 244 EXPECT_THAT(service()->state(), |
| 245 Eq(ContentSuggestionsService::State::DISABLED)); | 245 Eq(ContentSuggestionsService::State::DISABLED)); |
| 246 EXPECT_THAT(providers(), IsEmpty()); | 246 EXPECT_THAT(providers(), IsEmpty()); |
| 247 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 247 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 248 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); | 248 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); |
| 249 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 249 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 250 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); | 250 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); |
| 251 EXPECT_THAT(service()->GetCategories(), IsEmpty()); | 251 EXPECT_THAT(service()->GetCategories(), IsEmpty()); |
| 252 EXPECT_THAT(service()->GetSuggestionsForCategory(articles_category), | 252 EXPECT_THAT(service()->GetSuggestionsForCategory(articles_category), |
| 253 IsEmpty()); | 253 IsEmpty()); |
| 254 } | 254 } |
| 255 | 255 |
| 256 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) { | 256 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) { |
| 257 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 257 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 258 Category offline_pages_category = | 258 Category offline_pages_category = |
| 259 FromKnownCategory(KnownCategories::OFFLINE_PAGES); | 259 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 260 MockProvider* provider1 = MakeProvider(articles_category); | 260 MockProvider* provider1 = MakeProvider(articles_category); |
| 261 MockProvider* provider2 = MakeProvider(offline_pages_category); | 261 MockProvider* provider2 = MakeProvider(offline_pages_category); |
| 262 | 262 |
| 263 provider1->FireSuggestionsChanged(articles_category, {1}); | 263 provider1->FireSuggestionsChanged(articles_category, {1}); |
| 264 std::string suggestion_id = CreateSuggestion(1).id(); | 264 std::string suggestion_id = CreateSuggestion(1).id(); |
| 265 | 265 |
| 266 EXPECT_CALL(*provider1, FetchSuggestionImage(suggestion_id, _)).Times(1); | 266 EXPECT_CALL(*provider1, FetchSuggestionImage(suggestion_id, _)).Times(1); |
| 267 EXPECT_CALL(*provider2, FetchSuggestionImage(_, _)).Times(0); | 267 EXPECT_CALL(*provider2, FetchSuggestionImage(_, _)).Times(0); |
| 268 service()->FetchSuggestionImage( | 268 service()->FetchSuggestionImage( |
| 269 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, | 269 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 282 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 282 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 283 service()->FetchSuggestionImage( | 283 service()->FetchSuggestionImage( |
| 284 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, | 284 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, |
| 285 base::Unretained(this))); | 285 base::Unretained(this))); |
| 286 run_loop.Run(); | 286 run_loop.Run(); |
| 287 } | 287 } |
| 288 | 288 |
| 289 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectDismissSuggestion) { | 289 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectDismissSuggestion) { |
| 290 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 290 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 291 Category offline_pages_category = | 291 Category offline_pages_category = |
| 292 FromKnownCategory(KnownCategories::OFFLINE_PAGES); | 292 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 293 MockProvider* provider1 = MakeProvider(articles_category); | 293 MockProvider* provider1 = MakeProvider(articles_category); |
| 294 MockProvider* provider2 = MakeProvider(offline_pages_category); | 294 MockProvider* provider2 = MakeProvider(offline_pages_category); |
| 295 | 295 |
| 296 provider2->FireSuggestionsChanged(offline_pages_category, {11}); | 296 provider2->FireSuggestionsChanged(offline_pages_category, {11}); |
| 297 std::string suggestion_id = CreateSuggestion(11).id(); | 297 std::string suggestion_id = CreateSuggestion(11).id(); |
| 298 | 298 |
| 299 EXPECT_CALL(*provider1, DismissSuggestion(_)).Times(0); | 299 EXPECT_CALL(*provider1, DismissSuggestion(_)).Times(0); |
| 300 EXPECT_CALL(*provider2, DismissSuggestion(suggestion_id)).Times(1); | 300 EXPECT_CALL(*provider2, DismissSuggestion(suggestion_id)).Times(1); |
| 301 service()->DismissSuggestion(suggestion_id); | 301 service()->DismissSuggestion(suggestion_id); |
| 302 } | 302 } |
| 303 | 303 |
| 304 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { | 304 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { |
| 305 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 305 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 306 Category offline_pages_category = | 306 Category offline_pages_category = |
| 307 FromKnownCategory(KnownCategories::OFFLINE_PAGES); | 307 FromKnownCategory(KnownCategories::BOOKMARKS); |
| 308 | 308 |
| 309 // Create and register providers | 309 // Create and register providers |
| 310 MockProvider* provider1 = MakeProvider(articles_category); | 310 MockProvider* provider1 = MakeProvider(articles_category); |
| 311 MockProvider* provider2 = MakeProvider(offline_pages_category); | 311 MockProvider* provider2 = MakeProvider(offline_pages_category); |
| 312 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 312 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
| 313 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); | 313 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); |
| 314 | 314 |
| 315 // Create and register observer | 315 // Create and register observer |
| 316 MockServiceObserver observer; | 316 MockServiceObserver observer; |
| 317 service()->AddObserver(&observer); | 317 service()->AddObserver(&observer); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 336 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); | 336 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); |
| 337 Mock::VerifyAndClearExpectations(&observer); | 337 Mock::VerifyAndClearExpectations(&observer); |
| 338 | 338 |
| 339 // Send suggestion 1 only | 339 // Send suggestion 1 only |
| 340 EXPECT_CALL(observer, OnNewSuggestions(articles_category)).Times(1); | 340 EXPECT_CALL(observer, OnNewSuggestions(articles_category)).Times(1); |
| 341 provider1->FireSuggestionsChanged(articles_category, {1}); | 341 provider1->FireSuggestionsChanged(articles_category, {1}); |
| 342 ExpectThatSuggestionsAre(articles_category, {1}); | 342 ExpectThatSuggestionsAre(articles_category, {1}); |
| 343 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); | 343 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); |
| 344 Mock::VerifyAndClearExpectations(&observer); | 344 Mock::VerifyAndClearExpectations(&observer); |
| 345 | 345 |
| 346 // provider2 reports OFFLINE_PAGES as unavailable | 346 // provider2 reports BOOKMARKS as unavailable |
| 347 EXPECT_CALL(observer, OnCategoryStatusChanged( | 347 EXPECT_CALL(observer, OnCategoryStatusChanged( |
| 348 offline_pages_category, | 348 offline_pages_category, |
| 349 CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)) | 349 CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)) |
| 350 .Times(1); | 350 .Times(1); |
| 351 provider2->FireCategoryStatusChanged( | 351 provider2->FireCategoryStatusChanged( |
| 352 offline_pages_category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); | 352 offline_pages_category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
| 353 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 353 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
| 354 Eq(CategoryStatus::AVAILABLE)); | 354 Eq(CategoryStatus::AVAILABLE)); |
| 355 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 355 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
| 356 Eq(CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); | 356 Eq(CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); |
| 357 ExpectThatSuggestionsAre(articles_category, {1}); | 357 ExpectThatSuggestionsAre(articles_category, {1}); |
| 358 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); | 358 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); |
| 359 Mock::VerifyAndClearExpectations(&observer); | 359 Mock::VerifyAndClearExpectations(&observer); |
| 360 | 360 |
| 361 // Shutdown the service | 361 // Shutdown the service |
| 362 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); | 362 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); |
| 363 service()->Shutdown(); | 363 service()->Shutdown(); |
| 364 service()->RemoveObserver(&observer); | 364 service()->RemoveObserver(&observer); |
| 365 // The service will receive two Shutdown() calls. | 365 // The service will receive two Shutdown() calls. |
| 366 } | 366 } |
| 367 | 367 |
| 368 } // namespace ntp_snippets | 368 } // namespace ntp_snippets |
| OLD | NEW |