| 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/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 221 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 222 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 222 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 223 EXPECT_THAT( | 223 EXPECT_THAT( |
| 224 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 224 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 225 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 225 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| 226 EXPECT_THAT(histogram_tester().GetAllSamples( | 226 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 227 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 227 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 228 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 228 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 229 } | 229 } |
| 230 | 230 |
| 231 TEST_F(NTPSnippetsFetcherTest, ShouldReportEmptyHostsError) { | 231 // TODO(jkrcal) Return the tests ShouldReportEmptyHostsError and |
| 232 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 232 // ShouldRestrictToHosts once we have a way to change variation parameters from |
| 233 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), | 233 // unittests. The tests were tailored to previous default value of the parameter |
| 234 /*language_code=*/"en-US", | 234 // fetching_host_restrict, which changed now. |
| 235 /*count=*/1); | |
| 236 FastForwardUntilNoTasksRemain(); | |
| 237 EXPECT_THAT(snippets_fetcher().last_status(), | |
| 238 Eq("Cannot fetch for empty hosts list.")); | |
| 239 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | |
| 240 EXPECT_THAT( | |
| 241 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | |
| 242 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | |
| 243 EXPECT_THAT(histogram_tester().GetAllSamples( | |
| 244 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | |
| 245 IsEmpty()); | |
| 246 // This particular error gets triggered prior to JSON parsing and hence tests | |
| 247 // observe no fetch latency. | |
| 248 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | |
| 249 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | |
| 250 } | |
| 251 | |
| 252 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { | |
| 253 net::TestURLFetcherFactory test_url_fetcher_factory; | |
| 254 snippets_fetcher().FetchSnippetsFromHosts( | |
| 255 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17); | |
| 256 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); | |
| 257 ASSERT_THAT(fetcher, NotNull()); | |
| 258 std::unique_ptr<base::Value> value = | |
| 259 base::JSONReader::Read(fetcher->upload_data()); | |
| 260 ASSERT_TRUE(value) << " failed to parse JSON: " | |
| 261 << PrintToString(fetcher->upload_data()); | |
| 262 const base::DictionaryValue* dict = nullptr; | |
| 263 ASSERT_TRUE(value->GetAsDictionary(&dict)); | |
| 264 const base::DictionaryValue* local_scoring_params = nullptr; | |
| 265 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", | |
| 266 &local_scoring_params)); | |
| 267 const base::ListValue* content_selectors = nullptr; | |
| 268 ASSERT_TRUE( | |
| 269 local_scoring_params->GetList("content_selectors", &content_selectors)); | |
| 270 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); | |
| 271 const base::DictionaryValue* content_selector = nullptr; | |
| 272 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); | |
| 273 std::string content_selector_value; | |
| 274 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | |
| 275 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); | |
| 276 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); | |
| 277 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | |
| 278 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); | |
| 279 } | |
| 280 | |
| 281 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 235 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| 282 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 236 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 283 net::URLRequestStatus::FAILED); | 237 net::URLRequestStatus::FAILED); |
| 284 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 238 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 285 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 239 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 286 /*count=*/1); | 240 /*count=*/1); |
| 287 FastForwardUntilNoTasksRemain(); | 241 FastForwardUntilNoTasksRemain(); |
| 288 EXPECT_THAT(snippets_fetcher().last_status(), | 242 EXPECT_THAT(snippets_fetcher().last_status(), |
| 289 Eq("URLRequestStatus error -2")); | 243 Eq("URLRequestStatus error -2")); |
| 290 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 244 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 EXPECT_THAT(histogram_tester().GetAllSamples( | 356 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 403 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 357 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 404 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 358 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 405 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 359 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 406 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 360 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 407 /*count=*/1))); | 361 /*count=*/1))); |
| 408 } | 362 } |
| 409 | 363 |
| 410 } // namespace | 364 } // namespace |
| 411 } // namespace ntp_snippets | 365 } // namespace ntp_snippets |
| OLD | NEW |