Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/remote/remote_suggestions_provider.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.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/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 28 #include "components/image_fetcher/image_fetcher_delegate.h" | 28 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 29 #include "components/ntp_snippets/category.h" | 29 #include "components/ntp_snippets/category.h" |
| 30 #include "components/ntp_snippets/category_info.h" | 30 #include "components/ntp_snippets/category_info.h" |
| 31 #include "components/ntp_snippets/category_rankers/category_ranker.h" | 31 #include "components/ntp_snippets/category_rankers/category_ranker.h" |
| 32 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" | 32 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" |
| 33 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" | 33 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" |
| 34 #include "components/ntp_snippets/ntp_snippets_constants.h" | 34 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 35 #include "components/ntp_snippets/pref_names.h" | 35 #include "components/ntp_snippets/pref_names.h" |
| 36 #include "components/ntp_snippets/remote/ntp_snippet.h" | 36 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 37 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 37 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 38 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | 38 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
| 39 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 39 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
| 40 #include "components/ntp_snippets/remote/test_utils.h" | 40 #include "components/ntp_snippets/remote/test_utils.h" |
| 41 #include "components/ntp_snippets/user_classifier.h" | 41 #include "components/ntp_snippets/user_classifier.h" |
| 42 #include "components/prefs/testing_pref_service.h" | 42 #include "components/prefs/testing_pref_service.h" |
| 43 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 43 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 44 #include "components/signin/core/browser/fake_signin_manager.h" | 44 #include "components/signin/core/browser/fake_signin_manager.h" |
| 45 #include "components/variations/variations_params_manager.h" | 45 #include "components/variations/variations_params_manager.h" |
| 46 #include "net/url_request/test_url_fetcher_factory.h" | 46 #include "net/url_request/test_url_fetcher_factory.h" |
| 47 #include "net/url_request/url_request_test_util.h" | 47 #include "net/url_request/url_request_test_util.h" |
| 48 #include "testing/gmock/include/gmock/gmock.h" | 48 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 | 282 |
| 283 void ServeOneByOneImage( | 283 void ServeOneByOneImage( |
| 284 image_fetcher::ImageFetcherDelegate* notify, | 284 image_fetcher::ImageFetcherDelegate* notify, |
| 285 const std::string& id, | 285 const std::string& id, |
| 286 base::Callback<void(const std::string&, const gfx::Image&)> callback) { | 286 base::Callback<void(const std::string&, const gfx::Image&)> callback) { |
| 287 base::ThreadTaskRunnerHandle::Get()->PostTask( | 287 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 288 FROM_HERE, base::Bind(callback, id, gfx::test::CreateImage(1, 1))); | 288 FROM_HERE, base::Bind(callback, id, gfx::test::CreateImage(1, 1))); |
| 289 notify->OnImageDataFetched(id, "1-by-1-image-data"); | 289 notify->OnImageDataFetched(id, "1-by-1-image-data"); |
| 290 } | 290 } |
| 291 | 291 |
| 292 gfx::Image FetchImage(RemoteSuggestionsProvider* service, | 292 gfx::Image FetchImage(RemoteSuggestionsProviderImpl* service, |
| 293 const ContentSuggestion::ID& suggestion_id) { | 293 const ContentSuggestion::ID& suggestion_id) { |
| 294 gfx::Image result; | 294 gfx::Image result; |
| 295 base::RunLoop run_loop; | 295 base::RunLoop run_loop; |
| 296 service->FetchSuggestionImage(suggestion_id, | 296 service->FetchSuggestionImage(suggestion_id, |
| 297 base::Bind( | 297 base::Bind( |
| 298 [](base::Closure signal, gfx::Image* output, | 298 [](base::Closure signal, gfx::Image* output, |
| 299 const gfx::Image& loaded) { | 299 const gfx::Image& loaded) { |
| 300 *output = loaded; | 300 *output = loaded; |
| 301 signal.Run(); | 301 signal.Run(); |
| 302 }, | 302 }, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 325 int id, | 325 int id, |
| 326 const GURL& url, | 326 const GURL& url, |
| 327 net::URLFetcher::RequestType request_type, | 327 net::URLFetcher::RequestType request_type, |
| 328 net::URLFetcherDelegate* d) override { | 328 net::URLFetcherDelegate* d) override { |
| 329 return base::MakeUnique<net::FakeURLFetcher>( | 329 return base::MakeUnique<net::FakeURLFetcher>( |
| 330 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 330 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
| 331 net::URLRequestStatus::FAILED); | 331 net::URLRequestStatus::FAILED); |
| 332 } | 332 } |
| 333 }; | 333 }; |
| 334 | 334 |
| 335 class MockScheduler : public NTPSnippetsScheduler { | |
| 336 public: | |
| 337 MOCK_METHOD2(Schedule, | |
| 338 bool(base::TimeDelta period_wifi, | |
| 339 base::TimeDelta period_fallback)); | |
| 340 MOCK_METHOD0(Unschedule, bool()); | |
| 341 }; | |
| 342 | |
| 343 class MockImageFetcher : public ImageFetcher { | 335 class MockImageFetcher : public ImageFetcher { |
| 344 public: | 336 public: |
| 345 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | 337 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
| 346 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); | 338 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); |
| 347 MOCK_METHOD3( | 339 MOCK_METHOD3( |
| 348 StartOrQueueNetworkRequest, | 340 StartOrQueueNetworkRequest, |
| 349 void(const std::string&, | 341 void(const std::string&, |
| 350 const GURL&, | 342 const GURL&, |
| 351 base::Callback<void(const std::string&, const gfx::Image&)>)); | 343 base::Callback<void(const std::string&, const gfx::Image&)>)); |
| 352 }; | 344 }; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 } | 401 } |
| 410 | 402 |
| 411 void SetDecodedImage(const gfx::Image& image) { decoded_image_ = image; } | 403 void SetDecodedImage(const gfx::Image& image) { decoded_image_ = image; } |
| 412 | 404 |
| 413 private: | 405 private: |
| 414 gfx::Image decoded_image_; | 406 gfx::Image decoded_image_; |
| 415 }; | 407 }; |
| 416 | 408 |
| 417 } // namespace | 409 } // namespace |
| 418 | 410 |
| 419 class RemoteSuggestionsProviderTest : public ::testing::Test { | 411 class RemoteSuggestionsProviderImplTest : public ::testing::Test { |
| 420 public: | 412 public: |
| 421 RemoteSuggestionsProviderTest() | 413 RemoteSuggestionsProviderImplTest() |
| 422 : params_manager_(ntp_snippets::kStudyName, | 414 : params_manager_(ntp_snippets::kStudyName, |
| 423 {{"content_suggestions_backend", | 415 {{"content_suggestions_backend", |
| 424 kTestContentSuggestionsServerEndpoint}, | 416 kTestContentSuggestionsServerEndpoint}, |
| 425 {"fetching_personalization", "non_personal"}}), | 417 {"fetching_personalization", "non_personal"}}), |
| 426 fake_url_fetcher_factory_( | 418 fake_url_fetcher_factory_( |
| 427 /*default_factory=*/&failing_url_fetcher_factory_), | 419 /*default_factory=*/&failing_url_fetcher_factory_), |
| 428 test_url_(kTestContentSuggestionsServerWithAPIKey), | 420 test_url_(kTestContentSuggestionsServerWithAPIKey), |
| 429 category_ranker_(base::MakeUnique<ConstantCategoryRanker>()), | 421 category_ranker_(base::MakeUnique<ConstantCategoryRanker>()), |
| 430 user_classifier_(/*pref_service=*/nullptr), | 422 user_classifier_(/*pref_service=*/nullptr), |
| 431 image_fetcher_(nullptr), | 423 image_fetcher_(nullptr), |
| 432 image_decoder_(nullptr), | 424 image_decoder_(nullptr), |
| 433 database_(nullptr) { | 425 database_(nullptr) { |
| 434 RemoteSuggestionsProvider::RegisterProfilePrefs( | 426 RemoteSuggestionsProviderImpl::RegisterProfilePrefs( |
| 435 utils_.pref_service()->registry()); | 427 utils_.pref_service()->registry()); |
| 436 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 428 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 437 | 429 |
| 438 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | 430 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
| 439 } | 431 } |
| 440 | 432 |
| 441 ~RemoteSuggestionsProviderTest() override { | 433 ~RemoteSuggestionsProviderImplTest() override { |
| 442 // We need to run the message loop after deleting the database, because | 434 // We need to run the message loop after deleting the database, because |
| 443 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task | 435 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task |
| 444 // runner. Without this, we'd get reports of memory leaks. | 436 // runner. Without this, we'd get reports of memory leaks. |
| 445 base::RunLoop().RunUntilIdle(); | 437 base::RunLoop().RunUntilIdle(); |
| 446 } | 438 } |
| 447 | 439 |
| 448 // TODO(vitaliii): Rewrite this function to initialize a test class member | 440 // TODO(vitaliii): Rewrite this function to initialize a test class member |
| 449 // instead of creating a new service. | 441 // instead of creating a new service. |
| 450 std::unique_ptr<RemoteSuggestionsProvider> MakeSnippetsService( | 442 std::unique_ptr<RemoteSuggestionsProviderImpl> MakeSnippetsService( |
| 451 bool set_empty_response = true) { | 443 bool set_empty_response = true) { |
| 452 auto service = MakeSnippetsServiceWithoutInitialization(); | 444 auto service = MakeSnippetsServiceWithoutInitialization(); |
| 453 WaitForSnippetsServiceInitialization(service.get(), set_empty_response); | 445 WaitForSnippetsServiceInitialization(service.get(), set_empty_response); |
| 454 return service; | 446 return service; |
| 455 } | 447 } |
| 456 | 448 |
| 457 std::unique_ptr<RemoteSuggestionsProvider> | 449 std::unique_ptr<RemoteSuggestionsProviderImpl> |
| 458 MakeSnippetsServiceWithoutInitialization() { | 450 MakeSnippetsServiceWithoutInitialization() { |
| 459 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 451 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 460 base::ThreadTaskRunnerHandle::Get()); | 452 base::ThreadTaskRunnerHandle::Get()); |
| 461 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 453 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 462 new net::TestURLRequestContextGetter(task_runner.get()); | 454 new net::TestURLRequestContextGetter(task_runner.get()); |
| 463 | 455 |
| 464 utils_.ResetSigninManager(); | 456 utils_.ResetSigninManager(); |
| 465 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = | 457 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = |
| 466 base::MakeUnique<NTPSnippetsFetcher>( | 458 base::MakeUnique<NTPSnippetsFetcher>( |
| 467 utils_.fake_signin_manager(), fake_token_service_.get(), | 459 utils_.fake_signin_manager(), fake_token_service_.get(), |
| 468 std::move(request_context_getter), utils_.pref_service(), nullptr, | 460 std::move(request_context_getter), utils_.pref_service(), nullptr, |
| 469 base::Bind(&ParseJson), kAPIKey, &user_classifier_); | 461 base::Bind(&ParseJson), kAPIKey, &user_classifier_); |
| 470 | 462 |
| 471 utils_.fake_signin_manager()->SignIn("foo@bar.com"); | 463 utils_.fake_signin_manager()->SignIn("foo@bar.com"); |
| 472 | 464 |
| 473 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); | 465 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); |
| 474 | 466 |
| 475 image_fetcher_ = image_fetcher.get(); | 467 image_fetcher_ = image_fetcher.get(); |
| 476 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); | 468 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); |
| 477 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); | 469 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); |
| 478 image_decoder_ = image_decoder.get(); | 470 image_decoder_ = image_decoder.get(); |
| 479 EXPECT_FALSE(observer_); | 471 EXPECT_FALSE(observer_); |
| 480 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); | 472 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); |
| 481 auto database = base::MakeUnique<RemoteSuggestionsDatabase>( | 473 auto database = base::MakeUnique<RemoteSuggestionsDatabase>( |
| 482 database_dir_.GetPath(), task_runner); | 474 database_dir_.GetPath(), task_runner); |
| 483 database_ = database.get(); | 475 database_ = database.get(); |
| 484 return base::MakeUnique<RemoteSuggestionsProvider>( | 476 return base::MakeUnique<RemoteSuggestionsProviderImpl>( |
| 485 observer_.get(), utils_.pref_service(), "fr", category_ranker_.get(), | 477 observer_.get(), utils_.pref_service(), "fr", category_ranker_.get(), |
| 486 &user_classifier_, &scheduler_, std::move(snippets_fetcher), | 478 &user_classifier_, std::move(snippets_fetcher), |
|
jkrcal
2016/12/20 16:39:47
Also an omission in the rebase.
| |
| 487 std::move(image_fetcher), std::move(image_decoder), std::move(database), | 479 std::move(image_fetcher), std::move(image_decoder), std::move(database), |
| 488 base::MakeUnique<RemoteSuggestionsStatusService>( | 480 base::MakeUnique<RemoteSuggestionsStatusService>( |
| 489 utils_.fake_signin_manager(), utils_.pref_service())); | 481 utils_.fake_signin_manager(), utils_.pref_service())); |
| 490 } | 482 } |
| 491 | 483 |
| 492 void WaitForSnippetsServiceInitialization(RemoteSuggestionsProvider* service, | 484 void WaitForSnippetsServiceInitialization( |
| 493 bool set_empty_response) { | 485 RemoteSuggestionsProviderImpl* service, |
| 494 EXPECT_EQ(RemoteSuggestionsProvider::State::NOT_INITED, service->state_); | 486 bool set_empty_response) { |
| 487 EXPECT_EQ(RemoteSuggestionsProviderImpl::State::NOT_INITED, | |
| 488 service->state_); | |
| 495 | 489 |
| 496 // Add an initial fetch response, as the service tries to fetch when there | 490 // Add an initial fetch response, as the service tries to fetch when there |
| 497 // is nothing in the DB. | 491 // is nothing in the DB. |
| 498 if (set_empty_response) { | 492 if (set_empty_response) { |
| 499 SetUpFetchResponse(GetTestJson(std::vector<std::string>())); | 493 SetUpFetchResponse(GetTestJson(std::vector<std::string>())); |
| 500 } | 494 } |
| 501 | 495 |
| 502 // TODO(treib): Find a better way to wait for initialization to finish. | 496 // TODO(treib): Find a better way to wait for initialization to finish. |
| 503 base::RunLoop().RunUntilIdle(); | 497 base::RunLoop().RunUntilIdle(); |
| 504 EXPECT_NE(RemoteSuggestionsProvider::State::NOT_INITED, service->state_); | 498 EXPECT_NE(RemoteSuggestionsProviderImpl::State::NOT_INITED, |
| 499 service->state_); | |
| 505 } | 500 } |
| 506 | 501 |
| 507 void ResetSnippetsService(std::unique_ptr<RemoteSuggestionsProvider>* service, | 502 void ResetSnippetsService( |
| 508 bool set_empty_response) { | 503 std::unique_ptr<RemoteSuggestionsProviderImpl>* service, |
| 504 bool set_empty_response) { | |
| 509 service->reset(); | 505 service->reset(); |
| 510 observer_.reset(); | 506 observer_.reset(); |
| 511 *service = MakeSnippetsService(set_empty_response); | 507 *service = MakeSnippetsService(set_empty_response); |
| 512 } | 508 } |
| 513 | 509 |
| 514 void SetCategoryRanker(std::unique_ptr<CategoryRanker> category_ranker) { | 510 void SetCategoryRanker(std::unique_ptr<CategoryRanker> category_ranker) { |
| 515 category_ranker_ = std::move(category_ranker); | 511 category_ranker_ = std::move(category_ranker); |
| 516 } | 512 } |
| 517 | 513 |
| 518 ContentSuggestion::ID MakeArticleID(const std::string& id_within_category) { | 514 ContentSuggestion::ID MakeArticleID(const std::string& id_within_category) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 531 // unknown_category() helpers -- tests can just define their own. | 527 // unknown_category() helpers -- tests can just define their own. |
| 532 Category other_category() { return Category::FromRemoteCategory(2); } | 528 Category other_category() { return Category::FromRemoteCategory(2); } |
| 533 | 529 |
| 534 Category unknown_category() { | 530 Category unknown_category() { |
| 535 return Category::FromRemoteCategory(kUnknownRemoteCategoryId); | 531 return Category::FromRemoteCategory(kUnknownRemoteCategoryId); |
| 536 } | 532 } |
| 537 | 533 |
| 538 protected: | 534 protected: |
| 539 const GURL& test_url() { return test_url_; } | 535 const GURL& test_url() { return test_url_; } |
| 540 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } | 536 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } |
| 541 MockScheduler& mock_scheduler() { return scheduler_; } | |
| 542 // TODO(tschumann): Make this a strict-mock. We want to avoid unneccesary | 537 // TODO(tschumann): Make this a strict-mock. We want to avoid unneccesary |
| 543 // network requests. | 538 // network requests. |
| 544 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } | 539 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } |
| 545 FakeImageDecoder* image_decoder() { return image_decoder_; } | 540 FakeImageDecoder* image_decoder() { return image_decoder_; } |
| 546 PrefService* pref_service() { return utils_.pref_service(); } | 541 PrefService* pref_service() { return utils_.pref_service(); } |
| 547 RemoteSuggestionsDatabase* database() { return database_; } | 542 RemoteSuggestionsDatabase* database() { return database_; } |
| 548 | 543 |
| 549 // Provide the json to be returned by the fake fetcher. | 544 // Provide the json to be returned by the fake fetcher. |
| 550 void SetUpFetchResponse(const std::string& json) { | 545 void SetUpFetchResponse(const std::string& json) { |
| 551 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 546 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
| 552 net::URLRequestStatus::SUCCESS); | 547 net::URLRequestStatus::SUCCESS); |
| 553 } | 548 } |
| 554 | 549 |
| 555 // Have the fake fetcher fail due to a HTTP error like a 404. | 550 // Have the fake fetcher fail due to a HTTP error like a 404. |
| 556 void SetUpHttpError() { | 551 void SetUpHttpError() { |
| 557 fake_url_fetcher_factory_.SetFakeResponse(test_url_, /*json=*/std::string(), | 552 fake_url_fetcher_factory_.SetFakeResponse(test_url_, /*json=*/std::string(), |
| 558 net::HTTP_NOT_FOUND, | 553 net::HTTP_NOT_FOUND, |
| 559 net::URLRequestStatus::SUCCESS); | 554 net::URLRequestStatus::SUCCESS); |
| 560 } | 555 } |
| 561 | 556 |
| 562 void LoadFromJSONString(RemoteSuggestionsProvider* service, | 557 void LoadFromJSONString(RemoteSuggestionsProviderImpl* service, |
| 563 const std::string& json) { | 558 const std::string& json) { |
| 564 SetUpFetchResponse(json); | 559 SetUpFetchResponse(json); |
| 565 service->FetchSnippets(true); | 560 service->FetchSnippets(true); |
| 566 base::RunLoop().RunUntilIdle(); | 561 base::RunLoop().RunUntilIdle(); |
| 567 } | 562 } |
| 568 | 563 |
| 569 void LoadMoreFromJSONString(RemoteSuggestionsProvider* service, | 564 void LoadMoreFromJSONString(RemoteSuggestionsProviderImpl* service, |
| 570 const Category& category, | 565 const Category& category, |
| 571 const std::string& json, | 566 const std::string& json, |
| 572 const std::set<std::string>& known_ids, | 567 const std::set<std::string>& known_ids, |
| 573 FetchDoneCallback callback) { | 568 FetchDoneCallback callback) { |
| 574 SetUpFetchResponse(json); | 569 SetUpFetchResponse(json); |
| 575 service->Fetch(category, known_ids, callback); | 570 service->Fetch(category, known_ids, callback); |
| 576 base::RunLoop().RunUntilIdle(); | 571 base::RunLoop().RunUntilIdle(); |
| 577 } | 572 } |
| 578 | 573 |
| 579 private: | 574 private: |
| 580 variations::testing::VariationParamsManager params_manager_; | 575 variations::testing::VariationParamsManager params_manager_; |
| 581 test::RemoteSuggestionsTestUtils utils_; | 576 test::RemoteSuggestionsTestUtils utils_; |
| 582 base::MessageLoop message_loop_; | 577 base::MessageLoop message_loop_; |
| 583 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 578 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 584 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 579 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
| 585 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 580 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
| 586 const GURL test_url_; | 581 const GURL test_url_; |
| 587 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 582 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 588 std::unique_ptr<CategoryRanker> category_ranker_; | 583 std::unique_ptr<CategoryRanker> category_ranker_; |
| 589 UserClassifier user_classifier_; | 584 UserClassifier user_classifier_; |
| 590 NiceMock<MockScheduler> scheduler_; | |
| 591 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; | 585 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; |
| 592 NiceMock<MockImageFetcher>* image_fetcher_; | 586 NiceMock<MockImageFetcher>* image_fetcher_; |
| 593 FakeImageDecoder* image_decoder_; | 587 FakeImageDecoder* image_decoder_; |
| 594 | 588 |
| 595 base::ScopedTempDir database_dir_; | 589 base::ScopedTempDir database_dir_; |
| 596 RemoteSuggestionsDatabase* database_; | 590 RemoteSuggestionsDatabase* database_; |
| 597 | 591 |
| 598 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderTest); | 592 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImplTest); |
| 599 }; | 593 }; |
| 600 | 594 |
| 601 TEST_F(RemoteSuggestionsProviderTest, ScheduleOnStart) { | 595 TEST_F(RemoteSuggestionsProviderImplTest, Full) { |
| 602 // We should get two |Schedule| calls: The first when initialization | |
| 603 // completes, the second one after the automatic (since the service doesn't | |
| 604 // have any data yet) fetch finishes. | |
| 605 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 606 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 607 auto service = MakeSnippetsService(); | |
| 608 | |
| 609 // When we have no snippets are all, loading the service initiates a fetch. | |
| 610 EXPECT_EQ("OK", service->snippets_fetcher()->last_status()); | |
| 611 } | |
| 612 | |
| 613 TEST_F(RemoteSuggestionsProviderTest, DontRescheduleOnStart) { | |
| 614 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 615 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 616 SetUpFetchResponse(GetTestJson({GetSnippet()})); | |
| 617 auto service = MakeSnippetsService(/*set_empty_response=*/false); | |
| 618 | |
| 619 // When recreating the service, we should not get any |Schedule| calls: | |
| 620 // The tasks are already scheduled with the correct intervals, so nothing on | |
| 621 // initialization, and the service has data from the DB, so no automatic fetch | |
| 622 // should happen. | |
| 623 Mock::VerifyAndClearExpectations(&mock_scheduler()); | |
| 624 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0); | |
| 625 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 626 ResetSnippetsService(&service, /*set_empty_response=*/true); | |
| 627 } | |
| 628 | |
| 629 TEST_F(RemoteSuggestionsProviderTest, RescheduleAfterSuccessfulFetch) { | |
| 630 // We should get two |Schedule| calls: The first when initialization | |
| 631 // completes, the second one after the automatic (since the service doesn't | |
| 632 // have any data yet) fetch finishes. | |
| 633 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 634 auto service = MakeSnippetsService(); | |
| 635 | |
| 636 // A successful fetch should trigger another |Schedule|. | |
| 637 EXPECT_CALL(mock_scheduler(), Schedule(_, _)); | |
| 638 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | |
| 639 } | |
| 640 | |
| 641 TEST_F(RemoteSuggestionsProviderTest, DontRescheduleAfterFailedFetch) { | |
| 642 // We should get two |Schedule| calls: The first when initialization | |
| 643 // completes, the second one after the automatic (since the service doesn't | |
| 644 // have any data yet) fetch finishes. | |
| 645 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 646 auto service = MakeSnippetsService(); | |
| 647 | |
| 648 // A failed fetch should NOT trigger another |Schedule|. | |
| 649 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0); | |
| 650 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | |
| 651 } | |
| 652 | |
| 653 TEST_F(RemoteSuggestionsProviderTest, IgnoreRescheduleBeforeInit) { | |
| 654 // We should get two |Schedule| calls: The first when initialization | |
| 655 // completes, the second one after the automatic (since the service doesn't | |
| 656 // have any data yet) fetch finishes. | |
| 657 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 658 // The |RescheduleFetching| call shouldn't do anything (in particular not | |
| 659 // result in an |Unschedule|), since the service isn't initialized yet. | |
| 660 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 661 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 662 service->RescheduleFetching(false); | |
| 663 WaitForSnippetsServiceInitialization(service.get(), | |
| 664 /*set_empty_response=*/true); | |
| 665 } | |
| 666 | |
| 667 TEST_F(RemoteSuggestionsProviderTest, HandleForcedRescheduleBeforeInit) { | |
| 668 { | |
| 669 InSequence s; | |
| 670 // The |RescheduleFetching| call with force=true should result in an | |
| 671 // |Unschedule|, since the service isn't initialized yet. | |
| 672 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); | |
| 673 // We should get two |Schedule| calls: The first when initialization | |
| 674 // completes, the second one after the automatic (since the service doesn't | |
| 675 // have any data yet) fetch finishes. | |
| 676 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 677 } | |
| 678 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 679 service->RescheduleFetching(true); | |
| 680 WaitForSnippetsServiceInitialization(service.get(), | |
| 681 /*set_empty_response=*/true); | |
| 682 } | |
| 683 | |
| 684 TEST_F(RemoteSuggestionsProviderTest, RescheduleOnStateChange) { | |
| 685 { | |
| 686 InSequence s; | |
| 687 // Initial startup. | |
| 688 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 689 // Service gets disabled. | |
| 690 EXPECT_CALL(mock_scheduler(), Unschedule()); | |
| 691 // Service gets enabled again. | |
| 692 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 693 } | |
| 694 auto service = MakeSnippetsService(); | |
| 695 ASSERT_TRUE(service->ready()); | |
| 696 | |
| 697 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, | |
| 698 RemoteSuggestionsStatus::EXPLICITLY_DISABLED); | |
| 699 ASSERT_FALSE(service->ready()); | |
| 700 base::RunLoop().RunUntilIdle(); | |
| 701 | |
| 702 service->OnStatusChanged(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, | |
| 703 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT); | |
| 704 ASSERT_TRUE(service->ready()); | |
| 705 base::RunLoop().RunUntilIdle(); | |
| 706 } | |
| 707 | |
| 708 TEST_F(RemoteSuggestionsProviderTest, DontUnscheduleOnShutdown) { | |
| 709 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | |
| 710 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | |
| 711 | |
| 712 auto service = MakeSnippetsService(); | |
| 713 | |
| 714 service.reset(); | |
| 715 base::RunLoop().RunUntilIdle(); | |
| 716 } | |
| 717 | |
| 718 TEST_F(RemoteSuggestionsProviderTest, Full) { | |
| 719 std::string json_str(GetTestJson({GetSnippet()})); | 596 std::string json_str(GetTestJson({GetSnippet()})); |
| 720 | 597 |
| 721 auto service = MakeSnippetsService(); | 598 auto service = MakeSnippetsService(); |
| 722 | 599 |
| 723 LoadFromJSONString(service.get(), json_str); | 600 LoadFromJSONString(service.get(), json_str); |
| 724 | 601 |
| 725 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | 602 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 726 SizeIs(1)); | 603 SizeIs(1)); |
| 727 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 604 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 728 | 605 |
| 729 const ContentSuggestion& suggestion = | 606 const ContentSuggestion& suggestion = |
| 730 observer().SuggestionsForCategory(articles_category()).front(); | 607 observer().SuggestionsForCategory(articles_category()).front(); |
| 731 | 608 |
| 732 EXPECT_EQ(MakeArticleID(kSnippetUrl), suggestion.id()); | 609 EXPECT_EQ(MakeArticleID(kSnippetUrl), suggestion.id()); |
| 733 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | 610 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); |
| 734 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | 611 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); |
| 735 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | 612 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); |
| 736 EXPECT_EQ(kSnippetPublisherName, | 613 EXPECT_EQ(kSnippetPublisherName, |
| 737 base::UTF16ToUTF8(suggestion.publisher_name())); | 614 base::UTF16ToUTF8(suggestion.publisher_name())); |
| 738 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | 615 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); |
| 739 } | 616 } |
| 740 | 617 |
| 741 TEST_F(RemoteSuggestionsProviderTest, CategoryTitle) { | 618 TEST_F(RemoteSuggestionsProviderImplTest, CategoryTitle) { |
| 742 const base::string16 test_default_title = | 619 const base::string16 test_default_title = |
| 743 base::UTF8ToUTF16(kTestJsonDefaultCategoryTitle); | 620 base::UTF8ToUTF16(kTestJsonDefaultCategoryTitle); |
| 744 | 621 |
| 745 // Don't send an initial response -- we want to test what happens without any | 622 // Don't send an initial response -- we want to test what happens without any |
| 746 // server status. | 623 // server status. |
| 747 auto service = MakeSnippetsService(/*set_empty_response=*/false); | 624 auto service = MakeSnippetsService(/*set_empty_response=*/false); |
| 748 | 625 |
| 749 // The articles category should be there by default, and have a title. | 626 // The articles category should be there by default, and have a title. |
| 750 CategoryInfo info_before = service->GetCategoryInfo(articles_category()); | 627 CategoryInfo info_before = service->GetCategoryInfo(articles_category()); |
| 751 ASSERT_THAT(info_before.title(), Not(IsEmpty())); | 628 ASSERT_THAT(info_before.title(), Not(IsEmpty())); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 766 // Make sure we updated the title in the CategoryInfo. | 643 // Make sure we updated the title in the CategoryInfo. |
| 767 CategoryInfo info_with_title = service->GetCategoryInfo(articles_category()); | 644 CategoryInfo info_with_title = service->GetCategoryInfo(articles_category()); |
| 768 EXPECT_THAT(info_before.title(), Not(Eq(info_with_title.title()))); | 645 EXPECT_THAT(info_before.title(), Not(Eq(info_with_title.title()))); |
| 769 EXPECT_THAT(test_default_title, Eq(info_with_title.title())); | 646 EXPECT_THAT(test_default_title, Eq(info_with_title.title())); |
| 770 EXPECT_THAT(info_before.has_more_action(), Eq(true)); | 647 EXPECT_THAT(info_before.has_more_action(), Eq(true)); |
| 771 EXPECT_THAT(info_before.has_reload_action(), Eq(true)); | 648 EXPECT_THAT(info_before.has_reload_action(), Eq(true)); |
| 772 EXPECT_THAT(info_before.has_view_all_action(), Eq(false)); | 649 EXPECT_THAT(info_before.has_view_all_action(), Eq(false)); |
| 773 EXPECT_THAT(info_before.show_if_empty(), Eq(true)); | 650 EXPECT_THAT(info_before.show_if_empty(), Eq(true)); |
| 774 } | 651 } |
| 775 | 652 |
| 776 TEST_F(RemoteSuggestionsProviderTest, MultipleCategories) { | 653 TEST_F(RemoteSuggestionsProviderImplTest, MultipleCategories) { |
| 777 auto service = MakeSnippetsService(); | 654 auto service = MakeSnippetsService(); |
| 778 std::string json_str = | 655 std::string json_str = |
| 779 MultiCategoryJsonBuilder() | 656 MultiCategoryJsonBuilder() |
| 780 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) | 657 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) |
| 781 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/2) | 658 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/2) |
| 782 .Build(); | 659 .Build(); |
| 783 LoadFromJSONString(service.get(), json_str); | 660 LoadFromJSONString(service.get(), json_str); |
| 784 | 661 |
| 785 ASSERT_THAT(observer().statuses(), | 662 ASSERT_THAT(observer().statuses(), |
| 786 Eq(std::map<Category, CategoryStatus, Category::CompareByID>{ | 663 Eq(std::map<Category, CategoryStatus, Category::CompareByID>{ |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 814 EXPECT_EQ(MakeOtherID(std::string(kSnippetUrl) + "/1"), suggestion.id()); | 691 EXPECT_EQ(MakeOtherID(std::string(kSnippetUrl) + "/1"), suggestion.id()); |
| 815 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | 692 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); |
| 816 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | 693 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); |
| 817 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | 694 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); |
| 818 EXPECT_EQ(kSnippetPublisherName, | 695 EXPECT_EQ(kSnippetPublisherName, |
| 819 base::UTF16ToUTF8(suggestion.publisher_name())); | 696 base::UTF16ToUTF8(suggestion.publisher_name())); |
| 820 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | 697 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); |
| 821 } | 698 } |
| 822 } | 699 } |
| 823 | 700 |
| 824 TEST_F(RemoteSuggestionsProviderTest, ArticleCategoryInfo) { | 701 TEST_F(RemoteSuggestionsProviderImplTest, ArticleCategoryInfo) { |
| 825 auto service = MakeSnippetsService(); | 702 auto service = MakeSnippetsService(); |
| 826 CategoryInfo article_info = service->GetCategoryInfo(articles_category()); | 703 CategoryInfo article_info = service->GetCategoryInfo(articles_category()); |
| 827 EXPECT_THAT(article_info.has_more_action(), Eq(true)); | 704 EXPECT_THAT(article_info.has_more_action(), Eq(true)); |
| 828 EXPECT_THAT(article_info.has_reload_action(), Eq(true)); | 705 EXPECT_THAT(article_info.has_reload_action(), Eq(true)); |
| 829 EXPECT_THAT(article_info.has_view_all_action(), Eq(false)); | 706 EXPECT_THAT(article_info.has_view_all_action(), Eq(false)); |
| 830 EXPECT_THAT(article_info.show_if_empty(), Eq(true)); | 707 EXPECT_THAT(article_info.show_if_empty(), Eq(true)); |
| 831 } | 708 } |
| 832 | 709 |
| 833 TEST_F(RemoteSuggestionsProviderTest, ExperimentalCategoryInfo) { | 710 TEST_F(RemoteSuggestionsProviderImplTest, ExperimentalCategoryInfo) { |
| 834 auto service = MakeSnippetsService(); | 711 auto service = MakeSnippetsService(); |
| 835 std::string json_str = | 712 std::string json_str = |
| 836 MultiCategoryJsonBuilder() | 713 MultiCategoryJsonBuilder() |
| 837 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) | 714 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) |
| 838 .AddCategory({GetSnippetN(1)}, kUnknownRemoteCategoryId) | 715 .AddCategory({GetSnippetN(1)}, kUnknownRemoteCategoryId) |
| 839 .Build(); | 716 .Build(); |
| 840 // Load data with multiple categories so that a new experimental category gets | 717 // Load data with multiple categories so that a new experimental category gets |
| 841 // registered. | 718 // registered. |
| 842 LoadFromJSONString(service.get(), json_str); | 719 LoadFromJSONString(service.get(), json_str); |
| 843 | 720 |
| 844 CategoryInfo info = service->GetCategoryInfo(unknown_category()); | 721 CategoryInfo info = service->GetCategoryInfo(unknown_category()); |
| 845 EXPECT_THAT(info.has_more_action(), Eq(false)); | 722 EXPECT_THAT(info.has_more_action(), Eq(false)); |
| 846 EXPECT_THAT(info.has_reload_action(), Eq(false)); | 723 EXPECT_THAT(info.has_reload_action(), Eq(false)); |
| 847 EXPECT_THAT(info.has_view_all_action(), Eq(false)); | 724 EXPECT_THAT(info.has_view_all_action(), Eq(false)); |
| 848 EXPECT_THAT(info.show_if_empty(), Eq(false)); | 725 EXPECT_THAT(info.show_if_empty(), Eq(false)); |
| 849 } | 726 } |
| 850 | 727 |
| 851 TEST_F(RemoteSuggestionsProviderTest, AddRemoteCategoriesToCategoryRanker) { | 728 TEST_F(RemoteSuggestionsProviderImplTest, AddRemoteCategoriesToCategoryRanker) { |
| 852 auto mock_ranker = base::MakeUnique<MockCategoryRanker>(); | 729 auto mock_ranker = base::MakeUnique<MockCategoryRanker>(); |
| 853 MockCategoryRanker* raw_mock_ranker = mock_ranker.get(); | 730 MockCategoryRanker* raw_mock_ranker = mock_ranker.get(); |
| 854 SetCategoryRanker(std::move(mock_ranker)); | 731 SetCategoryRanker(std::move(mock_ranker)); |
| 855 std::string json_str = | 732 std::string json_str = |
| 856 MultiCategoryJsonBuilder() | 733 MultiCategoryJsonBuilder() |
| 857 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/11) | 734 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/11) |
| 858 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/13) | 735 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/13) |
| 859 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/12) | 736 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/12) |
| 860 .Build(); | 737 .Build(); |
| 861 SetUpFetchResponse(json_str); | 738 SetUpFetchResponse(json_str); |
| 862 { | 739 { |
| 863 // The order of categories is determined by the order in which they are | 740 // The order of categories is determined by the order in which they are |
| 864 // added. Thus, the latter is tested here. | 741 // added. Thus, the latter is tested here. |
| 865 InSequence s; | 742 InSequence s; |
| 866 EXPECT_CALL(*raw_mock_ranker, | 743 EXPECT_CALL(*raw_mock_ranker, |
| 867 AppendCategoryIfNecessary(Category::FromRemoteCategory(11))); | 744 AppendCategoryIfNecessary(Category::FromRemoteCategory(11))); |
| 868 EXPECT_CALL(*raw_mock_ranker, | 745 EXPECT_CALL(*raw_mock_ranker, |
| 869 AppendCategoryIfNecessary(Category::FromRemoteCategory(13))); | 746 AppendCategoryIfNecessary(Category::FromRemoteCategory(13))); |
| 870 EXPECT_CALL(*raw_mock_ranker, | 747 EXPECT_CALL(*raw_mock_ranker, |
| 871 AppendCategoryIfNecessary(Category::FromRemoteCategory(12))); | 748 AppendCategoryIfNecessary(Category::FromRemoteCategory(12))); |
| 872 } | 749 } |
| 873 auto service = MakeSnippetsService(/*set_empty_response=*/false); | 750 auto service = MakeSnippetsService(/*set_empty_response=*/false); |
| 874 } | 751 } |
| 875 | 752 |
| 876 TEST_F(RemoteSuggestionsProviderTest, PersistCategoryInfos) { | 753 TEST_F(RemoteSuggestionsProviderImplTest, PersistCategoryInfos) { |
| 877 auto service = MakeSnippetsService(); | 754 auto service = MakeSnippetsService(); |
| 878 // TODO(vitaliii): Use |articles_category()| instead of constant ID below. | 755 // TODO(vitaliii): Use |articles_category()| instead of constant ID below. |
| 879 std::string json_str = | 756 std::string json_str = |
| 880 MultiCategoryJsonBuilder() | 757 MultiCategoryJsonBuilder() |
| 881 .AddCategoryWithCustomTitle( | 758 .AddCategoryWithCustomTitle( |
| 882 {GetSnippetN(0)}, /*remote_category_id=*/1, "Articles for You") | 759 {GetSnippetN(0)}, /*remote_category_id=*/1, "Articles for You") |
| 883 .AddCategoryWithCustomTitle({GetSnippetN(1)}, | 760 .AddCategoryWithCustomTitle({GetSnippetN(1)}, |
| 884 kUnknownRemoteCategoryId, "Other Things") | 761 kUnknownRemoteCategoryId, "Other Things") |
| 885 .Build(); | 762 .Build(); |
| 886 LoadFromJSONString(service.get(), json_str); | 763 LoadFromJSONString(service.get(), json_str); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 911 | 788 |
| 912 CategoryInfo info_articles_after = | 789 CategoryInfo info_articles_after = |
| 913 service->GetCategoryInfo(articles_category()); | 790 service->GetCategoryInfo(articles_category()); |
| 914 CategoryInfo info_unknown_after = | 791 CategoryInfo info_unknown_after = |
| 915 service->GetCategoryInfo(unknown_category()); | 792 service->GetCategoryInfo(unknown_category()); |
| 916 | 793 |
| 917 EXPECT_EQ(info_articles_before.title(), info_articles_after.title()); | 794 EXPECT_EQ(info_articles_before.title(), info_articles_after.title()); |
| 918 EXPECT_EQ(info_unknown_before.title(), info_unknown_after.title()); | 795 EXPECT_EQ(info_unknown_before.title(), info_unknown_after.title()); |
| 919 } | 796 } |
| 920 | 797 |
| 921 TEST_F(RemoteSuggestionsProviderTest, PersistRemoteCategoryOrder) { | 798 TEST_F(RemoteSuggestionsProviderImplTest, PersistRemoteCategoryOrder) { |
| 922 // We create a service with a normal ranker to store the order. | 799 // We create a service with a normal ranker to store the order. |
| 923 std::string json_str = | 800 std::string json_str = |
| 924 MultiCategoryJsonBuilder() | 801 MultiCategoryJsonBuilder() |
| 925 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/11) | 802 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/11) |
| 926 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/13) | 803 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/13) |
| 927 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/12) | 804 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/12) |
| 928 .Build(); | 805 .Build(); |
| 929 SetUpFetchResponse(json_str); | 806 SetUpFetchResponse(json_str); |
| 930 auto service = MakeSnippetsService(/*set_empty_response=*/false); | 807 auto service = MakeSnippetsService(/*set_empty_response=*/false); |
| 931 | 808 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 947 EXPECT_CALL(*raw_mock_ranker, | 824 EXPECT_CALL(*raw_mock_ranker, |
| 948 AppendCategoryIfNecessary(Category::FromRemoteCategory(11))); | 825 AppendCategoryIfNecessary(Category::FromRemoteCategory(11))); |
| 949 EXPECT_CALL(*raw_mock_ranker, | 826 EXPECT_CALL(*raw_mock_ranker, |
| 950 AppendCategoryIfNecessary(Category::FromRemoteCategory(13))); | 827 AppendCategoryIfNecessary(Category::FromRemoteCategory(13))); |
| 951 EXPECT_CALL(*raw_mock_ranker, | 828 EXPECT_CALL(*raw_mock_ranker, |
| 952 AppendCategoryIfNecessary(Category::FromRemoteCategory(12))); | 829 AppendCategoryIfNecessary(Category::FromRemoteCategory(12))); |
| 953 } | 830 } |
| 954 ResetSnippetsService(&service, /*set_empty_response=*/false); | 831 ResetSnippetsService(&service, /*set_empty_response=*/false); |
| 955 } | 832 } |
| 956 | 833 |
| 957 TEST_F(RemoteSuggestionsProviderTest, PersistSuggestions) { | 834 TEST_F(RemoteSuggestionsProviderImplTest, PersistSuggestions) { |
| 958 auto service = MakeSnippetsService(); | 835 auto service = MakeSnippetsService(); |
| 959 std::string json_str = | 836 std::string json_str = |
| 960 MultiCategoryJsonBuilder() | 837 MultiCategoryJsonBuilder() |
| 961 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) | 838 .AddCategory({GetSnippetN(0)}, /*remote_category_id=*/1) |
| 962 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/2) | 839 .AddCategory({GetSnippetN(2)}, /*remote_category_id=*/2) |
| 963 .Build(); | 840 .Build(); |
| 964 LoadFromJSONString(service.get(), json_str); | 841 LoadFromJSONString(service.get(), json_str); |
| 965 | 842 |
| 966 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | 843 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 967 SizeIs(1)); | 844 SizeIs(1)); |
| 968 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | 845 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); |
| 969 | 846 |
| 970 // Recreate the service to simulate a Chrome restart. | 847 // Recreate the service to simulate a Chrome restart. |
| 971 ResetSnippetsService(&service, /*set_empty_response=*/true); | 848 ResetSnippetsService(&service, /*set_empty_response=*/true); |
| 972 | 849 |
| 973 // The suggestions in both categories should have been restored. | 850 // The suggestions in both categories should have been restored. |
| 974 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | 851 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 975 SizeIs(1)); | 852 SizeIs(1)); |
| 976 EXPECT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | 853 EXPECT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); |
| 977 } | 854 } |
| 978 | 855 |
| 979 TEST_F(RemoteSuggestionsProviderTest, DontNotifyIfNotAvailable) { | 856 TEST_F(RemoteSuggestionsProviderImplTest, DontNotifyIfNotAvailable) { |
| 980 // Get some suggestions into the database. | 857 // Get some suggestions into the database. |
| 981 auto service = MakeSnippetsService(); | 858 auto service = MakeSnippetsService(); |
| 982 std::string json_str = | 859 std::string json_str = |
| 983 MultiCategoryJsonBuilder() | 860 MultiCategoryJsonBuilder() |
| 984 .AddCategory({GetSnippetN(0)}, | 861 .AddCategory({GetSnippetN(0)}, |
| 985 /*remote_category_id=*/1) | 862 /*remote_category_id=*/1) |
| 986 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/2) | 863 .AddCategory({GetSnippetN(1)}, /*remote_category_id=*/2) |
| 987 .Build(); | 864 .Build(); |
| 988 LoadFromJSONString(service.get(), json_str); | 865 LoadFromJSONString(service.get(), json_str); |
| 989 | 866 |
| 990 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | 867 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 991 SizeIs(1)); | 868 SizeIs(1)); |
| 992 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | 869 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); |
| 993 | 870 |
| 994 service.reset(); | 871 service.reset(); |
| 995 | 872 |
| 996 // Set the pref that disables remote suggestions. | 873 // Set the pref that disables remote suggestions. |
| 997 pref_service()->SetBoolean(prefs::kEnableSnippets, false); | 874 pref_service()->SetBoolean(prefs::kEnableSnippets, false); |
| 998 | 875 |
| 999 // Recreate the service to simulate a Chrome start. | 876 // Recreate the service to simulate a Chrome start. |
| 1000 ResetSnippetsService(&service, /*set_empty_response=*/true); | 877 ResetSnippetsService(&service, /*set_empty_response=*/true); |
| 1001 | 878 |
| 1002 ASSERT_THAT(RemoteSuggestionsProvider::State::DISABLED, Eq(service->state_)); | 879 ASSERT_THAT(RemoteSuggestionsProviderImpl::State::DISABLED, |
| 880 Eq(service->state_)); | |
| 1003 | 881 |
| 1004 // Now the observer should not have received any suggestions. | 882 // Now the observer should not have received any suggestions. |
| 1005 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | 883 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 1006 IsEmpty()); | 884 IsEmpty()); |
| 1007 EXPECT_THAT(observer().SuggestionsForCategory(other_category()), IsEmpty()); | 885 EXPECT_THAT(observer().SuggestionsForCategory(other_category()), IsEmpty()); |
| 1008 } | 886 } |
| 1009 | 887 |
| 1010 TEST_F(RemoteSuggestionsProviderTest, Clear) { | 888 TEST_F(RemoteSuggestionsProviderImplTest, Clear) { |
| 1011 auto service = MakeSnippetsService(); | 889 auto service = MakeSnippetsService(); |
| 1012 | 890 |
| 1013 std::string json_str(GetTestJson({GetSnippet()})); | 891 std::string json_str(GetTestJson({GetSnippet()})); |
| 1014 | 892 |
| 1015 LoadFromJSONString(service.get(), json_str); | 893 LoadFromJSONString(service.get(), json_str); |
| 1016 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 894 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1017 | 895 |
| 1018 service->ClearCachedSuggestions(articles_category()); | 896 service->ClearCachedSuggestions(articles_category()); |
| 1019 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 897 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1020 } | 898 } |
| 1021 | 899 |
| 1022 TEST_F(RemoteSuggestionsProviderTest, ReplaceSnippets) { | 900 TEST_F(RemoteSuggestionsProviderImplTest, ReplaceSnippets) { |
| 1023 auto service = MakeSnippetsService(); | 901 auto service = MakeSnippetsService(); |
| 1024 | 902 |
| 1025 std::string first("http://first"); | 903 std::string first("http://first"); |
| 1026 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); | 904 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); |
| 1027 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 905 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 1028 ElementsAre(IdEq(first))); | 906 ElementsAre(IdEq(first))); |
| 1029 | 907 |
| 1030 std::string second("http://second"); | 908 std::string second("http://second"); |
| 1031 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); | 909 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); |
| 1032 // The snippets loaded last replace all that was loaded previously. | 910 // The snippets loaded last replace all that was loaded previously. |
| 1033 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 911 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 1034 ElementsAre(IdEq(second))); | 912 ElementsAre(IdEq(second))); |
| 1035 } | 913 } |
| 1036 | 914 |
| 1037 TEST_F(RemoteSuggestionsProviderTest, LoadsAdditionalSnippets) { | 915 TEST_F(RemoteSuggestionsProviderImplTest, LoadsAdditionalSnippets) { |
| 1038 auto service = MakeSnippetsService(); | 916 auto service = MakeSnippetsService(); |
| 1039 | 917 |
| 1040 LoadFromJSONString(service.get(), | 918 LoadFromJSONString(service.get(), |
| 1041 GetTestJson({GetSnippetWithUrl("http://first")})); | 919 GetTestJson({GetSnippetWithUrl("http://first")})); |
| 1042 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 920 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 1043 ElementsAre(IdEq("http://first"))); | 921 ElementsAre(IdEq("http://first"))); |
| 1044 | 922 |
| 1045 auto expect_only_second_suggestion_received = base::Bind([]( | 923 auto expect_only_second_suggestion_received = base::Bind([]( |
| 1046 Status status, std::vector<ContentSuggestion> suggestions) { | 924 Status status, std::vector<ContentSuggestion> suggestions) { |
| 1047 EXPECT_THAT(suggestions, SizeIs(1)); | 925 EXPECT_THAT(suggestions, SizeIs(1)); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1077 // The tests TestMergingFetchedMoreSnippetsFillup and | 955 // The tests TestMergingFetchedMoreSnippetsFillup and |
| 1078 // TestMergingFetchedMoreSnippetsReplaceAll simulate the following user story: | 956 // TestMergingFetchedMoreSnippetsReplaceAll simulate the following user story: |
| 1079 // 1) fetch suggestions in NTP A | 957 // 1) fetch suggestions in NTP A |
| 1080 // 2) fetch more suggestions in NTP A. | 958 // 2) fetch more suggestions in NTP A. |
| 1081 // 3) open new NTP B: See the last 10 results visible in step 2). | 959 // 3) open new NTP B: See the last 10 results visible in step 2). |
| 1082 // 4) fetch more suggestions in NTP B. Make sure no results from step 1) which | 960 // 4) fetch more suggestions in NTP B. Make sure no results from step 1) which |
| 1083 // were superseded in step 2) get merged back in again. | 961 // were superseded in step 2) get merged back in again. |
| 1084 // TODO(tschumann): Test step 4) on a higher level instead of peeking into the | 962 // TODO(tschumann): Test step 4) on a higher level instead of peeking into the |
| 1085 // internal 'dismissed' data. The proper check is to make sure we tell the | 963 // internal 'dismissed' data. The proper check is to make sure we tell the |
| 1086 // backend to exclude these snippets. | 964 // backend to exclude these snippets. |
| 1087 TEST_F(RemoteSuggestionsProviderTest, TestMergingFetchedMoreSnippetsFillup) { | 965 TEST_F(RemoteSuggestionsProviderImplTest, |
| 966 TestMergingFetchedMoreSnippetsFillup) { | |
| 1088 auto service = MakeSnippetsService(/*set_empty_response=*/false); | 967 auto service = MakeSnippetsService(/*set_empty_response=*/false); |
| 1089 LoadFromJSONString( | 968 LoadFromJSONString( |
| 1090 service.get(), | 969 service.get(), |
| 1091 GetTestJson( | 970 GetTestJson( |
| 1092 {GetSnippetWithUrl("http://id-1"), GetSnippetWithUrl("http://id-2"), | 971 {GetSnippetWithUrl("http://id-1"), GetSnippetWithUrl("http://id-2"), |
| 1093 GetSnippetWithUrl("http://id-3"), GetSnippetWithUrl("http://id-4"), | 972 GetSnippetWithUrl("http://id-3"), GetSnippetWithUrl("http://id-4"), |
| 1094 GetSnippetWithUrl("http://id-5"), GetSnippetWithUrl("http://id-6"), | 973 GetSnippetWithUrl("http://id-5"), GetSnippetWithUrl("http://id-6"), |
| 1095 GetSnippetWithUrl("http://id-7"), GetSnippetWithUrl("http://id-8"), | 974 GetSnippetWithUrl("http://id-7"), GetSnippetWithUrl("http://id-8"), |
| 1096 GetSnippetWithUrl("http://id-9"), | 975 GetSnippetWithUrl("http://id-9"), |
| 1097 GetSnippetWithUrl("http://id-10")})); | 976 GetSnippetWithUrl("http://id-10")})); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1130 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), | 1009 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), |
| 1131 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), | 1010 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), |
| 1132 IdWithinCategoryEq("http://id-9"), IdWithinCategoryEq("http://id-10"), | 1011 IdWithinCategoryEq("http://id-9"), IdWithinCategoryEq("http://id-10"), |
| 1133 IdWithinCategoryEq("http://more-id-1"), | 1012 IdWithinCategoryEq("http://more-id-1"), |
| 1134 IdWithinCategoryEq("http://more-id-2"))); | 1013 IdWithinCategoryEq("http://more-id-2"))); |
| 1135 // Verify the superseded suggestions got marked as dismissed. | 1014 // Verify the superseded suggestions got marked as dismissed. |
| 1136 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1015 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 1137 ElementsAre(IdEq("http://id-1"), IdEq("http://id-2"))); | 1016 ElementsAre(IdEq("http://id-1"), IdEq("http://id-2"))); |
| 1138 } | 1017 } |
| 1139 | 1018 |
| 1140 TEST_F(RemoteSuggestionsProviderTest, | 1019 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1141 TestMergingFetchedMoreSnippetsReplaceAll) { | 1020 TestMergingFetchedMoreSnippetsReplaceAll) { |
| 1142 auto service = MakeSnippetsService(/*set_empty_response=*/false); | 1021 auto service = MakeSnippetsService(/*set_empty_response=*/false); |
| 1143 LoadFromJSONString( | 1022 LoadFromJSONString( |
| 1144 service.get(), | 1023 service.get(), |
| 1145 GetTestJson( | 1024 GetTestJson( |
| 1146 {GetSnippetWithUrl("http://id-1"), GetSnippetWithUrl("http://id-2"), | 1025 {GetSnippetWithUrl("http://id-1"), GetSnippetWithUrl("http://id-2"), |
| 1147 GetSnippetWithUrl("http://id-3"), GetSnippetWithUrl("http://id-4"), | 1026 GetSnippetWithUrl("http://id-3"), GetSnippetWithUrl("http://id-4"), |
| 1148 GetSnippetWithUrl("http://id-5"), GetSnippetWithUrl("http://id-6"), | 1027 GetSnippetWithUrl("http://id-5"), GetSnippetWithUrl("http://id-6"), |
| 1149 GetSnippetWithUrl("http://id-7"), GetSnippetWithUrl("http://id-8"), | 1028 GetSnippetWithUrl("http://id-7"), GetSnippetWithUrl("http://id-8"), |
| 1150 GetSnippetWithUrl("http://id-9"), | 1029 GetSnippetWithUrl("http://id-9"), |
| 1151 GetSnippetWithUrl("http://id-10")})); | 1030 GetSnippetWithUrl("http://id-10")})); |
| 1152 EXPECT_THAT( | 1031 EXPECT_THAT( |
| 1153 observer().SuggestionsForCategory(articles_category()), | 1032 observer().SuggestionsForCategory(articles_category()), |
| 1154 ElementsAre( | 1033 ElementsAre( |
| 1155 IdWithinCategoryEq("http://id-1"), IdWithinCategoryEq("http://id-2"), | 1034 IdWithinCategoryEq("http://id-1"), IdWithinCategoryEq("http://id-2"), |
| 1156 IdWithinCategoryEq("http://id-3"), IdWithinCategoryEq("http://id-4"), | 1035 IdWithinCategoryEq("http://id-3"), IdWithinCategoryEq("http://id-4"), |
| 1157 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), | 1036 IdWithinCategoryEq("http://id-5"), IdWithinCategoryEq("http://id-6"), |
| 1158 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), | 1037 IdWithinCategoryEq("http://id-7"), IdWithinCategoryEq("http://id-8"), |
| 1159 IdWithinCategoryEq("http://id-9"), | 1038 IdWithinCategoryEq("http://id-9"), |
| 1160 IdWithinCategoryEq("http://id-10"))); | 1039 IdWithinCategoryEq("http://id-10"))); |
| 1161 | 1040 |
| 1162 auto expect_receiving_ten_new_snippets = | 1041 auto expect_receiving_ten_new_snippets = |
| 1163 base::Bind([](Status status, std::vector<ContentSuggestion> suggestions) { | 1042 base::Bind([](Status status, std::vector<ContentSuggestion> suggestions) { |
| 1164 EXPECT_THAT(suggestions, ElementsAre( | 1043 EXPECT_THAT(suggestions, |
| 1165 IdWithinCategoryEq("http://more-id-1"), | 1044 ElementsAre(IdWithinCategoryEq("http://more-id-1"), |
| 1166 IdWithinCategoryEq("http://more-id-2"), | 1045 IdWithinCategoryEq("http://more-id-2"), |
| 1167 IdWithinCategoryEq("http://more-id-3"), | 1046 IdWithinCategoryEq("http://more-id-3"), |
| 1168 IdWithinCategoryEq("http://more-id-4"), | 1047 IdWithinCategoryEq("http://more-id-4"), |
| 1169 IdWithinCategoryEq("http://more-id-5"), | 1048 IdWithinCategoryEq("http://more-id-5"), |
| 1170 IdWithinCategoryEq("http://more-id-6"), | 1049 IdWithinCategoryEq("http://more-id-6"), |
| 1171 IdWithinCategoryEq("http://more-id-7"), | 1050 IdWithinCategoryEq("http://more-id-7"), |
| 1172 IdWithinCategoryEq("http://more-id-8"), | 1051 IdWithinCategoryEq("http://more-id-8"), |
| 1173 IdWithinCategoryEq("http://more-id-9"), | 1052 IdWithinCategoryEq("http://more-id-9"), |
| 1174 IdWithinCategoryEq("http://more-id-10"))); | 1053 IdWithinCategoryEq("http://more-id-10"))); |
| 1175 }); | 1054 }); |
| 1176 LoadMoreFromJSONString( | 1055 LoadMoreFromJSONString( |
| 1177 service.get(), articles_category(), | 1056 service.get(), articles_category(), |
| 1178 GetTestJson({GetSnippetWithUrl("http://more-id-1"), | 1057 GetTestJson({GetSnippetWithUrl("http://more-id-1"), |
| 1179 GetSnippetWithUrl("http://more-id-2"), | 1058 GetSnippetWithUrl("http://more-id-2"), |
| 1180 GetSnippetWithUrl("http://more-id-3"), | 1059 GetSnippetWithUrl("http://more-id-3"), |
| 1181 GetSnippetWithUrl("http://more-id-4"), | 1060 GetSnippetWithUrl("http://more-id-4"), |
| 1182 GetSnippetWithUrl("http://more-id-5"), | 1061 GetSnippetWithUrl("http://more-id-5"), |
| 1183 GetSnippetWithUrl("http://more-id-6"), | 1062 GetSnippetWithUrl("http://more-id-6"), |
| 1184 GetSnippetWithUrl("http://more-id-7"), | 1063 GetSnippetWithUrl("http://more-id-7"), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1219 // Workaround for gMock's lack of support for movable types. | 1098 // Workaround for gMock's lack of support for movable types. |
| 1220 void SuggestionsLoaded( | 1099 void SuggestionsLoaded( |
| 1221 MockFunction<void(Status, const std::vector<ContentSuggestion>&)>* loaded, | 1100 MockFunction<void(Status, const std::vector<ContentSuggestion>&)>* loaded, |
| 1222 Status status, | 1101 Status status, |
| 1223 std::vector<ContentSuggestion> suggestions) { | 1102 std::vector<ContentSuggestion> suggestions) { |
| 1224 loaded->Call(status, suggestions); | 1103 loaded->Call(status, suggestions); |
| 1225 } | 1104 } |
| 1226 | 1105 |
| 1227 } // namespace | 1106 } // namespace |
| 1228 | 1107 |
| 1229 TEST_F(RemoteSuggestionsProviderTest, ReturnFetchRequestEmptyBeforeInit) { | 1108 TEST_F(RemoteSuggestionsProviderImplTest, ReturnFetchRequestEmptyBeforeInit) { |
| 1230 auto service = MakeSnippetsServiceWithoutInitialization(); | 1109 auto service = MakeSnippetsServiceWithoutInitialization(); |
| 1231 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | 1110 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; |
| 1232 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | 1111 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); |
| 1233 service->Fetch(articles_category(), std::set<std::string>(), | 1112 service->Fetch(articles_category(), std::set<std::string>(), |
| 1234 base::Bind(&SuggestionsLoaded, &loaded)); | 1113 base::Bind(&SuggestionsLoaded, &loaded)); |
| 1235 base::RunLoop().RunUntilIdle(); | 1114 base::RunLoop().RunUntilIdle(); |
| 1236 } | 1115 } |
| 1237 | 1116 |
| 1238 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForInvalidJson) { | 1117 TEST_F(RemoteSuggestionsProviderImplTest, ReturnTemporaryErrorForInvalidJson) { |
| 1239 auto service = MakeSnippetsService(); | 1118 auto service = MakeSnippetsService(); |
| 1240 | 1119 |
| 1241 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | 1120 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; |
| 1242 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | 1121 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); |
| 1243 LoadMoreFromJSONString(service.get(), articles_category(), | 1122 LoadMoreFromJSONString(service.get(), articles_category(), |
| 1244 "invalid json string}]}", | 1123 "invalid json string}]}", |
| 1245 /*known_ids=*/std::set<std::string>(), | 1124 /*known_ids=*/std::set<std::string>(), |
| 1246 base::Bind(&SuggestionsLoaded, &loaded)); | 1125 base::Bind(&SuggestionsLoaded, &loaded)); |
| 1247 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 1126 EXPECT_THAT( |
| 1248 StartsWith("Received invalid JSON")); | 1127 service->snippets_fetcher_for_testing_and_debugging()->last_status(), |
| 1128 StartsWith("Received invalid JSON")); | |
| 1249 } | 1129 } |
| 1250 | 1130 |
| 1251 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForInvalidSnippet) { | 1131 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1132 ReturnTemporaryErrorForInvalidSnippet) { | |
| 1252 auto service = MakeSnippetsService(); | 1133 auto service = MakeSnippetsService(); |
| 1253 | 1134 |
| 1254 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | 1135 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; |
| 1255 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | 1136 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); |
| 1256 LoadMoreFromJSONString(service.get(), articles_category(), | 1137 LoadMoreFromJSONString(service.get(), articles_category(), |
| 1257 GetTestJson({GetIncompleteSnippet()}), | 1138 GetTestJson({GetIncompleteSnippet()}), |
| 1258 /*known_ids=*/std::set<std::string>(), | 1139 /*known_ids=*/std::set<std::string>(), |
| 1259 base::Bind(&SuggestionsLoaded, &loaded)); | 1140 base::Bind(&SuggestionsLoaded, &loaded)); |
| 1260 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 1141 EXPECT_THAT( |
| 1261 StartsWith("Invalid / empty list")); | 1142 service->snippets_fetcher_for_testing_and_debugging()->last_status(), |
| 1143 StartsWith("Invalid / empty list")); | |
| 1262 } | 1144 } |
| 1263 | 1145 |
| 1264 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForRequestFailure) { | 1146 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1147 ReturnTemporaryErrorForRequestFailure) { | |
| 1265 // Created SnippetsService will fail by default with unsuccessful request. | 1148 // Created SnippetsService will fail by default with unsuccessful request. |
| 1266 auto service = MakeSnippetsService(/*set_empty_response=*/false); | 1149 auto service = MakeSnippetsService(/*set_empty_response=*/false); |
| 1267 | 1150 |
| 1268 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | 1151 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; |
| 1269 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | 1152 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); |
| 1270 service->Fetch(articles_category(), | 1153 service->Fetch(articles_category(), |
| 1271 /*known_ids=*/std::set<std::string>(), | 1154 /*known_ids=*/std::set<std::string>(), |
| 1272 base::Bind(&SuggestionsLoaded, &loaded)); | 1155 base::Bind(&SuggestionsLoaded, &loaded)); |
| 1273 base::RunLoop().RunUntilIdle(); | 1156 base::RunLoop().RunUntilIdle(); |
| 1274 } | 1157 } |
| 1275 | 1158 |
| 1276 TEST_F(RemoteSuggestionsProviderTest, ReturnTemporaryErrorForHttpFailure) { | 1159 TEST_F(RemoteSuggestionsProviderImplTest, ReturnTemporaryErrorForHttpFailure) { |
| 1277 auto service = MakeSnippetsService(); | 1160 auto service = MakeSnippetsService(); |
| 1278 SetUpHttpError(); | 1161 SetUpHttpError(); |
| 1279 | 1162 |
| 1280 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; | 1163 MockFunction<void(Status, const std::vector<ContentSuggestion>&)> loaded; |
| 1281 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); | 1164 EXPECT_CALL(loaded, Call(HasCode(StatusCode::TEMPORARY_ERROR), IsEmpty())); |
| 1282 service->Fetch(articles_category(), | 1165 service->Fetch(articles_category(), |
| 1283 /*known_ids=*/std::set<std::string>(), | 1166 /*known_ids=*/std::set<std::string>(), |
| 1284 base::Bind(&SuggestionsLoaded, &loaded)); | 1167 base::Bind(&SuggestionsLoaded, &loaded)); |
| 1285 base::RunLoop().RunUntilIdle(); | 1168 base::RunLoop().RunUntilIdle(); |
| 1286 } | 1169 } |
| 1287 | 1170 |
| 1288 TEST_F(RemoteSuggestionsProviderTest, LoadInvalidJson) { | 1171 TEST_F(RemoteSuggestionsProviderImplTest, LoadInvalidJson) { |
| 1289 auto service = MakeSnippetsService(); | 1172 auto service = MakeSnippetsService(); |
| 1290 | 1173 |
| 1291 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 1174 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
| 1292 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 1175 EXPECT_THAT( |
| 1293 StartsWith("Received invalid JSON")); | 1176 service->snippets_fetcher_for_testing_and_debugging()->last_status(), |
| 1177 StartsWith("Received invalid JSON")); | |
| 1294 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1178 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1295 } | 1179 } |
| 1296 | 1180 |
| 1297 TEST_F(RemoteSuggestionsProviderTest, LoadInvalidJsonWithExistingSnippets) { | 1181 TEST_F(RemoteSuggestionsProviderImplTest, LoadInvalidJsonWithExistingSnippets) { |
| 1298 auto service = MakeSnippetsService(); | 1182 auto service = MakeSnippetsService(); |
| 1299 | 1183 |
| 1300 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 1184 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 1301 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1185 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1302 ASSERT_EQ("OK", service->snippets_fetcher()->last_status()); | 1186 ASSERT_EQ( |
| 1187 "OK", | |
| 1188 service->snippets_fetcher_for_testing_and_debugging()->last_status()); | |
| 1303 | 1189 |
| 1304 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 1190 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
| 1305 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 1191 EXPECT_THAT( |
| 1306 StartsWith("Received invalid JSON")); | 1192 service->snippets_fetcher_for_testing_and_debugging()->last_status(), |
| 1193 StartsWith("Received invalid JSON")); | |
| 1307 // This should not have changed the existing snippets. | 1194 // This should not have changed the existing snippets. |
| 1308 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1195 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1309 } | 1196 } |
| 1310 | 1197 |
| 1311 TEST_F(RemoteSuggestionsProviderTest, LoadIncompleteJson) { | 1198 TEST_F(RemoteSuggestionsProviderImplTest, LoadIncompleteJson) { |
| 1312 auto service = MakeSnippetsService(); | 1199 auto service = MakeSnippetsService(); |
| 1313 | 1200 |
| 1314 LoadFromJSONString(service.get(), GetTestJson({GetIncompleteSnippet()})); | 1201 LoadFromJSONString(service.get(), GetTestJson({GetIncompleteSnippet()})); |
| 1315 EXPECT_EQ("Invalid / empty list.", | 1202 EXPECT_EQ( |
| 1316 service->snippets_fetcher()->last_status()); | 1203 "Invalid / empty list.", |
| 1204 service->snippets_fetcher_for_testing_and_debugging()->last_status()); | |
| 1317 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1205 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1318 } | 1206 } |
| 1319 | 1207 |
| 1320 TEST_F(RemoteSuggestionsProviderTest, LoadIncompleteJsonWithExistingSnippets) { | 1208 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1209 LoadIncompleteJsonWithExistingSnippets) { | |
| 1321 auto service = MakeSnippetsService(); | 1210 auto service = MakeSnippetsService(); |
| 1322 | 1211 |
| 1323 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 1212 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 1324 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1213 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1325 | 1214 |
| 1326 LoadFromJSONString(service.get(), GetTestJson({GetIncompleteSnippet()})); | 1215 LoadFromJSONString(service.get(), GetTestJson({GetIncompleteSnippet()})); |
| 1327 EXPECT_EQ("Invalid / empty list.", | 1216 EXPECT_EQ( |
| 1328 service->snippets_fetcher()->last_status()); | 1217 "Invalid / empty list.", |
| 1218 service->snippets_fetcher_for_testing_and_debugging()->last_status()); | |
| 1329 // This should not have changed the existing snippets. | 1219 // This should not have changed the existing snippets. |
| 1330 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1220 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1331 } | 1221 } |
| 1332 | 1222 |
| 1333 TEST_F(RemoteSuggestionsProviderTest, Dismiss) { | 1223 TEST_F(RemoteSuggestionsProviderImplTest, Dismiss) { |
| 1334 auto service = MakeSnippetsService(); | 1224 auto service = MakeSnippetsService(); |
| 1335 | 1225 |
| 1336 std::string json_str( | 1226 std::string json_str( |
| 1337 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")})); | 1227 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")})); |
| 1338 | 1228 |
| 1339 LoadFromJSONString(service.get(), json_str); | 1229 LoadFromJSONString(service.get(), json_str); |
| 1340 | 1230 |
| 1341 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1231 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1342 // Load the image to store it in the database. | 1232 // Load the image to store it in the database. |
| 1343 ServeImageCallback cb = | 1233 ServeImageCallback cb = |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1374 LoadFromJSONString(service.get(), json_str); | 1264 LoadFromJSONString(service.get(), json_str); |
| 1375 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1265 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1376 | 1266 |
| 1377 // The snippet can be added again after clearing dismissed snippets. | 1267 // The snippet can be added again after clearing dismissed snippets. |
| 1378 service->ClearDismissedSuggestionsForDebugging(articles_category()); | 1268 service->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 1379 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1269 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1380 LoadFromJSONString(service.get(), json_str); | 1270 LoadFromJSONString(service.get(), json_str); |
| 1381 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1271 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1382 } | 1272 } |
| 1383 | 1273 |
| 1384 TEST_F(RemoteSuggestionsProviderTest, GetDismissed) { | 1274 TEST_F(RemoteSuggestionsProviderImplTest, GetDismissed) { |
| 1385 auto service = MakeSnippetsService(); | 1275 auto service = MakeSnippetsService(); |
| 1386 | 1276 |
| 1387 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 1277 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 1388 | 1278 |
| 1389 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); | 1279 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); |
| 1390 | 1280 |
| 1391 service->GetDismissedSuggestionsForDebugging( | 1281 service->GetDismissedSuggestionsForDebugging( |
| 1392 articles_category(), | 1282 articles_category(), |
| 1393 base::Bind( | 1283 base::Bind( |
| 1394 [](RemoteSuggestionsProvider* service, | 1284 [](RemoteSuggestionsProviderImpl* service, |
| 1395 RemoteSuggestionsProviderTest* test, | 1285 RemoteSuggestionsProviderImplTest* test, |
| 1396 std::vector<ContentSuggestion> dismissed_suggestions) { | 1286 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 1397 EXPECT_EQ(1u, dismissed_suggestions.size()); | 1287 EXPECT_EQ(1u, dismissed_suggestions.size()); |
| 1398 for (auto& suggestion : dismissed_suggestions) { | 1288 for (auto& suggestion : dismissed_suggestions) { |
| 1399 EXPECT_EQ(test->MakeArticleID(kSnippetUrl), suggestion.id()); | 1289 EXPECT_EQ(test->MakeArticleID(kSnippetUrl), suggestion.id()); |
| 1400 } | 1290 } |
| 1401 }, | 1291 }, |
| 1402 service.get(), this)); | 1292 service.get(), this)); |
| 1403 base::RunLoop().RunUntilIdle(); | 1293 base::RunLoop().RunUntilIdle(); |
| 1404 | 1294 |
| 1405 // There should be no dismissed snippet after clearing the list. | 1295 // There should be no dismissed snippet after clearing the list. |
| 1406 service->ClearDismissedSuggestionsForDebugging(articles_category()); | 1296 service->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 1407 service->GetDismissedSuggestionsForDebugging( | 1297 service->GetDismissedSuggestionsForDebugging( |
| 1408 articles_category(), | 1298 articles_category(), |
| 1409 base::Bind( | 1299 base::Bind( |
| 1410 [](RemoteSuggestionsProvider* service, | 1300 [](RemoteSuggestionsProviderImpl* service, |
| 1411 RemoteSuggestionsProviderTest* test, | 1301 RemoteSuggestionsProviderImplTest* test, |
| 1412 std::vector<ContentSuggestion> dismissed_suggestions) { | 1302 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 1413 EXPECT_EQ(0u, dismissed_suggestions.size()); | 1303 EXPECT_EQ(0u, dismissed_suggestions.size()); |
| 1414 }, | 1304 }, |
| 1415 service.get(), this)); | 1305 service.get(), this)); |
| 1416 base::RunLoop().RunUntilIdle(); | 1306 base::RunLoop().RunUntilIdle(); |
| 1417 } | 1307 } |
| 1418 | 1308 |
| 1419 TEST_F(RemoteSuggestionsProviderTest, CreationTimestampParseFail) { | 1309 TEST_F(RemoteSuggestionsProviderImplTest, CreationTimestampParseFail) { |
| 1420 auto service = MakeSnippetsService(); | 1310 auto service = MakeSnippetsService(); |
| 1421 | 1311 |
| 1422 std::string json = | 1312 std::string json = |
| 1423 GetSnippetWithTimes(GetDefaultCreationTime(), GetDefaultExpirationTime()); | 1313 GetSnippetWithTimes(GetDefaultCreationTime(), GetDefaultExpirationTime()); |
| 1424 base::ReplaceFirstSubstringAfterOffset( | 1314 base::ReplaceFirstSubstringAfterOffset( |
| 1425 &json, 0, FormatTime(GetDefaultCreationTime()), "aaa1448459205"); | 1315 &json, 0, FormatTime(GetDefaultCreationTime()), "aaa1448459205"); |
| 1426 std::string json_str(GetTestJson({json})); | 1316 std::string json_str(GetTestJson({json})); |
| 1427 | 1317 |
| 1428 LoadFromJSONString(service.get(), json_str); | 1318 LoadFromJSONString(service.get(), json_str); |
| 1429 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1319 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1430 } | 1320 } |
| 1431 | 1321 |
| 1432 TEST_F(RemoteSuggestionsProviderTest, RemoveExpiredDismissedContent) { | 1322 TEST_F(RemoteSuggestionsProviderImplTest, RemoveExpiredDismissedContent) { |
| 1433 auto service = MakeSnippetsService(); | 1323 auto service = MakeSnippetsService(); |
| 1434 | 1324 |
| 1435 std::string json_str1(GetTestJson({GetExpiredSnippet()})); | 1325 std::string json_str1(GetTestJson({GetExpiredSnippet()})); |
| 1436 // Load it. | 1326 // Load it. |
| 1437 LoadFromJSONString(service.get(), json_str1); | 1327 LoadFromJSONString(service.get(), json_str1); |
| 1438 // Load the image to store it in the database. | 1328 // Load the image to store it in the database. |
| 1439 // TODO(tschumann): Introduce some abstraction to nicely work with image | 1329 // TODO(tschumann): Introduce some abstraction to nicely work with image |
| 1440 // fetching expectations. | 1330 // fetching expectations. |
| 1441 ServeImageCallback cb = | 1331 ServeImageCallback cb = |
| 1442 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | 1332 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 1455 std::string json_str2(GetTestJson({GetSnippetWithUrl(kSnippetUrl2)})); | 1345 std::string json_str2(GetTestJson({GetSnippetWithUrl(kSnippetUrl2)})); |
| 1456 LoadFromJSONString(service.get(), json_str2); | 1346 LoadFromJSONString(service.get(), json_str2); |
| 1457 | 1347 |
| 1458 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1348 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 1459 IsEmpty()); | 1349 IsEmpty()); |
| 1460 | 1350 |
| 1461 // Verify the image got removed, too. | 1351 // Verify the image got removed, too. |
| 1462 EXPECT_TRUE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); | 1352 EXPECT_TRUE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); |
| 1463 } | 1353 } |
| 1464 | 1354 |
| 1465 TEST_F(RemoteSuggestionsProviderTest, ExpiredContentNotRemoved) { | 1355 TEST_F(RemoteSuggestionsProviderImplTest, ExpiredContentNotRemoved) { |
| 1466 auto service = MakeSnippetsService(); | 1356 auto service = MakeSnippetsService(); |
| 1467 | 1357 |
| 1468 std::string json_str(GetTestJson({GetExpiredSnippet()})); | 1358 std::string json_str(GetTestJson({GetExpiredSnippet()})); |
| 1469 | 1359 |
| 1470 LoadFromJSONString(service.get(), json_str); | 1360 LoadFromJSONString(service.get(), json_str); |
| 1471 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1361 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1472 } | 1362 } |
| 1473 | 1363 |
| 1474 TEST_F(RemoteSuggestionsProviderTest, TestSingleSource) { | 1364 TEST_F(RemoteSuggestionsProviderImplTest, TestSingleSource) { |
| 1475 auto service = MakeSnippetsService(); | 1365 auto service = MakeSnippetsService(); |
| 1476 | 1366 |
| 1477 std::string json_str(GetTestJson({GetSnippetWithSources( | 1367 std::string json_str(GetTestJson({GetSnippetWithSources( |
| 1478 "http://source1.com", "Source 1", "http://source1.amp.com")})); | 1368 "http://source1.com", "Source 1", "http://source1.amp.com")})); |
| 1479 | 1369 |
| 1480 LoadFromJSONString(service.get(), json_str); | 1370 LoadFromJSONString(service.get(), json_str); |
| 1481 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1371 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1482 const NTPSnippet& snippet = | 1372 const NTPSnippet& snippet = |
| 1483 *service->GetSnippetsForTesting(articles_category()).front(); | 1373 *service->GetSnippetsForTesting(articles_category()).front(); |
| 1484 EXPECT_EQ(snippet.id(), kSnippetUrl); | 1374 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 1485 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | 1375 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 1486 EXPECT_EQ(snippet.publisher_name(), std::string("Source 1")); | 1376 EXPECT_EQ(snippet.publisher_name(), std::string("Source 1")); |
| 1487 EXPECT_EQ(snippet.amp_url(), GURL("http://source1.amp.com")); | 1377 EXPECT_EQ(snippet.amp_url(), GURL("http://source1.amp.com")); |
| 1488 } | 1378 } |
| 1489 | 1379 |
| 1490 TEST_F(RemoteSuggestionsProviderTest, TestSingleSourceWithMalformedUrl) { | 1380 TEST_F(RemoteSuggestionsProviderImplTest, TestSingleSourceWithMalformedUrl) { |
| 1491 auto service = MakeSnippetsService(); | 1381 auto service = MakeSnippetsService(); |
| 1492 | 1382 |
| 1493 std::string json_str(GetTestJson({GetSnippetWithSources( | 1383 std::string json_str(GetTestJson({GetSnippetWithSources( |
| 1494 "ceci n'est pas un url", "Source 1", "http://source1.amp.com")})); | 1384 "ceci n'est pas un url", "Source 1", "http://source1.amp.com")})); |
| 1495 | 1385 |
| 1496 LoadFromJSONString(service.get(), json_str); | 1386 LoadFromJSONString(service.get(), json_str); |
| 1497 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1387 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1498 } | 1388 } |
| 1499 | 1389 |
| 1500 TEST_F(RemoteSuggestionsProviderTest, TestSingleSourceWithMissingData) { | 1390 TEST_F(RemoteSuggestionsProviderImplTest, TestSingleSourceWithMissingData) { |
| 1501 auto service = MakeSnippetsService(); | 1391 auto service = MakeSnippetsService(); |
| 1502 | 1392 |
| 1503 std::string json_str( | 1393 std::string json_str( |
| 1504 GetTestJson({GetSnippetWithSources("http://source1.com", "", "")})); | 1394 GetTestJson({GetSnippetWithSources("http://source1.com", "", "")})); |
| 1505 | 1395 |
| 1506 LoadFromJSONString(service.get(), json_str); | 1396 LoadFromJSONString(service.get(), json_str); |
| 1507 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1397 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1508 } | 1398 } |
| 1509 | 1399 |
| 1510 TEST_F(RemoteSuggestionsProviderTest, LogNumArticlesHistogram) { | 1400 TEST_F(RemoteSuggestionsProviderImplTest, LogNumArticlesHistogram) { |
| 1511 auto service = MakeSnippetsService(); | 1401 auto service = MakeSnippetsService(); |
| 1512 | 1402 |
| 1513 base::HistogramTester tester; | 1403 base::HistogramTester tester; |
| 1514 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 1404 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
| 1515 | 1405 |
| 1516 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | 1406 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), |
| 1517 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 1407 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| 1518 | 1408 |
| 1519 // Invalid JSON shouldn't contribute to NumArticlesFetched. | 1409 // Invalid JSON shouldn't contribute to NumArticlesFetched. |
| 1520 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | 1410 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1577 // But if there's a non-dismissed snippet in the database, recreating it | 1467 // But if there's a non-dismissed snippet in the database, recreating it |
| 1578 // shouldn't trigger a fetch. | 1468 // shouldn't trigger a fetch. |
| 1579 LoadFromJSONString( | 1469 LoadFromJSONString( |
| 1580 service.get(), | 1470 service.get(), |
| 1581 GetTestJson({GetSnippetWithUrl("http://not-dismissed.com")})); | 1471 GetTestJson({GetSnippetWithUrl("http://not-dismissed.com")})); |
| 1582 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 6); | 1472 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 6); |
| 1583 ResetSnippetsService(&service, /*set_empty_response=*/true); | 1473 ResetSnippetsService(&service, /*set_empty_response=*/true); |
| 1584 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 6); | 1474 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 6); |
| 1585 } | 1475 } |
| 1586 | 1476 |
| 1587 TEST_F(RemoteSuggestionsProviderTest, DismissShouldRespectAllKnownUrls) { | 1477 TEST_F(RemoteSuggestionsProviderImplTest, DismissShouldRespectAllKnownUrls) { |
| 1588 auto service = MakeSnippetsService(); | 1478 auto service = MakeSnippetsService(); |
| 1589 | 1479 |
| 1590 const base::Time creation = GetDefaultCreationTime(); | 1480 const base::Time creation = GetDefaultCreationTime(); |
| 1591 const base::Time expiry = GetDefaultExpirationTime(); | 1481 const base::Time expiry = GetDefaultExpirationTime(); |
| 1592 const std::vector<std::string> source_urls = { | 1482 const std::vector<std::string> source_urls = { |
| 1593 "http://mashable.com/2016/05/11/stolen", | 1483 "http://mashable.com/2016/05/11/stolen", |
| 1594 "http://www.aol.com/article/2016/05/stolen-doggie"}; | 1484 "http://www.aol.com/article/2016/05/stolen-doggie"}; |
| 1595 const std::vector<std::string> publishers = {"Mashable", "AOL"}; | 1485 const std::vector<std::string> publishers = {"Mashable", "AOL"}; |
| 1596 const std::vector<std::string> amp_urls = { | 1486 const std::vector<std::string> amp_urls = { |
| 1597 "http://mashable-amphtml.googleusercontent.com/1", | 1487 "http://mashable-amphtml.googleusercontent.com/1", |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1608 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1498 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1609 | 1499 |
| 1610 // The same article from the AOL domain should now be detected as dismissed. | 1500 // The same article from the AOL domain should now be detected as dismissed. |
| 1611 LoadFromJSONString(service.get(), | 1501 LoadFromJSONString(service.get(), |
| 1612 GetTestJson({GetSnippetWithUrlAndTimesAndSource( | 1502 GetTestJson({GetSnippetWithUrlAndTimesAndSource( |
| 1613 source_urls, source_urls[1], creation, expiry, | 1503 source_urls, source_urls[1], creation, expiry, |
| 1614 publishers[1], amp_urls[1])})); | 1504 publishers[1], amp_urls[1])})); |
| 1615 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1505 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1616 } | 1506 } |
| 1617 | 1507 |
| 1618 TEST_F(RemoteSuggestionsProviderTest, StatusChanges) { | 1508 TEST_F(RemoteSuggestionsProviderImplTest, StatusChanges) { |
| 1619 auto service = MakeSnippetsService(); | 1509 auto service = MakeSnippetsService(); |
| 1620 | 1510 |
| 1621 // Simulate user signed out | 1511 // Simulate user signed out |
| 1622 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 1512 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 1623 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, | 1513 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, |
| 1624 RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED); | 1514 RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED); |
| 1625 | 1515 |
| 1626 base::RunLoop().RunUntilIdle(); | 1516 base::RunLoop().RunUntilIdle(); |
| 1627 EXPECT_THAT(observer().StatusForCategory(articles_category()), | 1517 EXPECT_THAT(observer().StatusForCategory(articles_category()), |
| 1628 Eq(CategoryStatus::SIGNED_OUT)); | 1518 Eq(CategoryStatus::SIGNED_OUT)); |
| 1629 EXPECT_THAT(RemoteSuggestionsProvider::State::DISABLED, Eq(service->state_)); | 1519 EXPECT_THAT(RemoteSuggestionsProviderImpl::State::DISABLED, |
| 1520 Eq(service->state_)); | |
| 1630 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1521 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 1631 IsEmpty()); // No fetch should be made. | 1522 IsEmpty()); // No fetch should be made. |
| 1632 | 1523 |
| 1633 // Simulate user sign in. The service should be ready again and load snippets. | 1524 // Simulate user sign in. The service should be ready again and load snippets. |
| 1634 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 1525 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 1635 service->OnStatusChanged(RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED, | 1526 service->OnStatusChanged(RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED, |
| 1636 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); | 1527 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); |
| 1637 EXPECT_THAT(observer().StatusForCategory(articles_category()), | 1528 EXPECT_THAT(observer().StatusForCategory(articles_category()), |
| 1638 Eq(CategoryStatus::AVAILABLE_LOADING)); | 1529 Eq(CategoryStatus::AVAILABLE_LOADING)); |
| 1639 | 1530 |
| 1640 base::RunLoop().RunUntilIdle(); | 1531 base::RunLoop().RunUntilIdle(); |
| 1641 EXPECT_THAT(observer().StatusForCategory(articles_category()), | 1532 EXPECT_THAT(observer().StatusForCategory(articles_category()), |
| 1642 Eq(CategoryStatus::AVAILABLE)); | 1533 Eq(CategoryStatus::AVAILABLE)); |
| 1643 EXPECT_THAT(RemoteSuggestionsProvider::State::READY, Eq(service->state_)); | 1534 EXPECT_THAT(RemoteSuggestionsProviderImpl::State::READY, Eq(service->state_)); |
| 1644 EXPECT_FALSE(service->GetSnippetsForTesting(articles_category()).empty()); | 1535 EXPECT_FALSE(service->GetSnippetsForTesting(articles_category()).empty()); |
| 1645 } | 1536 } |
| 1646 | 1537 |
| 1647 TEST_F(RemoteSuggestionsProviderTest, ImageReturnedWithTheSameId) { | 1538 TEST_F(RemoteSuggestionsProviderImplTest, ImageReturnedWithTheSameId) { |
| 1648 auto service = MakeSnippetsService(); | 1539 auto service = MakeSnippetsService(); |
| 1649 | 1540 |
| 1650 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 1541 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 1651 | 1542 |
| 1652 gfx::Image image; | 1543 gfx::Image image; |
| 1653 MockFunction<void(const gfx::Image&)> image_fetched; | 1544 MockFunction<void(const gfx::Image&)> image_fetched; |
| 1654 ServeImageCallback cb = | 1545 ServeImageCallback cb = |
| 1655 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | 1546 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); |
| 1656 { | 1547 { |
| 1657 InSequence s; | 1548 InSequence s; |
| 1658 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | 1549 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) |
| 1659 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | 1550 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); |
| 1660 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | 1551 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); |
| 1661 } | 1552 } |
| 1662 | 1553 |
| 1663 service->FetchSuggestionImage( | 1554 service->FetchSuggestionImage( |
| 1664 MakeArticleID(kSnippetUrl), | 1555 MakeArticleID(kSnippetUrl), |
| 1665 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | 1556 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, |
| 1666 base::Unretained(&image_fetched))); | 1557 base::Unretained(&image_fetched))); |
| 1667 base::RunLoop().RunUntilIdle(); | 1558 base::RunLoop().RunUntilIdle(); |
| 1668 // Check that the image by ServeOneByOneImage is really served. | 1559 // Check that the image by ServeOneByOneImage is really served. |
| 1669 EXPECT_EQ(1, image.Width()); | 1560 EXPECT_EQ(1, image.Width()); |
| 1670 } | 1561 } |
| 1671 | 1562 |
| 1672 TEST_F(RemoteSuggestionsProviderTest, EmptyImageReturnedForNonExistentId) { | 1563 TEST_F(RemoteSuggestionsProviderImplTest, EmptyImageReturnedForNonExistentId) { |
| 1673 auto service = MakeSnippetsService(); | 1564 auto service = MakeSnippetsService(); |
| 1674 | 1565 |
| 1675 // Create a non-empty image so that we can test the image gets updated. | 1566 // Create a non-empty image so that we can test the image gets updated. |
| 1676 gfx::Image image = gfx::test::CreateImage(1, 1); | 1567 gfx::Image image = gfx::test::CreateImage(1, 1); |
| 1677 MockFunction<void(const gfx::Image&)> image_fetched; | 1568 MockFunction<void(const gfx::Image&)> image_fetched; |
| 1678 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | 1569 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); |
| 1679 | 1570 |
| 1680 service->FetchSuggestionImage( | 1571 service->FetchSuggestionImage( |
| 1681 MakeArticleID(kSnippetUrl2), | 1572 MakeArticleID(kSnippetUrl2), |
| 1682 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | 1573 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, |
| 1683 base::Unretained(&image_fetched))); | 1574 base::Unretained(&image_fetched))); |
| 1684 | 1575 |
| 1685 base::RunLoop().RunUntilIdle(); | 1576 base::RunLoop().RunUntilIdle(); |
| 1686 EXPECT_TRUE(image.IsEmpty()); | 1577 EXPECT_TRUE(image.IsEmpty()); |
| 1687 } | 1578 } |
| 1688 | 1579 |
| 1689 TEST_F(RemoteSuggestionsProviderTest, | 1580 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1690 FetchingUnknownImageIdShouldNotHitDatabase) { | 1581 FetchingUnknownImageIdShouldNotHitDatabase) { |
| 1691 // Testing that the provider is not accessing the database is tricky. | 1582 // Testing that the provider is not accessing the database is tricky. |
| 1692 // Therefore, we simply put in some data making sure that if the provider asks | 1583 // Therefore, we simply put in some data making sure that if the provider asks |
| 1693 // the database, it will get a wrong answer. | 1584 // the database, it will get a wrong answer. |
| 1694 auto service = MakeSnippetsService(); | 1585 auto service = MakeSnippetsService(); |
| 1695 | 1586 |
| 1696 ContentSuggestion::ID unknown_id = MakeArticleID(kSnippetUrl2); | 1587 ContentSuggestion::ID unknown_id = MakeArticleID(kSnippetUrl2); |
| 1697 database()->SaveImage(unknown_id.id_within_category(), "some image blob"); | 1588 database()->SaveImage(unknown_id.id_within_category(), "some image blob"); |
| 1698 // Set up the image decoder to always return the 1x1 test image. | 1589 // Set up the image decoder to always return the 1x1 test image. |
| 1699 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | 1590 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); |
| 1700 | 1591 |
| 1701 // Create a non-empty image so that we can test the image gets updated. | 1592 // Create a non-empty image so that we can test the image gets updated. |
| 1702 gfx::Image image = gfx::test::CreateImage(2, 2); | 1593 gfx::Image image = gfx::test::CreateImage(2, 2); |
| 1703 MockFunction<void(const gfx::Image&)> image_fetched; | 1594 MockFunction<void(const gfx::Image&)> image_fetched; |
| 1704 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | 1595 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); |
| 1705 | 1596 |
| 1706 service->FetchSuggestionImage( | 1597 service->FetchSuggestionImage( |
| 1707 MakeArticleID(kSnippetUrl2), | 1598 MakeArticleID(kSnippetUrl2), |
| 1708 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | 1599 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, |
| 1709 base::Unretained(&image_fetched))); | 1600 base::Unretained(&image_fetched))); |
| 1710 | 1601 |
| 1711 base::RunLoop().RunUntilIdle(); | 1602 base::RunLoop().RunUntilIdle(); |
| 1712 EXPECT_TRUE(image.IsEmpty()) << "got image with width: " << image.Width(); | 1603 EXPECT_TRUE(image.IsEmpty()) << "got image with width: " << image.Width(); |
| 1713 } | 1604 } |
| 1714 | 1605 |
| 1715 TEST_F(RemoteSuggestionsProviderTest, ClearHistoryRemovesAllSuggestions) { | 1606 TEST_F(RemoteSuggestionsProviderImplTest, ClearHistoryRemovesAllSuggestions) { |
| 1716 auto service = MakeSnippetsService(); | 1607 auto service = MakeSnippetsService(); |
| 1717 | 1608 |
| 1718 std::string first_snippet = GetSnippetWithUrl("http://url1.com"); | 1609 std::string first_snippet = GetSnippetWithUrl("http://url1.com"); |
| 1719 std::string second_snippet = GetSnippetWithUrl("http://url2.com"); | 1610 std::string second_snippet = GetSnippetWithUrl("http://url2.com"); |
| 1720 std::string json_str = GetTestJson({first_snippet, second_snippet}); | 1611 std::string json_str = GetTestJson({first_snippet, second_snippet}); |
| 1721 LoadFromJSONString(service.get(), json_str); | 1612 LoadFromJSONString(service.get(), json_str); |
| 1722 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); | 1613 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); |
| 1723 | 1614 |
| 1724 service->DismissSuggestion(MakeArticleID("http://url1.com")); | 1615 service->DismissSuggestion(MakeArticleID("http://url1.com")); |
| 1725 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1616 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1726 ASSERT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1617 ASSERT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 1727 SizeIs(1)); | 1618 SizeIs(1)); |
| 1728 | 1619 |
| 1729 base::Time begin = base::Time::FromTimeT(123), | 1620 base::Time begin = base::Time::FromTimeT(123), |
| 1730 end = base::Time::FromTimeT(456); | 1621 end = base::Time::FromTimeT(456); |
| 1731 base::Callback<bool(const GURL& url)> filter; | 1622 base::Callback<bool(const GURL& url)> filter; |
| 1732 service->ClearHistory(begin, end, filter); | 1623 service->ClearHistory(begin, end, filter); |
| 1733 | 1624 |
| 1734 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1625 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1735 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1626 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 1736 IsEmpty()); | 1627 IsEmpty()); |
| 1737 } | 1628 } |
| 1738 | 1629 |
| 1739 TEST_F(RemoteSuggestionsProviderTest, SuggestionsFetchedOnSignInAndSignOut) { | 1630 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1631 SuggestionsFetchedOnSignInAndSignOut) { | |
| 1740 auto service = MakeSnippetsService(); | 1632 auto service = MakeSnippetsService(); |
| 1741 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1633 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1742 | 1634 |
| 1743 // |MakeSnippetsService()| creates a service where user is signed in already, | 1635 // |MakeSnippetsService()| creates a service where user is signed in already, |
| 1744 // so we start by signing out. | 1636 // so we start by signing out. |
| 1745 SetUpFetchResponse(GetTestJson({GetSnippetN(1)})); | 1637 SetUpFetchResponse(GetTestJson({GetSnippetN(1)})); |
| 1746 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, | 1638 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, |
| 1747 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT); | 1639 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT); |
| 1748 base::RunLoop().RunUntilIdle(); | 1640 base::RunLoop().RunUntilIdle(); |
| 1749 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1641 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1750 | 1642 |
| 1751 // Sign in to check a transition from signed out to signed in. | 1643 // Sign in to check a transition from signed out to signed in. |
| 1752 SetUpFetchResponse(GetTestJson({GetSnippetN(1), GetSnippetN(2)})); | 1644 SetUpFetchResponse(GetTestJson({GetSnippetN(1), GetSnippetN(2)})); |
| 1753 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, | 1645 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, |
| 1754 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); | 1646 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); |
| 1755 base::RunLoop().RunUntilIdle(); | 1647 base::RunLoop().RunUntilIdle(); |
| 1756 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); | 1648 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); |
| 1757 } | 1649 } |
| 1758 | 1650 |
| 1759 TEST_F(RemoteSuggestionsProviderTest, ShouldClearOrphanedImagesOnRestart) { | 1651 TEST_F(RemoteSuggestionsProviderImplTest, ShouldClearOrphanedImagesOnRestart) { |
| 1760 auto service = MakeSnippetsService(); | 1652 auto service = MakeSnippetsService(); |
| 1761 | 1653 |
| 1762 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 1654 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 1763 ServeImageCallback cb = | 1655 ServeImageCallback cb = |
| 1764 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); | 1656 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); |
| 1765 | 1657 |
| 1766 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | 1658 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) |
| 1767 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); | 1659 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); |
| 1768 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); | 1660 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); |
| 1769 | 1661 |
| 1770 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); | 1662 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); |
| 1771 EXPECT_EQ(1, image.Width()); | 1663 EXPECT_EQ(1, image.Width()); |
| 1772 EXPECT_FALSE(image.IsEmpty()); | 1664 EXPECT_FALSE(image.IsEmpty()); |
| 1773 | 1665 |
| 1774 // Send new suggestion which don't include the snippet referencing the image. | 1666 // Send new suggestion which don't include the snippet referencing the image. |
| 1775 LoadFromJSONString(service.get(), | 1667 LoadFromJSONString(service.get(), |
| 1776 GetTestJson({GetSnippetWithUrl( | 1668 GetTestJson({GetSnippetWithUrl( |
| 1777 "http://something.com/pletely/unrelated")})); | 1669 "http://something.com/pletely/unrelated")})); |
| 1778 // The image should still be available until a restart happens. | 1670 // The image should still be available until a restart happens. |
| 1779 EXPECT_FALSE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); | 1671 EXPECT_FALSE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); |
| 1780 ResetSnippetsService(&service, /*set_empty_response=*/true); | 1672 ResetSnippetsService(&service, /*set_empty_response=*/true); |
| 1781 // After the restart, the image should be garbage collected. | 1673 // After the restart, the image should be garbage collected. |
| 1782 EXPECT_TRUE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); | 1674 EXPECT_TRUE(FetchImage(service.get(), MakeArticleID(kSnippetUrl)).IsEmpty()); |
| 1783 } | 1675 } |
| 1784 | 1676 |
| 1785 TEST_F(RemoteSuggestionsProviderTest, | 1677 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1786 ShouldHandleMoreThanMaxSnippetsInResponse) { | 1678 ShouldHandleMoreThanMaxSnippetsInResponse) { |
| 1787 auto service = MakeSnippetsService(); | 1679 auto service = MakeSnippetsService(); |
| 1788 | 1680 |
| 1789 std::vector<std::string> suggestions; | 1681 std::vector<std::string> suggestions; |
| 1790 for (int i = 0; i < service->GetMaxSnippetCountForTesting() + 1; ++i) { | 1682 for (int i = 0; i < service->GetMaxSnippetCountForTesting() + 1; ++i) { |
| 1791 suggestions.push_back(GetSnippetWithUrl( | 1683 suggestions.push_back(GetSnippetWithUrl( |
| 1792 base::StringPrintf("http://localhost/snippet-id-%d", i))); | 1684 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
| 1793 } | 1685 } |
| 1794 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1686 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
| 1795 // TODO(tschumann): We should probably trim out any additional results and | 1687 // TODO(tschumann): We should probably trim out any additional results and |
| 1796 // only serve the MaxSnippetCount items. | 1688 // only serve the MaxSnippetCount items. |
| 1797 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1689 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 1798 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1690 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
| 1799 } | 1691 } |
| 1800 | 1692 |
| 1801 TEST_F(RemoteSuggestionsProviderTest, StoreLastSuccessfullBackgroundFetchTime) { | 1693 TEST_F(RemoteSuggestionsProviderImplTest, |
| 1802 // On initialization of the RemoteSuggestionsProvider a background fetch is | 1694 StoreLastSuccessfullBackgroundFetchTime) { |
| 1803 // triggered since the snippets DB is empty. Therefore the service must not be | 1695 // On initialization of the RemoteSuggestionsProviderImpl a background fetch |
| 1804 // initialized until the test clock is set. | 1696 // is triggered since the snippets DB is empty. Therefore the service must not |
| 1697 // be initialized until the test clock is set. | |
| 1805 auto service = MakeSnippetsServiceWithoutInitialization(); | 1698 auto service = MakeSnippetsServiceWithoutInitialization(); |
| 1806 | 1699 |
| 1807 auto simple_test_clock = base::MakeUnique<base::SimpleTestClock>(); | 1700 auto simple_test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1808 base::SimpleTestClock* simple_test_clock_ptr = simple_test_clock.get(); | 1701 base::SimpleTestClock* simple_test_clock_ptr = simple_test_clock.get(); |
| 1809 service->SetClockForTesting(std::move(simple_test_clock)); | 1702 service->SetClockForTesting(std::move(simple_test_clock)); |
| 1810 | 1703 |
| 1811 // Test that the preference is correctly initialized with the default value 0. | 1704 // Test that the preference is correctly initialized with the default value 0. |
| 1812 EXPECT_EQ( | 1705 EXPECT_EQ( |
| 1813 0, pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | 1706 0, pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); |
| 1814 | 1707 |
| 1815 WaitForSnippetsServiceInitialization(service.get(), | 1708 WaitForSnippetsServiceInitialization(service.get(), |
| 1816 /*set_empty_response=*/true); | 1709 /*set_empty_response=*/true); |
| 1817 EXPECT_EQ( | 1710 EXPECT_EQ( |
| 1818 simple_test_clock_ptr->Now().ToInternalValue(), | 1711 simple_test_clock_ptr->Now().ToInternalValue(), |
| 1819 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | 1712 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); |
| 1820 | 1713 |
| 1821 // Advance the time and check whether the time was updated correctly after the | 1714 // Advance the time and check whether the time was updated correctly after the |
| 1822 // background fetch. | 1715 // background fetch. |
| 1823 simple_test_clock_ptr->Advance(TimeDelta::FromHours(1)); | 1716 simple_test_clock_ptr->Advance(TimeDelta::FromHours(1)); |
| 1824 service->FetchSnippetsInTheBackground(); | 1717 // TODO(jkrcal): Move together with the pref storage into the scheduler. |
| 1718 static_cast<RemoteSuggestionsScheduler::Updater*>(service.get()) | |
| 1719 ->UpdateRemoteSuggestionsBySchedule(); | |
| 1825 base::RunLoop().RunUntilIdle(); | 1720 base::RunLoop().RunUntilIdle(); |
| 1826 EXPECT_EQ( | 1721 EXPECT_EQ( |
| 1827 simple_test_clock_ptr->Now().ToInternalValue(), | 1722 simple_test_clock_ptr->Now().ToInternalValue(), |
| 1828 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | 1723 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); |
| 1829 // TODO(markusheintz): Add a test that simulates a browser restart once the | 1724 // TODO(markusheintz): Add a test that simulates a browser restart once the |
| 1830 // scheduler refactoring is done (crbug.com/672434). | 1725 // scheduler refactoring is done (crbug.com/672434). |
| 1831 } | 1726 } |
| 1832 | 1727 |
| 1728 TEST_F(RemoteSuggestionsProviderImplTest, CallsProviderStatusCallbackIfReady) { | |
| 1729 // Initiate the service so that it is already READY. | |
| 1730 auto service = MakeSnippetsService(); | |
| 1731 | |
| 1732 StrictMock<MockFunction<void(bool)>> status_callback; | |
| 1733 // The callback should be called on registering. | |
| 1734 EXPECT_CALL(status_callback, Call(true)); | |
| 1735 service->SetProviderStatusCallback(status_callback); | |
| 1736 } | |
| 1737 | |
| 1738 TEST_F(RemoteSuggestionsProviderImplTest, | |
| 1739 DoesNotCallProviderStatusCallbackIfNotInited) { | |
| 1740 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1741 | |
| 1742 StrictMock<MockFunction<void(bool)>> status_callback; | |
| 1743 // The provider is not initialized yet, no callback should be called on | |
| 1744 // registering. | |
| 1745 service->SetProviderStatusCallback(status_callback); | |
| 1746 } | |
| 1747 | |
| 1748 TEST_F(RemoteSuggestionsProviderImplTest, | |
| 1749 CallsProviderStatusCallbackWhenReady) { | |
| 1750 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1751 StrictMock<MockFunction<void(bool)>> status_callback; | |
| 1752 service->SetProviderStatusCallback(status_callback); | |
| 1753 | |
| 1754 // Should be called when becoming ready. | |
| 1755 EXPECT_CALL(status_callback, Call(true)); | |
| 1756 EnterState(State::READY); | |
| 1757 } | |
| 1758 | |
| 1759 TEST_F(RemoteSuggestionsProviderImplTest, CallsProviderStatusCallbackOnError) { | |
| 1760 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1761 StrictMock<MockFunction<void(bool)>> status_callback; | |
| 1762 service->SetProviderStatusCallback(status_callback); | |
| 1763 | |
| 1764 // Should be called on error. | |
| 1765 EXPECT_CALL(status_callback, Call(false)); | |
| 1766 EnterState(State::ERROR); | |
| 1767 } | |
| 1768 | |
| 1769 TEST_F(RemoteSuggestionsProviderImplTest, | |
| 1770 CallsProviderStatusCallbackWhenDisabled) { | |
| 1771 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1772 StrictMock<MockFunction<void(bool)>> status_callback; | |
| 1773 service->SetProviderStatusCallback(status_callback); | |
| 1774 | |
| 1775 // Should be called when becoming disabled. | |
| 1776 EXPECT_CALL(status_callback, Call(false)); | |
| 1777 EnterState(State::DISABLED); | |
| 1778 } | |
| 1779 | |
| 1780 TEST_F(RemoteSuggestionsProviderImplTest, | |
| 1781 ShouldNotCrashWhenCallingEmptyCallback) { | |
| 1782 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1783 service->SetProviderStatusCallback( | |
| 1784 RemoteSuggestionsProvider::ProviderStatusCallback()); | |
| 1785 EnterState(State::READY); | |
| 1786 // The empty callback should get called, the process should not crash. | |
| 1787 } | |
| 1788 | |
| 1833 } // namespace ntp_snippets | 1789 } // namespace ntp_snippets |
| OLD | NEW |