Chromium Code Reviews| Index: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| diff --git a/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc b/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| index a0f7893ac6e11f2f6b2eccf3c5a587b2d7a6580e..aa5892cd7810c3d07e2ae0038e5892cc8d03f9e0 100644 |
| --- a/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| +++ b/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| @@ -123,20 +123,9 @@ void ParseJsonDelayed( |
| base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); |
| } |
| -class VariationParams { |
| - public: |
| - VariationParams(const std::string& trial_name, |
| - const std::map<std::string, std::string>& params) |
| - : field_trial_list_(new metrics::SHA1EntropyProvider("foo")) { |
| - variations::AssociateVariationParams(trial_name, "Group1", params); |
| - base::FieldTrialList::CreateFieldTrial(trial_name, "Group1"); |
| - } |
| - |
| - ~VariationParams() { variations::testing::ClearAllVariationParams(); } |
| - |
| - private: |
| - base::FieldTrialList field_trial_list_; |
| -}; |
| +GURL GetFetcherUrl(const char* url_format) { |
| + return GURL(base::StringPrintf(url_format, google_apis::GetAPIKey().c_str())); |
| +} |
| } // namespace |
| @@ -144,13 +133,12 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| public: |
| NTPSnippetsFetcherTest() |
| : NTPSnippetsFetcherTest( |
| - GURL(base::StringPrintf(kTestChromeReaderUrlFormat, |
| - google_apis::GetAPIKey().c_str())), |
| + GetFetcherUrl(kTestChromeReaderUrlFormat), |
| std::map<std::string, std::string>()) {} |
| NTPSnippetsFetcherTest(const GURL& gurl, |
| const std::map<std::string, std::string>& params) |
| - : params_(ntp_snippets::kStudyName, params), |
| + : params_manager_(ntp_snippets::kStudyName, params), |
| mock_task_runner_(new base::TestMockTimeTaskRunner()), |
| mock_task_runner_handle_(mock_task_runner_), |
| signin_client_(new TestSigninClient(nullptr)), |
| @@ -205,7 +193,7 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| } |
| private: |
| - VariationParams params_; |
| + variations::testing::VariationParamsManager params_manager_; |
| scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; |
| base::ThreadTaskRunnerHandle mock_task_runner_handle_; |
| FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| @@ -229,12 +217,20 @@ class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { |
| public: |
| NTPSnippetsContentSuggestionsFetcherTest() |
| : NTPSnippetsFetcherTest( |
| - GURL(base::StringPrintf(kTestChromeContentSuggestionsUrlFormat, |
| - google_apis::GetAPIKey().c_str())), |
| + GetFetcherUrl(kTestChromeContentSuggestionsUrlFormat), |
| std::map<std::string, std::string>{ |
| {"content_suggestions_backend", kContentSuggestionsBackend}}) {} |
| }; |
| +class NTPSnippetsFetcherHostRestrictedTest : public NTPSnippetsFetcherTest { |
| + public: |
| + NTPSnippetsFetcherHostRestrictedTest() |
| + : NTPSnippetsFetcherTest( |
| + GetFetcherUrl(kTestChromeReaderUrlFormat), |
| + std::map<std::string, std::string>{ |
| + {"fetching_host_restrict", "on"}}) {} |
|
Marc Treib
2016/07/20 08:34:45
Out of curiosity: Is the "std::map<...>" required
jkrcal
2016/07/20 12:17:31
You are right, not needed. Thanks!
|
| +}; |
| + |
| TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
| NTPSnippetsFetcher::RequestParams params; |
| params.obfuscated_gaia_id = "0BFUSGAIA"; |
| @@ -424,10 +420,56 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| } |
| -// TODO(jkrcal) Return the tests ShouldReportEmptyHostsError and |
| -// ShouldRestrictToHosts once we have a way to change variation parameters from |
| -// unittests. The tests were tailored to previous default value of the parameter |
| -// fetching_host_restrict, which changed now. |
| +TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { |
| + EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| + snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), |
| + /*language_code=*/"en-US", |
| + /*count=*/1); |
| + FastForwardUntilNoTasksRemain(); |
| + EXPECT_THAT(snippets_fetcher().last_status(), |
| + Eq("Cannot fetch for empty hosts list.")); |
| + EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| + EXPECT_THAT( |
| + histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| + ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester().GetAllSamples( |
| + "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| + IsEmpty()); |
| + // This particular error gets triggered prior to JSON parsing and hence tests |
| + // observe no fetch latency. |
| + EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| + ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| +} |
| + |
| +TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { |
| + net::TestURLFetcherFactory test_url_fetcher_factory; |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17); |
| + net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
| + ASSERT_THAT(fetcher, NotNull()); |
| + std::unique_ptr<base::Value> value = |
| + base::JSONReader::Read(fetcher->upload_data()); |
| + ASSERT_TRUE(value) << " failed to parse JSON: " |
| + << PrintToString(fetcher->upload_data()); |
| + const base::DictionaryValue* dict = nullptr; |
| + ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| + const base::DictionaryValue* local_scoring_params = nullptr; |
| + ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", |
| + &local_scoring_params)); |
| + const base::ListValue* content_selectors = nullptr; |
| + ASSERT_TRUE( |
| + local_scoring_params->GetList("content_selectors", &content_selectors)); |
| + ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); |
| + const base::DictionaryValue* content_selector = nullptr; |
| + ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); |
| + std::string content_selector_value; |
| + EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
| + EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); |
| + ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); |
| + EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
| + EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); |
| +} |
| + |
| TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| net::URLRequestStatus::FAILED); |