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

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

Issue 2146863002: More unittests of ntp_snippets_fetcher that depend on trial param value. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@finch-parameters-test
Patch Set: Marc's comments 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
8 #include <utility>
9
7 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
8 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
9 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
10 #include "base/test/histogram_tester.h" 13 #include "base/test/histogram_tester.h"
11 #include "base/test/test_mock_time_task_runner.h" 14 #include "base/test/test_mock_time_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/time/time.h" 16 #include "base/time/time.h"
14 #include "base/values.h" 17 #include "base/values.h"
15 #include "components/ntp_snippets/ntp_snippet.h" 18 #include "components/ntp_snippets/ntp_snippet.h"
16 #include "components/ntp_snippets/ntp_snippets_constants.h" 19 #include "components/ntp_snippets/ntp_snippets_constants.h"
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 119
117 void ParseJsonDelayed( 120 void ParseJsonDelayed(
118 const std::string& json, 121 const std::string& json,
119 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, 122 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback,
120 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { 123 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) {
121 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 124 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
122 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), 125 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback),
123 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); 126 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs));
124 } 127 }
125 128
126 class VariationParams { 129 GURL GetFetcherUrl(const char* url_format) {
127 public: 130 return GURL(base::StringPrintf(url_format, google_apis::GetAPIKey().c_str()));
128 VariationParams(const std::string& trial_name, 131 }
129 const std::map<std::string, std::string>& params)
130 : field_trial_list_(new metrics::SHA1EntropyProvider("foo")) {
131 variations::AssociateVariationParams(trial_name, "Group1", params);
132 base::FieldTrialList::CreateFieldTrial(trial_name, "Group1");
133 }
134
135 ~VariationParams() { variations::testing::ClearAllVariationParams(); }
136
137 private:
138 base::FieldTrialList field_trial_list_;
139 };
140 132
141 } // namespace 133 } // namespace
142 134
143 class NTPSnippetsFetcherTest : public testing::Test { 135 class NTPSnippetsFetcherTest : public testing::Test {
144 public: 136 public:
145 NTPSnippetsFetcherTest() 137 NTPSnippetsFetcherTest()
146 : NTPSnippetsFetcherTest( 138 : NTPSnippetsFetcherTest(
147 GURL(base::StringPrintf(kTestChromeReaderUrlFormat, 139 GetFetcherUrl(kTestChromeReaderUrlFormat),
148 google_apis::GetAPIKey().c_str())),
149 std::map<std::string, std::string>()) {} 140 std::map<std::string, std::string>()) {}
150 141
151 NTPSnippetsFetcherTest(const GURL& gurl, 142 NTPSnippetsFetcherTest(const GURL& gurl,
152 const std::map<std::string, std::string>& params) 143 const std::map<std::string, std::string>& params)
153 : params_(ntp_snippets::kStudyName, params), 144 : params_manager_(ntp_snippets::kStudyName, params),
154 mock_task_runner_(new base::TestMockTimeTaskRunner()), 145 mock_task_runner_(new base::TestMockTimeTaskRunner()),
155 mock_task_runner_handle_(mock_task_runner_), 146 mock_task_runner_handle_(mock_task_runner_),
156 signin_client_(new TestSigninClient(nullptr)), 147 signin_client_(new TestSigninClient(nullptr)),
157 account_tracker_(new AccountTrackerService()), 148 account_tracker_(new AccountTrackerService()),
158 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), 149 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(),
159 account_tracker_.get())), 150 account_tracker_.get())),
160 fake_token_service_(new FakeProfileOAuth2TokenService()), 151 fake_token_service_(new FakeProfileOAuth2TokenService()),
161 snippets_fetcher_( 152 snippets_fetcher_(
162 fake_signin_manager_.get(), 153 fake_signin_manager_.get(),
163 fake_token_service_.get(), 154 fake_token_service_.get(),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 189
199 void SetFakeResponse(const std::string& response_data, 190 void SetFakeResponse(const std::string& response_data,
200 net::HttpStatusCode response_code, 191 net::HttpStatusCode response_code,
201 net::URLRequestStatus::Status status) { 192 net::URLRequestStatus::Status status) {
202 InitFakeURLFetcherFactory(); 193 InitFakeURLFetcherFactory();
203 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, 194 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data,
204 response_code, status); 195 response_code, status);
205 } 196 }
206 197
207 private: 198 private:
208 VariationParams params_; 199 variations::testing::VariationParamsManager params_manager_;
209 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; 200 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
210 base::ThreadTaskRunnerHandle mock_task_runner_handle_; 201 base::ThreadTaskRunnerHandle mock_task_runner_handle_;
211 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 202 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
212 // Initialized lazily in SetFakeResponse(). 203 // Initialized lazily in SetFakeResponse().
213 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; 204 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_;
214 std::unique_ptr<TestSigninClient> signin_client_; 205 std::unique_ptr<TestSigninClient> signin_client_;
215 std::unique_ptr<AccountTrackerService> account_tracker_; 206 std::unique_ptr<AccountTrackerService> account_tracker_;
216 std::unique_ptr<SigninManagerBase> fake_signin_manager_; 207 std::unique_ptr<SigninManagerBase> fake_signin_manager_;
217 std::unique_ptr<OAuth2TokenService> fake_token_service_; 208 std::unique_ptr<OAuth2TokenService> fake_token_service_;
218 NTPSnippetsFetcher snippets_fetcher_; 209 NTPSnippetsFetcher snippets_fetcher_;
219 MockSnippetsAvailableCallback mock_callback_; 210 MockSnippetsAvailableCallback mock_callback_;
220 const std::string test_lang_; 211 const std::string test_lang_;
221 const GURL test_url_; 212 const GURL test_url_;
222 std::set<std::string> test_hosts_; 213 std::set<std::string> test_hosts_;
223 base::HistogramTester histogram_tester_; 214 base::HistogramTester histogram_tester_;
224 215
225 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 216 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
226 }; 217 };
227 218
228 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { 219 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest {
229 public: 220 public:
230 NTPSnippetsContentSuggestionsFetcherTest() 221 NTPSnippetsContentSuggestionsFetcherTest()
231 : NTPSnippetsFetcherTest( 222 : NTPSnippetsFetcherTest(
232 GURL(base::StringPrintf(kTestChromeContentSuggestionsUrlFormat, 223 GetFetcherUrl(kTestChromeContentSuggestionsUrlFormat),
233 google_apis::GetAPIKey().c_str())), 224 {{"content_suggestions_backend", kContentSuggestionsBackend}}) {}
234 std::map<std::string, std::string>{ 225 };
235 {"content_suggestions_backend", kContentSuggestionsBackend}}) {} 226
227 class NTPSnippetsFetcherHostRestrictedTest : public NTPSnippetsFetcherTest {
228 public:
229 NTPSnippetsFetcherHostRestrictedTest()
230 : NTPSnippetsFetcherTest(
231 GetFetcherUrl(kTestChromeReaderUrlFormat),
232 {{"fetching_host_restrict", "on"}}) {}
236 }; 233 };
237 234
238 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { 235 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) {
239 NTPSnippetsFetcher::RequestParams params; 236 NTPSnippetsFetcher::RequestParams params;
240 params.obfuscated_gaia_id = "0BFUSGAIA"; 237 params.obfuscated_gaia_id = "0BFUSGAIA";
241 params.only_return_personalized_results = true; 238 params.only_return_personalized_results = true;
242 params.user_locale = "en"; 239 params.user_locale = "en";
243 params.host_restricts = {"chromium.org"}; 240 params.host_restricts = {"chromium.org"};
244 params.count_to_fetch = 25; 241 params.count_to_fetch = 25;
245 242
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 414 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
418 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 415 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
419 EXPECT_THAT( 416 EXPECT_THAT(
420 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 417 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
421 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 418 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
422 EXPECT_THAT(histogram_tester().GetAllSamples( 419 EXPECT_THAT(histogram_tester().GetAllSamples(
423 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 420 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
424 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 421 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
425 } 422 }
426 423
427 // TODO(jkrcal) Return the tests ShouldReportEmptyHostsError and 424 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) {
428 // ShouldRestrictToHosts once we have a way to change variation parameters from 425 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
429 // unittests. The tests were tailored to previous default value of the parameter 426 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(),
430 // fetching_host_restrict, which changed now. 427 /*language_code=*/"en-US",
428 /*count=*/1);
429 FastForwardUntilNoTasksRemain();
430 EXPECT_THAT(snippets_fetcher().last_status(),
431 Eq("Cannot fetch for empty hosts list."));
432 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
433 EXPECT_THAT(
434 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
435 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
436 EXPECT_THAT(histogram_tester().GetAllSamples(
437 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
438 IsEmpty());
439 // This particular error gets triggered prior to JSON parsing and hence tests
440 // observe no fetch latency.
441 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
442 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
443 }
444
445 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) {
446 net::TestURLFetcherFactory test_url_fetcher_factory;
447 snippets_fetcher().FetchSnippetsFromHosts(
448 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17);
449 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
450 ASSERT_THAT(fetcher, NotNull());
451 std::unique_ptr<base::Value> value =
452 base::JSONReader::Read(fetcher->upload_data());
453 ASSERT_TRUE(value) << " failed to parse JSON: "
454 << PrintToString(fetcher->upload_data());
455 const base::DictionaryValue* dict = nullptr;
456 ASSERT_TRUE(value->GetAsDictionary(&dict));
457 const base::DictionaryValue* local_scoring_params = nullptr;
458 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params",
459 &local_scoring_params));
460 const base::ListValue* content_selectors = nullptr;
461 ASSERT_TRUE(
462 local_scoring_params->GetList("content_selectors", &content_selectors));
463 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2)));
464 const base::DictionaryValue* content_selector = nullptr;
465 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector));
466 std::string content_selector_value;
467 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
468 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com"));
469 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector));
470 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
471 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
472 }
473
431 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 474 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
432 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 475 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
433 net::URLRequestStatus::FAILED); 476 net::URLRequestStatus::FAILED);
434 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 477 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
435 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 478 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
436 /*count=*/1); 479 /*count=*/1);
437 FastForwardUntilNoTasksRemain(); 480 FastForwardUntilNoTasksRemain();
438 EXPECT_THAT(snippets_fetcher().last_status(), 481 EXPECT_THAT(snippets_fetcher().last_status(),
439 Eq("URLRequestStatus error -2")); 482 Eq("URLRequestStatus error -2"));
440 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 483 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 if (snippets) { 606 if (snippets) {
564 // Matchers above aren't any more precise than this, so this is sufficient 607 // Matchers above aren't any more precise than this, so this is sufficient
565 // for test-failure diagnostics. 608 // for test-failure diagnostics.
566 return os << "list with " << snippets->size() << " elements"; 609 return os << "list with " << snippets->size() << " elements";
567 } else { 610 } else {
568 return os << "null"; 611 return os << "null";
569 } 612 }
570 } 613 }
571 614
572 } // namespace ntp_snippets 615 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698