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

Unified Diff: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc

Issue 2454063002: [NTP Snippets] Clean up NTPSnippetsFetcher (Closed)
Patch Set: . 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
diff --git a/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc b/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
index 8539c34b796c42b7683664738a573fdc1ecbfb66..c239b772fbe1baf2e18df98e305806df7404bf3d 100644
--- a/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
+++ b/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc
@@ -195,6 +195,16 @@ class NTPSnippetsFetcherTest : public testing::Test {
const std::set<std::string>& test_excluded() const { return test_excluded_; }
base::HistogramTester& histogram_tester() { return histogram_tester_; }
+ NTPSnippetsFetcher::Params test_params() {
+ NTPSnippetsFetcher::Params result;
+ result.hosts = test_hosts();
+ result.language_code = test_lang();
+ result.excluded_ids = test_excluded();
+ result.count_to_fetch = 1;
+ result.interactive_request = true;
+ return result;
+ }
+
void InitFakeURLFetcherFactory() {
if (fake_url_fetcher_factory_)
return;
@@ -246,19 +256,18 @@ class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest {
};
TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) {
- NTPSnippetsFetcher::RequestParams params;
- params.obfuscated_gaia_id = "0BFUSGAIA";
- params.only_return_personalized_results = true;
- params.user_locale = "en";
- params.host_restricts = {"chromium.org"};
- params.excluded_ids = {"1234567890"};
- params.count_to_fetch = 25;
- params.interactive_request = false;
- params.user_class = "ACTIVE_NTP_USER";
-
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params.hosts = {"chromium.org"};
+ builder.params.excluded_ids = {"1234567890"};
+ builder.params.count_to_fetch = 25;
+ builder.params.language_code = "en";
+ builder.params.interactive_request = false;
+ builder.obfuscated_gaia_id = "0BFUSGAIA";
+ builder.only_return_personalized_results = true;
+ builder.user_class = "ACTIVE_NTP_USER";
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_READER_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"response_detail_level\": \"STANDARD\","
" \"obfuscated_gaia_id\": \"0BFUSGAIA\","
@@ -296,8 +305,8 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) {
" }"
"}"));
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"uiLanguage\": \"en\","
" \"priority\": \"BACKGROUND_PREFETCH\","
@@ -312,16 +321,13 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) {
}
TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) {
- NTPSnippetsFetcher::RequestParams params;
- params.only_return_personalized_results = false;
- params.host_restricts = {};
- params.count_to_fetch = 10;
- params.excluded_ids = {};
- params.interactive_request = true;
- params.user_class = "ACTIVE_NTP_USER";
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params = test_params();
+ builder.only_return_personalized_results = false;
+ builder.user_class = "ACTIVE_NTP_USER";
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_READER_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"response_detail_level\": \"STANDARD\","
" \"advanced_options\": {"
@@ -352,8 +358,8 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) {
" }"
"}"));
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"regularlyVisitedHostNames\": [],"
" \"priority\": \"USER_ACTION\","
@@ -363,18 +369,15 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) {
}
TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) {
- NTPSnippetsFetcher::RequestParams params;
- params.only_return_personalized_results = false;
- params.host_restricts = {};
- params.count_to_fetch = 10;
- params.interactive_request = false;
- for (int i = 0; i < 200; ++i) {
- params.excluded_ids.insert(base::StringPrintf("%03d", i));
- }
- params.user_class = "ACTIVE_NTP_USER";
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params = test_params();
+ for (int i = 0; i < 200; ++i)
+ builder.params.excluded_ids.insert(base::StringPrintf("%03d", i));
+ builder.only_return_personalized_results = false;
+ builder.user_class = "ACTIVE_NTP_USER";
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"regularlyVisitedHostNames\": [],"
" \"priority\": \"BACKGROUND_PREFETCH\","
@@ -407,14 +410,12 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) {
}
TEST_F(NTPSnippetsFetcherTest, BuildRequestNoUserClass) {
- NTPSnippetsFetcher::RequestParams params;
- params.only_return_personalized_results = false;
- params.host_restricts = {};
- params.count_to_fetch = 10;
- params.interactive_request = false;
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params = test_params();
+ builder.only_return_personalized_results = false;
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"regularlyVisitedHostNames\": [],"
" \"priority\": \"BACKGROUND_PREFETCH\","
@@ -423,18 +424,17 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestNoUserClass) {
}
TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) {
- NTPSnippetsFetcher::RequestParams params;
- params.only_return_personalized_results = false;
- params.host_restricts = {};
- params.count_to_fetch = 10;
- params.interactive_request = true;
- params.ui_language.language_code = "en";
- params.ui_language.frequency = 0.5f;
- params.other_top_language.language_code = "de";
- params.other_top_language.frequency = 0.5f;
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params = test_params();
+ builder.params.interactive_request = true;
+ builder.only_return_personalized_results = false;
+ builder.ui_language.language_code = "en";
+ builder.ui_language.frequency = 0.5f;
+ builder.other_top_language.language_code = "de";
+ builder.other_top_language.frequency = 0.5f;
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"regularlyVisitedHostNames\": [],"
" \"priority\": \"USER_ACTION\","
@@ -453,16 +453,15 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) {
}
TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) {
- NTPSnippetsFetcher::RequestParams params;
- params.only_return_personalized_results = false;
- params.host_restricts = {};
- params.count_to_fetch = 10;
- params.interactive_request = true;
- params.ui_language.language_code = "en";
- params.ui_language.frequency = 0.5f;
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params = test_params();
+ builder.params.interactive_request = true;
+ builder.only_return_personalized_results = false;
+ builder.ui_language.language_code = "en";
+ builder.ui_language.frequency = 0.5f;
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"regularlyVisitedHostNames\": [],"
" \"priority\": \"USER_ACTION\","
@@ -475,16 +474,15 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) {
}
TEST_F(NTPSnippetsFetcherTest, BuildRequestWithOtherLanguageOnly) {
- NTPSnippetsFetcher::RequestParams params;
- params.only_return_personalized_results = false;
- params.host_restricts = {};
- params.count_to_fetch = 10;
- params.interactive_request = true;
- params.other_top_language.language_code = "de";
- params.other_top_language.frequency = 0.5f;
-
- params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
- EXPECT_THAT(params.BuildRequest(),
+ NTPSnippetsFetcher::RequestBuilder builder;
+ builder.params = test_params();
+ builder.params.interactive_request = true;
+ builder.only_return_personalized_results = false;
+ builder.other_top_language.language_code = "de";
+ builder.other_top_language.frequency = 0.5f;
+
+ builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API;
+ EXPECT_THAT(builder.BuildRequest(),
EqualsJSON("{"
" \"regularlyVisitedHostNames\": [],"
" \"priority\": \"USER_ACTION\","
@@ -522,10 +520,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) {
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar")));
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
@@ -558,10 +553,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) {
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar")));
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
@@ -582,10 +574,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) {
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
@@ -635,10 +624,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories;
EXPECT_CALL(mock_callback(), Run(_))
.WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories)));
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
ASSERT_TRUE(fetched_categories);
@@ -676,10 +662,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
@@ -693,10 +676,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) {
net::TestURLFetcherFactory test_url_fetcher_factory;
- snippets_fetcher().FetchSnippetsFromHosts(
- {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(),
- /*count=*/17,
- /*interactive_request=*/true);
+ NTPSnippetsFetcher::Params params = test_params();
+ params.hosts = {"www.somehost1.com", "www.somehost2.com"};
+ params.count_to_fetch = 17;
+ snippets_fetcher().FetchSnippets(params);
net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
ASSERT_THAT(fetcher, NotNull());
std::unique_ptr<base::Value> value =
@@ -726,10 +709,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
net::URLRequestStatus::FAILED);
EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(),
Eq("URLRequestStatus error -2"));
@@ -748,10 +728,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
EXPECT_THAT(
@@ -769,10 +746,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(),
StartsWith("Received invalid JSON (error "));
@@ -792,10 +766,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_json(), std::string());
EXPECT_THAT(
@@ -812,10 +783,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
EXPECT_THAT(
@@ -833,10 +801,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
InitFakeURLFetcherFactory();
EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
}
@@ -845,16 +810,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
- // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
+ snippets_fetcher().FetchSnippets(test_params());
+ // Second call to FetchSnippets() overrides/cancels the previous.
// Callback is expected to be called once.
- snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
- test_excluded(),
- /*count=*/1,
- /*interactive_request=*/true);
+ snippets_fetcher().FetchSnippets(test_params());
FastForwardUntilNoTasksRemain();
EXPECT_THAT(
histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),

Powered by Google App Engine
This is Rietveld 408576698