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

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

Issue 2568033005: [NTP::SectionOrder] Replace CategoryFactory with a category ranker. (Closed)
Patch Set: rebase. Created 4 years 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
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 <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "components/ntp_snippets/category_info.h" 18 #include "components/ntp_snippets/category_info.h"
19 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h"
19 #include "components/ntp_snippets/category_status.h" 20 #include "components/ntp_snippets/category_status.h"
20 #include "components/ntp_snippets/content_suggestion.h" 21 #include "components/ntp_snippets/content_suggestion.h"
21 #include "components/ntp_snippets/content_suggestions_provider.h" 22 #include "components/ntp_snippets/content_suggestions_provider.h"
22 #include "components/ntp_snippets/user_classifier.h" 23 #include "components/ntp_snippets/user_classifier.h"
23 #include "components/prefs/testing_pref_service.h" 24 #include "components/prefs/testing_pref_service.h"
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/gfx/image/image.h" 27 #include "ui/gfx/image/image.h"
27 28
28 using testing::ElementsAre; 29 using testing::ElementsAre;
29 using testing::Eq; 30 using testing::Eq;
30 using testing::InvokeWithoutArgs; 31 using testing::InvokeWithoutArgs;
31 using testing::IsEmpty; 32 using testing::IsEmpty;
32 using testing::Mock; 33 using testing::Mock;
33 using testing::Property; 34 using testing::Property;
34 using testing::_; 35 using testing::_;
35 36
36 namespace ntp_snippets { 37 namespace ntp_snippets {
37 38
38 namespace { 39 namespace {
39 40
40 class MockProvider : public ContentSuggestionsProvider { 41 class MockProvider : public ContentSuggestionsProvider {
41 public: 42 public:
42 MockProvider(Observer* observer, 43 MockProvider(Observer* observer,
43 CategoryFactory* category_factory,
44 const std::vector<Category>& provided_categories) 44 const std::vector<Category>& provided_categories)
45 : ContentSuggestionsProvider(observer, category_factory) { 45 : ContentSuggestionsProvider(observer) {
46 SetProvidedCategories(provided_categories); 46 SetProvidedCategories(provided_categories);
47 } 47 }
48 48
49 void SetProvidedCategories(const std::vector<Category>& provided_categories) { 49 void SetProvidedCategories(const std::vector<Category>& provided_categories) {
50 statuses_.clear(); 50 statuses_.clear();
51 provided_categories_ = provided_categories; 51 provided_categories_ = provided_categories;
52 for (Category category : provided_categories) { 52 for (Category category : provided_categories) {
53 statuses_[category.id()] = CategoryStatus::AVAILABLE; 53 statuses_[category.id()] = CategoryStatus::AVAILABLE;
54 } 54 }
55 } 55 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 const std::map<Category, ContentSuggestionsProvider*, Category::CompareByID>& 173 const std::map<Category, ContentSuggestionsProvider*, Category::CompareByID>&
174 providers() { 174 providers() {
175 return service()->providers_by_category_; 175 return service()->providers_by_category_;
176 } 176 }
177 177
178 const std::map<Category, ContentSuggestionsProvider*, Category::CompareByID>& 178 const std::map<Category, ContentSuggestionsProvider*, Category::CompareByID>&
179 dismissed_providers() { 179 dismissed_providers() {
180 return service()->dismissed_providers_by_category_; 180 return service()->dismissed_providers_by_category_;
181 } 181 }
182 182
183 CategoryFactory* category_factory() { return service()->category_factory(); }
184
185 Category FromKnownCategory(KnownCategories known_category) {
186 return service()->category_factory()->FromKnownCategory(known_category);
187 }
188
189 Category FromRemoteCategory(int remote_category) {
190 return service()->category_factory()->FromRemoteCategory(remote_category);
191 }
192
193 MockProvider* RegisterProvider(Category provided_category) { 183 MockProvider* RegisterProvider(Category provided_category) {
194 return RegisterProvider(std::vector<Category>({provided_category})); 184 return RegisterProvider(std::vector<Category>({provided_category}));
195 } 185 }
196 186
197 MockProvider* RegisterProvider( 187 MockProvider* RegisterProvider(
198 const std::vector<Category>& provided_categories) { 188 const std::vector<Category>& provided_categories) {
199 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>( 189 std::unique_ptr<MockProvider> provider =
200 service(), category_factory(), provided_categories); 190 base::MakeUnique<MockProvider>(service(), provided_categories);
201 MockProvider* result = provider.get(); 191 MockProvider* result = provider.get();
202 service()->RegisterProvider(std::move(provider)); 192 service()->RegisterProvider(std::move(provider));
203 return result; 193 return result;
204 } 194 }
205 195
206 MOCK_METHOD1(OnImageFetched, void(const gfx::Image&)); 196 MOCK_METHOD1(OnImageFetched, void(const gfx::Image&));
207 197
208 protected: 198 protected:
209 void RegisterPrefs() { 199 void RegisterPrefs() {
210 ContentSuggestionsService::RegisterProfilePrefs(pref_service_->registry()); 200 ContentSuggestionsService::RegisterProfilePrefs(pref_service_->registry());
211 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); 201 UserClassifier::RegisterProfilePrefs(pref_service_->registry());
212 } 202 }
213 203
214 void CreateContentSuggestionsService( 204 void CreateContentSuggestionsService(
215 ContentSuggestionsService::State enabled) { 205 ContentSuggestionsService::State enabled) {
216 ASSERT_FALSE(service_); 206 ASSERT_FALSE(service_);
217 service_.reset(new ContentSuggestionsService( 207 service_.reset(new ContentSuggestionsService(
218 enabled, /*signin_manager=*/nullptr, /*history_service=*/nullptr, 208 enabled, /*signin_manager=*/nullptr, /*history_service=*/nullptr,
219 pref_service_.get())); 209 pref_service_.get(),
210 base::MakeUnique<ntp_snippets::ConstantCategoryRanker>()));
220 } 211 }
221 212
222 void ResetService() { 213 void ResetService() {
223 service_->Shutdown(); 214 service_->Shutdown();
224 service_.reset(); 215 service_.reset();
225 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); 216 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED);
226 } 217 }
227 218
228 ContentSuggestionsService* service() { return service_.get(); } 219 ContentSuggestionsService* service() { return service_.get(); }
229 220
(...skipping 26 matching lines...) Expand all
256 public: 247 public:
257 void SetUp() override { 248 void SetUp() override {
258 RegisterPrefs(); 249 RegisterPrefs();
259 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); 250 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED);
260 } 251 }
261 }; 252 };
262 253
263 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { 254 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) {
264 EXPECT_THAT(service()->state(), 255 EXPECT_THAT(service()->state(),
265 Eq(ContentSuggestionsService::State::ENABLED)); 256 Eq(ContentSuggestionsService::State::ENABLED));
266 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 257 Category articles_category =
258 Category::FromKnownCategory(KnownCategories::ARTICLES);
267 Category offline_pages_category = 259 Category offline_pages_category =
268 FromKnownCategory(KnownCategories::DOWNLOADS); 260 Category::FromKnownCategory(KnownCategories::DOWNLOADS);
269 ASSERT_THAT(providers(), IsEmpty()); 261 ASSERT_THAT(providers(), IsEmpty());
270 EXPECT_THAT(service()->GetCategories(), IsEmpty()); 262 EXPECT_THAT(service()->GetCategories(), IsEmpty());
271 EXPECT_THAT(service()->GetCategoryStatus(articles_category), 263 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
272 Eq(CategoryStatus::NOT_PROVIDED)); 264 Eq(CategoryStatus::NOT_PROVIDED));
273 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), 265 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
274 Eq(CategoryStatus::NOT_PROVIDED)); 266 Eq(CategoryStatus::NOT_PROVIDED));
275 267
276 MockProvider* provider1 = RegisterProvider(articles_category); 268 MockProvider* provider1 = RegisterProvider(articles_category);
277 provider1->FireCategoryStatusChangedWithCurrentStatus(articles_category); 269 provider1->FireCategoryStatusChangedWithCurrentStatus(articles_category);
278 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul)); 270 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul));
(...skipping 15 matching lines...) Expand all
294 EXPECT_THAT(providers().size(), Eq(2ul)); 286 EXPECT_THAT(providers().size(), Eq(2ul));
295 EXPECT_THAT(service()->GetCategories(), 287 EXPECT_THAT(service()->GetCategories(),
296 ElementsAre(offline_pages_category, articles_category)); 288 ElementsAre(offline_pages_category, articles_category));
297 EXPECT_THAT(service()->GetCategoryStatus(articles_category), 289 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
298 Eq(CategoryStatus::AVAILABLE)); 290 Eq(CategoryStatus::AVAILABLE));
299 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), 291 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
300 Eq(CategoryStatus::AVAILABLE)); 292 Eq(CategoryStatus::AVAILABLE));
301 } 293 }
302 294
303 TEST_F(ContentSuggestionsServiceDisabledTest, ShouldDoNothingWhenDisabled) { 295 TEST_F(ContentSuggestionsServiceDisabledTest, ShouldDoNothingWhenDisabled) {
304 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 296 Category articles_category =
297 Category::FromKnownCategory(KnownCategories::ARTICLES);
305 Category offline_pages_category = 298 Category offline_pages_category =
306 FromKnownCategory(KnownCategories::DOWNLOADS); 299 Category::FromKnownCategory(KnownCategories::DOWNLOADS);
307 EXPECT_THAT(service()->state(), 300 EXPECT_THAT(service()->state(),
308 Eq(ContentSuggestionsService::State::DISABLED)); 301 Eq(ContentSuggestionsService::State::DISABLED));
309 EXPECT_THAT(providers(), IsEmpty()); 302 EXPECT_THAT(providers(), IsEmpty());
310 EXPECT_THAT(service()->GetCategoryStatus(articles_category), 303 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
311 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); 304 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED));
312 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), 305 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
313 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); 306 Eq(CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED));
314 EXPECT_THAT(service()->GetCategories(), IsEmpty()); 307 EXPECT_THAT(service()->GetCategories(), IsEmpty());
315 EXPECT_THAT(service()->GetSuggestionsForCategory(articles_category), 308 EXPECT_THAT(service()->GetSuggestionsForCategory(articles_category),
316 IsEmpty()); 309 IsEmpty());
317 } 310 }
318 311
319 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) { 312 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) {
320 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 313 Category articles_category =
314 Category::FromKnownCategory(KnownCategories::ARTICLES);
321 Category offline_pages_category = 315 Category offline_pages_category =
322 FromKnownCategory(KnownCategories::DOWNLOADS); 316 Category::FromKnownCategory(KnownCategories::DOWNLOADS);
323 MockProvider* provider1 = RegisterProvider(articles_category); 317 MockProvider* provider1 = RegisterProvider(articles_category);
324 MockProvider* provider2 = RegisterProvider(offline_pages_category); 318 MockProvider* provider2 = RegisterProvider(offline_pages_category);
325 319
326 provider1->FireSuggestionsChanged(articles_category, 320 provider1->FireSuggestionsChanged(articles_category,
327 CreateSuggestions(articles_category, {1})); 321 CreateSuggestions(articles_category, {1}));
328 ContentSuggestion::ID suggestion_id(articles_category, "1"); 322 ContentSuggestion::ID suggestion_id(articles_category, "1");
329 323
330 EXPECT_CALL(*provider1, FetchSuggestionImage(suggestion_id, _)); 324 EXPECT_CALL(*provider1, FetchSuggestionImage(suggestion_id, _));
331 EXPECT_CALL(*provider2, FetchSuggestionImage(_, _)).Times(0); 325 EXPECT_CALL(*provider2, FetchSuggestionImage(_, _)).Times(0);
332 service()->FetchSuggestionImage( 326 service()->FetchSuggestionImage(
333 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, 327 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched,
334 base::Unretained(this))); 328 base::Unretained(this)));
335 } 329 }
336 330
337 TEST_F(ContentSuggestionsServiceTest, 331 TEST_F(ContentSuggestionsServiceTest,
338 ShouldCallbackEmptyImageForUnavailableProvider) { 332 ShouldCallbackEmptyImageForUnavailableProvider) {
339 // Setup the current thread's MessageLoop. 333 // Setup the current thread's MessageLoop.
340 base::MessageLoop message_loop; 334 base::MessageLoop message_loop;
341 335
342 base::RunLoop run_loop; 336 base::RunLoop run_loop;
343 // Assuming there will never be a category with the id below. 337 // Assuming there will never be a category with the id below.
344 ContentSuggestion::ID suggestion_id(category_factory()->FromIDValue(21563), 338 ContentSuggestion::ID suggestion_id(Category::FromIDValue(21563), "TestID");
345 "TestID");
346 EXPECT_CALL(*this, OnImageFetched(Property(&gfx::Image::IsEmpty, Eq(true)))) 339 EXPECT_CALL(*this, OnImageFetched(Property(&gfx::Image::IsEmpty, Eq(true))))
347 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); 340 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
348 service()->FetchSuggestionImage( 341 service()->FetchSuggestionImage(
349 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, 342 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched,
350 base::Unretained(this))); 343 base::Unretained(this)));
351 run_loop.Run(); 344 run_loop.Run();
352 } 345 }
353 346
354 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectDismissSuggestion) { 347 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectDismissSuggestion) {
355 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 348 Category articles_category =
349 Category::FromKnownCategory(KnownCategories::ARTICLES);
356 Category offline_pages_category = 350 Category offline_pages_category =
357 FromKnownCategory(KnownCategories::DOWNLOADS); 351 Category::FromKnownCategory(KnownCategories::DOWNLOADS);
358 MockProvider* provider1 = RegisterProvider(articles_category); 352 MockProvider* provider1 = RegisterProvider(articles_category);
359 MockProvider* provider2 = RegisterProvider(offline_pages_category); 353 MockProvider* provider2 = RegisterProvider(offline_pages_category);
360 354
361 provider2->FireSuggestionsChanged( 355 provider2->FireSuggestionsChanged(
362 offline_pages_category, CreateSuggestions(offline_pages_category, {11})); 356 offline_pages_category, CreateSuggestions(offline_pages_category, {11}));
363 ContentSuggestion::ID suggestion_id(offline_pages_category, "11"); 357 ContentSuggestion::ID suggestion_id(offline_pages_category, "11");
364 358
365 EXPECT_CALL(*provider1, DismissSuggestion(_)).Times(0); 359 EXPECT_CALL(*provider1, DismissSuggestion(_)).Times(0);
366 EXPECT_CALL(*provider2, DismissSuggestion(suggestion_id)); 360 EXPECT_CALL(*provider2, DismissSuggestion(suggestion_id));
367 service()->DismissSuggestion(suggestion_id); 361 service()->DismissSuggestion(suggestion_id);
368 } 362 }
369 363
370 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectSuggestionInvalidated) { 364 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectSuggestionInvalidated) {
371 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 365 Category articles_category =
366 Category::FromKnownCategory(KnownCategories::ARTICLES);
372 367
373 MockProvider* provider = RegisterProvider(articles_category); 368 MockProvider* provider = RegisterProvider(articles_category);
374 MockServiceObserver observer; 369 MockServiceObserver observer;
375 service()->AddObserver(&observer); 370 service()->AddObserver(&observer);
376 371
377 provider->FireSuggestionsChanged( 372 provider->FireSuggestionsChanged(
378 articles_category, CreateSuggestions(articles_category, {11, 12, 13})); 373 articles_category, CreateSuggestions(articles_category, {11, 12, 13}));
379 ExpectThatSuggestionsAre(articles_category, {11, 12, 13}); 374 ExpectThatSuggestionsAre(articles_category, {11, 12, 13});
380 375
381 ContentSuggestion::ID suggestion_id(articles_category, "12"); 376 ContentSuggestion::ID suggestion_id(articles_category, "12");
382 EXPECT_CALL(observer, OnSuggestionInvalidated(suggestion_id)); 377 EXPECT_CALL(observer, OnSuggestionInvalidated(suggestion_id));
383 provider->FireSuggestionInvalidated(suggestion_id); 378 provider->FireSuggestionInvalidated(suggestion_id);
384 ExpectThatSuggestionsAre(articles_category, {11, 13}); 379 ExpectThatSuggestionsAre(articles_category, {11, 13});
385 Mock::VerifyAndClearExpectations(&observer); 380 Mock::VerifyAndClearExpectations(&observer);
386 381
387 // Unknown IDs must be forwarded (though no change happens to the service's 382 // Unknown IDs must be forwarded (though no change happens to the service's
388 // internal data structures) because previously opened UIs, which can still 383 // internal data structures) because previously opened UIs, which can still
389 // show the invalidated suggestion, must be notified. 384 // show the invalidated suggestion, must be notified.
390 ContentSuggestion::ID unknown_id(articles_category, "1234"); 385 ContentSuggestion::ID unknown_id(articles_category, "1234");
391 EXPECT_CALL(observer, OnSuggestionInvalidated(unknown_id)); 386 EXPECT_CALL(observer, OnSuggestionInvalidated(unknown_id));
392 provider->FireSuggestionInvalidated(unknown_id); 387 provider->FireSuggestionInvalidated(unknown_id);
393 ExpectThatSuggestionsAre(articles_category, {11, 13}); 388 ExpectThatSuggestionsAre(articles_category, {11, 13});
394 Mock::VerifyAndClearExpectations(&observer); 389 Mock::VerifyAndClearExpectations(&observer);
395 390
396 service()->RemoveObserver(&observer); 391 service()->RemoveObserver(&observer);
397 } 392 }
398 393
399 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { 394 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) {
400 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 395 Category articles_category =
396 Category::FromKnownCategory(KnownCategories::ARTICLES);
401 Category offline_pages_category = 397 Category offline_pages_category =
402 FromKnownCategory(KnownCategories::DOWNLOADS); 398 Category::FromKnownCategory(KnownCategories::DOWNLOADS);
403 399
404 // Create and register providers 400 // Create and register providers
405 MockProvider* provider1 = RegisterProvider(articles_category); 401 MockProvider* provider1 = RegisterProvider(articles_category);
406 provider1->FireCategoryStatusChangedWithCurrentStatus(articles_category); 402 provider1->FireCategoryStatusChangedWithCurrentStatus(articles_category);
407 MockProvider* provider2 = RegisterProvider(offline_pages_category); 403 MockProvider* provider2 = RegisterProvider(offline_pages_category);
408 provider2->FireCategoryStatusChangedWithCurrentStatus(offline_pages_category); 404 provider2->FireCategoryStatusChangedWithCurrentStatus(offline_pages_category);
409 ASSERT_THAT(providers().count(articles_category), Eq(1ul)); 405 ASSERT_THAT(providers().count(articles_category), Eq(1ul));
410 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); 406 EXPECT_THAT(providers().at(articles_category), Eq(provider1));
411 ASSERT_THAT(providers().count(offline_pages_category), Eq(1ul)); 407 ASSERT_THAT(providers().count(offline_pages_category), Eq(1ul));
412 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); 408 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 458
463 // Shutdown the service 459 // Shutdown the service
464 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); 460 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown());
465 service()->Shutdown(); 461 service()->Shutdown();
466 service()->RemoveObserver(&observer); 462 service()->RemoveObserver(&observer);
467 // The service will receive two Shutdown() calls. 463 // The service will receive two Shutdown() calls.
468 } 464 }
469 465
470 TEST_F(ContentSuggestionsServiceTest, 466 TEST_F(ContentSuggestionsServiceTest,
471 ShouldNotReturnCategoryInfoForNonexistentCategory) { 467 ShouldNotReturnCategoryInfoForNonexistentCategory) {
472 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); 468 Category category = Category::FromKnownCategory(KnownCategories::DOWNLOADS);
473 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category); 469 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category);
474 EXPECT_FALSE(result.has_value()); 470 EXPECT_FALSE(result.has_value());
475 } 471 }
476 472
477 TEST_F(ContentSuggestionsServiceTest, ShouldReturnCategoryInfo) { 473 TEST_F(ContentSuggestionsServiceTest, ShouldReturnCategoryInfo) {
478 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); 474 Category category = Category::FromKnownCategory(KnownCategories::DOWNLOADS);
479 MockProvider* provider = RegisterProvider(category); 475 MockProvider* provider = RegisterProvider(category);
480 provider->FireCategoryStatusChangedWithCurrentStatus(category); 476 provider->FireCategoryStatusChangedWithCurrentStatus(category);
481 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category); 477 base::Optional<CategoryInfo> result = service()->GetCategoryInfo(category);
482 ASSERT_TRUE(result.has_value()); 478 ASSERT_TRUE(result.has_value());
483 CategoryInfo expected = provider->GetCategoryInfo(category); 479 CategoryInfo expected = provider->GetCategoryInfo(category);
484 const CategoryInfo& actual = result.value(); 480 const CategoryInfo& actual = result.value();
485 EXPECT_THAT(expected.title(), Eq(actual.title())); 481 EXPECT_THAT(expected.title(), Eq(actual.title()));
486 EXPECT_THAT(expected.card_layout(), Eq(actual.card_layout())); 482 EXPECT_THAT(expected.card_layout(), Eq(actual.card_layout()));
487 EXPECT_THAT(expected.has_more_action(), Eq(actual.has_more_action())); 483 EXPECT_THAT(expected.has_more_action(), Eq(actual.has_more_action()));
488 EXPECT_THAT(expected.has_reload_action(), Eq(actual.has_reload_action())); 484 EXPECT_THAT(expected.has_reload_action(), Eq(actual.has_reload_action()));
489 EXPECT_THAT(expected.has_view_all_action(), Eq(actual.has_view_all_action())); 485 EXPECT_THAT(expected.has_view_all_action(), Eq(actual.has_view_all_action()));
490 } 486 }
491 487
492 TEST_F(ContentSuggestionsServiceTest, 488 TEST_F(ContentSuggestionsServiceTest,
493 ShouldRegisterNewCategoryOnNewSuggestions) { 489 ShouldRegisterNewCategoryOnNewSuggestions) {
494 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); 490 Category category = Category::FromKnownCategory(KnownCategories::DOWNLOADS);
495 MockProvider* provider = RegisterProvider(category); 491 MockProvider* provider = RegisterProvider(category);
496 provider->FireCategoryStatusChangedWithCurrentStatus(category); 492 provider->FireCategoryStatusChangedWithCurrentStatus(category);
497 MockServiceObserver observer; 493 MockServiceObserver observer;
498 service()->AddObserver(&observer); 494 service()->AddObserver(&observer);
499 495
500 // Provider starts providing |new_category| without calling 496 // Provider starts providing |new_category| without calling
501 // |OnCategoryStatusChanged|. This is supported for now until further 497 // |OnCategoryStatusChanged|. This is supported for now until further
502 // reconsideration. 498 // reconsideration.
503 Category new_category = FromKnownCategory(KnownCategories::ARTICLES); 499 Category new_category =
500 Category::FromKnownCategory(KnownCategories::ARTICLES);
504 provider->SetProvidedCategories( 501 provider->SetProvidedCategories(
505 std::vector<Category>({category, new_category})); 502 std::vector<Category>({category, new_category}));
506 503
507 EXPECT_CALL(observer, OnNewSuggestions(new_category)); 504 EXPECT_CALL(observer, OnNewSuggestions(new_category));
508 EXPECT_CALL(observer, 505 EXPECT_CALL(observer,
509 OnCategoryStatusChanged(new_category, CategoryStatus::AVAILABLE)); 506 OnCategoryStatusChanged(new_category, CategoryStatus::AVAILABLE));
510 provider->FireSuggestionsChanged(new_category, 507 provider->FireSuggestionsChanged(new_category,
511 CreateSuggestions(new_category, {1, 2})); 508 CreateSuggestions(new_category, {1, 2}));
512 509
513 ExpectThatSuggestionsAre(new_category, {1, 2}); 510 ExpectThatSuggestionsAre(new_category, {1, 2});
514 ASSERT_THAT(providers().count(category), Eq(1ul)); 511 ASSERT_THAT(providers().count(category), Eq(1ul));
515 EXPECT_THAT(providers().at(category), Eq(provider)); 512 EXPECT_THAT(providers().at(category), Eq(provider));
516 EXPECT_THAT(service()->GetCategoryStatus(category), 513 EXPECT_THAT(service()->GetCategoryStatus(category),
517 Eq(CategoryStatus::AVAILABLE)); 514 Eq(CategoryStatus::AVAILABLE));
518 ASSERT_THAT(providers().count(new_category), Eq(1ul)); 515 ASSERT_THAT(providers().count(new_category), Eq(1ul));
519 EXPECT_THAT(providers().at(new_category), Eq(provider)); 516 EXPECT_THAT(providers().at(new_category), Eq(provider));
520 EXPECT_THAT(service()->GetCategoryStatus(new_category), 517 EXPECT_THAT(service()->GetCategoryStatus(new_category),
521 Eq(CategoryStatus::AVAILABLE)); 518 Eq(CategoryStatus::AVAILABLE));
522 519
523 service()->RemoveObserver(&observer); 520 service()->RemoveObserver(&observer);
524 } 521 }
525 522
526 TEST_F(ContentSuggestionsServiceTest, 523 TEST_F(ContentSuggestionsServiceTest,
527 ShouldRegisterNewCategoryOnCategoryStatusChanged) { 524 ShouldRegisterNewCategoryOnCategoryStatusChanged) {
528 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); 525 Category category = Category::FromKnownCategory(KnownCategories::DOWNLOADS);
529 MockProvider* provider = RegisterProvider(category); 526 MockProvider* provider = RegisterProvider(category);
530 provider->FireCategoryStatusChangedWithCurrentStatus(category); 527 provider->FireCategoryStatusChangedWithCurrentStatus(category);
531 MockServiceObserver observer; 528 MockServiceObserver observer;
532 service()->AddObserver(&observer); 529 service()->AddObserver(&observer);
533 530
534 // Provider starts providing |new_category| and calls 531 // Provider starts providing |new_category| and calls
535 // |OnCategoryStatusChanged|, but the category is not yet available. 532 // |OnCategoryStatusChanged|, but the category is not yet available.
536 Category new_category = FromKnownCategory(KnownCategories::ARTICLES); 533 Category new_category =
534 Category::FromKnownCategory(KnownCategories::ARTICLES);
537 provider->SetProvidedCategories( 535 provider->SetProvidedCategories(
538 std::vector<Category>({category, new_category})); 536 std::vector<Category>({category, new_category}));
539 EXPECT_CALL(observer, OnCategoryStatusChanged(new_category, 537 EXPECT_CALL(observer, OnCategoryStatusChanged(new_category,
540 CategoryStatus::INITIALIZING)); 538 CategoryStatus::INITIALIZING));
541 provider->FireCategoryStatusChanged(new_category, 539 provider->FireCategoryStatusChanged(new_category,
542 CategoryStatus::INITIALIZING); 540 CategoryStatus::INITIALIZING);
543 541
544 ASSERT_THAT(providers().count(new_category), Eq(1ul)); 542 ASSERT_THAT(providers().count(new_category), Eq(1ul));
545 EXPECT_THAT(providers().at(new_category), Eq(provider)); 543 EXPECT_THAT(providers().at(new_category), Eq(provider));
546 ExpectThatSuggestionsAre(new_category, std::vector<int>()); 544 ExpectThatSuggestionsAre(new_category, std::vector<int>());
547 EXPECT_THAT(service()->GetCategoryStatus(new_category), 545 EXPECT_THAT(service()->GetCategoryStatus(new_category),
548 Eq(CategoryStatus::INITIALIZING)); 546 Eq(CategoryStatus::INITIALIZING));
549 EXPECT_THAT(service()->GetCategories(), 547 EXPECT_THAT(service()->GetCategories(),
550 Eq(std::vector<Category>({category, new_category}))); 548 Eq(std::vector<Category>({category, new_category})));
551 549
552 service()->RemoveObserver(&observer); 550 service()->RemoveObserver(&observer);
553 } 551 }
554 552
555 TEST_F(ContentSuggestionsServiceTest, ShouldRemoveCategoryWhenNotProvided) { 553 TEST_F(ContentSuggestionsServiceTest, ShouldRemoveCategoryWhenNotProvided) {
556 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); 554 Category category = Category::FromKnownCategory(KnownCategories::DOWNLOADS);
557 MockProvider* provider = RegisterProvider(category); 555 MockProvider* provider = RegisterProvider(category);
558 MockServiceObserver observer; 556 MockServiceObserver observer;
559 service()->AddObserver(&observer); 557 service()->AddObserver(&observer);
560 558
561 provider->FireSuggestionsChanged(category, 559 provider->FireSuggestionsChanged(category,
562 CreateSuggestions(category, {1, 2})); 560 CreateSuggestions(category, {1, 2}));
563 ExpectThatSuggestionsAre(category, {1, 2}); 561 ExpectThatSuggestionsAre(category, {1, 2});
564 562
565 EXPECT_CALL(observer, 563 EXPECT_CALL(observer,
566 OnCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED)); 564 OnCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED));
567 provider->FireCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED); 565 provider->FireCategoryStatusChanged(category, CategoryStatus::NOT_PROVIDED);
568 566
569 EXPECT_THAT(service()->GetCategoryStatus(category), 567 EXPECT_THAT(service()->GetCategoryStatus(category),
570 Eq(CategoryStatus::NOT_PROVIDED)); 568 Eq(CategoryStatus::NOT_PROVIDED));
571 EXPECT_TRUE(service()->GetCategories().empty()); 569 EXPECT_TRUE(service()->GetCategories().empty());
572 ExpectThatSuggestionsAre(category, std::vector<int>()); 570 ExpectThatSuggestionsAre(category, std::vector<int>());
573 571
574 service()->RemoveObserver(&observer); 572 service()->RemoveObserver(&observer);
575 } 573 }
576 574
577 TEST_F(ContentSuggestionsServiceTest, ShouldForwardClearHistory) { 575 TEST_F(ContentSuggestionsServiceTest, ShouldForwardClearHistory) {
578 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); 576 Category category = Category::FromKnownCategory(KnownCategories::DOWNLOADS);
579 MockProvider* provider = RegisterProvider(category); 577 MockProvider* provider = RegisterProvider(category);
580 base::Time begin = base::Time::FromTimeT(123), 578 base::Time begin = base::Time::FromTimeT(123),
581 end = base::Time::FromTimeT(456); 579 end = base::Time::FromTimeT(456);
582 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); 580 EXPECT_CALL(*provider, ClearHistory(begin, end, _));
583 base::Callback<bool(const GURL& url)> filter; 581 base::Callback<bool(const GURL& url)> filter;
584 service()->ClearHistory(begin, end, filter); 582 service()->ClearHistory(begin, end, filter);
585 } 583 }
586 584
587 TEST_F(ContentSuggestionsServiceTest, ShouldForwardFetch) { 585 TEST_F(ContentSuggestionsServiceTest, ShouldForwardFetch) {
588 Category category = FromKnownCategory(KnownCategories::ARTICLES); 586 Category category = Category::FromKnownCategory(KnownCategories::ARTICLES);
589 std::set<std::string> known_suggestions; 587 std::set<std::string> known_suggestions;
590 MockProvider* provider = RegisterProvider(category); 588 MockProvider* provider = RegisterProvider(category);
591 provider->FireCategoryStatusChangedWithCurrentStatus(category); 589 provider->FireCategoryStatusChangedWithCurrentStatus(category);
592 EXPECT_CALL(*provider, Fetch(category, known_suggestions, _)); 590 EXPECT_CALL(*provider, Fetch(category, known_suggestions, _));
593 service()->Fetch(category, known_suggestions, FetchDoneCallback()); 591 service()->Fetch(category, known_suggestions, FetchDoneCallback());
594 } 592 }
595 593
596 TEST_F(ContentSuggestionsServiceTest, DismissAndRestoreCategory) { 594 TEST_F(ContentSuggestionsServiceTest, DismissAndRestoreCategory) {
597 // Register a category with one suggestion. 595 // Register a category with one suggestion.
598 Category category = FromKnownCategory(KnownCategories::ARTICLES); 596 Category category = Category::FromKnownCategory(KnownCategories::ARTICLES);
599 MockProvider* provider = RegisterProvider(category); 597 MockProvider* provider = RegisterProvider(category);
600 provider->FireCategoryStatusChangedWithCurrentStatus(category); 598 provider->FireCategoryStatusChangedWithCurrentStatus(category);
601 provider->FireSuggestionsChanged(category, CreateSuggestions(category, {42})); 599 provider->FireSuggestionsChanged(category, CreateSuggestions(category, {42}));
602 600
603 EXPECT_THAT(service()->GetCategories(), ElementsAre(category)); 601 EXPECT_THAT(service()->GetCategories(), ElementsAre(category));
604 EXPECT_THAT(service()->GetCategoryStatus(category), 602 EXPECT_THAT(service()->GetCategoryStatus(category),
605 Eq(CategoryStatus::AVAILABLE)); 603 Eq(CategoryStatus::AVAILABLE));
606 ExpectThatSuggestionsAre(category, {42}); 604 ExpectThatSuggestionsAre(category, {42});
607 EXPECT_THAT(providers().count(category), Eq(1ul)); 605 EXPECT_THAT(providers().count(category), Eq(1ul));
608 EXPECT_THAT(dismissed_providers(), IsEmpty()); 606 EXPECT_THAT(dismissed_providers(), IsEmpty());
(...skipping 15 matching lines...) Expand all
624 EXPECT_THAT(service()->GetCategories(), ElementsAre(category)); 622 EXPECT_THAT(service()->GetCategories(), ElementsAre(category));
625 EXPECT_THAT(service()->GetCategoryStatus(category), 623 EXPECT_THAT(service()->GetCategoryStatus(category),
626 Eq(CategoryStatus::AVAILABLE)); 624 Eq(CategoryStatus::AVAILABLE));
627 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty()); 625 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty());
628 EXPECT_THAT(providers().count(category), Eq(1ul)); 626 EXPECT_THAT(providers().count(category), Eq(1ul));
629 EXPECT_THAT(dismissed_providers(), IsEmpty()); 627 EXPECT_THAT(dismissed_providers(), IsEmpty());
630 } 628 }
631 629
632 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissedCategories) { 630 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissedCategories) {
633 // Create and register provider. 631 // Create and register provider.
634 Category category1 = service()->category_factory()->FromIDValue(1); 632 Category category1 = Category::FromIDValue(1);
635 Category category2 = service()->category_factory()->FromIDValue(2); 633 Category category2 = Category::FromIDValue(2);
636 634
637 // Setup and verify initial state. 635 // Setup and verify initial state.
638 MockProvider* provider = RegisterProvider({category1, category2}); 636 MockProvider* provider = RegisterProvider({category1, category2});
639 provider->FireCategoryStatusChangedWithCurrentStatus(category1); 637 provider->FireCategoryStatusChangedWithCurrentStatus(category1);
640 provider->FireCategoryStatusChangedWithCurrentStatus(category2); 638 provider->FireCategoryStatusChangedWithCurrentStatus(category2);
641 639
642 ASSERT_THAT(service()->GetCategoryStatus(category1), 640 ASSERT_THAT(service()->GetCategoryStatus(category1),
643 Eq(CategoryStatus::AVAILABLE)); 641 Eq(CategoryStatus::AVAILABLE));
644 ASSERT_THAT(service()->GetCategoryStatus(category2), 642 ASSERT_THAT(service()->GetCategoryStatus(category2),
645 Eq(CategoryStatus::AVAILABLE)); 643 Eq(CategoryStatus::AVAILABLE));
(...skipping 28 matching lines...) Expand all
674 CreateSuggestions(category1, {1, 2})); 672 CreateSuggestions(category1, {1, 2}));
675 673
676 EXPECT_THAT(service()->GetCategoryStatus(category1), 674 EXPECT_THAT(service()->GetCategoryStatus(category1),
677 Eq(CategoryStatus::AVAILABLE)); 675 Eq(CategoryStatus::AVAILABLE));
678 EXPECT_THAT(service()->GetCategoryStatus(category2), 676 EXPECT_THAT(service()->GetCategoryStatus(category2),
679 Eq(CategoryStatus::NOT_PROVIDED)); 677 Eq(CategoryStatus::NOT_PROVIDED));
680 } 678 }
681 679
682 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissalsFromPrefs) { 680 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissalsFromPrefs) {
683 // Register a category with one suggestion. 681 // Register a category with one suggestion.
684 Category category = FromKnownCategory(KnownCategories::ARTICLES); 682 Category category = Category::FromKnownCategory(KnownCategories::ARTICLES);
685 MockProvider* provider = RegisterProvider(category); 683 MockProvider* provider = RegisterProvider(category);
686 provider->FireCategoryStatusChangedWithCurrentStatus(category); 684 provider->FireCategoryStatusChangedWithCurrentStatus(category);
687 685
688 // For a regular initialisation, the category is not dismissed. 686 // For a regular initialisation, the category is not dismissed.
689 ASSERT_FALSE(service()->IsCategoryDismissed(category)); 687 ASSERT_FALSE(service()->IsCategoryDismissed(category));
690 688
691 // Dismiss the category. 689 // Dismiss the category.
692 service()->DismissCategory(category); 690 service()->DismissCategory(category);
693 ASSERT_TRUE(service()->IsCategoryDismissed(category)); 691 ASSERT_TRUE(service()->IsCategoryDismissed(category));
694 692
695 // Simulate a Chrome restart. The category should still be dismissed. 693 // Simulate a Chrome restart. The category should still be dismissed.
696 ResetService(); 694 ResetService();
697 EXPECT_TRUE(service()->IsCategoryDismissed(category)); 695 EXPECT_TRUE(service()->IsCategoryDismissed(category));
698 696
699 // Ensure that the provider registered at initialisation is used after 697 // Ensure that the provider registered at initialisation is used after
700 // restoration. 698 // restoration.
701 provider = RegisterProvider(category); 699 provider = RegisterProvider(category);
702 provider->FireCategoryStatusChangedWithCurrentStatus(category); 700 provider->FireCategoryStatusChangedWithCurrentStatus(category);
703 EXPECT_TRUE(service()->IsCategoryDismissed(category)); 701 EXPECT_TRUE(service()->IsCategoryDismissed(category));
704 702
705 service()->RestoreDismissedCategories(); 703 service()->RestoreDismissedCategories();
706 EXPECT_FALSE(service()->IsCategoryDismissed(category)); 704 EXPECT_FALSE(service()->IsCategoryDismissed(category));
707 EXPECT_THAT(providers().find(category)->second, Eq(provider)); 705 EXPECT_THAT(providers().find(category)->second, Eq(provider));
708 } 706 }
709 707
710 } // namespace ntp_snippets 708 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698