| 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/ntp_snippets_constants.h" | 19 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 20 #include "components/ntp_snippets/remote/ntp_snippet.h" | 20 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 21 #include "components/ntp_snippets/user_classifier.h" |
| 21 #include "components/prefs/testing_pref_service.h" | 22 #include "components/prefs/testing_pref_service.h" |
| 22 #include "components/signin/core/browser/account_tracker_service.h" | 23 #include "components/signin/core/browser/account_tracker_service.h" |
| 23 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 24 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 24 #include "components/signin/core/browser/fake_signin_manager.h" | 25 #include "components/signin/core/browser/fake_signin_manager.h" |
| 25 #include "components/signin/core/browser/test_signin_client.h" | 26 #include "components/signin/core/browser/test_signin_client.h" |
| 26 #include "components/variations/entropy_provider.h" | 27 #include "components/variations/entropy_provider.h" |
| 27 #include "components/variations/variations_associated_data.h" | 28 #include "components/variations/variations_associated_data.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 mock_task_runner_handle_(mock_task_runner_), | 157 mock_task_runner_handle_(mock_task_runner_), |
| 157 signin_client_(new TestSigninClient(nullptr)), | 158 signin_client_(new TestSigninClient(nullptr)), |
| 158 account_tracker_(new AccountTrackerService()), | 159 account_tracker_(new AccountTrackerService()), |
| 159 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), | 160 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), |
| 160 account_tracker_.get())), | 161 account_tracker_.get())), |
| 161 fake_token_service_(new FakeProfileOAuth2TokenService()), | 162 fake_token_service_(new FakeProfileOAuth2TokenService()), |
| 162 pref_service_(new TestingPrefServiceSimple()), | 163 pref_service_(new TestingPrefServiceSimple()), |
| 163 test_lang_("en-US"), | 164 test_lang_("en-US"), |
| 164 test_url_(gurl) { | 165 test_url_(gurl) { |
| 165 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); | 166 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); |
| 167 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); |
| 168 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get()); |
| 166 | 169 |
| 167 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 170 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
| 168 fake_signin_manager_.get(), fake_token_service_.get(), | 171 fake_signin_manager_.get(), fake_token_service_.get(), |
| 169 scoped_refptr<net::TestURLRequestContextGetter>( | 172 scoped_refptr<net::TestURLRequestContextGetter>( |
| 170 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 173 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
| 171 pref_service_.get(), &category_factory_, nullptr, | 174 pref_service_.get(), &category_factory_, nullptr, |
| 172 base::Bind(&ParseJsonDelayed), kAPIKey); | 175 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); |
| 173 | 176 |
| 174 snippets_fetcher_->SetCallback( | 177 snippets_fetcher_->SetCallback( |
| 175 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 178 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
| 176 base::Unretained(&mock_callback_))); | 179 base::Unretained(&mock_callback_))); |
| 177 snippets_fetcher_->SetTickClockForTesting( | 180 snippets_fetcher_->SetTickClockForTesting( |
| 178 mock_task_runner_->GetMockTickClock()); | 181 mock_task_runner_->GetMockTickClock()); |
| 179 test_excluded_.insert("1234567890"); | 182 test_excluded_.insert("1234567890"); |
| 180 // Increase initial time such that ticks are non-zero. | 183 // Increase initial time such that ticks are non-zero. |
| 181 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 184 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| 182 } | 185 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 base::ThreadTaskRunnerHandle mock_task_runner_handle_; | 218 base::ThreadTaskRunnerHandle mock_task_runner_handle_; |
| 216 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 219 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 217 // Initialized lazily in SetFakeResponse(). | 220 // Initialized lazily in SetFakeResponse(). |
| 218 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 221 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 219 std::unique_ptr<TestSigninClient> signin_client_; | 222 std::unique_ptr<TestSigninClient> signin_client_; |
| 220 std::unique_ptr<AccountTrackerService> account_tracker_; | 223 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 221 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 224 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 222 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 225 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 223 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 226 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 224 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 227 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 228 std::unique_ptr<UserClassifier> user_classifier_; |
| 225 CategoryFactory category_factory_; | 229 CategoryFactory category_factory_; |
| 226 MockSnippetsAvailableCallback mock_callback_; | 230 MockSnippetsAvailableCallback mock_callback_; |
| 227 const std::string test_lang_; | 231 const std::string test_lang_; |
| 228 const GURL test_url_; | 232 const GURL test_url_; |
| 229 std::set<std::string> test_hosts_; | 233 std::set<std::string> test_hosts_; |
| 230 std::set<std::string> test_excluded_; | 234 std::set<std::string> test_excluded_; |
| 231 base::HistogramTester histogram_tester_; | 235 base::HistogramTester histogram_tester_; |
| 232 | 236 |
| 233 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 237 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
| 234 }; | 238 }; |
| 235 | 239 |
| 236 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { | 240 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { |
| 237 public: | 241 public: |
| 238 NTPSnippetsContentSuggestionsFetcherTest() | 242 NTPSnippetsContentSuggestionsFetcherTest() |
| 239 : NTPSnippetsFetcherTest( | 243 : NTPSnippetsFetcherTest( |
| 240 GURL(kTestChromeContentSuggestionsUrl), | 244 GURL(kTestChromeContentSuggestionsUrl), |
| 241 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} | 245 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} |
| 242 }; | 246 }; |
| 243 | 247 |
| 244 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { | 248 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
| 245 NTPSnippetsFetcher::RequestParams params; | 249 NTPSnippetsFetcher::RequestParams params; |
| 246 params.obfuscated_gaia_id = "0BFUSGAIA"; | 250 params.obfuscated_gaia_id = "0BFUSGAIA"; |
| 247 params.only_return_personalized_results = true; | 251 params.only_return_personalized_results = true; |
| 248 params.user_locale = "en"; | 252 params.user_locale = "en"; |
| 249 params.host_restricts = {"chromium.org"}; | 253 params.host_restricts = {"chromium.org"}; |
| 250 params.excluded_ids = {"1234567890"}; | 254 params.excluded_ids = {"1234567890"}; |
| 251 params.count_to_fetch = 25; | 255 params.count_to_fetch = 25; |
| 252 params.interactive_request = false; | 256 params.interactive_request = false; |
| 257 params.user_class = "ACTIVE_NTP_USER"; |
| 253 params.ui_language.frequency = 0.0f; | 258 params.ui_language.frequency = 0.0f; |
| 254 params.other_top_language.frequency = 0.0f; | 259 params.other_top_language.frequency = 0.0f; |
| 255 | 260 |
| 256 | 261 |
| 257 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 262 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| 258 EXPECT_THAT(params.BuildRequest(), | 263 EXPECT_THAT(params.BuildRequest(), |
| 259 EqualsJSON("{" | 264 EqualsJSON("{" |
| 260 " \"response_detail_level\": \"STANDARD\"," | 265 " \"response_detail_level\": \"STANDARD\"," |
| 261 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," | 266 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," |
| 262 " \"user_locale\": \"en\"," | 267 " \"user_locale\": \"en\"," |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 301 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 297 EXPECT_THAT(params.BuildRequest(), | 302 EXPECT_THAT(params.BuildRequest(), |
| 298 EqualsJSON("{" | 303 EqualsJSON("{" |
| 299 " \"uiLanguage\": \"en\"," | 304 " \"uiLanguage\": \"en\"," |
| 300 " \"priority\": \"BACKGROUND_PREFETCH\"," | 305 " \"priority\": \"BACKGROUND_PREFETCH\"," |
| 301 " \"regularlyVisitedHostNames\": [" | 306 " \"regularlyVisitedHostNames\": [" |
| 302 " \"chromium.org\"" | 307 " \"chromium.org\"" |
| 303 " ]," | 308 " ]," |
| 304 " \"excludedSuggestionIds\": [" | 309 " \"excludedSuggestionIds\": [" |
| 305 " \"1234567890\"" | 310 " \"1234567890\"" |
| 306 " ]" | 311 " ]," |
| 312 " \"user_activeness_class\": \"ACTIVE_NTP_USER\"" |
| 307 "}")); | 313 "}")); |
| 308 } | 314 } |
| 309 | 315 |
| 310 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { | 316 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
| 311 NTPSnippetsFetcher::RequestParams params; | 317 NTPSnippetsFetcher::RequestParams params; |
| 312 params.only_return_personalized_results = false; | 318 params.only_return_personalized_results = false; |
| 313 params.host_restricts = {}; | 319 params.host_restricts = {}; |
| 314 params.count_to_fetch = 10; | 320 params.count_to_fetch = 10; |
| 315 params.excluded_ids = {}; | 321 params.excluded_ids = {}; |
| 316 params.interactive_request = true; | 322 params.interactive_request = true; |
| 323 params.user_class = "ACTIVE_NTP_USER"; |
| 317 params.ui_language.frequency = 0.0f; | 324 params.ui_language.frequency = 0.0f; |
| 318 params.other_top_language.frequency = 0.0f; | 325 params.other_top_language.frequency = 0.0f; |
| 319 | 326 |
| 320 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 327 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| 321 EXPECT_THAT(params.BuildRequest(), | 328 EXPECT_THAT(params.BuildRequest(), |
| 322 EqualsJSON("{" | 329 EqualsJSON("{" |
| 323 " \"response_detail_level\": \"STANDARD\"," | 330 " \"response_detail_level\": \"STANDARD\"," |
| 324 " \"advanced_options\": {" | 331 " \"advanced_options\": {" |
| 325 " \"local_scoring_params\": {" | 332 " \"local_scoring_params\": {" |
| 326 " \"content_params\": {" | 333 " \"content_params\": {" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 347 " \"sort_type\": 1" | 354 " \"sort_type\": 1" |
| 348 " }" | 355 " }" |
| 349 " }" | 356 " }" |
| 350 "}")); | 357 "}")); |
| 351 | 358 |
| 352 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 359 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 353 EXPECT_THAT(params.BuildRequest(), | 360 EXPECT_THAT(params.BuildRequest(), |
| 354 EqualsJSON("{" | 361 EqualsJSON("{" |
| 355 " \"regularlyVisitedHostNames\": []," | 362 " \"regularlyVisitedHostNames\": []," |
| 356 " \"priority\": \"USER_ACTION\"," | 363 " \"priority\": \"USER_ACTION\"," |
| 357 " \"excludedSuggestionIds\": []" | 364 " \"excludedSuggestionIds\": []," |
| 365 " \"user_activeness_class\": \"ACTIVE_NTP_USER\"" |
| 358 "}")); | 366 "}")); |
| 359 } | 367 } |
| 360 | 368 |
| 361 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { | 369 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
| 362 NTPSnippetsFetcher::RequestParams params; | 370 NTPSnippetsFetcher::RequestParams params; |
| 363 params.only_return_personalized_results = false; | 371 params.only_return_personalized_results = false; |
| 364 params.host_restricts = {}; | 372 params.host_restricts = {}; |
| 365 params.count_to_fetch = 10; | 373 params.count_to_fetch = 10; |
| 366 params.interactive_request = false; | 374 params.interactive_request = false; |
| 367 for (int i = 0; i < 200; ++i) { | 375 for (int i = 0; i < 200; ++i) { |
| 368 params.excluded_ids.insert(base::StringPrintf("%03d", i)); | 376 params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
| 369 } | 377 } |
| 378 params.user_class = "ACTIVE_NTP_USER"; |
| 370 params.ui_language.frequency = 0.0f; | 379 params.ui_language.frequency = 0.0f; |
| 371 params.other_top_language.frequency = 0.0f; | 380 params.other_top_language.frequency = 0.0f; |
| 372 | 381 |
| 373 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 382 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 374 EXPECT_THAT(params.BuildRequest(), | 383 EXPECT_THAT(params.BuildRequest(), |
| 375 EqualsJSON("{" | 384 EqualsJSON("{" |
| 376 " \"regularlyVisitedHostNames\": []," | 385 " \"regularlyVisitedHostNames\": []," |
| 377 " \"priority\": \"BACKGROUND_PREFETCH\"," | 386 " \"priority\": \"BACKGROUND_PREFETCH\"," |
| 378 " \"excludedSuggestionIds\": [" | 387 " \"excludedSuggestionIds\": [" |
| 379 " \"000\", \"001\", \"002\", \"003\", \"004\"," | 388 " \"000\", \"001\", \"002\", \"003\", \"004\"," |
| (...skipping 11 matching lines...) Expand all Loading... |
| 391 " \"060\", \"061\", \"062\", \"063\", \"064\"," | 400 " \"060\", \"061\", \"062\", \"063\", \"064\"," |
| 392 " \"065\", \"066\", \"067\", \"068\", \"069\"," | 401 " \"065\", \"066\", \"067\", \"068\", \"069\"," |
| 393 " \"070\", \"071\", \"072\", \"073\", \"074\"," | 402 " \"070\", \"071\", \"072\", \"073\", \"074\"," |
| 394 " \"075\", \"076\", \"077\", \"078\", \"079\"," | 403 " \"075\", \"076\", \"077\", \"078\", \"079\"," |
| 395 " \"080\", \"081\", \"082\", \"083\", \"084\"," | 404 " \"080\", \"081\", \"082\", \"083\", \"084\"," |
| 396 " \"085\", \"086\", \"087\", \"088\", \"089\"," | 405 " \"085\", \"086\", \"087\", \"088\", \"089\"," |
| 397 " \"090\", \"091\", \"092\", \"093\", \"094\"," | 406 " \"090\", \"091\", \"092\", \"093\", \"094\"," |
| 398 " \"095\", \"096\", \"097\", \"098\", \"099\"" | 407 " \"095\", \"096\", \"097\", \"098\", \"099\"" |
| 399 // Truncated to 100 entries. Currently, they happen to | 408 // Truncated to 100 entries. Currently, they happen to |
| 400 // be those lexically first. | 409 // be those lexically first. |
| 401 " ]" | 410 " ]," |
| 411 " \"user_activeness_class\": \"ACTIVE_NTP_USER\"" |
| 402 "}")); | 412 "}")); |
| 403 } | 413 } |
| 404 | 414 |
| 405 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { | 415 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { |
| 406 NTPSnippetsFetcher::RequestParams params; | 416 NTPSnippetsFetcher::RequestParams params; |
| 407 params.only_return_personalized_results = false; | 417 params.only_return_personalized_results = false; |
| 408 params.host_restricts = {}; | 418 params.host_restricts = {}; |
| 409 params.count_to_fetch = 10; | 419 params.count_to_fetch = 10; |
| 410 params.interactive_request = true; | 420 params.interactive_request = true; |
| 411 params.ui_language.language_code = "en"; | 421 params.ui_language.language_code = "en"; |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 864 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
| 855 if (fetched_categories) { | 865 if (fetched_categories) { |
| 856 // Matchers above aren't any more precise than this, so this is sufficient | 866 // Matchers above aren't any more precise than this, so this is sufficient |
| 857 // for test-failure diagnostics. | 867 // for test-failure diagnostics. |
| 858 return os << "list with " << fetched_categories->size() << " elements"; | 868 return os << "list with " << fetched_categories->size() << " elements"; |
| 859 } | 869 } |
| 860 return os << "null"; | 870 return os << "null"; |
| 861 } | 871 } |
| 862 | 872 |
| 863 } // namespace ntp_snippets | 873 } // namespace ntp_snippets |
| OLD | NEW |