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/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 21 #include "base/test/histogram_tester.h" | 22 #include "base/test/histogram_tester.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 24 #include "components/image_fetcher/image_decoder.h" | 25 #include "components/image_fetcher/image_decoder.h" |
| 25 #include "components/image_fetcher/image_fetcher.h" | 26 #include "components/image_fetcher/image_fetcher.h" |
| 27 #include "components/image_fetcher/image_fetcher_delegate.h" | |
| 26 #include "components/ntp_snippets/category_factory.h" | 28 #include "components/ntp_snippets/category_factory.h" |
| 27 #include "components/ntp_snippets/ntp_snippet.h" | 29 #include "components/ntp_snippets/ntp_snippet.h" |
| 28 #include "components/ntp_snippets/ntp_snippets_database.h" | 30 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 29 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 31 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 30 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 32 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 31 #include "components/ntp_snippets/ntp_snippets_test_utils.h" | 33 #include "components/ntp_snippets/ntp_snippets_test_utils.h" |
| 32 #include "components/ntp_snippets/switches.h" | 34 #include "components/ntp_snippets/switches.h" |
| 33 #include "components/prefs/testing_pref_service.h" | 35 #include "components/prefs/testing_pref_service.h" |
| 34 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 36 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 35 #include "components/signin/core/browser/fake_signin_manager.h" | 37 #include "components/signin/core/browser/fake_signin_manager.h" |
| 36 #include "google_apis/google_api_keys.h" | 38 #include "google_apis/google_api_keys.h" |
| 37 #include "net/url_request/test_url_fetcher_factory.h" | 39 #include "net/url_request/test_url_fetcher_factory.h" |
| 38 #include "net/url_request/url_request_test_util.h" | 40 #include "net/url_request/url_request_test_util.h" |
| 39 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
| 40 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 43 #include "ui/gfx/image/image.h" | |
| 41 | 44 |
| 45 using image_fetcher::ImageFetcher; | |
| 46 using image_fetcher::ImageFetcherDelegate; | |
| 42 using testing::ElementsAre; | 47 using testing::ElementsAre; |
| 43 using testing::Eq; | 48 using testing::Eq; |
| 44 using testing::Invoke; | 49 using testing::Invoke; |
| 45 using testing::IsEmpty; | 50 using testing::IsEmpty; |
| 46 using testing::Mock; | 51 using testing::Mock; |
| 47 using testing::Return; | 52 using testing::Return; |
| 48 using testing::SizeIs; | 53 using testing::SizeIs; |
| 49 using testing::StartsWith; | 54 using testing::StartsWith; |
| 50 using testing::_; | 55 using testing::_; |
| 51 | 56 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 62 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | 67 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; |
| 63 | 68 |
| 64 const char kSnippetUrl[] = "http://localhost/foobar"; | 69 const char kSnippetUrl[] = "http://localhost/foobar"; |
| 65 const char kSnippetTitle[] = "Title"; | 70 const char kSnippetTitle[] = "Title"; |
| 66 const char kSnippetText[] = "Snippet"; | 71 const char kSnippetText[] = "Snippet"; |
| 67 const char kSnippetSalientImage[] = "http://localhost/salient_image"; | 72 const char kSnippetSalientImage[] = "http://localhost/salient_image"; |
| 68 const char kSnippetPublisherName[] = "Foo News"; | 73 const char kSnippetPublisherName[] = "Foo News"; |
| 69 const char kSnippetAmpUrl[] = "http://localhost/amp"; | 74 const char kSnippetAmpUrl[] = "http://localhost/amp"; |
| 70 const float kSnippetScore = 5.0; | 75 const float kSnippetScore = 5.0; |
| 71 | 76 |
| 77 const char kSnippetUrl2[] = "http://foo.com/bar"; | |
| 78 | |
| 72 base::Time GetDefaultCreationTime() { | 79 base::Time GetDefaultCreationTime() { |
| 73 base::Time out_time; | 80 base::Time out_time; |
| 74 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time)); | 81 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time)); |
| 75 return out_time; | 82 return out_time; |
| 76 } | 83 } |
| 77 | 84 |
| 78 base::Time GetDefaultExpirationTime() { | 85 base::Time GetDefaultExpirationTime() { |
| 79 return base::Time::Now() + base::TimeDelta::FromHours(1); | 86 return base::Time::Now() + base::TimeDelta::FromHours(1); |
| 80 } | 87 } |
| 81 | 88 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING); | 263 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING); |
| 257 run_loop_.Quit(); | 264 run_loop_.Quit(); |
| 258 } | 265 } |
| 259 | 266 |
| 260 MockProviderObserver* observer_; | 267 MockProviderObserver* observer_; |
| 261 base::RunLoop run_loop_; | 268 base::RunLoop run_loop_; |
| 262 | 269 |
| 263 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); | 270 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); |
| 264 }; | 271 }; |
| 265 | 272 |
| 273 class MockImageFetcher : public ImageFetcher { | |
| 274 public: | |
| 275 void SetImageFetcherDelegate(ImageFetcherDelegate* delegate) override {} | |
|
tschumann
2016/08/12 09:55:36
let's not mix up Mocks with fake implementations.
jkrcal
2016/08/12 12:06:10
Done.
Marc Treib
2016/08/12 12:10:59
One alternative to make things more explicit: Use
| |
| 276 void SetDataUseServiceName( | |
| 277 DataUseServiceName data_use_service_name) override {} | |
| 278 | |
| 279 MOCK_METHOD3( | |
| 280 StartOrQueueNetworkRequest, | |
| 281 void(const std::string&, | |
| 282 const GURL&, | |
| 283 base::Callback<void(const std::string&, const gfx::Image&)>)); | |
| 284 }; | |
| 285 | |
| 266 } // namespace | 286 } // namespace |
| 267 | 287 |
| 268 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { | 288 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { |
| 269 public: | 289 public: |
| 270 NTPSnippetsServiceTest() | 290 NTPSnippetsServiceTest() |
| 271 : fake_url_fetcher_factory_( | 291 : fake_url_fetcher_factory_( |
| 272 /*default_factory=*/&failing_url_fetcher_factory_), | 292 /*default_factory=*/&failing_url_fetcher_factory_), |
| 273 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, | 293 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, |
| 274 google_apis::GetAPIKey().c_str())) { | 294 google_apis::GetAPIKey().c_str())) { |
| 275 NTPSnippetsService::RegisterProfilePrefs(pref_service()->registry()); | 295 NTPSnippetsService::RegisterProfilePrefs(pref_service()->registry()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 base::MakeUnique<NTPSnippetsFetcher>( | 338 base::MakeUnique<NTPSnippetsFetcher>( |
| 319 fake_signin_manager(), fake_token_service_.get(), | 339 fake_signin_manager(), fake_token_service_.get(), |
| 320 std::move(request_context_getter), pref_service(), | 340 std::move(request_context_getter), pref_service(), |
| 321 base::Bind(&ParseJson), | 341 base::Bind(&ParseJson), |
| 322 /*is_stable_channel=*/true); | 342 /*is_stable_channel=*/true); |
| 323 | 343 |
| 324 fake_signin_manager()->SignIn("foo@bar.com"); | 344 fake_signin_manager()->SignIn("foo@bar.com"); |
| 325 snippets_fetcher->SetPersonalizationForTesting( | 345 snippets_fetcher->SetPersonalizationForTesting( |
| 326 NTPSnippetsFetcher::Personalization::kNonPersonal); | 346 NTPSnippetsFetcher::Personalization::kNonPersonal); |
| 327 | 347 |
| 348 auto image_fetcher = base::MakeUnique<MockImageFetcher>(); | |
| 349 image_fetcher_ = image_fetcher.get(); | |
| 350 | |
| 328 // Add an initial fetch response, as the service tries to fetch when there | 351 // Add an initial fetch response, as the service tries to fetch when there |
| 329 // is nothing in the DB. | 352 // is nothing in the DB. |
| 330 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 353 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 331 | 354 |
| 332 service_.reset(new NTPSnippetsService( | 355 service_.reset(new NTPSnippetsService( |
| 333 &observer_, &category_factory_, pref_service(), nullptr, "fr", | 356 &observer_, &category_factory_, pref_service(), nullptr, "fr", |
| 334 &scheduler_, std::move(snippets_fetcher), /*image_fetcher=*/nullptr, | 357 &scheduler_, std::move(snippets_fetcher), |
| 335 /*image_fetcher=*/nullptr, base::MakeUnique<NTPSnippetsDatabase>( | 358 std::move(image_fetcher), /*image_decoder=*/nullptr, |
| 336 database_dir_.path(), task_runner), | 359 base::MakeUnique<NTPSnippetsDatabase>(database_dir_.path(), |
| 360 task_runner), | |
| 337 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), | 361 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), |
| 338 pref_service()))); | 362 pref_service()))); |
| 339 | 363 |
| 340 WaitForDBLoad(&observer_, service_.get()); | 364 WaitForDBLoad(&observer_, service_.get()); |
| 341 } | 365 } |
| 342 | 366 |
| 343 std::string MakeUniqueID(const std::string& within_category_id) { | 367 std::string MakeUniqueID(const std::string& within_category_id) { |
| 344 return service()->MakeUniqueID(articles_category(), within_category_id); | 368 return service()->MakeUniqueID(articles_category(), within_category_id); |
| 345 } | 369 } |
| 346 | 370 |
| 347 Category articles_category() { | 371 Category articles_category() { |
| 348 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); | 372 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); |
| 349 } | 373 } |
| 350 | 374 |
| 375 MOCK_METHOD2(OnImageFetched, void(const std::string&, const gfx::Image&)); | |
|
tschumann
2016/08/12 09:55:36
while it might sound tempting, this is not how you
jkrcal
2016/08/12 12:06:10
Whoo, that's fancy! Thanks!
| |
| 376 | |
| 351 protected: | 377 protected: |
| 352 const GURL& test_url() { return test_url_; } | 378 const GURL& test_url() { return test_url_; } |
| 353 NTPSnippetsService* service() { return service_.get(); } | 379 NTPSnippetsService* service() { return service_.get(); } |
| 354 MockProviderObserver& observer() { return observer_; } | 380 MockProviderObserver& observer() { return observer_; } |
| 355 MockScheduler& mock_scheduler() { return scheduler_; } | 381 MockScheduler& mock_scheduler() { return scheduler_; } |
| 382 MockImageFetcher* image_fetcher() { return image_fetcher_; } | |
| 356 | 383 |
| 357 // Provide the json to be returned by the fake fetcher. | 384 // Provide the json to be returned by the fake fetcher. |
| 358 void SetUpFetchResponse(const std::string& json) { | 385 void SetUpFetchResponse(const std::string& json) { |
| 359 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 386 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
| 360 net::URLRequestStatus::SUCCESS); | 387 net::URLRequestStatus::SUCCESS); |
| 361 } | 388 } |
| 362 | 389 |
| 363 void LoadFromJSONString(const std::string& json) { | 390 void LoadFromJSONString(const std::string& json) { |
| 364 SetUpFetchResponse(json); | 391 SetUpFetchResponse(json); |
| 365 service()->FetchSnippets(true); | 392 service()->FetchSnippets(true); |
| 366 base::RunLoop().RunUntilIdle(); | 393 base::RunLoop().RunUntilIdle(); |
| 367 } | 394 } |
| 368 | 395 |
| 396 static void ReturnEmptyImage( | |
|
tschumann
2016/08/12 09:55:36
no need to define this inside the text fixture cla
jkrcal
2016/08/12 12:06:09
Done.
| |
| 397 const std::string& id, | |
| 398 base::Callback<void(const std::string&, const gfx::Image&)> callback) { | |
| 399 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 400 FROM_HERE, base::Bind(callback, id, gfx::Image())); | |
| 401 } | |
| 402 | |
| 369 private: | 403 private: |
| 370 base::MessageLoop message_loop_; | 404 base::MessageLoop message_loop_; |
| 371 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 405 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 372 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 406 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
| 373 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 407 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
| 374 const GURL test_url_; | 408 const GURL test_url_; |
| 375 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 409 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 376 MockScheduler scheduler_; | 410 MockScheduler scheduler_; |
| 377 MockProviderObserver observer_; | 411 MockProviderObserver observer_; |
| 378 CategoryFactory category_factory_; | 412 CategoryFactory category_factory_; |
| 413 MockImageFetcher* image_fetcher_; | |
| 379 // Last so that the dependencies are deleted after the service. | 414 // Last so that the dependencies are deleted after the service. |
| 380 std::unique_ptr<NTPSnippetsService> service_; | 415 std::unique_ptr<NTPSnippetsService> service_; |
| 381 | 416 |
| 382 base::ScopedTempDir database_dir_; | 417 base::ScopedTempDir database_dir_; |
| 383 | 418 |
| 384 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 419 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
| 385 }; | 420 }; |
| 386 | 421 |
| 387 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { | 422 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { |
| 388 // SetUp() checks that Schedule is called. | 423 // SetUp() checks that Schedule is called. |
| (...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 879 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); | 914 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); |
| 880 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 915 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 881 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 916 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
| 882 EXPECT_CALL(observer(), | 917 EXPECT_CALL(observer(), |
| 883 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); | 918 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); |
| 884 base::RunLoop().RunUntilIdle(); | 919 base::RunLoop().RunUntilIdle(); |
| 885 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 920 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
| 886 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); | 921 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); |
| 887 } | 922 } |
| 888 | 923 |
| 924 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { | |
| 925 LoadFromJSONString(GetTestJson({GetSnippet()})); | |
| 926 | |
| 927 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | |
| 928 .Times(1) | |
| 929 .WillOnce(testing::WithArgs<0, 2>(Invoke(ReturnEmptyImage))); | |
| 930 EXPECT_CALL(*this, OnImageFetched(MakeUniqueID(kSnippetUrl), _)).Times(1); | |
| 931 | |
| 932 service()->FetchSuggestionImage( | |
| 933 MakeUniqueID(kSnippetUrl), | |
| 934 base::Bind(&NTPSnippetsServiceTest::OnImageFetched, | |
| 935 base::Unretained(this))); | |
| 936 base::RunLoop().RunUntilIdle(); | |
| 937 } | |
| 938 | |
| 939 TEST_F(NTPSnippetsServiceTest, EmptyImageReturnedForNonExistentId) { | |
| 940 gfx::Image image; | |
| 941 EXPECT_CALL(*this, OnImageFetched(MakeUniqueID(kSnippetUrl2), _)) | |
| 942 .Times(1) | |
| 943 .WillOnce(testing::SaveArg<1>(&image)); | |
| 944 | |
| 945 service()->FetchSuggestionImage( | |
| 946 MakeUniqueID(kSnippetUrl2), | |
| 947 base::Bind(&NTPSnippetsServiceTest::OnImageFetched, | |
| 948 base::Unretained(this))); | |
| 949 | |
| 950 base::RunLoop().RunUntilIdle(); | |
| 951 EXPECT_TRUE(image.IsEmpty()); | |
|
tschumann
2016/08/12 09:55:36
hmm... do we have some test image? i guess the def
jkrcal
2016/08/12 12:06:09
Well, this is a bit pointless, I agree. This is to
tschumann
2016/08/12 12:37:09
src/ui/gfx/image/image_unittest_util.h has some he
| |
| 952 } | |
| 953 | |
| 889 } // namespace ntp_snippets | 954 } // namespace ntp_snippets |
| OLD | NEW |