Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "base/test/test_mock_time_task_runner.h" | 14 #include "base/test/test_mock_time_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "components/ntp_snippets/category_factory.h" | 18 #include "components/ntp_snippets/category_factory.h" |
| 19 #include "components/ntp_snippets/features.h" | |
| 19 #include "components/ntp_snippets/ntp_snippets_constants.h" | 20 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 20 #include "components/ntp_snippets/remote/ntp_snippet.h" | 21 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 21 #include "components/ntp_snippets/user_classifier.h" | 22 #include "components/ntp_snippets/user_classifier.h" |
| 22 #include "components/prefs/testing_pref_service.h" | 23 #include "components/prefs/testing_pref_service.h" |
| 23 #include "components/signin/core/browser/account_tracker_service.h" | 24 #include "components/signin/core/browser/account_tracker_service.h" |
| 24 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 25 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 25 #include "components/signin/core/browser/fake_signin_manager.h" | 26 #include "components/signin/core/browser/fake_signin_manager.h" |
| 26 #include "components/signin/core/browser/test_signin_client.h" | 27 #include "components/signin/core/browser/test_signin_client.h" |
| 27 #include "components/variations/entropy_provider.h" | 28 #include "components/variations/entropy_provider.h" |
| 28 #include "components/variations/variations_params_manager.h" | 29 #include "components/variations/variations_params_manager.h" |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 191 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 191 FROM_HERE, base::Bind(&ParseJson, json, std::move(success_callback), | 192 FROM_HERE, base::Bind(&ParseJson, json, std::move(success_callback), |
| 192 std::move(error_callback)), | 193 std::move(error_callback)), |
| 193 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); | 194 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); |
| 194 } | 195 } |
| 195 | 196 |
| 196 } // namespace | 197 } // namespace |
| 197 | 198 |
| 198 class NTPSnippetsFetcherTest : public testing::Test { | 199 class NTPSnippetsFetcherTest : public testing::Test { |
| 199 public: | 200 public: |
| 200 // TODO(fhorschig): As soon as crbug.com/645447 is resolved, use | |
| 201 // variations::testing::VariationParamsManager to configure these values. | |
| 202 class RequestBuilderWithMockedFlagsForTesting | |
| 203 : public NTPSnippetsFetcher::RequestBuilder { | |
| 204 public: | |
| 205 RequestBuilderWithMockedFlagsForTesting() : RequestBuilder() {} | |
| 206 | |
| 207 private: | |
| 208 bool IsSendingUserClassEnabled() const override { return true; } | |
| 209 bool IsSendingTopLanguagesEnabled() const override { return true; } | |
| 210 }; | |
| 211 | |
| 212 NTPSnippetsFetcherTest() | 201 NTPSnippetsFetcherTest() |
|
tschumann
2016/12/06 17:58:16
nit: As you're touching this code, can we clean up
fhorschig
2016/12/12 14:16:37
Makes sense. Done.
| |
| 213 : NTPSnippetsFetcherTest(GURL(kTestChromeReaderUrl), | 202 : NTPSnippetsFetcherTest( |
| 214 std::map<std::string, std::string>()) {} | 203 GURL(kTestChromeReaderUrl), |
| 204 {{"send_top_languages", "true"}, {"send_user_class", "true"}}) {} | |
| 215 | 205 |
| 216 NTPSnippetsFetcherTest(const GURL& gurl, | 206 NTPSnippetsFetcherTest(const GURL& gurl, |
| 217 const std::map<std::string, std::string>& params) | 207 const std::map<std::string, std::string>& params) |
| 218 : params_manager_( | 208 : params_manager_( |
| 219 base::MakeUnique<variations::testing::VariationParamsManager>( | 209 base::MakeUnique<variations::testing::VariationParamsManager>( |
| 220 ntp_snippets::kStudyName, | 210 ntp_snippets::kStudyName, |
| 221 params)), | 211 params, |
| 212 std::set<std::string>( | |
|
jkrcal
2016/12/06 16:50:47
Do you need to wrap the initializer list with "std
fhorschig
2016/12/12 14:16:37
For the unique_ptr: Sadly, yes.
jkrcal
2016/12/12 14:27:14
Interesting :)
| |
| 213 {ntp_snippets::kArticleSuggestionsFeature.name}))), | |
| 222 mock_task_runner_(new base::TestMockTimeTaskRunner()), | 214 mock_task_runner_(new base::TestMockTimeTaskRunner()), |
| 223 mock_task_runner_handle_(mock_task_runner_), | 215 mock_task_runner_handle_(mock_task_runner_), |
| 224 signin_client_(base::MakeUnique<TestSigninClient>(nullptr)), | 216 signin_client_(base::MakeUnique<TestSigninClient>(nullptr)), |
| 225 account_tracker_(base::MakeUnique<AccountTrackerService>()), | 217 account_tracker_(base::MakeUnique<AccountTrackerService>()), |
| 226 fake_signin_manager_( | 218 fake_signin_manager_( |
| 227 base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(), | 219 base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(), |
| 228 account_tracker_.get())), | 220 account_tracker_.get())), |
| 229 fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()), | 221 fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()), |
| 230 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()), | 222 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()), |
| 231 test_url_(gurl) { | 223 test_url_(gurl) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 language_model->OnPageVisited(code); | 300 language_model->OnPageVisited(code); |
| 309 } | 301 } |
| 310 } | 302 } |
| 311 return language_model; | 303 return language_model; |
| 312 } | 304 } |
| 313 | 305 |
| 314 TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); } | 306 TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); } |
| 315 | 307 |
| 316 private: | 308 private: |
| 317 // TODO(fhorschig): Make it a simple member as soon as it resets properly. | 309 // TODO(fhorschig): Make it a simple member as soon as it resets properly. |
| 318 std::unique_ptr<variations::testing::VariationParamsManager> params_manager_; | 310 std::unique_ptr<variations::testing::VariationParamsManager> params_manager_; |
|
jkrcal
2016/12/06 16:50:47
What do you mean by "resets properly"? Cannot you
fhorschig
2016/12/12 14:16:37
Thanks for pointing this out! This is the big visi
| |
| 319 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; | 311 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; |
| 320 base::ThreadTaskRunnerHandle mock_task_runner_handle_; | 312 base::ThreadTaskRunnerHandle mock_task_runner_handle_; |
| 321 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 313 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 322 // Initialized lazily in SetFakeResponse(). | 314 // Initialized lazily in SetFakeResponse(). |
| 323 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 315 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 324 std::unique_ptr<TestSigninClient> signin_client_; | 316 std::unique_ptr<TestSigninClient> signin_client_; |
| 325 std::unique_ptr<AccountTrackerService> account_tracker_; | 317 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 326 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 318 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 327 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 319 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 328 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 320 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 | 515 |
| 524 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), | 516 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), |
| 525 EqualsJSON("{" | 517 EqualsJSON("{" |
| 526 " \"regularlyVisitedHostNames\": []," | 518 " \"regularlyVisitedHostNames\": []," |
| 527 " \"priority\": \"BACKGROUND_PREFETCH\"," | 519 " \"priority\": \"BACKGROUND_PREFETCH\"," |
| 528 " \"excludedSuggestionIds\": []" | 520 " \"excludedSuggestionIds\": []" |
| 529 "}")); | 521 "}")); |
| 530 } | 522 } |
| 531 | 523 |
| 532 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { | 524 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { |
| 533 RequestBuilderWithMockedFlagsForTesting builder; | 525 NTPSnippetsFetcher::RequestBuilder builder; |
| 534 std::unique_ptr<translate::LanguageModel> language_model = | 526 std::unique_ptr<translate::LanguageModel> language_model = |
| 535 MakeLanguageModel({"de", "en"}); | 527 MakeLanguageModel({"de", "en"}); |
| 536 NTPSnippetsFetcher::Params params = test_params(); | 528 NTPSnippetsFetcher::Params params = test_params(); |
| 537 params.language_code = "en"; | 529 params.language_code = "en"; |
| 538 builder.SetParams(params) | 530 builder.SetParams(params) |
| 539 .SetLanguageModel(language_model.get()) | 531 .SetLanguageModel(language_model.get()) |
| 540 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) | 532 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) |
| 541 .SetFetchAPI( | 533 .SetFetchAPI( |
| 542 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); | 534 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); |
| 543 | 535 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 554 " }," | 546 " }," |
| 555 " {" | 547 " {" |
| 556 " \"language\" : \"de\"," | 548 " \"language\" : \"de\"," |
| 557 " \"frequency\" : 0.5" | 549 " \"frequency\" : 0.5" |
| 558 " }" | 550 " }" |
| 559 " ]" | 551 " ]" |
| 560 "}")); | 552 "}")); |
| 561 } | 553 } |
| 562 | 554 |
| 563 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) { | 555 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) { |
| 564 RequestBuilderWithMockedFlagsForTesting builder; | 556 NTPSnippetsFetcher::RequestBuilder builder; |
| 565 std::unique_ptr<translate::LanguageModel> language_model = | 557 std::unique_ptr<translate::LanguageModel> language_model = |
| 566 MakeLanguageModel({"en"}); | 558 MakeLanguageModel({"en"}); |
| 567 NTPSnippetsFetcher::Params params = test_params(); | 559 NTPSnippetsFetcher::Params params = test_params(); |
| 568 params.language_code = "en"; | 560 params.language_code = "en"; |
| 569 builder.SetParams(params) | 561 builder.SetParams(params) |
| 570 .SetLanguageModel(language_model.get()) | 562 .SetLanguageModel(language_model.get()) |
| 571 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) | 563 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) |
| 572 .SetFetchAPI( | 564 .SetFetchAPI( |
| 573 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); | 565 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); |
| 574 | 566 |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1088 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 1080 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
| 1089 if (fetched_categories) { | 1081 if (fetched_categories) { |
| 1090 // Matchers above aren't any more precise than this, so this is sufficient | 1082 // Matchers above aren't any more precise than this, so this is sufficient |
| 1091 // for test-failure diagnostics. | 1083 // for test-failure diagnostics. |
| 1092 return os << "list with " << fetched_categories->size() << " elements"; | 1084 return os << "list with " << fetched_categories->size() << " elements"; |
| 1093 } | 1085 } |
| 1094 return os << "null"; | 1086 return os << "null"; |
| 1095 } | 1087 } |
| 1096 | 1088 |
| 1097 } // namespace ntp_snippets | 1089 } // namespace ntp_snippets |
| OLD | NEW |