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

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

Issue 2162533002: Adding a request counter to the snippets fetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@traffic-monitor
Patch Set: Rebase + rename to throttler Created 4 years, 5 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/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/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/ntp_snippet.h" 18 #include "components/ntp_snippets/ntp_snippet.h"
19 #include "components/ntp_snippets/ntp_snippets_constants.h" 19 #include "components/ntp_snippets/ntp_snippets_constants.h"
20 #include "components/prefs/testing_pref_service.h"
20 #include "components/signin/core/browser/account_tracker_service.h" 21 #include "components/signin/core/browser/account_tracker_service.h"
21 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 22 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
22 #include "components/signin/core/browser/fake_signin_manager.h" 23 #include "components/signin/core/browser/fake_signin_manager.h"
23 #include "components/signin/core/browser/test_signin_client.h" 24 #include "components/signin/core/browser/test_signin_client.h"
24 #include "components/variations/entropy_provider.h" 25 #include "components/variations/entropy_provider.h"
25 #include "components/variations/variations_associated_data.h" 26 #include "components/variations/variations_associated_data.h"
26 #include "google_apis/google_api_keys.h" 27 #include "google_apis/google_api_keys.h"
27 #include "net/url_request/test_url_fetcher_factory.h" 28 #include "net/url_request/test_url_fetcher_factory.h"
28 #include "net/url_request/url_request_test_util.h" 29 #include "net/url_request/url_request_test_util.h"
29 #include "testing/gmock/include/gmock/gmock.h" 30 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 NTPSnippetsFetcherTest(const GURL& gurl, 143 NTPSnippetsFetcherTest(const GURL& gurl,
143 const std::map<std::string, std::string>& params) 144 const std::map<std::string, std::string>& params)
144 : params_manager_(ntp_snippets::kStudyName, params), 145 : params_manager_(ntp_snippets::kStudyName, params),
145 mock_task_runner_(new base::TestMockTimeTaskRunner()), 146 mock_task_runner_(new base::TestMockTimeTaskRunner()),
146 mock_task_runner_handle_(mock_task_runner_), 147 mock_task_runner_handle_(mock_task_runner_),
147 signin_client_(new TestSigninClient(nullptr)), 148 signin_client_(new TestSigninClient(nullptr)),
148 account_tracker_(new AccountTrackerService()), 149 account_tracker_(new AccountTrackerService()),
149 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), 150 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(),
150 account_tracker_.get())), 151 account_tracker_.get())),
151 fake_token_service_(new FakeProfileOAuth2TokenService()), 152 fake_token_service_(new FakeProfileOAuth2TokenService()),
152 snippets_fetcher_( 153 pref_service_(new TestingPrefServiceSimple()),
154 test_lang_("en-US"),
155 test_url_(gurl) {
156 RequestThrottler::RegisterProfilePrefs(pref_service_->registry());
157
158 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>(
153 fake_signin_manager_.get(), 159 fake_signin_manager_.get(),
154 fake_token_service_.get(), 160 fake_token_service_.get(),
155 scoped_refptr<net::TestURLRequestContextGetter>( 161 scoped_refptr<net::TestURLRequestContextGetter>(
156 new net::TestURLRequestContextGetter(mock_task_runner_.get())), 162 new net::TestURLRequestContextGetter(mock_task_runner_.get())),
163 pref_service_.get(),
157 base::Bind(&ParseJsonDelayed), 164 base::Bind(&ParseJsonDelayed),
158 /*is_stable_channel=*/true), 165 /*is_stable_channel=*/true);
159 test_lang_("en-US"), 166
160 test_url_(gurl) { 167 snippets_fetcher_->SetCallback(
161 snippets_fetcher_.SetCallback(
162 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, 168 base::Bind(&MockSnippetsAvailableCallback::WrappedRun,
163 base::Unretained(&mock_callback_))); 169 base::Unretained(&mock_callback_)));
164 snippets_fetcher_.SetTickClockForTesting( 170 snippets_fetcher_->SetTickClockForTesting(
165 mock_task_runner_->GetMockTickClock()); 171 mock_task_runner_->GetMockTickClock());
166 test_hosts_.insert("www.somehost.com"); 172 test_hosts_.insert("www.somehost.com");
167 // Increase initial time such that ticks are non-zero. 173 // Increase initial time such that ticks are non-zero.
168 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); 174 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234));
169 } 175 }
170 176
171 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } 177 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_.get(); }
Marc Treib 2016/07/22 13:52:52 I think the .get() isn't needed?
jkrcal 2016/07/25 10:05:38 Done. Thanks! I am still not used to the magic of
172 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } 178 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
173 void FastForwardUntilNoTasksRemain() { 179 void FastForwardUntilNoTasksRemain() {
174 mock_task_runner_->FastForwardUntilNoTasksRemain(); 180 mock_task_runner_->FastForwardUntilNoTasksRemain();
175 } 181 }
176 const std::string& test_lang() const { return test_lang_; } 182 const std::string& test_lang() const { return test_lang_; }
177 const GURL& test_url() { return test_url_; } 183 const GURL& test_url() { return test_url_; }
178 const std::set<std::string>& test_hosts() const { return test_hosts_; } 184 const std::set<std::string>& test_hosts() const { return test_hosts_; }
179 base::HistogramTester& histogram_tester() { return histogram_tester_; } 185 base::HistogramTester& histogram_tester() { return histogram_tester_; }
180 186
181 void InitFakeURLFetcherFactory() { 187 void InitFakeURLFetcherFactory() {
(...skipping 17 matching lines...) Expand all
199 variations::testing::VariationParamsManager params_manager_; 205 variations::testing::VariationParamsManager params_manager_;
200 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; 206 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
201 base::ThreadTaskRunnerHandle mock_task_runner_handle_; 207 base::ThreadTaskRunnerHandle mock_task_runner_handle_;
202 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 208 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
203 // Initialized lazily in SetFakeResponse(). 209 // Initialized lazily in SetFakeResponse().
204 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; 210 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_;
205 std::unique_ptr<TestSigninClient> signin_client_; 211 std::unique_ptr<TestSigninClient> signin_client_;
206 std::unique_ptr<AccountTrackerService> account_tracker_; 212 std::unique_ptr<AccountTrackerService> account_tracker_;
207 std::unique_ptr<SigninManagerBase> fake_signin_manager_; 213 std::unique_ptr<SigninManagerBase> fake_signin_manager_;
208 std::unique_ptr<OAuth2TokenService> fake_token_service_; 214 std::unique_ptr<OAuth2TokenService> fake_token_service_;
209 NTPSnippetsFetcher snippets_fetcher_; 215 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_;
216 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
210 MockSnippetsAvailableCallback mock_callback_; 217 MockSnippetsAvailableCallback mock_callback_;
211 const std::string test_lang_; 218 const std::string test_lang_;
212 const GURL test_url_; 219 const GURL test_url_;
213 std::set<std::string> test_hosts_; 220 std::set<std::string> test_hosts_;
214 base::HistogramTester histogram_tester_; 221 base::HistogramTester histogram_tester_;
215 222
216 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 223 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
217 }; 224 };
218 225
219 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { 226 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest {
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 " \"ampUrl\" : \"http://localhost/amp\"," 361 " \"ampUrl\" : \"http://localhost/amp\","
355 " \"corpusId\" : \"http://localhost/foobar\"," 362 " \"corpusId\" : \"http://localhost/foobar\","
356 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 363 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
357 " }]" 364 " }]"
358 " }" 365 " }"
359 "}]}"; 366 "}]}";
360 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 367 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
361 net::URLRequestStatus::SUCCESS); 368 net::URLRequestStatus::SUCCESS);
362 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1); 369 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1);
363 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 370 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
364 /*count=*/1); 371 /*count=*/1,
372 /*force_request=*/true);
365 FastForwardUntilNoTasksRemain(); 373 FastForwardUntilNoTasksRemain();
366 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 374 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
367 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 375 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
368 EXPECT_THAT(histogram_tester().GetAllSamples( 376 EXPECT_THAT(histogram_tester().GetAllSamples(
369 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 377 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
370 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 378 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
371 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 379 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
372 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 380 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
373 /*count=*/1))); 381 /*count=*/1)));
374 } 382 }
375 383
376 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) { 384 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) {
377 const std::string kJsonStr = 385 const std::string kJsonStr =
378 "{\"snippet\" : [{" 386 "{\"snippet\" : [{"
379 " \"id\" : [\"http://localhost/foobar\"]," 387 " \"id\" : [\"http://localhost/foobar\"],"
380 " \"title\" : \"Foo Barred from Baz\"," 388 " \"title\" : \"Foo Barred from Baz\","
381 " \"summaryText\" : \"...\"," 389 " \"summaryText\" : \"...\","
382 " \"fullPageUrl\" : \"http://localhost/foobar\"," 390 " \"fullPageUrl\" : \"http://localhost/foobar\","
383 " \"publishTime\" : \"2016-06-30T11:01:37.000Z\"," 391 " \"publishTime\" : \"2016-06-30T11:01:37.000Z\","
384 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," 392 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
385 " \"publisherName\" : \"Foo News\"," 393 " \"publisherName\" : \"Foo News\","
386 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 394 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
387 " \"ampUrl\" : \"http://localhost/amp\"," 395 " \"ampUrl\" : \"http://localhost/amp\","
388 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 396 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
389 "}]}"; 397 "}]}";
390 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 398 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
391 net::URLRequestStatus::SUCCESS); 399 net::URLRequestStatus::SUCCESS);
392 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1); 400 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1);
393 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 401 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
394 /*count=*/1); 402 /*count=*/1,
403 /*force_request=*/true);
395 FastForwardUntilNoTasksRemain(); 404 FastForwardUntilNoTasksRemain();
396 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 405 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
397 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 406 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
398 EXPECT_THAT(histogram_tester().GetAllSamples( 407 EXPECT_THAT(histogram_tester().GetAllSamples(
399 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 408 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
400 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 409 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
401 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 410 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
402 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 411 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
403 /*count=*/1))); 412 /*count=*/1)));
404 } 413 }
405 414
406 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 415 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
407 const std::string kJsonStr = "{\"recos\": []}"; 416 const std::string kJsonStr = "{\"recos\": []}";
408 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 417 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
409 net::URLRequestStatus::SUCCESS); 418 net::URLRequestStatus::SUCCESS);
410 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1); 419 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1);
411 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 420 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
412 /*count=*/1); 421 /*count=*/1,
422 /*force_request=*/true);
413 FastForwardUntilNoTasksRemain(); 423 FastForwardUntilNoTasksRemain();
414 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 424 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
415 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 425 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
416 EXPECT_THAT( 426 EXPECT_THAT(
417 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 427 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
418 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 428 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
419 EXPECT_THAT(histogram_tester().GetAllSamples( 429 EXPECT_THAT(histogram_tester().GetAllSamples(
420 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 430 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
421 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 431 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
422 } 432 }
423 433
424 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { 434 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) {
425 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 435 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
426 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), 436 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(),
427 /*language_code=*/"en-US", 437 /*language_code=*/"en-US",
428 /*count=*/1); 438 /*count=*/1,
439 /*force_request=*/true);
429 FastForwardUntilNoTasksRemain(); 440 FastForwardUntilNoTasksRemain();
430 EXPECT_THAT(snippets_fetcher().last_status(), 441 EXPECT_THAT(snippets_fetcher().last_status(),
431 Eq("Cannot fetch for empty hosts list.")); 442 Eq("Cannot fetch for empty hosts list."));
432 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 443 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
433 EXPECT_THAT( 444 EXPECT_THAT(
434 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 445 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
435 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 446 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
436 EXPECT_THAT(histogram_tester().GetAllSamples( 447 EXPECT_THAT(histogram_tester().GetAllSamples(
437 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 448 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
438 IsEmpty()); 449 IsEmpty());
439 // This particular error gets triggered prior to JSON parsing and hence tests 450 // This particular error gets triggered prior to JSON parsing and hence tests
440 // observe no fetch latency. 451 // observe no fetch latency.
441 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 452 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
442 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 453 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
443 } 454 }
444 455
445 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { 456 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) {
446 net::TestURLFetcherFactory test_url_fetcher_factory; 457 net::TestURLFetcherFactory test_url_fetcher_factory;
447 snippets_fetcher().FetchSnippetsFromHosts( 458 snippets_fetcher().FetchSnippetsFromHosts(
448 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17); 459 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17,
460 /*force_request=*/true);
449 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); 461 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
450 ASSERT_THAT(fetcher, NotNull()); 462 ASSERT_THAT(fetcher, NotNull());
451 std::unique_ptr<base::Value> value = 463 std::unique_ptr<base::Value> value =
452 base::JSONReader::Read(fetcher->upload_data()); 464 base::JSONReader::Read(fetcher->upload_data());
453 ASSERT_TRUE(value) << " failed to parse JSON: " 465 ASSERT_TRUE(value) << " failed to parse JSON: "
454 << PrintToString(fetcher->upload_data()); 466 << PrintToString(fetcher->upload_data());
455 const base::DictionaryValue* dict = nullptr; 467 const base::DictionaryValue* dict = nullptr;
456 ASSERT_TRUE(value->GetAsDictionary(&dict)); 468 ASSERT_TRUE(value->GetAsDictionary(&dict));
457 const base::DictionaryValue* local_scoring_params = nullptr; 469 const base::DictionaryValue* local_scoring_params = nullptr;
458 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", 470 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params",
(...skipping 10 matching lines...) Expand all
469 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); 481 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector));
470 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 482 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
471 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); 483 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
472 } 484 }
473 485
474 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 486 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
475 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 487 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
476 net::URLRequestStatus::FAILED); 488 net::URLRequestStatus::FAILED);
477 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 489 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
478 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 490 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
479 /*count=*/1); 491 /*count=*/1,
492 /*force_request=*/true);
480 FastForwardUntilNoTasksRemain(); 493 FastForwardUntilNoTasksRemain();
481 EXPECT_THAT(snippets_fetcher().last_status(), 494 EXPECT_THAT(snippets_fetcher().last_status(),
482 Eq("URLRequestStatus error -2")); 495 Eq("URLRequestStatus error -2"));
483 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 496 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
484 EXPECT_THAT( 497 EXPECT_THAT(
485 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 498 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
486 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); 499 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
487 EXPECT_THAT(histogram_tester().GetAllSamples( 500 EXPECT_THAT(histogram_tester().GetAllSamples(
488 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 501 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
489 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 502 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
490 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 503 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
491 Not(IsEmpty())); 504 Not(IsEmpty()));
492 } 505 }
493 506
494 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 507 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
495 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 508 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
496 net::URLRequestStatus::SUCCESS); 509 net::URLRequestStatus::SUCCESS);
497 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 510 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
498 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 511 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
499 /*count=*/1); 512 /*count=*/1,
513 /*force_request=*/true);
500 FastForwardUntilNoTasksRemain(); 514 FastForwardUntilNoTasksRemain();
501 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 515 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
502 EXPECT_THAT( 516 EXPECT_THAT(
503 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 517 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
504 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); 518 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
505 EXPECT_THAT(histogram_tester().GetAllSamples( 519 EXPECT_THAT(histogram_tester().GetAllSamples(
506 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 520 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
507 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 521 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
508 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 522 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
509 Not(IsEmpty())); 523 Not(IsEmpty()));
510 } 524 }
511 525
512 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 526 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
513 const std::string kInvalidJsonStr = "{ \"recos\": []"; 527 const std::string kInvalidJsonStr = "{ \"recos\": []";
514 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, 528 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK,
515 net::URLRequestStatus::SUCCESS); 529 net::URLRequestStatus::SUCCESS);
516 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 530 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
517 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 531 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
518 /*count=*/1); 532 /*count=*/1,
533 /*force_request=*/true);
519 FastForwardUntilNoTasksRemain(); 534 FastForwardUntilNoTasksRemain();
520 EXPECT_THAT(snippets_fetcher().last_status(), 535 EXPECT_THAT(snippets_fetcher().last_status(),
521 StartsWith("Received invalid JSON (error ")); 536 StartsWith("Received invalid JSON (error "));
522 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); 537 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
523 EXPECT_THAT( 538 EXPECT_THAT(
524 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 539 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
525 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 540 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
526 EXPECT_THAT(histogram_tester().GetAllSamples( 541 EXPECT_THAT(histogram_tester().GetAllSamples(
527 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 542 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
528 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 543 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
529 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 544 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
530 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 545 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
531 /*count=*/1))); 546 /*count=*/1)));
532 } 547 }
533 548
534 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { 549 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
535 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, 550 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK,
536 net::URLRequestStatus::SUCCESS); 551 net::URLRequestStatus::SUCCESS);
537 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 552 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
538 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 553 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
539 /*count=*/1); 554 /*count=*/1,
555 /*force_request=*/true);
540 FastForwardUntilNoTasksRemain(); 556 FastForwardUntilNoTasksRemain();
541 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); 557 EXPECT_THAT(snippets_fetcher().last_json(), std::string());
542 EXPECT_THAT( 558 EXPECT_THAT(
543 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 559 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
544 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 560 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
545 EXPECT_THAT(histogram_tester().GetAllSamples( 561 EXPECT_THAT(histogram_tester().GetAllSamples(
546 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 562 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
547 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 563 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
548 } 564 }
549 565
550 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { 566 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
551 const std::string kJsonStr = 567 const std::string kJsonStr =
552 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; 568 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
553 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 569 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
554 net::URLRequestStatus::SUCCESS); 570 net::URLRequestStatus::SUCCESS);
555 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 571 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
556 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 572 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
557 /*count=*/1); 573 /*count=*/1,
574 /*force_request=*/true);
558 FastForwardUntilNoTasksRemain(); 575 FastForwardUntilNoTasksRemain();
559 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 576 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
560 EXPECT_THAT( 577 EXPECT_THAT(
561 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 578 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
562 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); 579 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
563 EXPECT_THAT(histogram_tester().GetAllSamples( 580 EXPECT_THAT(histogram_tester().GetAllSamples(
564 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 581 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
565 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 582 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
566 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 583 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
567 Not(IsEmpty())); 584 Not(IsEmpty()));
568 } 585 }
569 586
570 // This test actually verifies that the test setup itself is sane, to prevent 587 // This test actually verifies that the test setup itself is sane, to prevent
571 // hard-to-reproduce test failures. 588 // hard-to-reproduce test failures.
572 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 589 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
573 InitFakeURLFetcherFactory(); 590 InitFakeURLFetcherFactory();
574 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 591 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
575 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 592 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
576 /*count=*/1); 593 /*count=*/1,
594 /*force_request=*/true);
577 FastForwardUntilNoTasksRemain(); 595 FastForwardUntilNoTasksRemain();
578 } 596 }
579 597
580 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 598 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
581 const std::string kJsonStr = "{ \"recos\": [] }"; 599 const std::string kJsonStr = "{ \"recos\": [] }";
582 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 600 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
583 net::URLRequestStatus::SUCCESS); 601 net::URLRequestStatus::SUCCESS);
584 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1); 602 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1);
585 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 603 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
586 /*count=*/1); 604 /*count=*/1,
605 /*force_request=*/true);
587 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. 606 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
588 // Callback is expected to be called once. 607 // Callback is expected to be called once.
589 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 608 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
590 /*count=*/1); 609 /*count=*/1,
610 /*force_request=*/true);
591 FastForwardUntilNoTasksRemain(); 611 FastForwardUntilNoTasksRemain();
592 EXPECT_THAT( 612 EXPECT_THAT(
593 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 613 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
594 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 614 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
595 EXPECT_THAT(histogram_tester().GetAllSamples( 615 EXPECT_THAT(histogram_tester().GetAllSamples(
596 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 616 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
597 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 617 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
598 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 618 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
599 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 619 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
600 /*count=*/1))); 620 /*count=*/1)));
601 } 621 }
602 622
603 ::std::ostream& operator<<( 623 ::std::ostream& operator<<(
604 ::std::ostream& os, 624 ::std::ostream& os,
605 const NTPSnippetsFetcher::OptionalSnippets& snippets) { 625 const NTPSnippetsFetcher::OptionalSnippets& snippets) {
606 if (snippets) { 626 if (snippets) {
607 // Matchers above aren't any more precise than this, so this is sufficient 627 // Matchers above aren't any more precise than this, so this is sufficient
608 // for test-failure diagnostics. 628 // for test-failure diagnostics.
609 return os << "list with " << snippets->size() << " elements"; 629 return os << "list with " << snippets->size() << " elements";
610 } else { 630 } else {
611 return os << "null"; 631 return os << "null";
612 } 632 }
613 } 633 }
614 634
615 } // namespace ntp_snippets 635 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698