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

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc

Issue 2395123002: Connecting UserClassifier to NtpSnippetsFetcher (Closed)
Patch Set: Rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/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
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_, base::Bind(&ParseJsonDelayed), 174 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed),
172 kAPIKey); 175 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
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 258
254 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; 259 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API;
255 EXPECT_THAT(params.BuildRequest(), 260 EXPECT_THAT(params.BuildRequest(),
256 EqualsJSON("{" 261 EqualsJSON("{"
257 " \"response_detail_level\": \"STANDARD\"," 262 " \"response_detail_level\": \"STANDARD\","
258 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," 263 " \"obfuscated_gaia_id\": \"0BFUSGAIA\","
259 " \"user_locale\": \"en\"," 264 " \"user_locale\": \"en\","
260 " \"advanced_options\": {" 265 " \"advanced_options\": {"
261 " \"local_scoring_params\": {" 266 " \"local_scoring_params\": {"
262 " \"content_params\": {" 267 " \"content_params\": {"
(...skipping 30 matching lines...) Expand all
293 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; 298 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
294 EXPECT_THAT(params.BuildRequest(), 299 EXPECT_THAT(params.BuildRequest(),
295 EqualsJSON("{" 300 EqualsJSON("{"
296 " \"uiLanguage\": \"en\"," 301 " \"uiLanguage\": \"en\","
297 " \"priority\": \"BACKGROUND_PREFETCH\"," 302 " \"priority\": \"BACKGROUND_PREFETCH\","
298 " \"regularlyVisitedHostNames\": [" 303 " \"regularlyVisitedHostNames\": ["
299 " \"chromium.org\"" 304 " \"chromium.org\""
300 " ]," 305 " ],"
301 " \"excludedSuggestionIds\": [" 306 " \"excludedSuggestionIds\": ["
302 " \"1234567890\"" 307 " \"1234567890\""
303 " ]" 308 " ],"
309 " \"user_activeness_class\": \"ACTIVE_NTP_USER\""
304 "}")); 310 "}"));
305 } 311 }
306 312
307 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { 313 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) {
308 NTPSnippetsFetcher::RequestParams params; 314 NTPSnippetsFetcher::RequestParams params;
309 params.only_return_personalized_results = false; 315 params.only_return_personalized_results = false;
310 params.host_restricts = {}; 316 params.host_restricts = {};
311 params.count_to_fetch = 10; 317 params.count_to_fetch = 10;
312 params.excluded_ids = {}; 318 params.excluded_ids = {};
313 params.interactive_request = true; 319 params.interactive_request = true;
320 params.user_class = "ACTIVE_NTP_USER";
314 321
315 322
316 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; 323 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API;
317 EXPECT_THAT(params.BuildRequest(), 324 EXPECT_THAT(params.BuildRequest(),
318 EqualsJSON("{" 325 EqualsJSON("{"
319 " \"response_detail_level\": \"STANDARD\"," 326 " \"response_detail_level\": \"STANDARD\","
320 " \"advanced_options\": {" 327 " \"advanced_options\": {"
321 " \"local_scoring_params\": {" 328 " \"local_scoring_params\": {"
322 " \"content_params\": {" 329 " \"content_params\": {"
323 " \"only_return_personalized_results\": false" 330 " \"only_return_personalized_results\": false"
(...skipping 19 matching lines...) Expand all
343 " \"sort_type\": 1" 350 " \"sort_type\": 1"
344 " }" 351 " }"
345 " }" 352 " }"
346 "}")); 353 "}"));
347 354
348 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; 355 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
349 EXPECT_THAT(params.BuildRequest(), 356 EXPECT_THAT(params.BuildRequest(),
350 EqualsJSON("{" 357 EqualsJSON("{"
351 " \"regularlyVisitedHostNames\": []," 358 " \"regularlyVisitedHostNames\": [],"
352 " \"priority\": \"USER_ACTION\"," 359 " \"priority\": \"USER_ACTION\","
353 " \"excludedSuggestionIds\": []" 360 " \"excludedSuggestionIds\": [],"
361 " \"user_activeness_class\": \"ACTIVE_NTP_USER\""
354 "}")); 362 "}"));
355 } 363 }
356 364
357 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { 365 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) {
358 NTPSnippetsFetcher::RequestParams params; 366 NTPSnippetsFetcher::RequestParams params;
359 params.only_return_personalized_results = false; 367 params.only_return_personalized_results = false;
360 params.host_restricts = {}; 368 params.host_restricts = {};
361 params.count_to_fetch = 10; 369 params.count_to_fetch = 10;
362 params.interactive_request = false; 370 params.interactive_request = false;
363 for (int i = 0; i < 200; ++i) { 371 for (int i = 0; i < 200; ++i) {
364 params.excluded_ids.insert(base::StringPrintf("%03d", i)); 372 params.excluded_ids.insert(base::StringPrintf("%03d", i));
365 } 373 }
374 params.user_class = "ACTIVE_NTP_USER";
366 375
367 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; 376 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
368 EXPECT_THAT(params.BuildRequest(), 377 EXPECT_THAT(params.BuildRequest(),
369 EqualsJSON("{" 378 EqualsJSON("{"
370 " \"regularlyVisitedHostNames\": []," 379 " \"regularlyVisitedHostNames\": [],"
371 " \"priority\": \"BACKGROUND_PREFETCH\"," 380 " \"priority\": \"BACKGROUND_PREFETCH\","
372 " \"excludedSuggestionIds\": [" 381 " \"excludedSuggestionIds\": ["
373 " \"000\", \"001\", \"002\", \"003\", \"004\"," 382 " \"000\", \"001\", \"002\", \"003\", \"004\","
374 " \"005\", \"006\", \"007\", \"008\", \"009\"," 383 " \"005\", \"006\", \"007\", \"008\", \"009\","
375 " \"010\", \"011\", \"012\", \"013\", \"014\"," 384 " \"010\", \"011\", \"012\", \"013\", \"014\","
376 " \"015\", \"016\", \"017\", \"018\", \"019\"," 385 " \"015\", \"016\", \"017\", \"018\", \"019\","
377 " \"020\", \"021\", \"022\", \"023\", \"024\"," 386 " \"020\", \"021\", \"022\", \"023\", \"024\","
378 " \"025\", \"026\", \"027\", \"028\", \"029\"," 387 " \"025\", \"026\", \"027\", \"028\", \"029\","
379 " \"030\", \"031\", \"032\", \"033\", \"034\"," 388 " \"030\", \"031\", \"032\", \"033\", \"034\","
380 " \"035\", \"036\", \"037\", \"038\", \"039\"," 389 " \"035\", \"036\", \"037\", \"038\", \"039\","
381 " \"040\", \"041\", \"042\", \"043\", \"044\"," 390 " \"040\", \"041\", \"042\", \"043\", \"044\","
382 " \"045\", \"046\", \"047\", \"048\", \"049\"," 391 " \"045\", \"046\", \"047\", \"048\", \"049\","
383 " \"050\", \"051\", \"052\", \"053\", \"054\"," 392 " \"050\", \"051\", \"052\", \"053\", \"054\","
384 " \"055\", \"056\", \"057\", \"058\", \"059\"," 393 " \"055\", \"056\", \"057\", \"058\", \"059\","
385 " \"060\", \"061\", \"062\", \"063\", \"064\"," 394 " \"060\", \"061\", \"062\", \"063\", \"064\","
386 " \"065\", \"066\", \"067\", \"068\", \"069\"," 395 " \"065\", \"066\", \"067\", \"068\", \"069\","
387 " \"070\", \"071\", \"072\", \"073\", \"074\"," 396 " \"070\", \"071\", \"072\", \"073\", \"074\","
388 " \"075\", \"076\", \"077\", \"078\", \"079\"," 397 " \"075\", \"076\", \"077\", \"078\", \"079\","
389 " \"080\", \"081\", \"082\", \"083\", \"084\"," 398 " \"080\", \"081\", \"082\", \"083\", \"084\","
390 " \"085\", \"086\", \"087\", \"088\", \"089\"," 399 " \"085\", \"086\", \"087\", \"088\", \"089\","
391 " \"090\", \"091\", \"092\", \"093\", \"094\"," 400 " \"090\", \"091\", \"092\", \"093\", \"094\","
392 " \"095\", \"096\", \"097\", \"098\", \"099\"" 401 " \"095\", \"096\", \"097\", \"098\", \"099\""
393 // Truncated to 100 entries. Currently, they happen to 402 // Truncated to 100 entries. Currently, they happen to
394 // be those lexically first. 403 // be those lexically first.
395 " ]" 404 " ],"
405 " \"user_activeness_class\": \"ACTIVE_NTP_USER\""
396 "}")); 406 "}"));
397 } 407 }
398 408
399 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 409 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
400 // The lack of registered baked in responses would cause any fetch to fail. 410 // The lack of registered baked in responses would cause any fetch to fail.
401 FastForwardUntilNoTasksRemain(); 411 FastForwardUntilNoTasksRemain();
402 EXPECT_THAT(histogram_tester().GetAllSamples( 412 EXPECT_THAT(histogram_tester().GetAllSamples(
403 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 413 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
404 IsEmpty()); 414 IsEmpty());
405 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 415 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { 782 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) {
773 if (fetched_categories) { 783 if (fetched_categories) {
774 // Matchers above aren't any more precise than this, so this is sufficient 784 // Matchers above aren't any more precise than this, so this is sufficient
775 // for test-failure diagnostics. 785 // for test-failure diagnostics.
776 return os << "list with " << fetched_categories->size() << " elements"; 786 return os << "list with " << fetched_categories->size() << " elements";
777 } 787 }
778 return os << "null"; 788 return os << "null";
779 } 789 }
780 790
781 } // namespace ntp_snippets 791 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698